Selaa lähdekoodia

Add curl-based HTTP fallback for minimal OpenWrt

Works when no websocket or LuaHTTP library is available.
Gogs 2 kuukautta sitten
vanhempi
sitoutus
3a635317e0
1 muutettua tiedostoa jossa 20 lisäystä ja 8 poistoa
  1. 20 8
      usr/sbin/client2server-luv.lua

+ 20 - 8
usr/sbin/client2server-luv.lua

@@ -23,7 +23,6 @@ local luv_ok, luv = pcall(require, "luv")
 local ws_client = nil
 local has_websocket, websocket = pcall(require, "websocket")
 
-
 if has_websocket then
     ws_client = websocket.client.sync()
 end
@@ -35,6 +34,18 @@ if has_http then
     http_client = http
 end
 
+-- Shell-based HTTP fallback (works on minimal OpenWrt)
+local function http_post_shell(url, data)
+    local cmd = string.format(
+        'curl -s -X POST -H "Content-Type: application/json" -d "%s" "%s" 2>/dev/null',
+        data:gsub('"', '\\"'), url
+    )
+    local f = io.popen(cmd)
+    local resp = f and f:read("*a") or ""
+    if f then f:close() end
+    return resp ~= ""
+end
+
 -- ============================================================================
 -- CONFIG
 -- ============================================================================
@@ -609,21 +620,22 @@ local function main()
                 end
                 
                 -- Fallback to HTTP POST if no WebSocket
-                if not sock and http_client then
-                    -- Send events via HTTP POST
+                if not sock then
+                    -- Send events via curl (shell fallback)
                     local url = cfg.url:gsub("wss", "https"):gsub("ws", "http")
                     if url == cfg.url then url = cfg.url .. "/events" end
                     
                     -- POST buffered events
+                    local sent = false
                     for _, event in ipairs(buffer.events) do
-                        local ok = http_client.request(url, "POST", event, {
-                            ["Content-Type"] = "application/json"
-                        })
-                        if ok then
+                        if http_post_shell(url, event) then
                             log("info", "Event sent: " .. event)
+                            sent = true
                         end
                     end
-                    buffer.clear()
+                    if sent then
+                        buffer.clear()
+                    end
                     sock = "http"  -- Mark as connected
                 end