-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
200 lines (185 loc) · 5.23 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
const hostile = require('hostile');
const chalk = require('chalk');
const exec = require('child_process').exec;
const isWindowsAdmin = require('is-admin');
/**
* Print an error and exit the program
* @param {string} message
*/
function error(err) {
console.error(chalk.red(err.message || err));
process.exit(-1);
}
function activatePortForwarding(minIp, port) {
return new Promise((resolve) => {
if (process.platform === 'win32') {
exec(`netsh interface portproxy add v4tov4 listenport=80 listenaddress=${minIp} connectport=${port} connectaddress=127.0.0.1`, {}, (err) => {
if (err) {
error(err);
}
resolve();
});
} else if (process.platform === 'darwin') {
exec(`echo "rdr pass on lo0 inet proto tcp from any to ${minIp} port 80 -> 127.0.0.1 port ${port}" | sudo pfctl -ef -`, {}, () => {
resolve();
});
}
});
}
function removeFromEtcHosts(url) {
return new Promise((resolve) => {
let ip = '';
function filterFunc(line) {
return !((Array.isArray(line) && line[1] === url) || (typeof line === 'string' && line.split(' ')[2] === this.port));
}
function hostileRemove(lines, port) {
// Try to remove entry, if it exists
const filteredLines = lines.filter(filterFunc, { port });
return hostile.writeFile(filteredLines, () => {
resolve(ip);
});
}
hostile.get(true, (err, lines) => {
if (err) error(err);
let port = '';
for (let i = 0; i < lines.length; i += 1) {
if (typeof lines[i] === 'object' && lines[i][1] === url) {
port = lines[i - 1].split(' ')[2];
ip = lines[i][0];
}
}
if (port === '') {
error('This local.dev is not found');
}
return hostileRemove(lines, port);
});
});
}
function removeFromNetwokr(ip) {
return new Promise((resolve) => {
if (process.platform !== 'win32') {
resolve();
} else {
exec(`netsh interface portproxy delete v4tov4 listenport=80 listenaddress=${ip}`, {}, (err) => {
if (err) {
error(err);
}
resolve();
});
}
});
}
function isAdmin() {
return new Promise((resolve) => {
if (process.platform === 'linux') {
error('Your OS is not supported yet, sorry !');
}
if (process.platform === 'win32') {
isWindowsAdmin().then((admin) => {
resolve(admin);
});
} else {
resolve(process.getuid && process.getuid() === 0);
}
});
}
/**
* Return the next local IP available in a callback
* @param {function} cb
*/
function getNextAvailableIP(port, url) {
return new Promise((resolve) => {
hostile.get(true, (err, lines) => {
if (err) {
error(err);
}
const ipList = [];
for (let i = 0; i < lines.length; i += 1) {
if (typeof lines[i] === 'object') {
if (lines[i][1] === url) {
error(`
The url "${url}" is already being used in your hostfile
If it's a local.dev url, you can by remove it with the remove command
$ local.dev remove ${url}
`);
}
if (i >= 1 && typeof lines[i - 1] === 'string' && lines[i - 1].split(' ')[2] === port) {
error(`
The port "${port}" is already being used by a local.dev
You can delete the url associated with the remove command
$ local.dev remove ${lines[i][1]}
`);
}
if (lines[i][0].slice(0, 7) === '127.0.0') {
ipList.push(lines[i][0]);
}
}
}
let minimal = 1;
ipList.forEach((ip) => {
if (Number(ip.split('.')[3]) >= minimal) {
minimal = Number(ip.split('.')[3]);
}
});
if (minimal === 255) {
error('Max IP is already took');
}
resolve(`127.0.0.${minimal + 1}`);
});
});
}
exports.add = function add(port, url) {
isAdmin().then((admin) => {
if (!(((port - port) + 1) >= 0)) {
error('Please give a right port number');
}
if (!admin) {
error('Please launch as root in order to add a new local.dev');
}
getNextAvailableIP(port, url).then((minIp) => {
activatePortForwarding(minIp, port).then(() => {
hostile.set('# local.dev', port);
hostile.set(minIp, url);
console.log(chalk.green(`
Added local.dev :
localhost:${chalk.green(`${port} <-`)} ${chalk.green(url)}
`));
});
});
});
};
/**
* Ouputs the list of local.dev url
* Example : localhost:2000 <- dev.local
*/
exports.list = function list() {
hostile.get(true, (err, lines) => {
if (err) {
error(err);
}
for (let i = 0; i < lines.length; i += 1) {
if (lines[i].slice(0, 11) === '# local.dev') {
const port = lines[i].split(' ')[2];
const url = lines[i + 1][1];
console.log(`localhost:${chalk.green(`${port} <-`)} ${chalk.green(url)}`);
i += 1;
}
}
});
};
exports.remove = function remove(url) {
isAdmin().then((admin) => {
if (!admin) {
error('Please launch as root in order to remove a local.dev');
}
removeFromEtcHosts(url)
.then((ip) => {
removeFromNetwokr(ip)
.then(() => {
console.log(chalk.green(`
Successfully removed ${url}
`));
});
});
});
};