Layer-3 test (both tcp and udp) of non-http services

This commit is contained in:
Tim Wilkinson 2022-07-21 12:16:21 -07:00 committed by Joe AE6XE
parent 6eb4f64d23
commit 12e33e3d21
1 changed files with 29 additions and 4 deletions

View File

@ -171,15 +171,40 @@ if validate then
do do
local proto, hostname, port, path = service:match("^(%w+)://([%w%-%.]+):(%d+)(.*)|...|[^|]+$") local proto, hostname, port, path = service:match("^(%w+)://([%w%-%.]+):(%d+)(.*)|...|[^|]+$")
if valid_hosts[hostname:lower()] then if valid_hosts[hostname:lower()] then
if port == "0" or proto ~= "http" then if port == "0" then
-- no port or not http, so not a link - we can only check the hostname -- no port so not a link - we can only check the hostname so have to assume the service is good
valid_services[#valid_services + 1] = service valid_services[#valid_services + 1] = service
else elseif proto == "http" then
-- http and a port, so looks like a link. http check it -- http so looks like a link. http check it
local status = io.popen("/usr/bin/curl --retry 0 --connect-timeout 2 --speed-time 5 --speed-limit 1000 --silent --output /dev/null --location --write-out '%{http_code}' " .. "http://" .. hostname .. ":" .. port .. path):read("*a") local status = io.popen("/usr/bin/curl --retry 0 --connect-timeout 2 --speed-time 5 --speed-limit 1000 --silent --output /dev/null --location --write-out '%{http_code}' " .. "http://" .. hostname .. ":" .. port .. path):read("*a")
if status == "200" then if status == "200" then
valid_services[#valid_services + 1] = service valid_services[#valid_services + 1] = service
end end
else
-- valid port, but we dont know the protocol (we cannot trust the one defined in the services file because the UI wont set
-- anything but 'tcp'). Check both tcp and udp and assume valid it either is okay
-- tcp
local s = nixio.socket("inet", "stream")
s:setopt("socket", "sndtimeo", 2)
local r = s:connect(hostname, tonumber(port))
s:close()
if r == true then
-- tcp connection succeeded
valid_services[#valid_services + 1] = service
else
-- udp
s = nixio.socket("inet", "dgram")
s:setopt("socket", "rcvtimeo", 2)
s:connect(hostname, tonumber(port))
s:send("")
r = s:recv(0)
s:close()
if r ~= nil then
-- A nil response is an explicity rejection of the udp request. Otherwise we have
-- to assume the service is valid
valid_services[#valid_services + 1] = service
end
end
end end
end end
end end