-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
152 lines (119 loc) · 3.07 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
#!/usr/bin/env node
let fs = require('fs');
let fsSync = require('fs-sync');
let IpRegex = /^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/
let CommentChars = ['#']
let HostsName = 'hosts'
let BackName = 'hosts.bak'
let path = null
switch (process.platform) {
case 'win32':
path = 'c:\\Windows\\System32\\drivers\\etc\\'
break;
case 'linux':
path = '/etc/'
break;
case 'darwin':
path = '/private/etc/'
break;
}
if (!path) {
log(`Platform: ${process.platform} not supported`)
return
}
let HostsPath = path + HostsName
let BakPath = path + BackName
let ArgStart = 2,
ArgCount = 2,
TotalCount = ArgStart + ArgCount
let argv = process.argv
if (argv.length < TotalCount) {
log(`not enough parameters, give me a ip and a name`)
return;
}
let ip = argv[ArgStart],
name = argv[ArgStart + 1]
if (!isIp(ip)) {
log(`Invalid ip: ${ip}`)
return;
}
// create backup file
if (fs.existsSync(BakPath))
fs.unlink(BakPath);
fsSync.copy(HostsPath, BakPath);
let lineReader = require('readline').createInterface({
input: fs.createReadStream(BakPath)
})
let file = fs.createWriteStream(HostsPath)
let lineNo = 0;
let done = false;
lineReader.on('line', function processLine(line) {
let entry = parseLine(line)
if (!isValidEntry(entry)) {
file.write(`${line}\n`);
return;
}
let entryIp = entry[0]
if (entryIp === ip) {
if (entry.includes(name)) {
log(`\t[${name}] for [${ip}] already exist!`)
return
}
entry[0] = name
let entryLine = normalizeEntry(entry);
file.write(`${ip}\t${entryLine}\n`)
done = true
return
} else if (entry.includes(name)) {
for (let i = 0; i < entry.length; ++i) {
if (entry[i] === name) {
entry[i] = ''
}
}
let entryLine = normalizeEntry(entry);
file.write(`${entryLine}\n`)
return
}
file.write(`${line}\n`);
})
lineReader.on('close', () => {
if (!done) {
file.write(`${ip}\t${name}\n`)
}
})
// console.log(`This platform is ${process.platform}`);
// console.log(`Hello from version: ${process.version}`)
function isIp(str) {
return IpRegex.test(str)
}
function log(content) {
console.log(content)
}
function isValidEntry(array) {
if (array.length >= 1 && isIp(array[0]))
return true;
return false;
}
function isComment(strLine) {
var trimLine = strLine.trim()
for (let i = 0; i < CommentChars.length; ++i) {
if (trimLine.startsWith(CommentChars[i])) {
return true;
}
}
return false;
}
function parseLine(strLine) {
if (!strLine)
return []
var line = strLine.trim()
return line.split(/\s+/);
}
function normalizeEntry(arr) {
return arr.join(' ')
}
// function test() {
// let arr = parseLine('127.0.0.1 ');
// let valid = isValidEntry(arr);
// log(valid)
// }