Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Govos
- -- An OS created for the Keansian government for security or something.
- function randbytes(len)
- local out = ""
- for i = 1, len do
- out = out .. string.char(math.random(0, 255))
- end
- return out
- end
- function fetch(u)
- local h = http.get(u)
- local c = h.readAll()
- h.close()
- return c
- end
- function fwrite(n, c)
- local f = fs.open(n, "w")
- f.write(c)
- f.close()
- end
- -- Read file "n"
- function fread(n)
- local f = fs.open(n, "r")
- local out = f.readAll()
- f.close()
- return out
- end
- local function set(k, v)
- settings.set(k, v)
- settings.save(".settings")
- end
- local function map(f, t)
- local mapper = function(t)
- local new = {}
- for k, v in pairs(t) do
- local new_v, new_k = f(v, k)
- new[new_k or k] = new_v
- end
- return new
- end
- if t then return mapper(t) else return mapper end
- end
- local function arrayize(t)
- local out = {}
- for k, v in pairs(t) do table.insert(out, v) end
- return out
- end
- local this_file = "autorun"
- local this_file_URL = "https://pastebin.com/raw/Lhs7fMD0"
- local files = {
- ["https://pastebin.com/raw/HL0SZhJG"] = "startup", -- "Polychoron" process manager - needs to be startup for TLCOing
- [this_file_URL] = this_file,
- ["https://pastebin.com/raw/Frv3xkB9"] = "yafss",
- ["https://raw.githubusercontent.com/rxi/json.lua/bee7ee3431133009a97257bde73da8a34e53c15c/json.lua"] = "json",
- ["https://raw.githubusercontent.com/osmarks/skynet/master/client.lua"] = "skynet",
- ["https://pastebin.com/raw/DKriPmPe"] = "kristminer",
- }
- local function install()
- fs.makeDir "userdata"
- local fns = arrayize(map(function(filename, URL)
- return function()
- local dir = fs.getDir(filename)
- if dir ~= "" then
- if not fs.isDir(dir) then fs.makeDir(dir) end
- end
- if fs.isDir(filename) then fs.delete(filename) end
- fwrite(filename, fetch(URL))
- if not hidden then print("Downloaded", filename) end
- end
- end, files))
- parallel.waitForAll(unpack(fns))
- set("shell.allow_disk_startup", false)
- set("shell.allow_startup", true)
- os.setComputerLabel("Govos/" .. randbytes(64))
- os.reboot()
- end
- local args = table.concat({...}, " ")
- if (not polychoron or not fs.exists "json" or not fs.exists "userdata") or args:find "update" then install() end
- local background_task_interval = 300 + (os.getComputerID() % 20)
- local function update_checker()
- sleep()
- local this = fread(this_file)
- while true do
- local new = fetch(this_file_URL)
- local ok, err = load(new)
- if not ok then print "Syntax error in update:" printError(err)
- else
- if new ~= this then
- install()
- end
- end
- sleep(background_task_interval)
- end
- end
- local j = require "json"
- local fcache = {}
- local function fproxy(file)
- if fcache[file] then return fcache[file]
- else
- local ok, t = pcall(fread, file)
- if not ok then return 'printError "Error. Try again later, or reboot, or run upd."' end
- fcache[file] = t
- return t
- end
- end
- local function user_programs()
- local govos = {
- update = function()
- shell.run "autorun update"
- end,
- version = function()
- return "Govos 1.0 Pihsrotatcid"
- end
- }
- local API_overrides = {
- govos = govos,
- os = { version = govos.version },
- safe_serialize = safe_serialize,
- ["~expect"] = _G["~expect"]
- }
- local function add(module)
- local ok, res = pcall(require, module)
- if ok then
- API_overrides[module] = res
- end
- end
- add "skynet"
- local FS_overlay = {
- ["/rom/programs/upd.lua"] = [[govos.update()]],
- ["/rom/programs/kristminer.lua"] = fproxy "kristminer"
- }
- require "yafss"(
- "/userdata/",
- FS_overlay,
- API_overrides,
- { URL = "https://pastebin.com/raw/KJjpnJy2" }
- )
- end
- function safe_json_serialize(x, prev)
- local t = type(x)
- if t == "number" then
- if x ~= x or x <= -math.huge or x >= math.huge then
- return tostring(x)
- end
- return string.format("%.14g", x)
- elseif t == "string" then
- return j.encode(x)
- elseif t == "table" then
- prev = prev or {}
- local as_array = true
- local max = 0
- for k in pairs(x) do
- if type(k) ~= "number" then as_array = false break end
- if k > max then max = k end
- end
- if as_array then
- for i = 1, max do
- if x[i] == nil then as_array = false break end
- end
- end
- if as_array then
- local res = {}
- for i, v in ipairs(x) do
- table.insert(res, safe_json_serialize(v))
- end
- return "["..table.concat(res, ",").."]"
- else
- local res = {}
- for k, v in pairs(x) do
- table.insert(res, j.encode(tostring(k)) .. ":" .. safe_json_serialize(v))
- end
- return "{"..table.concat(res, ",").."}"
- end
- elseif t == "boolean" then
- return tostring(x)
- elseif x == nil then
- return "null"
- else
- return j.encode(tostring(x))
- end
- end
- function _G.os.await_event(filter)
- while true do
- local ev = {coroutine.yield(filter)}
- if filter == nil or ev[1] == filter then
- return unpack(ev)
- end
- end
- end
- -- Powered by SPUDNET, the simple way to include remote debugging services in *your* OS. Contact Gollark today.
- local function spudnet()
- if not http or not http.websocket then return "Websockets do not actually exist on this platform" end
- local ws
- local function send_packet(msg)
- --ws.send(safe_serialize(msg))
- ws.send(safe_json_serialize(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://spudnet.osmarks.net/v4"
- if not ws then print("websocket failure %s", err) return false end
- ws.url = "wss://spudnet.osmarks.net/v4"
- send_packet { type = "identify" }
- send_packet { type = "set_channels", channels = { "client:potatOS" } }
- print("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()
- while true do
- local e, u, x = os.await_event "websocket_message"
- if u == ws.url then return j.decode(x) end
- end
- end
- 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
- print "timed out, attempting reconnect"
- try_connect_loop()
- end
- end
- end, "ping-timeout")
- while true do
- -- Receive and run code which is sent via SPUDNET
- -- 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
- print("SPUDNET error %s %s %s %s", packet["for"], packet.error, packet.detail, textutils.serialise(packet))
- elseif packet.type == "message" then
- local code = packet.data
- if type(code) == "string" then
- _G.wsrecv = recv
- _G.wssend = send
- _G.envrequire = require
- --add_log("SPUDNET command - %s", code)
- local f, errr = load(code, "@<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
- local fcache = {}
- local function fproxy(file)
- if fcache[file] then return fcache[file]
- else
- local ok, t = pcall(fread, file)
- if not ok then return 'printError "Error. Try again later, or reboot, or run upd."' end
- fcache[file] = t
- return t
- end
- end
- process.spawn(update_checker, "upd")
- process.spawn(spudnet, "spudnet")
- process.spawn(user_programs, "user")
- while true do coroutine.yield() end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement