|
|
@@ -199,24 +199,13 @@ local ws = { sock = nil, connected = false, key = "" }
|
|
|
|
|
|
-- Simple base64 encoder
|
|
|
local function base64_encode(data)
|
|
|
- local b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
|
|
|
- local result = {}
|
|
|
- local i = 1
|
|
|
- while i <= #data do
|
|
|
- local b1, b2, b3 = string.byte(data, i, i+2)
|
|
|
- b2 = b2 or 0
|
|
|
- b3 = b3 or 0
|
|
|
- table.insert(result, string.sub(b64, math.floor(b1/4)+1, math.floor(b1/4)+1))
|
|
|
- table.insert(result, string.sub(b64, ((b1%16)*4) + math.floor(b2/16)+1, ((b1%16)*4) + math.floor(b2/16)+1))
|
|
|
- if i+1 > #data then table.insert(result, "=") else
|
|
|
- table.insert(result, string.sub(b64, ((b2%16)*4) + math.floor(b3/64)+1, ((b2%16)*4) + math.floor(b3/64)+1))
|
|
|
- end
|
|
|
- if i+2 > #data then table.insert(result, "=") else
|
|
|
- table.insert(result, string.sub(b64, (b3%64)+1, (b3%64)+1))
|
|
|
- end
|
|
|
- i = i + 3
|
|
|
+ local f = io.popen("echo -n '" .. data:gsub("'", "'\\''") .. "' | base64 | tr -d '\\n' 2>/dev/null")
|
|
|
+ if f then
|
|
|
+ local result = f:read("*a")
|
|
|
+ f:close()
|
|
|
+ return result:gsub("%s+$", "")
|
|
|
end
|
|
|
- return table.concat(result)
|
|
|
+ return ""
|
|
|
end
|
|
|
|
|
|
-- SHA1 (for WebSocket handshake)
|
|
|
@@ -254,7 +243,9 @@ function ws.connect(url)
|
|
|
-- Generate random Sec-WebSocket-Key
|
|
|
local key = ""
|
|
|
for i = 1, 16 do key = key .. string.char(math.random(32, 126)) end
|
|
|
+ if cfg.debug then log("debug", "WS Key raw: " .. #key) end
|
|
|
key = base64_encode(key)
|
|
|
+ if cfg.debug then log("debug", "WS Key b64: " .. key) end
|
|
|
ws.key = key
|
|
|
|
|
|
local request = "GET /ws HTTP/1.1\r\n" ..
|
|
|
@@ -720,6 +711,9 @@ local function main()
|
|
|
if pf then pf:write(pid); pf:close() end
|
|
|
|
|
|
-- Connect
|
|
|
+ -- Seed random
|
|
|
+ math.randomseed(os.time())
|
|
|
+
|
|
|
log_info("Initial connection...")
|
|
|
local sock = ws.connect(cfg.server_url)
|
|
|
|