Improved server wake-on-lan.

This commit is contained in:
Ylian Saint-Hilaire 2020-10-28 12:52:05 -07:00
parent e3ae2733af
commit 341c6e72b2
2 changed files with 23 additions and 16 deletions

View File

@ -89,14 +89,14 @@ module.exports.CreateMeshScanner = function (parent) {
server4.xxclear = false; server4.xxclear = false;
server4.xxtype = 4; server4.xxtype = 4;
server4.xxlocal = localAddress; server4.xxlocal = localAddress;
server4.on('error', function (err) { if (this.xxlocal == '*') { console.log("ERROR: Server port 16989 not available, check if server is running twice."); } this.close(); delete obj.servers6[this.xxlocal]; }); server4.on('error', function (err) { /*if (this.xxlocal == '*') { console.log("ERROR: Server port 16989 not available, check if server is running twice."); } this.close(); delete obj.servers6[this.xxlocal];*/ });
bindOptions = { port: 16989, exclusive: true }; bindOptions = { port: 16989, exclusive: true };
if (server4.xxlocal != '*') { bindOptions.address = server4.xxlocal; } if (server4.xxlocal != '*') { bindOptions.address = server4.xxlocal; }
server4.bind(bindOptions, function () { server4.bind(bindOptions, function () {
try { try {
var doscan = true; var doscan = true;
try { this.setBroadcast(true); this.setMulticastTTL(128); this.addMembership(membershipIPv4, this.xxlocal); } catch (e) { doscan = false; } try { this.setBroadcast(true); this.setMulticastTTL(128); this.addMembership(membershipIPv4, this.xxlocal); } catch (e) { doscan = false; }
this.on('error', function (error) { console.log('Error: ' + error); }); this.on('error', function (error) { /*console.log('Error: ' + error);*/ });
this.on('message', function (msg, info) { onUdpPacket(msg, info, this); }); this.on('message', function (msg, info) { onUdpPacket(msg, info, this); });
if (doscan == true) { obj.performScan(this); obj.performScan(this); } if (doscan == true) { obj.performScan(this); obj.performScan(this); }
} catch (e) { console.log(e); } } catch (e) { console.log(e); }
@ -120,7 +120,7 @@ module.exports.CreateMeshScanner = function (parent) {
server6.xxclear = false; server6.xxclear = false;
server6.xxtype = 6; server6.xxtype = 6;
server6.xxlocal = localAddress; server6.xxlocal = localAddress;
server6.on('error', function (err) { this.close(); delete obj.servers6[this.xxlocal]; }); server6.on('error', function (err) { /*this.close(); delete obj.servers6[this.xxlocal];*/ });
bindOptions = { port: 16989, exclusive: true }; bindOptions = { port: 16989, exclusive: true };
if (server6.xxlocal != '*') { bindOptions.address = server6.xxlocal; } if (server6.xxlocal != '*') { bindOptions.address = server6.xxlocal; }
server6.bind(bindOptions, function () { server6.bind(bindOptions, function () {
@ -228,7 +228,7 @@ module.exports.CreateMeshScanner = function (parent) {
} }
// As a side job, we also send server wake-on-lan packets // As a side job, we also send server wake-on-lan packets
obj.wakeOnLan = function (macs) { obj.wakeOnLan = function (macs, host) {
var i, j; var i, j;
for (i in macs) { for (i in macs) {
var mac = macs[i].split(':').join(''); var mac = macs[i].split(':').join('');
@ -237,17 +237,24 @@ module.exports.CreateMeshScanner = function (parent) {
var wakepacket = Buffer.from(hexpacket, 'hex'); var wakepacket = Buffer.from(hexpacket, 'hex');
//console.log(wakepacket.toString('hex')); //console.log(wakepacket.toString('hex'));
// Send the wake packet 3 times with small time intervals // Setup the wake function
for (j in obj.servers4) { obj.servers4[j].send(wakepacket, 0, wakepacket.length, 7, "255.255.255.255"); obj.servers4[j].send(wakepacket, 0, wakepacket.length, 16990, membershipIPv4); } const func = function wakeFunc() {
for (j in obj.servers6) { obj.servers6[j].send(wakepacket, 0, wakepacket.length, 16990, membershipIPv6); } for (j in obj.servers4) {
setTimeout(function () { obj.servers4[j].send(wakeFunc.wakepacket, 0, wakeFunc.wakepacket.length, 7, '255.255.255.255');
for (j in obj.servers4) { obj.servers4[j].send(wakepacket, 0, wakepacket.length, 7, "255.255.255.255"); obj.servers4[j].send(wakepacket, 0, wakepacket.length, 16990, membershipIPv4); } obj.servers4[j].send(wakeFunc.wakepacket, 0, wakeFunc.wakepacket.length, 16990, membershipIPv4);
for (j in obj.servers6) { obj.servers6[j].send(wakepacket, 0, wakepacket.length, 16990, membershipIPv6); } if (wakeFunc.host != null) { obj.servers4[j].send(wakeFunc.wakepacket, 0, wakeFunc.wakepacket.length, 7, wakeFunc.host); }
}, 200); }
setTimeout(function () { for (j in obj.servers6) {
for (j in obj.servers4) { obj.servers4[j].send(wakepacket, 0, wakepacket.length, 7, "255.255.255.255"); obj.servers4[j].send(wakepacket, 0, wakepacket.length, 16990, membershipIPv4); } obj.servers6[j].send(wakeFunc.wakepacket, 0, wakeFunc.wakepacket.length, 16990, membershipIPv6);
for (j in obj.servers6) { obj.servers6[j].send(wakepacket, 0, wakepacket.length, 16990, membershipIPv6); } }
}, 500); }
func.wakepacket = wakepacket;
func.host = host;
// Call the wake function 3 times with small time intervals
func();
setTimeout(func, 200);
setTimeout(func, 500);
} }
}; };

View File

@ -3594,7 +3594,7 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
if (macs.length == 0) return; if (macs.length == 0) return;
// Have the server send a wake-on-lan packet (Will not work in WAN-only) // Have the server send a wake-on-lan packet (Will not work in WAN-only)
if (parent.parent.meshScanner != null) { parent.parent.meshScanner.wakeOnLan(macs); } if (parent.parent.meshScanner != null) { parent.parent.meshScanner.wakeOnLan(macs, node.host); }
// Get the list of device groups this user as wake permissions on // Get the list of device groups this user as wake permissions on
var targets = [], targetDeviceGroups = parent.GetAllMeshWithRights(user, MESHRIGHT_WAKEDEVICE); var targets = [], targetDeviceGroups = parent.GetAllMeshWithRights(user, MESHRIGHT_WAKEDEVICE);