-
Notifications
You must be signed in to change notification settings - Fork 15
/
hosts.js
113 lines (97 loc) · 2.93 KB
/
hosts.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
"use strict"
var fs = require('fs');
var os = require('os');
var ipaddr = require('ipaddr.js');
require('./customcolors');
var staticHosts = {};
var smartHosts = [];
function query(host){
if(staticHosts[host]){
return staticHosts[host];
}
var result = false;
smartHosts.some(function(item,i){
if(item[0].test(host)){
result = item[1];
return true;
}else{
return false;
}
})
return result;
}
function getAllHosts(){
var all = [];
Object.keys(staticHosts).forEach(function(item,i){
all.push(staticHosts[item] + ' '+ item);
})
smartHosts.forEach(function(item,i){
all.push(item[1] + ' ' +item[2]);
})
return all;
}
function initWithFile(fpath){
var fpath = fpath;
try{
var data = fs.readFileSync(fpath,{
encoding:'utf-8'
});
var hostsArr = data.split('\n');
initWithArr(hostsArr);
}catch(e){
console.error(`read hosts file ${fpath} fail: ${e.message}`.error);
}
}
function initWithArr(arr){
arr.forEach(function(item,i){
var line = item.trim();
if(line.length === 0){
return;
}
if(line.charAt(0)==='#'){
return;
}
var params = line.split(' ');
var ip = params.shift().trim();
if(/^\$.*\$$/.test(ip)){ // use local ip
var iname = ip.replace(/\$/g,'');
var interfaces = os.networkInterfaces();
var haveFinded = Object.keys(interfaces).some(function(key,i){
if(key === iname){
var network = interfaces[key];
return network.some(function(item,i){
if(item.family === 'IPv4'){
ip = item.address;
var log = 'replace $'+iname+'$ to '+ ip + ' successful!';
console.log(log.info);
return true;
}
})
}
})
if(!haveFinded){
var log = 'can\'t find out '+iname+'\'s IPv4 address, please check!';
console.log(log.warn);
return;
}
}
if(!ipaddr.isValid(ip)){
var log = 'parse hosts file error: '+ ip + ' is not a valid ip address.';
console.log(log.warn)
return;
}
params.forEach(function(item,i){
var host = item.trim();
if(/\*/.test(host)){ // wildcard mode
var reg = new RegExp('^'+ host.replace(/\./g,'\\.').replace('*','.*') +'$');
smartHosts.push([reg,ip,host]);
}else{ // explicit host mode
staticHosts[host] = ip;
}
});
})
}
exports.initWithFile = initWithFile;
exports.initWithArr = initWithArr;
exports.getAllHosts = getAllHosts;
exports.query = query;