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