-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
342 lines (334 loc) · 8.41 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/**
* @author xiaojue
* @email [email protected]
* @fileoverview hosts-group
*/
var fs = require('fs');
var os = require('os');
var util = require('util');
var HOSTS = os.platform() == 'win32' ? 'C:/Windows/System32/drivers/etc/hosts': '/etc/hosts';
var EOL = os.EOL;
var groupReg = /^## @(.*?)$/;
var blankReg = /\s+/;
var notes = {};
function hosts(defaultName) {
this.defaultName = defaultName || 'defaultGroup';
this.format();
}
/**
notes
{
group1: '',
...
}
hosts
{
group1: [
{
ip:'',
domain: '',
disabled: '',
note:''
},
{
}
],
group2: [
]
}
*/
hosts.prototype = {
constructor: hosts,
//新增方法,避免互斥,修改domain之前,先把所有domain都禁用
_disableAllDomain:function(domain,hostsobject){
for(var group in hostsobject){
var hosts = hostsobject[group];
for(var i=0;i<hosts.length;i++){
var host = hosts[i];
if(domain == host.domain) host.disabled = true;
}
}
return hostsobject;
},
//读取host内容并解析成一个json对象
get: function() {
var hostsstr = fs.readFileSync(HOSTS, 'utf-8');
//一个domain 对应一个ip
//先分组,再去重
//之后去重
var lines = hostsstr.split(EOL);
var hostsobject = {};
var currentName;
var currentLog = [];
var uniq = {};
notes = {};
for (var i = 0; i < lines.length; i++) {
var line = this._parseLine(lines[i]);
if (!line) continue;
var type = line.type;
var val = line.value;
switch (type) {
case 1:
if (!currentName) {
currentName = this.defaultName;
hostsobject[currentName] = [];
if (currentLog.length > 0) {
notes[currentName] = currentLog.join(EOL);
currentLog = [];
}
}
val.forEach(function(item) {
var _item = uniq[currentName + item.domain + item.ip];
if (!_item) {
hostsobject[currentName].push(item);
_item = uniq[currentName + item.domain + item.ip] = item;
}
if (currentLog.length > 0) {
_item.note = (_item.note ? (_item.note + EOL) : '') + currentLog.join(EOL);
}
});
currentLog = [];
break;
case 2:
currentLog.push(val);
break;
case 3:
currentName = val;
hostsobject[currentName] = [];
if (currentLog.length > 0) {
notes[currentName] = currentLog.join(EOL);
currentLog = [];
}
break;
default:
break;
}
}
if (currentLog.length > 0) {
notes.bottom = currentLog.join(EOL);
currentLog = [];
}
return hostsobject;
},
//初始化hosts文件,生成默认分组
format: function() {
this._batchHost();
},
//设置一个domain
set: function(domain, ip, options) {
var self = this;
var defaultName = this.defaultName;
var updateHost = function(host) {
host.domain = domain;
host.ip = ip;
typeof options.disabled !== 'undefined' ? host.disabled = options.disabled: '';
typeof options.note !== 'undefined' ? host.note = options.note: '';
};
ip = ip || '127.0.0.1';
options = options || {};
this._batchHost(function(hostsobject) {
groupName = options.groupName || defaultName;
var group = hostsobject[groupName] = hostsobject[groupName] || [];
for (var i = 0; i < group.length; i++) {
var host = group[i];
if ((host.domain === domain && host.ip === ip) || (host.domain === options.olddomain && host.ip === options.oldip)) {
if(!options.disabled) self._disableAllDomain(domain,hostsobject);
updateHost(host);
return hostsobject;
}
}
group.push({
ip: ip,
domain: domain,
disabled: !! options.disabled,
note: options.note || ''
});
return hostsobject;
});
},
//删除一个host
remove: function(domain, ip, groupName) {
groupName = groupName || this.defaultName;
this._batchHost(function(hostsobject) {
var group = hostsobject[groupName];
if (group) {
var _group = [];
for (var i = 0; i < group.length; i++) {
var host = group[i];
if (host.domain === domain && host.ip === ip) continue;
_group.push(host);
}
if (_group.length === group.length) return false;
hostsobject[groupName] = _group;
return hostsobject;
}
});
},
//添加分组
addGroup: function(groupName, log) {
if (log) {
notes[groupName] = log;
}
this._batchHost(function(hostsobject) {
if (hostsobject[groupName]) return false;
hostsobject[groupName] = [];
return hostsobject;
});
},
//删除分组
removeGroup: function(groupName) {
this._batchHost(function(hostsobject) {
if (hostsobject[groupName]) {
delete hostsobject[groupName];
return hostsobject;
}
});
},
//修改分组名
setGroup: function(oldName, newName) {
this._batchHost(function(hostsobject) {
if (hostsobject[oldName] && ! hostsobject[newName]) {
hostsobject[newName] = hostsobject[oldName];
delete hostsobject[oldName];
return hostsobject;
}
});
},
//将一条host从一个组移动到另一个组
move: function(domain, ip, groupName, target_groupName) {
this._batchHost(function(hostsobject) {
var group = hostsobject[groupName];
var t_group = hostsobject[target_groupName] = hostsobject[target_groupName] || [];
if (group) {
var host, move, i;
//目标分组里是否存在该条host,若存在则不作处理。
for (i = 0; i < t_group.length; i++) {
host = t_group[i];
if (host.domain === domain && host.ip === ip) {
return false;
}
}
for (i = 0; i < group.length; i++) {
host = group[i];
if (host.domain === domain && host.ip === ip) {
t_group.push(host);
group.splice(i, 1);
break;
}
}
return hostsobject;
}
});
},
//注释一个domain
disable: function(domain, ip, groupName) {
this.setDomainDisabled(domain, ip, groupName || defaultName, 1);
},
//开启一个domain
active: function(domain, ip, groupName) {
this.setDomainDisabled(domain, ip, groupName || defaultName, 0);
},
setDomainDisabled: function(domain, ip, groupName, disabled) {
var self = this;
this._batchHost(function(hostsobject) {
if(!disabled) self._disableAllDomain(domain,hostsobject);
var group = hostsobject[groupName];
if (group) {
for (var i = 0; i < group.length; i++) {
var host = group[i];
if (host.domain === domain && host.ip === ip) {
if (host.disabled == disabled) {
return false;
}
host.disabled = disabled;
return hostsobject;
}
}
}
return false;
});
},
//注释一个组
disableGroup: function(groupName) {
this.setGroupDisabled(groupName, 1);
},
//激活一个组
activeGroup: function(groupName) {
this.setGroupDisabled(groupName, 0);
},
setGroupDisabled: function(groupName, disabled) {
var self = this;
this._batchHost(function(hostsobject) {
var group = hostsobject[groupName];
if (group) {
for (var i = 0; i < group.length; i++) {
var host = group[i];
if(!disabled) self._disableAllDomain(host.domain,hostsobject);
host.disabled = disabled;
}
return hostsobject;
}
});
},
_parseLine: function(line) {
var obj;
line = line.trim();
if (line) {
var isGroupLine = line.match(groupReg);
obj = {};
if (isGroupLine) {
obj.type = 3;
obj.value = isGroupLine[1];
} else {
var line2 = line.split(blankReg);
if (line2.length < 2 || ! line2[0].match(/^\d|#[\d]/g)) {
obj.type = 2;
obj.value = line;
} else {
var ip = line2.shift();
var domains = line2;
var disabled = (/^#/).test(ip);
if (disabled) {
ip = ip.slice(1);
}
obj.type = 1;
obj.value = [];
domains.forEach(function(domain) {
obj.value.push({
ip: ip,
domain: domain,
disabled: disabled
});
});
}
}
}
return obj;
},
_batchHost: function(fn) {
var hostsobject = this.get();
hostsobject = fn ? fn(hostsobject) : hostsobject;
if (!hostsobject) return;
var linesStr = this._hostTostr(hostsobject);
fs.writeFileSync(HOSTS, linesStr);
},
_hostTostr: function(hostobj) {
var lines = [];
for (var i in hostobj) {
var group = hostobj[i];
notes[i] ? lines.push(notes[i]) : '';
lines.push('## @' + i);
for (var k = 0; k < group.length; k++) {
var host = group[k];
var line = host.disabled ? "#": '';
host.note ? lines.push(host.note) : '';
line += host.ip + ' ' + host.domain;
lines.push(line);
}
lines.push("");
}
notes.bottom ? lines.push(notes.bottom) : '';
return lines.join(EOL);
}
};
module.exports = new hosts();