Updated wakeonlan logic

This commit is contained in:
Bryan Roe 2021-04-24 10:20:35 -07:00
parent 8ba5b2763e
commit 34d7f00226

View File

@ -745,38 +745,76 @@ function getServerTargetUrlEx(url) {
return url; return url;
} }
// Send a wake-on-lan packet function sendWakeOnLanEx_interval()
function sendWakeOnLan(hexMac) { {
hexMac = hexMac.split(':').join(''); var t = require('MeshAgent').wakesockets;
var count = 0; if (t.list.length == 0)
try { {
var interfaces = require('os').networkInterfaces(); clearInterval(t);
var magic = 'FFFFFFFFFFFF'; require('MeshAgent').wakesockets = null;
for (var x = 1; x <= 16; ++x) { magic += hexMac; } return;
var magicbin = Buffer.from(magic, 'hex'); }
for (var adapter in interfaces) { var mac = t.list.shift().split(':').join('')
if (interfaces.hasOwnProperty(adapter)) { var magic = 'FFFFFFFFFFFF';
for (var i = 0; i < interfaces[adapter].length; ++i) { for (var x = 1; x <= 16; ++x) { magic += mac; }
var magicbin = Buffer.from(magic, 'hex');
for(var i in t.sockets)
{
t.sockets[i].send(magicbin, 7, '255.255.255.255');
//sendConsoleText('Sending wake packet on ' + JSON.stringify(t.sockets[i].address()));
}
}
function sendWakeOnLanEx(hexMacList)
{
var ret = 0;
if (require('MeshAgent').wakesockets == null)
{
// Create a new interval timer
require('MeshAgent').wakesockets = setInterval(sendWakeOnLanEx_interval, 10);
require('MeshAgent').wakesockets.sockets = [];
require('MeshAgent').wakesockets.list = hexMacList;
var interfaces = require('os').networkInterfaces();
for (var adapter in interfaces)
{
if (interfaces.hasOwnProperty(adapter))
{
for (var i = 0; i < interfaces[adapter].length; ++i)
{
var addr = interfaces[adapter][i]; var addr = interfaces[adapter][i];
if ((addr.family == 'IPv4') && (addr.mac != '00:00:00:00:00:00')) { if ((addr.family == 'IPv4') && (addr.mac != '00:00:00:00:00:00'))
try { {
try
{
var socket = require('dgram').createSocket({ type: 'udp4' }); var socket = require('dgram').createSocket({ type: 'udp4' });
socket.bind({ address: addr.address }); socket.bind({ address: addr.address });
socket.setBroadcast(true); socket.setBroadcast(true);
socket.setMulticastInterface(addr.address); socket.setMulticastInterface(addr.address);
socket.setMulticastTTL(1); socket.setMulticastTTL(1);
socket.send(magicbin, 7, '255.255.255.255'); socket.descriptorMetadata = 'WoL (' + addr.address + ')';
socket.descriptorMetadata = 'WoL (' + addr.address + ' => ' + hexMac + ')'; require('MeshAgent').wakesockets.sockets.push(socket);
count++; ++ret;
} }
catch (e) { } catch (e) { }
} }
} }
} }
} }
} catch (e) { } }
return count; else
{
// Append to an existing interval timer
for(var i in hexMacList)
{
require('MeshAgent').wakesockets.list.push(hexMacList[i]);
}
ret = require('MeshAgent').wakesockets.sockets.length;
}
return (ret);
} }
// Handle a mesh agent command // Handle a mesh agent command
@ -1079,7 +1117,7 @@ function handleServerCommand(data) {
case 'wakeonlan': { case 'wakeonlan': {
// Send wake-on-lan on all interfaces for all MAC addresses in data.macs array. The array is a list of HEX MAC addresses. // Send wake-on-lan on all interfaces for all MAC addresses in data.macs array. The array is a list of HEX MAC addresses.
//sendConsoleText("Server requesting wake-on-lan for: " + data.macs.join(', ')); //sendConsoleText("Server requesting wake-on-lan for: " + data.macs.join(', '));
for (var i in data.macs) { sendWakeOnLan(data.macs[i]); } sendWakeOnLanEx(data.macs);
break; break;
} }
case 'runcommands': { case 'runcommands': {
@ -3733,8 +3771,8 @@ function processConsoleCommand(cmd, args, rights, sessionid) {
if ((args['_'].length != 1) || (args['_'][0].length != 12)) { if ((args['_'].length != 1) || (args['_'][0].length != 12)) {
response = 'Proper usage: wakeonlan [mac], for example "wakeonlan 010203040506".'; response = 'Proper usage: wakeonlan [mac], for example "wakeonlan 010203040506".';
} else { } else {
var count = sendWakeOnLan(args['_'][0]); var count = sendWakeOnLanEx([args['_'][0]]);
response = 'Sent wake-on-lan on ' + count + ' interface(s).'; response = 'Sending wake-on-lan on ' + count + ' interface(s).';
} }
break; break;
} }