From 6f082b20e3a3fcb7269fc3ae6dc69f012dbb7a1b Mon Sep 17 00:00:00 2001 From: osmarks Date: Sun, 23 Aug 2020 21:27:13 +0100 Subject: [PATCH] overhaul SPUDNET daemon --- manifest | 4 +-- src/main.lua | 91 +++++++++++++++++++++++++++++++++++++++++----------- 2 files changed, 74 insertions(+), 21 deletions(-) diff --git a/manifest b/manifest index 840432f..d48dd05 100644 --- a/manifest +++ b/manifest @@ -1,2 +1,2 @@ -{"build":132,"description":"potatOS primary potatality","files":{"LICENSES":"f3549d84d66eb53dd4a421a4341d77d3d217c1b117d67e3be8f5211adcda0952","autorun.lua":"ee8d33d4cd579c96bad90f4a4c972634ba8fa3d2468c86495abcea4305449d65","bin/5rot26.lua":"91b66cd6d4b33081b25c456142dd7efcb894e819e842693c9e1e17ff48872ff5","bin/chronometer.lua":"db5363993a04382145aef7db2fbe262f0bf10697a589e1e2d2f9ce0f87430dd8","bin/kristminer.lua":"7e7f9fe2a6493d584ad6926cda915e02c1c3d800dc209680898ce930d0bb0e6f","bin/livegps.lua":"c3d17d495cda01aa1261e4c4fcd43439b29af422671972117ec34f68e32c5bba","bin/loading.lua":"c85f7aa1765170325155b921c1fceeb62643f552f12d41b529a22af3a67f5a97","bin/potatoflight.lua":"2fbb0b6f8d78728d8cb0ec64af1bc598bd00cb55f202378e7acdb86bba71efd1","bin/potatoplex.lua":"86c9e7597bbe23d7de7e7f1bfc976d0b94dcdf3af9e6c7c6c9b18b98596898c8","bin/relay.lua":"261ae6c220b83506e3326e8f2b091d246baae458ff0d2ee87512be2c4e35a75d","bin/tryhaskell.lua":"07810d85145da65a3e434154c79d5a9d72f2dcbe59c8d6829040fb925df878ec","potatobios.lua":"06ec570ee39792f41d536b7fce6abbc48e7151727867ae5c41a8bb87297db4ad","signing-key.tbl":"b32af5229c23af3bc03d538e42751b26044e404a7b1af064ed89894efe421607","startup":"bf06ef416de50aa610d491b1c10881609af02448560b4cfe369d6fd8baf98085","update-key.hex":"8d8afb7a45833bb7d68f929421ad60a211d4d73e0ee03b24dc0106ba1de2e1a0","xlib/00_cbor.lua":"464b075e4f094b8db42506bd4bdaad0db87699ea7fbf80e5b87739b4aa9279af","xlib/01_skynet.lua":"dd1642395dd73a1b85e0a152939ac844f583ef694c82093022fce0e7b2320dd3"},"timestamp":1596397193} -{"hash":"3c1bbd14c65ce5ca459e1d01c793a05bbb822a81fa6e970b9cd2ea5a9deefe10","sig":"07e6a84e25593885054181f9a0e68c3c5e30848839660280b19279c35b9f60f52b3875aefe815a54d637"} \ No newline at end of file +{"build":142,"description":null,"files":{"LICENSES":"f3549d84d66eb53dd4a421a4341d77d3d217c1b117d67e3be8f5211adcda0952","autorun.lua":"d443a9c2b97a173e2b7d6b1e180207cad0d0e9ce91557fffab6082c69a9f5bc2","bin/5rot26.lua":"91b66cd6d4b33081b25c456142dd7efcb894e819e842693c9e1e17ff48872ff5","bin/chronometer.lua":"db5363993a04382145aef7db2fbe262f0bf10697a589e1e2d2f9ce0f87430dd8","bin/kristminer.lua":"7e7f9fe2a6493d584ad6926cda915e02c1c3d800dc209680898ce930d0bb0e6f","bin/livegps.lua":"c3d17d495cda01aa1261e4c4fcd43439b29af422671972117ec34f68e32c5bba","bin/loading.lua":"c85f7aa1765170325155b921c1fceeb62643f552f12d41b529a22af3a67f5a97","bin/potatoflight.lua":"2fbb0b6f8d78728d8cb0ec64af1bc598bd00cb55f202378e7acdb86bba71efd1","bin/potatoplex.lua":"86c9e7597bbe23d7de7e7f1bfc976d0b94dcdf3af9e6c7c6c9b18b98596898c8","bin/relay.lua":"261ae6c220b83506e3326e8f2b091d246baae458ff0d2ee87512be2c4e35a75d","bin/tryhaskell.lua":"07810d85145da65a3e434154c79d5a9d72f2dcbe59c8d6829040fb925df878ec","potatobios.lua":"9d32752f5bab07085dd574dc22d15141f4dfdf736820e7d70d11c4f836747397","signing-key.tbl":"b32af5229c23af3bc03d538e42751b26044e404a7b1af064ed89894efe421607","startup":"f68338a6240f5fbf9f9da160b5072536739be91b139999ec11322577d711c2af","xlib/00_cbor.lua":"464b075e4f094b8db42506bd4bdaad0db87699ea7fbf80e5b87739b4aa9279af","xlib/01_skynet.lua":"1d2902d89a61f6c5853157e8ee1b3f2ba2664b1b08af16ad21b41115d551b78f"},"timestamp":1598213842} +{"hash":"6db57494edaad2aba9241c9e8f8634b37ad8ebe36feebd6152f60c65590168b5"} \ No newline at end of file diff --git a/src/main.lua b/src/main.lua index dabe378..6a5d5a8 100644 --- a/src/main.lua +++ b/src/main.lua @@ -127,7 +127,7 @@ The `terminate` event being returned by coroutine.yield sometimes even when you function _G.os.await_event(filter) while true do local ev = {coroutine.yield(filter)} - if ev[1] ~= "terminate" or filter == nil or ev[1] == filter then + if filter == nil or ev[1] == filter then return unpack(ev) end end @@ -604,31 +604,83 @@ end local function websocket_remote_debugging() if not http or not http.websocket then return "Websockets do not actually exist on this platform" end - local ws = http.websocket "wss://osmarks.tk/wsthing/potatOS" - - if not ws then return end - - local function send(msg) - ws.send(safe_serialize(msg)) + local ws + + local function send_packet(msg) + --ws.send(safe_serialize(msg)) + ws.send(json.encode(msg)) + end + + local function send(data) + send_packet { type = "send", channel = "client:potatOS", data = data } + end + + local function connect() + if ws then ws.close() end + ws, err = http.websocket "wss://osmarks.tk/wsthing/v4" + ws.url = "wss://osmarks.tk/wsthing/v4" + if not ws then add_log("websocket failure %s", err) return false end + + send_packet { type = "identify" } + send_packet { type = "set_channels", channels = { "client:potatOS" } } + + add_log("websocket connected") + + return true end + local function try_connect_loop() + while not connect() do + sleep(0.5) + end + end + + try_connect_loop() + local function recv() - return ws.receive() + while true do + local e, u, x = os.await_event "websocket_message" + if u == ws.url then return json.decode(x) end + end end - send { "connect", os.getComputerID() } + local ping_timeout_timer = nil + + process.thread(function() + while true do + local _, t = os.await_event "timer" + if t == ping_timeout_timer and ping_timeout_timer then + -- 15 seconds since last ping, we probably got disconnected + add_log "timed out, attempting reconnect" + try_connect_loop() + end + end + end, "ping-timeout") while true do -- Receive and run code which is sent via SPUDNET - local code = recv() - _G.wsrecv = recv - _G.wssend = send - add_log("SPUDNET command - %s", code) - local f, error = load(code, "@", "t", _G) - if f then -- run safely in background, send back response - process.thread(function() local resp = {pcall(f)} send(resp) end, "spudnetexecutor") - else - send {false, error} + -- Also handle SPUDNETv4 protocol, primarily pings + local packet = recv() + --add_log("test %s", textutils.serialise(packet)) + if packet.type == "ping" then + send_packet { type = "pong", seq = packet.seq } + if ping_timeout_timer then os.cancelTimer(ping_timeout_timer) end + ping_timeout_timer = os.startTimer(15) + elseif packet.type == "error" then + add_log("SPUDNET error %s %s %s", packet["for"], packet.error, packet.detail) + elseif packet.type == "message" then + local code = packet.data + if type(code) == "string" then + _G.wsrecv = recv + _G.wssend = send + add_log("SPUDNET command - %s", code) + local f, errr = load(code, "@", "t", _G) + if f then -- run safely in background, send back response + process.thread(function() local resp = {pcall(f)} send(resp) end, "spudnetexecutor") + else + send {false, errr} + end + end end end end @@ -833,7 +885,6 @@ local function install(force) end local res = process_manifest(manifest, force) - add_log("update complete", tostring(res) or "[some weirdness]") if (res == 0 or res == false) and not force then return false end @@ -854,6 +905,8 @@ local function install(force) set("potatOS.uuid", gen_uuid()) end + add_log("update complete", tostring(res) or "[some weirdness]") + os.reboot() end