changeset 2961:33227efa2cdc

mod_net_proxy: Automatically listen on all mapped ports if proxy_ports was not configured
author Pascal Mathis <mail@pascalmathis.com>
date Wed, 28 Mar 2018 19:00:13 +0200
parents b8834fec4b7e
children 6b01600b9c02
files mod_net_proxy/README.markdown mod_net_proxy/mod_net_proxy.lua
diffstat 2 files changed, 25 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- 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"
--- 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;
 });