Use EXPOSE_PORTS to chose what remote ports are exposed locally
@@ -73,15 +73,10 @@ local function make_receiver(dest)
end)
end
-local function on_message(err, data, addr)
+local function make_sender(dest)
+ return wrap(function (err, data, addr)
assert(not err, err)
- if not data or #data < 2 then return end
local source = addr.port
- local dest = bor(
- lshift(byte(data, 1), 8),
- byte(data, 2)
- )
- data = sub(data, 3)
p('udp-req -> ' .. transport_name, {
source = source,
dest = dest,
@@ -92,6 +87,7 @@ local function on_message(err, data, addr)
dest = dest,
data = data,
}
+ end)
end
wrap(function ()
@@ -101,16 +97,19 @@ wrap(function ()
read = decoder(read, udp.decode)
write = encoder(write, udp.encode)
- -- Listen on known port for messages with target port embedded in first 2
- -- bytes
+ -- Expose remote ports locally if requested via EXPOSE_PORTS env variable.
+ local host = getenv("HOST") or "127.0.0.1"
+ local expose = getenv 'EXPOSE_PORTS'
+ if expose then
+ for dest in expose:gmatch("%d+") do
+ dest = tonumber(dest)
local server = uv.new_udp()
- local host = (getenv 'HOST') or '127.0.0.1'
- local port = getenv 'PORT'
- port = port and tonumber(port) or 5000
- assert(server:bind(host, port))
- print 'Communications service listening UDP:'
- p(server:getsockname())
- assert(server:recv_start(wrap(on_message)))
+ assert(server:bind(host, dest))
+ assert(server:recv_start(make_sender(dest)))
+ print 'Communications service forwarding UDP:'
+ p("repeater", server:getsockname())
+ end
+ end
-- Listen for messages over the custom transport
for message in read do
