|
|
@@ -198,15 +198,28 @@ end
|
|
|
local ws = { sock = nil, connected = false, key = "" }
|
|
|
|
|
|
-- Simple base64 encoder
|
|
|
+-- Pure Lua base64 encoder (no external deps)
|
|
|
local function base64_encode(data)
|
|
|
- -- Use openssl for base64 encoding
|
|
|
- local f = io.popen("echo -n '" .. data:gsub("'", "'\\''") .. "' | openssl base64 -A 2>/dev/null")
|
|
|
- if f then
|
|
|
- local result = f:read("*a")
|
|
|
- f:close()
|
|
|
- return result:gsub("%s+$", "")
|
|
|
+ local b64_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
|
|
|
+ local result = {}
|
|
|
+ for i = 1, #data, 3 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_chars, math.floor(b1/4)+1, math.floor(b1/4)+1))
|
|
|
+ table.insert(result, string.sub(b64_chars, ((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_chars, ((b2%16)*4) + math.floor(b3/64)+1, ((b2%16)*4) + math.floor(b3/64)+1))
|
|
|
+ if i+2 > #data then
|
|
|
+ table.insert(result, "=")
|
|
|
+ else
|
|
|
+ table.insert(result, string.sub(b64_chars, (b3%64)+1, (b3%64)+1))
|
|
|
+ end
|
|
|
+ end
|
|
|
end
|
|
|
- return ""
|
|
|
+ return table.concat(result)
|
|
|
end
|
|
|
|
|
|
-- SHA1 (for WebSocket handshake)
|