forked from firewalla/firewalla
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVpnManager.js
317 lines (281 loc) · 10.6 KB
/
VpnManager.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
/* Copyright 2016 Rottiesoft LLC
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
'use strict';
var instance = null;
var log = null;
var SysManager = require('../net2/SysManager.js');
var sysManager = new SysManager('info');
var Firewalla = require('../net2/Firewalla.js');
//TODO: support real config file for Firewalla class
var firewalla = new Firewalla('/path/to/config', 'info');
var fHome = firewalla.getFirewallaHome();
var redis = require("redis");
var rclient = redis.createClient();
var later = require('later');
var publicIp = require('public-ip');
var fs = require('fs');
var network = require('network');
var natpmp = require('nat-pmp');
var natupnp = require('nat-upnp');
var ip = require('ip');
var async = require('async');
var util = require('util');
var ttlExpire = 60*60*12;
module.exports = class {
constructor(path, loglevel) {
if (instance == null) {
log = require("../net2/logger.js")("vpn manager", loglevel);
instance = this;
}
return instance;
}
punchNat(opts, success, error) {
opts = opts || {}
opts.timeout = opts.timeout || 5000
log.info("VpnManager:PunchNat",opts);
let replied = false;
// returns the IP of the gateway that your active network interface is linked to
network.get_gateway_ip((err, gateway) => {
if (err) return error(err)
if (ip.isPublic(gateway))
return success(gateway)
if (this.upnpClient == null) {
this.upnpClient = natupnp.createClient();
}
this.upnpClient.portMapping(opts, (err) => {
if (err == null) {
log.info("VpnManager:NatUPNP Success");
if (replied == false) {
replied = true;
success(null, "success", null);
}
} else {
if (replied == false) {
log.error("VpnManager:NatUPNP failed",err);
replied = true;
error(err);
}
}
this.upnpClient.close();
this.upnpClient = null;
});
});
}
unpunchNat(opts, callback) {
log.info("VpnManager:UnpunchNat", opts);
if (this.upnpClient == null) {
this.upnpClient = natupnp.createClient();
}
this.upnpClient.portUnmapping(opts,(err)=>{
this.upnpClient.close();
this.upnpClient = null;
this.portmapped = false;
if (callback) {
callback(err);
}
});
}
setupNat2(opts, success, error) {
opts = opts || {}
opts.timeout = opts.timeout || 5000
// returns the IP of the gateway that your active network interface is linked to
network.get_gateway_ip((err, gateway) => {
if (err) return error(err)
// use regex to check if ip address is public
if (ip.isPublic(gateway))
return success(gateway)
var strategies = {
// pmp: natpmp.connect(gateway),
upnp: natupnp.createClient()
};
// find a strategy to traverse nat
try {
async.detectSeries(strategies, (client, next) => {
let portMapping = async.timeout(client.portMapping.bind(client), opts.timeout)
portMapping(opts, (err) => {
next(null, !err)
});
}, (err, client) => {
if (err) return error(err)
log.info("VpnManager:SetupNat:Success");
if (!client) return error(new Error('All NAT strategies failed or timed out'))
log.info("VpnManager:SetupNat:Success2");
client.externalIp((err, external) => {
if (err) return error(err)
success(external)
})
})
} catch (e) {}
})
}
install(callback) {
let install1_cmd = util.format('cd %s/vpn; sudo -E ./install1.sh', fHome);
this.install1 = require('child_process').exec(install1_cmd, (err, out, code) => {
if (err) {
log.error("VPNManager:INSTALL:Error", "Unable to install1.sh", err);
}
if (err == null) {
publicIp.v4((err, ip) => {
if (err != null) {
if (callback)
callback(err, null);
return;
}
// !! Pay attention to the parameter "-E" which is used to preserve the
// enviornment valueables when running sudo commands
var mydns = sysManager.myDNS()[0];
if (mydns == null) {
mydns = "8.8.8.8"; // use google DNS as default
}
let install2_cmd = util.format("cd %s/vpn; sudo -E ./install2.sh %s %s", fHome, sysManager.myIp(), ip, mydns);
log.info("VPNManager:INSTALL:cmd", install2_cmd);
this.install2 = require('child_process').exec(install2_cmd, (err, out, code) => {
if (err) {
log.error("VPNManager:INSTALL:Error", "Unable to install2.sh", err);
}
log.info("VPNManager:INSTALL:Done");
if (callback) {
callback(err, null);
}
});
});
} else {
if (callback)
callback(err, null);
}
});
}
configure(callback) {}
stop(callback) {
this.started = false;
this.unpunchNat({
protocol: 'udp',
private: 1194,
public: 1194
});
require('child_process').exec("sudo service openvpn stop", (err, out, code) => {
log.info("Stopping OpenVpn", err);
if (callback) {
callback(err);
}
});
}
setNat(callback) {
if (this.started == false) {
if (callback)
callback(null, null, null);
return;
}
this.punchNat({
type: 'udp',
protocol: 'udp',
private: 1194,
public: 1194,
ttl: 0,
description: "Firewalla VPN"
}, (external) => {
log.info("VpnManager:Start:portMap", external);
setTimeout(() => {
log.info("VpnManager:Restart:portMap");
this.setNat(null)
}, ttlExpire/3*1000);
if (callback) {
this.portmapped = true;
callback(null, external, 1194);
}
}, (err) => {
log.info("VpnManager:Start:portMap:Failed: " + err);
setTimeout(() => {
log.info("VpnManager:Restart:portMap");
this.setNat(null)
}, ttlExpire/3*1000);
if (callback) {
callback(null, null, null);
}
})
}
start(callback) {
if (this.started) {
log.info("VpnManager::StartedAlready");
if (callback)
callback(null, this.portmapped, this.portmapped);
return;
}
this.unpunchNat({
protocol: 'udp',
private: 1194,
public: 1194
},(err)=>{
require('child_process').exec("sudo service openvpn start", (err, out, code) => {
log.info("VpnManager:Start", err);
if (err && this.started == false) {
if (callback) {
callback(err);
}
return;
}
this.started = true;
this.setNat(callback);
});
});
}
generatePassword(len) {
var length = len,
charset = "0123456789",
retVal = "";
for (var i = 0, n = charset.length; i < length; ++i) {
retVal += charset.charAt(Math.floor(Math.random() * n));
}
return retVal;
}
getOvpnFile(clientname, password, regenerate, callback) {
let ovpn_file = util.format("%s/ovpns/%s.ovpn", process.env.HOME, clientname);
let ovpn_password = util.format("%s/ovpns/%s.ovpn.password", process.env.HOME, clientname);
fs.readFile(ovpn_file, 'utf8', (err, ovpn) => {
if (ovpn != null && regenerate == false) {
let password = fs.readFileSync(ovpn_password, 'utf8');
log.info("VPNManager:Found older ovpn file");
callback(null, ovpn, password);
return;
}
if (regenerate == true) {
clientname = clientname + this.generatePassword(10);
}
if (password == null) {
password = this.generatePassword(5);
}
let ip = sysManager.myDDNS();
if (ip == null) {
ip = sysManager.publicIp;
}
var mydns = sysManager.myDNS()[0];
if (mydns == null) {
mydns = "8.8.8.8"; // use google DNS as default
}
let cmd = util.format("cd %s/vpn; sudo -E ./ovpngen.sh %s %s %s %s %s; sync", fHome, clientname, password, sysManager.myIp(), ip, mydns);
log.info("VPNManager:GEN", cmd);
this.getovpn = require('child_process').exec(cmd, (err, out, code) => {
if (err) {
log.error("VPNManager:INSTALL:Error", "Unable to install2.sh", err);
}
fs.readFile(ovpn_file, 'utf8', (err, ovpn) => {
if (callback) {
callback(err, ovpn, password);
}
});
});
});
}
}