Skip to content

Commit 5cb70b3

Browse files
authored
Merge pull request #1228 from javierbrk/RemoveHttpClient
Remove http client
2 parents cacb2db + e943b90 commit 5cb70b3

4 files changed

Lines changed: 62 additions & 21 deletions

File tree

packages/check-date-http/Makefile

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ define Package/$(PKG_NAME)
99
CATEGORY:=LibreMesh
1010
MAINTAINER:=Gioacchino Mazzurco <gio@altermundi.net>
1111
URL:=http://libremesh.org
12-
DEPENDS:=+libuci-lua +lua \
13-
+luci-lib-httpclient
12+
DEPENDS:=+libuci-lua +lua
1413
PKGARCH:=all
1514
endef
1615

packages/check-date-http/Readme.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ NTP daemon shipped with busybox is not capable to gracefully handle cases of ext
44

55
Check local time against a list of HTTP(s) services, if the time skew is more then 15 minutes, restart sysntpd to force time syncronization, or reset trought date command depending on configuration.
66

7+
Remember to set your time zone in /etc/TZ. For argentina "echo 'UTC3' > /etc/TZ"
8+
will do the job
9+
710
## Configurations
811
You can change the behavior and the list of servers in ```/etc/config/check-date```
912

packages/check-date-http/files/etc/config/check-date

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,4 @@ config timeserver 'http'
99
list server 'http://openwrt.org/'
1010
list server 'http://eigenlab.org/'
1111
list server 'http://www.google.com/'
12-
list server 'http://ip4.me/'
13-
list server 'http://ip6only.me/'
14-
list server 'http://ip6.me/'
12+

packages/check-date-http/files/usr/bin/check-date-http

Lines changed: 57 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,49 @@
44
--! Copyright (C) 2019 Gioacchino Mazzurco <gio@altermundi.net>
55
--! Copyright (C) 2019 Nicolas Echaniz <nicoechaniz@altermundi.net>
66

7-
local http = require("luci.httpclient")
87
local libuci = require("uci")
98

9+
-- Function to get HTTP headers using netcat
10+
local function http_head_request(url)
11+
-- Parse URL to extract host and path
12+
local host = url:gsub("^https?://", ""):gsub("/.*", "")
13+
local port = 80
14+
15+
-- Check if port is specified in host
16+
if host:match(":") then
17+
host, port = host:match("([^:]+):(%d+)")
18+
port = tonumber(port)
19+
end
20+
21+
-- Build netcat command with HTTP HEAD request
22+
local cmd = string.format(
23+
"printf 'HEAD / HTTP/1.1\\r\\nHost: %s\\r\\nConnection: close\\r\\n\\r\\n' | nc %s %d 2>/dev/null",
24+
host, host, port
25+
)
26+
27+
local handle = io.popen(cmd)
28+
if not handle then
29+
return nil
30+
end
31+
32+
local response = handle:read("*a")
33+
local success = handle:close()
34+
35+
if not success or response == "" then
36+
return nil
37+
end
38+
39+
-- Parse headers into a table
40+
local headers = {}
41+
for line in response:gmatch("[^\r\n]+") do
42+
local key, value = line:match("^([^:]+):%s*(.+)$")
43+
if key and value then
44+
headers[key] = value
45+
end
46+
end
47+
48+
return { headers = headers }
49+
end
1050

1151
local config = libuci:cursor()
1252
local url = config:get("check-date", "http", "server")
@@ -19,27 +59,28 @@ restartSysntpd = (restartSysntpd == nil) or restartSysntpd
1959
config = nil
2060

2161
local localCurrDate = assert(io.popen("date --utc -Iminutes", 'r'):read())
22-
local lFormat="(%d+)-(%d+)-(%d+)T(%d+):(%d+)UTC"
62+
local lFormat="(%d+)-(%d+)-(%d+)T(%d+):(%d+)"
2363
local lYear, lMonth, lDay, lHour, lMin = localCurrDate:match(lFormat)
2464

25-
local httpOptions = {}
26-
httpOptions.rcvtimeo = 5
27-
httpOptions.sndtimeo = 5
28-
httpOptions.depth = 0 -- Avoid annoying redirects
29-
httpOptions.method = 'HEAD'
30-
local success, response, algo = pcall(http.request_raw, url, httpOptions)
31-
32-
if(not success) then
33-
print(arg[0], "HTTP request failed", url, success, response, algo)
34-
os.exit(42)
35-
elseif response == nil then
36-
print(arg[0], "The server cannot be reached", url)
37-
os.exit(43)
65+
local response = http_head_request(url)
66+
67+
if not response then
68+
print(arg[0], "HTTP request failed", url)
69+
os.exit(42)
70+
elseif not response.headers or not response.headers['Date'] then
71+
print(arg[0], "The server cannot be reached or Date header missing", url)
72+
os.exit(43)
3873
end
3974

40-
local s = algo['headers']['Date']
75+
local s = response.headers['Date']
4176
local p="%a+, (%d+) (%a+) (%d+) (%d+):(%d+):(%d+) GMT"
4277
local day, month, year, hour, min, sec = s:match(p)
78+
79+
if not day then
80+
print(arg[0], "Could not parse Date header:", s)
81+
os.exit(44)
82+
end
83+
4384
local MON = { Jan="01", Feb="02", Mar="03", Apr="04", May="05", Jun="06", Jul="07", Aug="08", Sep="09", Oct="10", Nov="11", Dec="12"}
4485
month = MON[month]
4586

0 commit comments

Comments
 (0)