# HG changeset patch # User Pascal Mathis # Date 1522256413 -7200 # Node ID 33227efa2cdcc5e29c8cd6d97a101adb1f0204e8 # Parent b8834fec4b7e63f98c97b9d70fe09f138355e184 mod_net_proxy: Automatically listen on all mapped ports if proxy_ports was not configured diff -r b8834fec4b7e -r 33227efa2cdc mod_net_proxy/README.markdown --- a/mod_net_proxy/README.markdown Wed Mar 28 00:02:37 2018 +0200 +++ b/mod_net_proxy/README.markdown Wed Mar 28 19:00:13 2018 +0200 @@ -36,7 +36,13 @@ that should be exposed with PROXY protocol support: ```lua -proxy_ports = {15222, 15269} +--[[ + Hint: While you can manually override the ports this module is listening on with + the "proxy_ports" directive, it is highly recommended to not set it and instead + only configure the appropriate mappings with "proxy_port_mappings", which will + automatically start listening on all mapped ports. +]]-- + proxy_port_mappings = { [15222] = "c2s", [15269] = "s2s" @@ -84,7 +90,6 @@ ```lua c2s_ports = {5222} s2s_ports = {5269} -proxy_ports = {15222, 15269} proxy_port_mappings = { [15222] = "c2s", [15269] = "s2s" diff -r b8834fec4b7e -r 33227efa2cdc mod_net_proxy/mod_net_proxy.lua --- a/mod_net_proxy/mod_net_proxy.lua Wed Mar 28 00:02:37 2018 +0200 +++ b/mod_net_proxy/mod_net_proxy.lua Wed Mar 28 19:00:13 2018 +0200 @@ -12,6 +12,7 @@ local hex = require "util.hex"; local ip = require "util.ip"; local net = require "util.net"; +local set = require "util.set"; local portmanager = require "core.portmanager"; -- Backwards Compatibility @@ -387,17 +388,23 @@ listener.ondetach = listener.ondisconnect; --- Initialize the module by processing all configured port mappings -local config_ports = module:get_option_set("proxy_ports", {}); +-- Process all configured port mappings and generate a list of mapped ports +local mapped_ports = {}; local config_mappings = module:get_option("proxy_port_mappings", {}); -for port in config_ports do - if config_mappings[port] ~= nil then - mappings[port] = { - service_name = config_mappings[port], - service = nil - }; - else - module:log("warn", "No port<>service mapping found for port: %d", port); +for port, mapping in pairs(config_mappings) do + table.insert(mapped_ports, port); + mappings[port] = { + service_name = mapping, + service = nil, + }; +end + +-- Log error message when user manually specifies ports without configuring the necessary port mappings +local config_ports = module:get_option_set("proxy_ports", {}); +if not config_ports:empty() then + local missing_ports = config_ports - set.new(mapped_ports); + if not missing_ports:empty() then + module:log("error", "Missing port<>service mappings for these ports: %s", tostring(missing_ports)); end end @@ -405,4 +412,5 @@ module:provides("net", { name = "proxy"; listener = listener; + default_ports = mapped_ports; });