1
0
mirror of https://github.com/DigitalDevices/octonet.git synced 2023-10-10 13:36:52 +02:00
octonet/octoserve/var/www/updateserver.lua

116 lines
2.9 KiB
Lua
Raw Normal View History

2015-09-11 17:05:43 +02:00
#!/usr/bin/lua
local socket = require("socket")
local url = require("socket.url")
2015-09-11 17:05:43 +02:00
local host = os.getenv("HTTP_HOST")
local proto = os.getenv("SERVER_PROTOCOL")
local query = os.getenv("QUERY_STRING")
function http_print(s)
if s then
io.stdout:write(tostring(s).."\r\n")
else
io.stdout:write("\r\n")
end
end
function SendError(err,desc)
http_print(proto.." "..err)
http_print("Content-Type: text/html")
http_print()
local file = io.open("e404.html")
if file then
local tmp = file:read("*a")
tmp = string.gsub(tmp,"404 Not Found",err .. " " .. desc)
http_print(tmp)
file:close()
end
end
local hex_to_char = function(x)
return string.char(tonumber(x,16))
end
2015-09-11 17:05:43 +02:00
local userver = "download.digital-devices.de/download/linux"
2015-12-07 15:05:09 +01:00
local beta_userver = "download.digital-devices.de/download/linux/beta"
2015-09-11 17:05:43 +02:00
local data = nil
local delimages = false
if query == "set=beta" then
local file = io.open("/config/updateserver","w")
if file then
2015-12-07 15:05:09 +01:00
file:write(beta_userver"\n")
2015-09-11 17:05:43 +02:00
file:close()
delimages = true
end
elseif query == "set=std" then
2015-09-11 17:46:26 +02:00
local file = io.open("/config/updateserver","r")
if file then
2015-09-11 17:05:43 +02:00
file:close()
os.remove("/config/updateserver")
delimages = true
end
elseif query:sub(1,4) == "set=" then
userver = query:sub(5)
if userver ~= "" then
userver = userver:gsub("%%(%x%x)",hex_to_char)
local valid = false
local path = url.parse("http://"..userver)
if path.host then
local ip = socket.dns.toip(path.host)
if ip == nil then
ip = path.host
end
local p1,p2 = ip:match("(%d+)%.(%d+)%.%d+%.%d+")
p1 = tonumber(p1)
p2 = tonumber(p2)
valid = (p1 == 10) or ((p1 == 172) and (p2 >= 16) and (p2 <= 31)) or ((p1 == 192) and (p2 == 168))
end
if valid then
local file = io.open("/config/updateserver","w")
if file then
file:write(userver.."\n")
file:close()
delimages = true
end
else
SendError(400, "Invalid or not local: ".. userver)
2015-09-11 17:05:43 +02:00
end
else
os.remove("/config/updateserver")
delimages = true
end
else
data = "{ \"UpdateServer\":\""
local file = io.open("/config/updateserver","r")
if file then
local tmp = file:read("*l")
file:close()
2015-12-07 15:05:09 +01:00
if tmp ~= beta_userver then
2015-09-11 17:05:43 +02:00
data = data .. tmp
end
end
data = data.."\" }"
end
if delimages then
os.execute("rm -f /config/octonet.*.img")
os.execute("rm -f /config/octonet.*.sha")
end
if data then
http_print(proto.." 200" )
http_print("Pragma: no-cache")
http_print("Cache-Control: no-cache")
2015-09-11 17:05:43 +02:00
http_print("Content-Type: application/json; charset=UTF-8")
http_print(string.format("Content-Length: %d",#data))
http_print()
http_print(data)
else
http_print(proto.." 303")
http_print("Location: http://"..host.."/update.html")
http_print("")
end