Skip to content

Commit 5ab3dfb

Browse files
committed
handle sigint gracefully
1 parent eeef833 commit 5ab3dfb

File tree

9 files changed

+55
-22
lines changed

9 files changed

+55
-22
lines changed

dist/dhcp/index.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ module.exports = function(ip, acsurl, acspass) {
1818
acsurl = toHexArray(acsurl);
1919
acspass = acspass ? toHexArray(acspass) : [84, 101, 108, 115, 116, 114, 97];
2020
vendor = [1, acsurl.length].concat(acsurl.concat([2, acspass.length].concat(acspass)));
21-
return server.createServer({
21+
server.createServer({
2222
range: [ip + '.10', ip + '.15'],
2323
forceOptions: ['router', 'hostname', 'vendor'],
2424
randomIP: true,
@@ -42,4 +42,5 @@ module.exports = function(ip, acsurl, acspass) {
4242
}
4343
return console.log('!!! ERROR', err, data);
4444
}).listen(67);
45+
return server;
4546
};

dist/http/index.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -82,5 +82,6 @@ module.exports = function(ip, port, url) {
8282
return console.error(e);
8383
}
8484
});
85-
return srv.listen(port);
85+
srv.listen(port);
86+
return srv;
8687
};

dist/index.js

+17-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
var args, ask, dhcpd, httpd, ip, pkg, port, tftp;
1+
var args, ask, dhcpd, httpd, ip, pkg, port, servers, tftp;
22

33
pkg = require('../package.json');
44

@@ -18,18 +18,30 @@ port = require('./get-port');
1818

1919
tftp = require('./tftp');
2020

21+
servers = [];
22+
2123
if (args.tftp) {
22-
tftp(args);
24+
servers.push.apply(servers, tftp(args));
2325
} else if (args.dhcponly) {
24-
dhcpd(ip, args.acsurl, args.acspass);
26+
servers.push(dhcpd(ip, args.acsurl, args.acspass));
2527
} else {
2628
ask(ip).then(port).then(function(p) {
2729
var u, url;
2830
u = new URL(args.acsurl || ("http://" + ip));
2931
u.port = p;
3032
url = u.toString();
3133
console.log("listening for cwmp requests at " + url);
32-
dhcpd(ip, url, args.acspass);
33-
return httpd(ip, p, url);
34+
servers.push(dhcpd(ip, url, args.acspass));
35+
return servers.push(httpd(ip, p, url));
3436
});
3537
}
38+
39+
process.on('SIGINT', function() {
40+
console.log("\nshutting down servers from SIGINT (Ctrl+C)");
41+
servers.forEach(function(server) {
42+
return server.close();
43+
});
44+
return setTimeout(function() {
45+
return process.exit();
46+
}, 2000);
47+
});

dist/tftp.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ ProgressIndicator = (function(superClass) {
4444
})(Transform);
4545

4646
module.exports = function(arg) {
47-
var addr, eth, ip, network, server, tftp;
47+
var addr, dhcpd, eth, ip, network, server, tftp;
4848
eth = arg.eth, ip = arg.ip, tftp = arg.tftp;
4949
if (eth != null) {
5050
network = ips.find(function(arg1) {
@@ -59,7 +59,7 @@ module.exports = function(arg) {
5959
addr = ip.split('.');
6060
addr.pop();
6161
addr = addr.join('.');
62-
dhcp.createServer({
62+
dhcpd = dhcp.createServer({
6363
range: [addr + '.10', addr + '.15'],
6464
forceOptions: ['router', 'hostname', 'bootFile'],
6565
randomIP: true,
@@ -124,5 +124,6 @@ module.exports = function(arg) {
124124
return console.error('ERROR:', err);
125125
});
126126
console.log('Starting tftp server, listening on ' + ip + ':69');
127-
return server.listen();
127+
server.listen();
128+
return [dhcpd, server];
128129
};

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "tch-exploit",
3-
"version": "2.0.1-rc4",
3+
"version": "2.0.1-rc5",
44
"main": "dist/index.js",
55
"bin": "dist/index.js",
66
"scripts": {

src/dhcp/index.coffee

+2
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,5 @@ module.exports = (ip, acsurl, acspass) ->
4444

4545
console.log '!!! ERROR', err, data
4646
.listen 67
47+
48+
server

src/http/index.coffee

+2
Original file line numberDiff line numberDiff line change
@@ -93,3 +93,5 @@ module.exports = (ip, port, url) ->
9393
else console.error e
9494

9595
srv.listen port
96+
97+
srv

src/index.coffee

+16-4
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,12 @@ httpd = require './http'
2121
port = require './get-port'
2222
tftp = require './tftp'
2323

24+
servers = []
25+
2426
if args.tftp
25-
tftp args
27+
servers.push tftp(args)...
2628
else if args.dhcponly
27-
dhcpd ip, args.acsurl, args.acspass
29+
servers.push dhcpd ip, args.acsurl, args.acspass
2830
else
2931
ask ip
3032
.then port
@@ -36,5 +38,15 @@ else
3638

3739
console.log "listening for cwmp requests at #{ url }"
3840

39-
dhcpd ip, url, args.acspass
40-
httpd ip, p, url
41+
servers.push dhcpd ip, url, args.acspass
42+
servers.push httpd ip, p, url
43+
44+
process.on 'SIGINT', ->
45+
console.log "\nshutting down servers from SIGINT (Ctrl+C)"
46+
47+
servers.forEach (server) ->
48+
server.close()
49+
50+
setTimeout ->
51+
process.exit()
52+
, 2000

src/tftp.coffee

+9-7
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ dhcp = require './dhcp/server'
88
ips = require('./ips')()
99

1010

11-
class ProgressIndicator extends Transform
11+
class ProgressIndicator extends Transform
1212
constructor: (@size, options) ->
1313
super options
14-
14+
1515
@last = 0
1616
@bytes = 0
17-
17+
1818
_transform: (chunk, encoding, cb) ->
1919
@bytes += chunk.length
2020

@@ -35,14 +35,14 @@ class ProgressIndicator extends Transform
3535
module.exports = ({ eth, ip, tftp }) ->
3636
if eth?
3737
network = ips.find ({ name }) -> name is eth
38-
39-
ip ?= network?.address
38+
39+
ip ?= network?.address
4040

4141
addr = ip.split '.'
4242
addr.pop()
4343
addr = addr.join '.'
4444

45-
dhcp
45+
dhcpd = dhcp
4646
.createServer
4747
range: [
4848
addr + '.10'
@@ -54,7 +54,7 @@ module.exports = ({ eth, ip, tftp }) ->
5454
router: [ ip ]
5555
hostname: 'second.gateway'
5656
broadcast: addr + '.255'
57-
bootFile: (req, res) ->
57+
bootFile: (req, res) ->
5858
console.log req, res
5959

6060
path.basename tftp
@@ -123,3 +123,5 @@ module.exports = ({ eth, ip, tftp }) ->
123123
console.log 'Starting tftp server, listening on ' + ip + ':69'
124124

125125
server.listen()
126+
127+
[ dhcpd, server ]

0 commit comments

Comments
 (0)