Skip to content

Commit ba17e17

Browse files
committed
Rework OAuth server launch code to avoid shared port usage
1 parent 1a58cba commit ba17e17

1 file changed

Lines changed: 22 additions & 2 deletions

File tree

src/LaunchServer.lua

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,27 @@
11
-- Start a server
22
local url = ...
3-
local socket = require("socket")
4-
local server = assert(socket.bind("*", 49082) or socket.bind("*", 49083) or socket.bind("*", 49084))
3+
local luaSocket = require("socket")
4+
-- localhost redirects to 127.0.0.1 AKA IPv4. luaSocket.bind() will use IPv6 if something is already running on the IPv4 host
5+
local server = luaSocket.tcp4()
6+
local function bindSocket()
7+
local res, err
8+
-- `reuseaddr` being true here would allow other applications to reuse the same port
9+
server:setoption("reuseaddr", false)
10+
-- Bind to localhost explicitly instead of all interfaces because LuaSocket doesn't recognize something running on localhost as conflicting with `*` or `0.0.0.0`
11+
res, err = server:bind("localhost", 49082) or server:bind("localhost", 49083) or server:bind("localhost", 49084)
12+
if not res then
13+
server:close()
14+
else
15+
res, err = server:listen(1)
16+
if not res then
17+
server:close()
18+
else
19+
return server
20+
end
21+
end
22+
return nil, err
23+
end
24+
assert(bindSocket())
525
local host, port = server:getsockname()
626
ConPrintf("Server started on %s:%s", host, port)
727

0 commit comments

Comments
 (0)