Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions packages/discovery-provider/nginx_conf/nginx.conf
Original file line number Diff line number Diff line change
Expand Up @@ -320,14 +320,14 @@ http {
resolver 127.0.0.11 valid=30s;
content_by_lua_block {
local port = require "port"
local ip = port.get_public_ip()
local can_get_ip, ip = port.get_public_ip()
local can_connect = port.get_is_port_exposed(ip, 30300)
if can_connect then
if can_get_ip and can_connect then
ngx.status = ngx.HTTP_OK
else
ngx.status = ngx.HTTP_INTERNAL_SERVER_ERROR
end
ngx.say(can_connect)
ngx.say(ip .. "\n" .. tostring(can_connect))
}
}

Expand Down
31 changes: 22 additions & 9 deletions packages/discovery-provider/nginx_conf/port.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,30 @@ local _M = {}

function _M.get_public_ip()
local httpc = http.new()
local res, err = httpc:request_uri("http://ipv4.icanhazip.com")
local ip_services = {
"http://ipv4.icanhazip.com",
"http://ipv4bot.whatismyipaddress.com",
"http://checkip.amazonaws.com",
"http://ipinfo.io/ip",
"http://api.ipify.org"
}

if not res then
ngx.log(ngx.ERR, "error: ", err)
return nil
for _, url in ipairs(ip_services) do
local res, err = httpc:request_uri(url)
if res then
local ip = res.body
ip = string.gsub(ip, "\\", "")
ip = string.gsub(ip, " ", "")
ip = string.gsub(ip, "\n", "")
if ip and ip ~= "" then
return true, ip
end
else
ngx.log(ngx.ERR, "Error contacting ", url, ": ", err)
end
end
local ip = res.body
ip = string.gsub(ip, "\\", "")
ip = string.gsub(ip, " ", "")
ip = string.gsub(ip, "\n", "")
return ip

return false, "Could not get external IP"
end

function _M.get_is_port_exposed(ip, port)
Expand Down