mod_net_proxy/mod_net_proxy.lua
changeset 2965 33227efa2cdc
parent 2939 7319fd5dbc89
child 2966 6b01600b9c02
equal deleted inserted replaced
2964:b8834fec4b7e 2965:33227efa2cdc
    10 local softreq = require "util.dependencies".softreq;
    10 local softreq = require "util.dependencies".softreq;
    11 local bit = assert(softreq "bit" or softreq "bit32", "No bit module found. See https://prosody.im/doc/depends#bitop");
    11 local bit = assert(softreq "bit" or softreq "bit32", "No bit module found. See https://prosody.im/doc/depends#bitop");
    12 local hex = require "util.hex";
    12 local hex = require "util.hex";
    13 local ip = require "util.ip";
    13 local ip = require "util.ip";
    14 local net = require "util.net";
    14 local net = require "util.net";
       
    15 local set = require "util.set";
    15 local portmanager = require "core.portmanager";
    16 local portmanager = require "core.portmanager";
    16 
    17 
    17 -- Backwards Compatibility
    18 -- Backwards Compatibility
    18 local function net_ntop_bc(input)
    19 local function net_ntop_bc(input)
    19 	if input:len() == 4 then
    20 	if input:len() == 4 then
   385 	sessions[conn] = nil;
   386 	sessions[conn] = nil;
   386 end
   387 end
   387 
   388 
   388 listener.ondetach = listener.ondisconnect;
   389 listener.ondetach = listener.ondisconnect;
   389 
   390 
   390 -- Initialize the module by processing all configured port mappings
   391 -- Process all configured port mappings and generate a list of mapped ports
       
   392 local mapped_ports = {};
       
   393 local config_mappings = module:get_option("proxy_port_mappings", {});
       
   394 for port, mapping in pairs(config_mappings) do
       
   395 	table.insert(mapped_ports, port);
       
   396 	mappings[port] = {
       
   397 		service_name = mapping,
       
   398 		service = nil,
       
   399 	};
       
   400 end
       
   401 
       
   402 -- Log error message when user manually specifies ports without configuring the necessary port mappings
   391 local config_ports = module:get_option_set("proxy_ports", {});
   403 local config_ports = module:get_option_set("proxy_ports", {});
   392 local config_mappings = module:get_option("proxy_port_mappings", {});
   404 if not config_ports:empty() then
   393 for port in config_ports do
   405 	local missing_ports = config_ports - set.new(mapped_ports);
   394 	if config_mappings[port] ~= nil then
   406 	if not missing_ports:empty() then
   395 		mappings[port] = {
   407 		module:log("error", "Missing port<>service mappings for these ports: %s", tostring(missing_ports));
   396 			service_name = config_mappings[port],
       
   397 			service = nil
       
   398 		};
       
   399 	else
       
   400 		module:log("warn", "No port<>service mapping found for port: %d", port);
       
   401 	end
   408 	end
   402 end
   409 end
   403 
   410 
   404 -- Register the previously declared network listener
   411 -- Register the previously declared network listener
   405 module:provides("net", {
   412 module:provides("net", {
   406 	name = "proxy";
   413 	name = "proxy";
   407 	listener = listener;
   414 	listener = listener;
       
   415 	default_ports = mapped_ports;
   408 });
   416 });