103 lines
2.8 KiB
Lua
103 lines
2.8 KiB
Lua
|
|
local URL_GATE = "http://gitea.fse-media.group/Henry_Du/FSE-Ticket.sys/raw/branch/main/gate.lua"
|
|
local URL_UPDATE_GATE = "http://gitea.fse-media.group/Henry_Du/FSE-Ticket.sys/raw/branch/main/update_gate.lua"
|
|
local args = { ... }
|
|
local silent = AUTO_UPDATE_SILENT == true or args[1] == "--silent"
|
|
|
|
local function getCacheBustedUrl(url)
|
|
local sep = url:find("?") and "&" or "?"
|
|
return url .. sep .. "_t=" .. tostring(os.epoch and os.epoch("utc") or math.random(1, 999999))
|
|
end
|
|
|
|
local function log(msg)
|
|
if not silent then
|
|
print(msg)
|
|
end
|
|
end
|
|
|
|
local function writeFile(path, content, binary)
|
|
local mode = binary and "wb" or "w"
|
|
local f = fs.open(path, mode)
|
|
if not f then return false end
|
|
f.write(content)
|
|
f.close()
|
|
return true
|
|
end
|
|
|
|
local function atomicWrite(path, content, binary)
|
|
local tmp = path .. ".new"
|
|
if fs.exists(tmp) then fs.delete(tmp) end
|
|
if not writeFile(tmp, content, binary) then return false end
|
|
if fs.exists(path) then fs.delete(path) end
|
|
fs.move(tmp, path)
|
|
return true
|
|
end
|
|
|
|
local function httpGet(url)
|
|
if not http then return false, "HTTP API disabled" end
|
|
local okReq, err = pcall(function()
|
|
http.request({ url = url, method = "GET" })
|
|
end)
|
|
if not okReq then return false, tostring(err) end
|
|
|
|
while true do
|
|
local ev, p1, p2, p3 = os.pullEvent()
|
|
if ev == "http_success" and p1 == url then
|
|
local res = p2
|
|
if type(res) == "table" and type(res.readAll) == "function" then
|
|
local data = res.readAll()
|
|
res.close()
|
|
return true, data
|
|
end
|
|
return false, "invalid http response"
|
|
end
|
|
if ev == "http_failure" and p1 == url then
|
|
local err = p2
|
|
local res = p3
|
|
if type(p2) == "table" and type(p2.readAll) == "function" then
|
|
res = p2
|
|
err = p3
|
|
end
|
|
if type(res) == "table" and type(res.readAll) == "function" then
|
|
local data = res.readAll()
|
|
res.close()
|
|
return false, data
|
|
end
|
|
return false, tostring(err or "http_failure")
|
|
end
|
|
end
|
|
end
|
|
|
|
if not silent then
|
|
term.clear()
|
|
term.setCursorPos(1, 1)
|
|
log("Ticket Gate Updater")
|
|
log("")
|
|
end
|
|
log("Downloading gate program...")
|
|
|
|
local ok, code = httpGet(getCacheBustedUrl(URL_GATE))
|
|
if not ok or type(code) ~= "string" or #code == 0 then
|
|
error("Download failed: " .. tostring(code or ""))
|
|
end
|
|
|
|
if not atomicWrite("startup.lua", code, false) then
|
|
error("Write failed: startup.lua")
|
|
end
|
|
atomicWrite("startup", code, false)
|
|
if fs.exists("gate.lua") then atomicWrite("gate.lua", code, false) end
|
|
|
|
log("Downloading updater program...")
|
|
local okUp, codeUp = httpGet(getCacheBustedUrl(URL_UPDATE_GATE))
|
|
if okUp and type(codeUp) == "string" and #codeUp > 0 then
|
|
atomicWrite("update_gate.lua", codeUp, false)
|
|
end
|
|
|
|
if not silent then
|
|
log("")
|
|
log("Done.")
|
|
log("Reboot the computer to apply the update.")
|
|
end
|
|
|
|
return true
|