-
Notifications
You must be signed in to change notification settings - Fork 2
/
flasher.js
executable file
·69 lines (56 loc) · 1.75 KB
/
flasher.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
#!/usr/bin/env nodejs
var os = require('os');
var argv = require('minimist')(process.argv.slice(2), {
boolean: ['nocheck']
});
var TPFlasher = require('./index.js');
var tpIP = argv.ip || '192.168.0.66'; // these devices expect a tftp server at this IP
var tpPort = argv.port || 69;
function usage(p) {
p("sudo flasher.js /path/to/firmware.image");
p(" --nocheck: Don't check if the router model is compatible with firmware file");
p(" --ip: Alternate IP to use (default: 192.168.0.66)");
p(" --port: Alternate port to use (default: 69)");
p(" --help: Show this info");
}
if(argv._.length != 1) {
usage(console.error);
process.exit(1);
}
var firmwareFile = argv._[0];
function checkForIP(correctIP) {
if(!os.networkInterfaces) {
return false
}
var ifaces = os.networkInterfaces();
var i, iface, addrs, addr;
for(iface in ifaces) {
addrs = ifaces[iface];
for(i=0; i < addrs.length; i++) {
addr = addrs[i];
if(addr.internal || addr.family != 'IPv4') continue;
// console.log(addr);
if(addr.address == correctIP) {
return true
}
// TODO check netmask
}
}
return false;
}
if(!checkForIP(tpIP)) {
console.error("Error: Remember to give you ethernet interface the static IP", tpIP, "and ensure that your ethernet cable is plugged in and the router turned on.");
return false;
}
var flasher = new TPFlasher();
flasher.flash(firmwareFile, {
ip: tpIP,
port: tpPort,
nocheck: argv.nocheck
}, function(err) {
if(err) {
console.error("Error:", err);
process.exit(1);
}
console.log("Firmware sent! Now just wait for the router to reboot :)");
})