Skip to content

Commit

Permalink
fix(api): fix init servers; gracefully handle port already in use
Browse files Browse the repository at this point in the history
  • Loading branch information
open-dynaMIX committed Aug 23, 2021
1 parent 87fd9e7 commit ebde38c
Showing 1 changed file with 28 additions and 8 deletions.
36 changes: 28 additions & 8 deletions webui.lua
Original file line number Diff line number Diff line change
Expand Up @@ -864,23 +864,43 @@ local function get_ip(udp_method, check_ip)
return ip
end

local function get_server(ipv)
local address = "0.0.0.0"
local udp_method = socket.udp
local check_ip = "91.198.174.192"
local listen_format = "%s:%s"
if ipv == 6 then
address = "::0"
udp_method = socket.udp6
check_ip = "2620:0:862:ed1a::1"
listen_format = "[%s]:%s"
end

local server = {}
local s = socket.bind(address, options.port)
if s == nil then
return {}
end

server.server = s
local ip = get_ip(udp_method, check_ip)

server.listen = listen_format:format(ip, options.port)

return {[address] = server}
end

local function init_servers()
local servers = {}
if not options.ipv4 and not options.ipv6 then
mp.msg.error("Error: ipv4 and ipv6 is disabled!")
return servers
end
if options.ipv6 then
local address = '::0'
servers[address] = {server = socket.bind(address, options.port)}
local ip = get_ip(socket.udp6, "2620:0:862:ed1a::1")
servers[address].listen = "[" .. ip .. "]:" .. options.port
for k,v in pairs(get_server(6)) do servers[k] = v end
end
if options.ipv4 then
local address = '0.0.0.0'
servers[address] = {server = socket.bind(address, options.port)}
local ip = get_ip(socket.udp, "91.198.174.192")
servers[address].listen = ip .. ":" .. options.port
for k,v in pairs(get_server(4)) do servers[k] = v end
end

return servers
Expand Down

0 comments on commit ebde38c

Please sign in to comment.