-- we don't use wget anymore, call `update` :3 local pretty = require "cc.pretty" local function startServer() local id = os.getComputerID() local name = string.format("sshd-%s", id) local pass = "4YLhxRQnY/JnzvMsG+uM" -- very funny local auth_str = string.format("server %s %s", name, pass) print(auth_str) local ws = assert(http.websocket("wss://mc.cortan122.de/ws/")) ws.send(auth_str, false) local response = ws.receive() print(response) assert(response == "connection established!") return ws end local ws = startServer() local function wrapTerm(callback, arg) local saved_term = term.current() local _, prev_y = saved_term.getCursorPos() local function write_wrapped(s) saved_term.write(s) ws.send("\0" .. s, false) end local function setTextColor_wrapped(color) saved_term.setTextColor(color) local r,g,b = saved_term.getPaletteColor(color) ws.send(string.format("\0\x1b[38;2;%d;%d;%dm", r*255, g*255, b*255), false) end local function setCursorPos_wrapped(x, y) saved_term.setCursorPos(x, y) if prev_y ~= y then ws.send("\0\n", false) end if x > 1 or prev_y == y then ws.send(string.format("\0\x1b[%dG", x), false) end prev_y = y end local function getSize_wrapped() local _, y = saved_term.getSize() return 80, y end local function scroll_wrapped(y) saved_term.scroll(y) prev_y = prev_y - y setCursorPos_wrapped(saved_term.getCursorPos()) end local fake_term = setmetatable( { write = write_wrapped, setCursorPos = setCursorPos_wrapped, setTextColor = setTextColor_wrapped, setTextColour = setTextColor_wrapped, getSize = getSize_wrapped, scroll = scroll_wrapped, getCursorPos = saved_term.getCursorPos, }, { __index = saved_term } ) term.redirect(fake_term) local result = callback(arg) term.redirect(saved_term) return result end local function execLua(s) local env = setmetatable({ shell = shell, require = require }, { __index = _G }) local code, err = load(s, "", nil, env) if not code then ws.send("\1parse error: " .. tostring(err), false) else local res = nil local status, err = pcall(function() res = code() end) if status then ws.send(pretty.render(pretty.pretty(res), 80), false) else ws.send("\1runtime error: " .. tostring(err), false) end end end local function execCommand(s) local result = shell.run(s:sub(2,-1)) ws.send(tostring(result), false) end local function importFile(s) local name = s:sub(20,-1) ws.send("waiting for file data...", false) local file_data = ws.receive() local file = fs.open(name, "w") file.write(file_data) file.close() ws.send("received file!", false) end local function exportFile(s) local name = s:sub(20,-1) local file = fs.open(name, "r") local file_data = file.readAll() file.close() ws.send(file_data, false) end local function listen() while true do local s = ws.receive() if s:sub(1,19) == "!!sshd_import_file " then importFile(s) elseif s:sub(1,19) == "!!sshd_export_file " then exportFile(s) elseif s == "!!restart" then ws.send("restarting...", false) ws.close() os.sleep(1) shell.run("bg sshd") return elseif s:sub(1,2) == "!!" then ws.send("\1unknown !! command", false) elseif s:sub(1,1) == "!" then wrapTerm(execCommand, s) else wrapTerm(execLua, s) end end end local status, err = pcall(listen) if not status then print(err) end ws.close()