79 lines
2.9 KiB
Lua
79 lines
2.9 KiB
Lua
|
|
-- Wake-on-LAN — sends a magic packet to a target MAC stored in long-term memory.
|
|||
|
|
--
|
|||
|
|
-- Setup (one-time, via voice):
|
|||
|
|
-- "запомни wol_server AA:BB:CC:DD:EE:FF"
|
|||
|
|
-- Then later, anytime:
|
|||
|
|
-- "разбуди сервер" → packet goes out to broadcast.
|
|||
|
|
--
|
|||
|
|
-- Lua can't open raw UDP, so we shell out to PowerShell (System.Net.Sockets).
|
|||
|
|
|
|||
|
|
local lang = jarvis.context.language
|
|||
|
|
local phrase = (jarvis.context.phrase or ""):lower()
|
|||
|
|
|
|||
|
|
-- Figure out which alias the user wants. Right now we only support "server"
|
|||
|
|
-- (the most common case) but the data model already supports more.
|
|||
|
|
local alias = "server"
|
|||
|
|
|
|||
|
|
local mac = jarvis.memory.recall("wol_" .. alias)
|
|||
|
|
if not mac or mac == "" then
|
|||
|
|
-- Fall back to env so power users can ship a default without touching memory.
|
|||
|
|
mac = jarvis.system.env("JARVIS_WOL_TARGET") or ""
|
|||
|
|
end
|
|||
|
|
|
|||
|
|
if mac == "" then
|
|||
|
|
return jarvis.cmd.error(lang == "ru"
|
|||
|
|
and "Сэр, MAC-адрес не задан. Скажи: запомни wol_server, а потом адрес."
|
|||
|
|
or "Sir, MAC address is not set. Say: remember wol_server, then the address.")
|
|||
|
|
end
|
|||
|
|
|
|||
|
|
-- Normalise: AA-BB-CC-DD-EE-FF, AA:BB:CC:DD:EE:FF, AABB.CCDD.EEFF, AABBCCDDEEFF
|
|||
|
|
local cleaned = mac:gsub("[^%w]", ""):upper()
|
|||
|
|
if #cleaned ~= 12 or not cleaned:match("^[0-9A-F]+$") then
|
|||
|
|
return jarvis.cmd.error(lang == "ru"
|
|||
|
|
and "Сэр, MAC-адрес в памяти выглядит подозрительно: " .. tostring(mac)
|
|||
|
|
or "Sir, the stored MAC looks malformed: " .. tostring(mac))
|
|||
|
|
end
|
|||
|
|
|
|||
|
|
-- Inject the cleaned MAC as a constant in the PowerShell script so we don't
|
|||
|
|
-- have to argv-escape it. The packet is 6 × 0xFF + 16 × MAC = 102 bytes,
|
|||
|
|
-- broadcast on UDP/9.
|
|||
|
|
local ps_template = [[
|
|||
|
|
$mac = '%s'
|
|||
|
|
$macBytes = @()
|
|||
|
|
for ($i = 0; $i -lt 12; $i += 2) {
|
|||
|
|
$macBytes += [Convert]::ToByte($mac.Substring($i, 2), 16)
|
|||
|
|
}
|
|||
|
|
$packet = New-Object byte[] 102
|
|||
|
|
for ($i = 0; $i -lt 6; $i++) { $packet[$i] = 0xFF }
|
|||
|
|
for ($r = 0; $r -lt 16; $r++) {
|
|||
|
|
for ($j = 0; $j -lt 6; $j++) {
|
|||
|
|
$packet[6 + $r * 6 + $j] = $macBytes[$j]
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
$udp = New-Object System.Net.Sockets.UdpClient
|
|||
|
|
$udp.EnableBroadcast = $true
|
|||
|
|
$null = $udp.Send($packet, $packet.Length, '255.255.255.255', 9)
|
|||
|
|
$udp.Close()
|
|||
|
|
Write-Output 'sent'
|
|||
|
|
]]
|
|||
|
|
|
|||
|
|
local ps = string.format(ps_template, cleaned)
|
|||
|
|
local cmd = 'powershell -NoProfile -ExecutionPolicy Bypass -Command "& {'
|
|||
|
|
.. ps:gsub('\r?\n', '; '):gsub('"', '\\"')
|
|||
|
|
.. '}"'
|
|||
|
|
|
|||
|
|
jarvis.log("info", "WOL packet → " .. cleaned)
|
|||
|
|
local res = jarvis.system.exec(cmd)
|
|||
|
|
if not res.success then
|
|||
|
|
jarvis.log("error", "WOL failed: " .. tostring(res.stderr))
|
|||
|
|
return jarvis.cmd.error(lang == "ru"
|
|||
|
|
and "Не получилось отправить пакет."
|
|||
|
|
or "Failed to send packet.")
|
|||
|
|
end
|
|||
|
|
|
|||
|
|
-- Format the MAC back for speech: "AA-BB-CC-..." sounds clearer than "AABB..."
|
|||
|
|
local pretty = cleaned:gsub("(%w%w)", "%1-"):sub(1, -2)
|
|||
|
|
return jarvis.cmd.ok(lang == "ru"
|
|||
|
|
and ("Пакет отправлен на " .. pretty)
|
|||
|
|
or ("Packet sent to " .. pretty))
|