UNPKG

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