1 | var pathLib = require("path");
|
2 | var fsLib = require("fs");
|
3 | var dns = require("dns");
|
4 | var util = require("util");
|
5 | var events = require("events");
|
6 | var exec = require("child_process").exec;
|
7 | var async = require("async");
|
8 | var mkdirp = require("mkdirp");
|
9 | var sys = require("./lib/system");
|
10 |
|
11 | function str2regx(str) {
|
12 | return str.replace(/[\*\.\?\+\$\^\[\]\(\)\{\}\|\\\/]/g, function (all) {
|
13 | return "\\" + all;
|
14 | });
|
15 | }
|
16 |
|
17 | function FlexHosts(param, dir, cb) {
|
18 | this.cb = cb;
|
19 | this.host2ip = {};
|
20 | this.hostsFuncArr = [];
|
21 | this.hostsTextArr = [];
|
22 | this.content = '';
|
23 |
|
24 | this.beginTag = "##### " + process.cwd() + " Begin #####";
|
25 | this.endTag = "##### " + process.cwd() + " End #####";
|
26 |
|
27 | if (dir) {
|
28 | var confFile = pathLib.join(process.cwd(), dir || ".config", pathLib.basename(__dirname) + ".json");
|
29 | var confDir = pathLib.dirname(confFile);
|
30 |
|
31 | if (!fsLib.existsSync(confDir)) {
|
32 | mkdirp.sync(confDir);
|
33 | fsLib.chmod(confDir, 0777);
|
34 | }
|
35 |
|
36 | if (!fsLib.existsSync(confFile)) {
|
37 | fsLib.writeFileSync(confFile, JSON.stringify(require("./lib/param"), null, 2), {encoding: "utf-8"});
|
38 | fsLib.chmod(confFile, 0777);
|
39 | }
|
40 |
|
41 | try {
|
42 | this.param = JSON.parse(fsLib.readFileSync(confFile));
|
43 |
|
44 | for (var ip in param) {
|
45 | if (this.param[ip]) {
|
46 | this.param[ip] = this.param[ip].concat(param[ip]);
|
47 | }
|
48 | else {
|
49 | this.param[ip] = param[ip];
|
50 | }
|
51 | }
|
52 | }
|
53 | catch (e) {
|
54 | console.log("Params Error!");
|
55 | this.param = {};
|
56 | }
|
57 | }
|
58 | else {
|
59 | this.param = param;
|
60 | }
|
61 |
|
62 | var hostList;
|
63 | for (var host in this.param) {
|
64 | hostList = [];
|
65 | if (typeof this.param[host] == "string") {
|
66 | hostList = this.param[host].split(/\s{1,}/g);
|
67 | }
|
68 | else if (util.isArray(this.param[host]) && this.param[host].length) {
|
69 | hostList = this.param[host];
|
70 | hostList = hostList.filter(function (elem, pos) {
|
71 | return hostList.indexOf(elem) == pos;
|
72 | });
|
73 | }
|
74 |
|
75 | if (hostList && hostList.length) {
|
76 | this.hostsTextArr.push(host + " " + hostList.join(' '));
|
77 | this.hostfunc(hostList);
|
78 | }
|
79 | }
|
80 |
|
81 | dns.lookup("ju.taobao.com", function (err) {
|
82 | if (err) {
|
83 | this.start(err);
|
84 | }
|
85 | else {
|
86 | async.parallel(this.hostsFuncArr, (function (e, result) {
|
87 | var host, ip;
|
88 | for (var i = 0, len = result.length; i < len; i++) {
|
89 | if (result[i]) {
|
90 | host = result[i][0];
|
91 | ip = result[i][1];
|
92 | if (host && ip && ip != "127.0.0.1") {
|
93 | this.host2ip[host] = ip;
|
94 | }
|
95 | }
|
96 | }
|
97 | console.log("\n-------------------");
|
98 | console.log(" MAP of host to IP");
|
99 | console.log("-------------------");
|
100 | console.log("\x1b[37m%s\x1b[0m\n", JSON.stringify(this.host2ip, null, 2));
|
101 |
|
102 | this.start(null);
|
103 | }).bind(this));
|
104 | }
|
105 | }.bind(this));
|
106 |
|
107 | return this;
|
108 | };
|
109 |
|
110 | util.inherits(FlexHosts, events.EventEmitter);
|
111 |
|
112 | var prototype = {
|
113 | constructor: FlexHosts,
|
114 | read: function () {
|
115 | this.content = fsLib.readFileSync(sys.path, "utf-8");
|
116 | },
|
117 | hostfunc: function (hosts) {
|
118 | for (var i = 0, len = hosts.length; i < len; i++) {
|
119 | this.hostsFuncArr.push((function (host) {
|
120 | return function (callback) {
|
121 | dns.resolve(host, function (e, address) {
|
122 | if (e) {
|
123 | console.log("Warning: \x1b[33m%s\x1b[0m can't be resolved!", e.hostname || host);
|
124 | callback(null, host, null);
|
125 | }
|
126 | else {
|
127 | callback(null, host, address[0]);
|
128 | }
|
129 | });
|
130 | }
|
131 | })(hosts[i]));
|
132 | }
|
133 | },
|
134 | finish: function (isStart) {
|
135 | this.read();
|
136 |
|
137 | if (this.content) {
|
138 | console.log("\n---------------");
|
139 | console.log(" Current HOSTS");
|
140 | console.log("---------------");
|
141 | console.log("\x1b[37m%s\x1b[0m\n", this.content);
|
142 | }
|
143 |
|
144 | if (isStart) {
|
145 | this.cb(null, this.host2ip);
|
146 | }
|
147 | else {
|
148 | console.log("\x1b[32m%s\x1b[0m\n", "Bye-bye!");
|
149 | process.exit();
|
150 | }
|
151 | },
|
152 | write: function (isStart) {
|
153 | var self = this;
|
154 | fsLib.writeFile(sys.path, this.content, function () {
|
155 | if (typeof sys.cmd == "string") {
|
156 | exec(sys.cmd, function () {
|
157 | self.finish(isStart);
|
158 | });
|
159 | }
|
160 | else if (util.isArray(sys.cmd) && sys.cmd.length == 2) {
|
161 | exec(sys.cmd[0], function () {
|
162 | exec(sys.cmd[1], function () {
|
163 | self.finish(isStart);
|
164 | });
|
165 | });
|
166 | }
|
167 | else {
|
168 | console.log("Unknown Command!");
|
169 | }
|
170 | });
|
171 | },
|
172 | add: function () {
|
173 | if (this.hostsTextArr.length) {
|
174 | this.content += "\n\n" + this.beginTag + "\n" + this.hostsTextArr.join("\n") + "\n" + this.endTag;
|
175 | }
|
176 | },
|
177 | clear: function () {
|
178 | if (fsLib.existsSync(sys.path)) {
|
179 | this.read();
|
180 | fsLib.writeFile(sys.path + ".backup", this.content);
|
181 |
|
182 | this.content = this.content.replace(
|
183 | new RegExp("\\s{0,}" + str2regx(this.beginTag) + "[\\s\\S]*?" + str2regx(this.endTag) + "\\s{0,}", 'g'),
|
184 | ''
|
185 | );
|
186 |
|
187 | return true;
|
188 | }
|
189 | else {
|
190 | console.log("hosts file NOT FOUND!");
|
191 | return false;
|
192 | }
|
193 | },
|
194 | start: function (err) {
|
195 | if (err) {
|
196 | this.cb(err, this.host2ip);
|
197 | }
|
198 | else if (this.clear()) {
|
199 | this.add();
|
200 | this.write(true);
|
201 | }
|
202 | },
|
203 | restore: function () {
|
204 | if (this.clear()) {
|
205 | this.write(false);
|
206 | }
|
207 | }
|
208 | };
|
209 |
|
210 | for (var k in prototype) {
|
211 | FlexHosts.prototype[k] = prototype[k];
|
212 | }
|
213 |
|
214 | exports = module.exports = FlexHosts;
|