Annotate

core/portmanager.lua @ 5535:0df0afc041d7

mod_saslauth, mod_compression: Fix some cases where open_stream() was not being passed to/from (see df3c78221f26 and issue #338)
author Matthew Wild <mwild1@gmail.com>
date Mon, 29 Apr 2013 10:43:44 +0100
parent 5432:53b16286509a
child 5549:cce17bcb7c94
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4741
0653476ac3a3 portmanager: Explicitly import some libraries
Matthew Wild <mwild1@gmail.com>
parents: 4687
diff changeset
1 local config = require "core.configmanager";
4856
3e3e282f20a3 portmanager: Support for per-port SSL certificates
Matthew Wild <mwild1@gmail.com>
parents: 4809
diff changeset
2 local certmanager = require "core.certmanager";
4741
0653476ac3a3 portmanager: Explicitly import some libraries
Matthew Wild <mwild1@gmail.com>
parents: 4687
diff changeset
3 local server = require "net.server";
5391
0d49a4e9963b portmanager: use_ipv6 defaults to true if luasocket has ipv6 support
Kim Alvefur <zash@zash.se>
parents: 5319
diff changeset
4 local socket = require "socket";
4542
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5
4741
0653476ac3a3 portmanager: Explicitly import some libraries
Matthew Wild <mwild1@gmail.com>
parents: 4687
diff changeset
6 local log = require "util.logger".init("portmanager");
4542
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7 local multitable = require "util.multitable";
4741
0653476ac3a3 portmanager: Explicitly import some libraries
Matthew Wild <mwild1@gmail.com>
parents: 4687
diff changeset
8 local set = require "util.set";
0653476ac3a3 portmanager: Explicitly import some libraries
Matthew Wild <mwild1@gmail.com>
parents: 4687
diff changeset
9
4857
0991a127ac43 portmanager: Remove unused import of 'package'
Matthew Wild <mwild1@gmail.com>
parents: 4856
diff changeset
10 local table = table;
4744
3be37768720d portmanager: Fix breakage (import ALL the functions)
Matthew Wild <mwild1@gmail.com>
parents: 4743
diff changeset
11 local setmetatable, rawset, rawget = setmetatable, rawset, rawget;
5432
53b16286509a portmanager: import pairs() (thanks Maranda)
Matthew Wild <mwild1@gmail.com>
parents: 5426
diff changeset
12 local type, tonumber, tostring, ipairs, pairs = type, tonumber, tostring, ipairs, pairs;
4744
3be37768720d portmanager: Fix breakage (import ALL the functions)
Matthew Wild <mwild1@gmail.com>
parents: 4743
diff changeset
13
4741
0653476ac3a3 portmanager: Explicitly import some libraries
Matthew Wild <mwild1@gmail.com>
parents: 4687
diff changeset
14 local prosody = prosody;
4542
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 local fire_event = prosody.events.fire_event;
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16
4742
23c2ece2c8bc portmanager: Add module() definition
Matthew Wild <mwild1@gmail.com>
parents: 4741
diff changeset
17 module "portmanager";
23c2ece2c8bc portmanager: Add module() definition
Matthew Wild <mwild1@gmail.com>
parents: 4741
diff changeset
18
4542
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 --- Config
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20
5392
613959dbd0b2 portmanager: Add use_ipv4 option, default to true.
Kim Alvefur <zash@zash.se>
parents: 5391
diff changeset
21 local default_interfaces = { };
613959dbd0b2 portmanager: Add use_ipv4 option, default to true.
Kim Alvefur <zash@zash.se>
parents: 5391
diff changeset
22 local default_local_interfaces = { };
613959dbd0b2 portmanager: Add use_ipv4 option, default to true.
Kim Alvefur <zash@zash.se>
parents: 5391
diff changeset
23 if config.get("*", "use_ipv4") ~= false then
613959dbd0b2 portmanager: Add use_ipv4 option, default to true.
Kim Alvefur <zash@zash.se>
parents: 5391
diff changeset
24 table.insert(default_interfaces, "*");
613959dbd0b2 portmanager: Add use_ipv4 option, default to true.
Kim Alvefur <zash@zash.se>
parents: 5391
diff changeset
25 table.insert(default_local_interfaces, "127.0.0.1");
613959dbd0b2 portmanager: Add use_ipv4 option, default to true.
Kim Alvefur <zash@zash.se>
parents: 5391
diff changeset
26 end
5391
0d49a4e9963b portmanager: use_ipv6 defaults to true if luasocket has ipv6 support
Kim Alvefur <zash@zash.se>
parents: 5319
diff changeset
27 if socket.tcp6 and config.get("*", "use_ipv6") ~= false then
4542
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 table.insert(default_interfaces, "::");
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 table.insert(default_local_interfaces, "::1");
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 end
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 --- Private state
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33
4607
7f45b2cb3c03 portmanager: Add unregister_service(), and allow multiple services with the same name (they get queued)
Matthew Wild <mwild1@gmail.com>
parents: 4598
diff changeset
34 -- service_name -> { service_info, ... }
7f45b2cb3c03 portmanager: Add unregister_service(), and allow multiple services with the same name (they get queued)
Matthew Wild <mwild1@gmail.com>
parents: 4598
diff changeset
35 local services = setmetatable({}, { __index = function (t, k) rawset(t, k, {}); return rawget(t, k); end });
4542
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 -- service_name, interface (string), port (number)
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 local active_services = multitable.new();
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40 --- Private helpers
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41
4546
c686860ef410 portmanager: Pass port to friendly_error_message()
Matthew Wild <mwild1@gmail.com>
parents: 4542
diff changeset
42 local function error_to_friendly_message(service_name, port, err)
4542
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 local friendly_message = err;
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44 if err:match(" in use") then
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45 -- FIXME: Use service_name here
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46 if port == 5222 or port == 5223 or port == 5269 then
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47 friendly_message = "check that Prosody or another XMPP server is "
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 .."not already running and using this port";
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 elseif port == 80 or port == 81 then
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50 friendly_message = "check that a HTTP server is not already using "
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51 .."this port";
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52 elseif port == 5280 then
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53 friendly_message = "check that Prosody or a BOSH connection manager "
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54 .."is not already running";
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
55 else
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56 friendly_message = "this port is in use by another application";
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57 end
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
58 elseif err:match("permission") then
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
59 friendly_message = "Prosody does not have sufficient privileges to use this port";
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
60 end
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61 return friendly_message;
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
62 end
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
63
4608
01d52f31b6b3 portmanager: Support item-added/net-provider (global and shared modules only!)
Matthew Wild <mwild1@gmail.com>
parents: 4607
diff changeset
64 prosody.events.add_handler("item-added/net-provider", function (event)
01d52f31b6b3 portmanager: Support item-added/net-provider (global and shared modules only!)
Matthew Wild <mwild1@gmail.com>
parents: 4607
diff changeset
65 local item = event.item;
01d52f31b6b3 portmanager: Support item-added/net-provider (global and shared modules only!)
Matthew Wild <mwild1@gmail.com>
parents: 4607
diff changeset
66 register_service(item.name, item);
01d52f31b6b3 portmanager: Support item-added/net-provider (global and shared modules only!)
Matthew Wild <mwild1@gmail.com>
parents: 4607
diff changeset
67 end);
01d52f31b6b3 portmanager: Support item-added/net-provider (global and shared modules only!)
Matthew Wild <mwild1@gmail.com>
parents: 4607
diff changeset
68 prosody.events.add_handler("item-removed/net-provider", function (event)
01d52f31b6b3 portmanager: Support item-added/net-provider (global and shared modules only!)
Matthew Wild <mwild1@gmail.com>
parents: 4607
diff changeset
69 local item = event.item;
01d52f31b6b3 portmanager: Support item-added/net-provider (global and shared modules only!)
Matthew Wild <mwild1@gmail.com>
parents: 4607
diff changeset
70 unregister_service(item.name, item);
01d52f31b6b3 portmanager: Support item-added/net-provider (global and shared modules only!)
Matthew Wild <mwild1@gmail.com>
parents: 4607
diff changeset
71 end);
01d52f31b6b3 portmanager: Support item-added/net-provider (global and shared modules only!)
Matthew Wild <mwild1@gmail.com>
parents: 4607
diff changeset
72
5426
1b20095eb230 portmanager: add logic to allow specification of service default values for ssl config and / or overrides.
Marco Cirillo <maranda@lightwitch.org>
parents: 5399
diff changeset
73 local function duplicate_ssl_config(ssl_config)
1b20095eb230 portmanager: add logic to allow specification of service default values for ssl config and / or overrides.
Marco Cirillo <maranda@lightwitch.org>
parents: 5399
diff changeset
74 local ssl_config = type(ssl_config) == "table" and ssl_config or {};
1b20095eb230 portmanager: add logic to allow specification of service default values for ssl config and / or overrides.
Marco Cirillo <maranda@lightwitch.org>
parents: 5399
diff changeset
75
1b20095eb230 portmanager: add logic to allow specification of service default values for ssl config and / or overrides.
Marco Cirillo <maranda@lightwitch.org>
parents: 5399
diff changeset
76 local _config = {};
1b20095eb230 portmanager: add logic to allow specification of service default values for ssl config and / or overrides.
Marco Cirillo <maranda@lightwitch.org>
parents: 5399
diff changeset
77 for k, v in pairs(ssl_config) do
1b20095eb230 portmanager: add logic to allow specification of service default values for ssl config and / or overrides.
Marco Cirillo <maranda@lightwitch.org>
parents: 5399
diff changeset
78 _config[k] = v;
1b20095eb230 portmanager: add logic to allow specification of service default values for ssl config and / or overrides.
Marco Cirillo <maranda@lightwitch.org>
parents: 5399
diff changeset
79 end
1b20095eb230 portmanager: add logic to allow specification of service default values for ssl config and / or overrides.
Marco Cirillo <maranda@lightwitch.org>
parents: 5399
diff changeset
80 return _config;
1b20095eb230 portmanager: add logic to allow specification of service default values for ssl config and / or overrides.
Marco Cirillo <maranda@lightwitch.org>
parents: 5399
diff changeset
81 end
1b20095eb230 portmanager: add logic to allow specification of service default values for ssl config and / or overrides.
Marco Cirillo <maranda@lightwitch.org>
parents: 5399
diff changeset
82
4542
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
83 --- Public API
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
84
4743
70d68e789d93 portmanager: Rename activate_service() to activate() (to match deactivate())
Matthew Wild <mwild1@gmail.com>
parents: 4742
diff changeset
85 function activate(service_name)
4607
7f45b2cb3c03 portmanager: Add unregister_service(), and allow multiple services with the same name (they get queued)
Matthew Wild <mwild1@gmail.com>
parents: 4598
diff changeset
86 local service_info = services[service_name][1];
4542
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
87 if not service_info then
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
88 return nil, "Unknown service: "..service_name;
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
89 end
4615
29a9988c1e1b portmanager: Allow services to specify their config option prefix
Matthew Wild <mwild1@gmail.com>
parents: 4612
diff changeset
90
4616
03d9fe1bcdd3 portmanager: Fix pre-0.9 compatibility by taking default_interface and default_port from the listener instead of service table
Matthew Wild <mwild1@gmail.com>
parents: 4615
diff changeset
91 local listener = service_info.listener;
03d9fe1bcdd3 portmanager: Fix pre-0.9 compatibility by taking default_interface and default_port from the listener instead of service table
Matthew Wild <mwild1@gmail.com>
parents: 4615
diff changeset
92
4615
29a9988c1e1b portmanager: Allow services to specify their config option prefix
Matthew Wild <mwild1@gmail.com>
parents: 4612
diff changeset
93 local config_prefix = (service_info.config_prefix or service_name).."_";
29a9988c1e1b portmanager: Allow services to specify their config option prefix
Matthew Wild <mwild1@gmail.com>
parents: 4612
diff changeset
94 if config_prefix == "_" then
29a9988c1e1b portmanager: Allow services to specify their config option prefix
Matthew Wild <mwild1@gmail.com>
parents: 4612
diff changeset
95 config_prefix = "";
29a9988c1e1b portmanager: Allow services to specify their config option prefix
Matthew Wild <mwild1@gmail.com>
parents: 4612
diff changeset
96 end
4542
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
97
4687
bd3a852b949a portmanager: Fix selecting bind_interfaces from pre-0.9 config options.
Kim Alvefur <zash@zash.se>
parents: 4677
diff changeset
98 local bind_interfaces = config.get("*", config_prefix.."interfaces")
4615
29a9988c1e1b portmanager: Allow services to specify their config option prefix
Matthew Wild <mwild1@gmail.com>
parents: 4612
diff changeset
99 or config.get("*", config_prefix.."interface") -- COMPAT w/pre-0.9
5087
71a5a6a6c74c portmanager: Support 'local_interfaces' config option (default for private listeners like components, telnet, etc.) (thanks mva)
Matthew Wild <mwild1@gmail.com>
parents: 5047
diff changeset
100 or (service_info.private and (config.get("*", "local_interfaces") or default_local_interfaces))
4542
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
101 or config.get("*", "interfaces")
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
102 or config.get("*", "interface") -- COMPAT w/pre-0.9
4616
03d9fe1bcdd3 portmanager: Fix pre-0.9 compatibility by taking default_interface and default_port from the listener instead of service table
Matthew Wild <mwild1@gmail.com>
parents: 4615
diff changeset
103 or listener.default_interface -- COMPAT w/pre0.9
4687
bd3a852b949a portmanager: Fix selecting bind_interfaces from pre-0.9 config options.
Kim Alvefur <zash@zash.se>
parents: 4677
diff changeset
104 or default_interfaces
bd3a852b949a portmanager: Fix selecting bind_interfaces from pre-0.9 config options.
Kim Alvefur <zash@zash.se>
parents: 4677
diff changeset
105 bind_interfaces = set.new(type(bind_interfaces)~="table" and {bind_interfaces} or bind_interfaces);
4542
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
106
5319
d20861bf900b portmanager: Make sure foo_ports is a table
Kim Alvefur <zash@zash.se>
parents: 5225
diff changeset
107 local bind_ports = config.get("*", config_prefix.."ports")
4542
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
108 or service_info.default_ports
4624
3e4715d44561 portmanager: Support 'default_port' in service options
Matthew Wild <mwild1@gmail.com>
parents: 4618
diff changeset
109 or {service_info.default_port
3e4715d44561 portmanager: Support 'default_port' in service options
Matthew Wild <mwild1@gmail.com>
parents: 4618
diff changeset
110 or listener.default_port -- COMPAT w/pre-0.9
5319
d20861bf900b portmanager: Make sure foo_ports is a table
Kim Alvefur <zash@zash.se>
parents: 5225
diff changeset
111 }
d20861bf900b portmanager: Make sure foo_ports is a table
Kim Alvefur <zash@zash.se>
parents: 5225
diff changeset
112 bind_ports = set.new(type(bind_ports) ~= "table" and { bind_ports } or bind_ports );
4542
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
113
4861
2ee71fa500d6 portmanager: Fix missing variable declaration
Matthew Wild <mwild1@gmail.com>
parents: 4857
diff changeset
114 local mode, ssl = listener.default_mode or "*a";
4542
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
115
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
116 for interface in bind_interfaces do
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
117 for port in bind_ports do
5399
13454b2b86bf portmanager: Log error and fail to bind when port is invalid (not a number)
Matthew Wild <mwild1@gmail.com>
parents: 5392
diff changeset
118 local port_number = tonumber(port);
13454b2b86bf portmanager: Log error and fail to bind when port is invalid (not a number)
Matthew Wild <mwild1@gmail.com>
parents: 5392
diff changeset
119 if not port_number then
13454b2b86bf portmanager: Log error and fail to bind when port is invalid (not a number)
Matthew Wild <mwild1@gmail.com>
parents: 5392
diff changeset
120 log("error", "Invalid port number specified for service '%s': %s", service_info.name, tostring(port));
13454b2b86bf portmanager: Log error and fail to bind when port is invalid (not a number)
Matthew Wild <mwild1@gmail.com>
parents: 5392
diff changeset
121 elseif #active_services:search(nil, interface, port_number) > 0 then
4609
83a5377ffea2 portmanager: Fix log message when multiple services are configured to use the same port
Matthew Wild <mwild1@gmail.com>
parents: 4608
diff changeset
122 log("error", "Multiple services configured to listen on the same port ([%s]:%d): %s, %s", interface, port, active_services:search(nil, interface, port)[1][1].service.name or "<unnamed>", service_name or "<unnamed>");
4542
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
123 else
5009
b27ba2c83dd4 portmanager: Show a friendly error message when initializing SSL fails (thanks MattJ for the entire patch that I fixed one line in)
Kim Alvefur <zash@zash.se>
parents: 4902
diff changeset
124 local err;
4856
3e3e282f20a3 portmanager: Support for per-port SSL certificates
Matthew Wild <mwild1@gmail.com>
parents: 4809
diff changeset
125 -- Create SSL context for this service/port
3e3e282f20a3 portmanager: Support for per-port SSL certificates
Matthew Wild <mwild1@gmail.com>
parents: 4809
diff changeset
126 if service_info.encryption == "ssl" then
5426
1b20095eb230 portmanager: add logic to allow specification of service default values for ssl config and / or overrides.
Marco Cirillo <maranda@lightwitch.org>
parents: 5399
diff changeset
127 local ssl_config = duplicate_ssl_config((config.get("*", config_prefix.."ssl") and config.get("*", config_prefix.."ssl")[interface])
1b20095eb230 portmanager: add logic to allow specification of service default values for ssl config and / or overrides.
Marco Cirillo <maranda@lightwitch.org>
parents: 5399
diff changeset
128 or (config.get("*", config_prefix.."ssl") and config.get("*", config_prefix.."ssl")[port])
1b20095eb230 portmanager: add logic to allow specification of service default values for ssl config and / or overrides.
Marco Cirillo <maranda@lightwitch.org>
parents: 5399
diff changeset
129 or config.get("*", config_prefix.."ssl")
1b20095eb230 portmanager: add logic to allow specification of service default values for ssl config and / or overrides.
Marco Cirillo <maranda@lightwitch.org>
parents: 5399
diff changeset
130 or (config.get("*", "ssl") and config.get("*", "ssl")[interface])
1b20095eb230 portmanager: add logic to allow specification of service default values for ssl config and / or overrides.
Marco Cirillo <maranda@lightwitch.org>
parents: 5399
diff changeset
131 or (config.get("*", "ssl") and config.get("*", "ssl")[port])
1b20095eb230 portmanager: add logic to allow specification of service default values for ssl config and / or overrides.
Marco Cirillo <maranda@lightwitch.org>
parents: 5399
diff changeset
132 or config.get("*", "ssl"));
1b20095eb230 portmanager: add logic to allow specification of service default values for ssl config and / or overrides.
Marco Cirillo <maranda@lightwitch.org>
parents: 5399
diff changeset
133 -- add default entries for, or override ssl configuration
1b20095eb230 portmanager: add logic to allow specification of service default values for ssl config and / or overrides.
Marco Cirillo <maranda@lightwitch.org>
parents: 5399
diff changeset
134 if ssl_config and service_info.ssl_config then
1b20095eb230 portmanager: add logic to allow specification of service default values for ssl config and / or overrides.
Marco Cirillo <maranda@lightwitch.org>
parents: 5399
diff changeset
135 for key, value in pairs(service_info.ssl_config) do
1b20095eb230 portmanager: add logic to allow specification of service default values for ssl config and / or overrides.
Marco Cirillo <maranda@lightwitch.org>
parents: 5399
diff changeset
136 if not service_info.ssl_config_override and not ssl_config[key] then
1b20095eb230 portmanager: add logic to allow specification of service default values for ssl config and / or overrides.
Marco Cirillo <maranda@lightwitch.org>
parents: 5399
diff changeset
137 ssl_config[key] = value;
1b20095eb230 portmanager: add logic to allow specification of service default values for ssl config and / or overrides.
Marco Cirillo <maranda@lightwitch.org>
parents: 5399
diff changeset
138 elseif service_info.ssl_config_override then
1b20095eb230 portmanager: add logic to allow specification of service default values for ssl config and / or overrides.
Marco Cirillo <maranda@lightwitch.org>
parents: 5399
diff changeset
139 ssl_config[key] = value;
1b20095eb230 portmanager: add logic to allow specification of service default values for ssl config and / or overrides.
Marco Cirillo <maranda@lightwitch.org>
parents: 5399
diff changeset
140 end
1b20095eb230 portmanager: add logic to allow specification of service default values for ssl config and / or overrides.
Marco Cirillo <maranda@lightwitch.org>
parents: 5399
diff changeset
141 end
1b20095eb230 portmanager: add logic to allow specification of service default values for ssl config and / or overrides.
Marco Cirillo <maranda@lightwitch.org>
parents: 5399
diff changeset
142 end
1b20095eb230 portmanager: add logic to allow specification of service default values for ssl config and / or overrides.
Marco Cirillo <maranda@lightwitch.org>
parents: 5399
diff changeset
143
1b20095eb230 portmanager: add logic to allow specification of service default values for ssl config and / or overrides.
Marco Cirillo <maranda@lightwitch.org>
parents: 5399
diff changeset
144 ssl, err = certmanager.create_context(service_info.name.." port "..port, "server", ssl_config);
5009
b27ba2c83dd4 portmanager: Show a friendly error message when initializing SSL fails (thanks MattJ for the entire patch that I fixed one line in)
Kim Alvefur <zash@zash.se>
parents: 4902
diff changeset
145 if not ssl then
5399
13454b2b86bf portmanager: Log error and fail to bind when port is invalid (not a number)
Matthew Wild <mwild1@gmail.com>
parents: 5392
diff changeset
146 log("error", "Error binding encrypted port for %s: %s", service_info.name, error_to_friendly_message(service_name, port_number, err) or "unknown error");
5009
b27ba2c83dd4 portmanager: Show a friendly error message when initializing SSL fails (thanks MattJ for the entire patch that I fixed one line in)
Kim Alvefur <zash@zash.se>
parents: 4902
diff changeset
147 end
4856
3e3e282f20a3 portmanager: Support for per-port SSL certificates
Matthew Wild <mwild1@gmail.com>
parents: 4809
diff changeset
148 end
5009
b27ba2c83dd4 portmanager: Show a friendly error message when initializing SSL fails (thanks MattJ for the entire patch that I fixed one line in)
Kim Alvefur <zash@zash.se>
parents: 4902
diff changeset
149 if not err then
b27ba2c83dd4 portmanager: Show a friendly error message when initializing SSL fails (thanks MattJ for the entire patch that I fixed one line in)
Kim Alvefur <zash@zash.se>
parents: 4902
diff changeset
150 -- Start listening on interface+port
5399
13454b2b86bf portmanager: Log error and fail to bind when port is invalid (not a number)
Matthew Wild <mwild1@gmail.com>
parents: 5392
diff changeset
151 local handler, err = server.addserver(interface, port_number, listener, mode, ssl);
5009
b27ba2c83dd4 portmanager: Show a friendly error message when initializing SSL fails (thanks MattJ for the entire patch that I fixed one line in)
Kim Alvefur <zash@zash.se>
parents: 4902
diff changeset
152 if not handler then
5399
13454b2b86bf portmanager: Log error and fail to bind when port is invalid (not a number)
Matthew Wild <mwild1@gmail.com>
parents: 5392
diff changeset
153 log("error", "Failed to open server port %d on %s, %s", port_number, interface, error_to_friendly_message(service_name, port_number, err));
5009
b27ba2c83dd4 portmanager: Show a friendly error message when initializing SSL fails (thanks MattJ for the entire patch that I fixed one line in)
Kim Alvefur <zash@zash.se>
parents: 4902
diff changeset
154 else
5399
13454b2b86bf portmanager: Log error and fail to bind when port is invalid (not a number)
Matthew Wild <mwild1@gmail.com>
parents: 5392
diff changeset
155 log("debug", "Added listening service %s to [%s]:%d", service_name, interface, port_number);
13454b2b86bf portmanager: Log error and fail to bind when port is invalid (not a number)
Matthew Wild <mwild1@gmail.com>
parents: 5392
diff changeset
156 active_services:add(service_name, interface, port_number, {
5009
b27ba2c83dd4 portmanager: Show a friendly error message when initializing SSL fails (thanks MattJ for the entire patch that I fixed one line in)
Kim Alvefur <zash@zash.se>
parents: 4902
diff changeset
157 server = handler;
b27ba2c83dd4 portmanager: Show a friendly error message when initializing SSL fails (thanks MattJ for the entire patch that I fixed one line in)
Kim Alvefur <zash@zash.se>
parents: 4902
diff changeset
158 service = service_info;
b27ba2c83dd4 portmanager: Show a friendly error message when initializing SSL fails (thanks MattJ for the entire patch that I fixed one line in)
Kim Alvefur <zash@zash.se>
parents: 4902
diff changeset
159 });
b27ba2c83dd4 portmanager: Show a friendly error message when initializing SSL fails (thanks MattJ for the entire patch that I fixed one line in)
Kim Alvefur <zash@zash.se>
parents: 4902
diff changeset
160 end
4542
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
161 end
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
162 end
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
163 end
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
164 end
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
165 log("info", "Activated service '%s'", service_name);
4607
7f45b2cb3c03 portmanager: Add unregister_service(), and allow multiple services with the same name (they get queued)
Matthew Wild <mwild1@gmail.com>
parents: 4598
diff changeset
166 return true;
4542
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
167 end
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
168
4897
1a90e5225b22 portmanager: Fix to deactivate services when they are unregistered (metatable:iter() wins)
Matthew Wild <mwild1@gmail.com>
parents: 4861
diff changeset
169 function deactivate(service_name, service_info)
4902
a7c799a7a34b portmanager: Match service against service_info (:iter() doesn't match values)
Matthew Wild <mwild1@gmail.com>
parents: 4897
diff changeset
170 for name, interface, port, n, active_service
a7c799a7a34b portmanager: Match service against service_info (:iter() doesn't match values)
Matthew Wild <mwild1@gmail.com>
parents: 4897
diff changeset
171 in active_services:iter(service_name or service_info and service_info.name, nil, nil, nil) do
a7c799a7a34b portmanager: Match service against service_info (:iter() doesn't match values)
Matthew Wild <mwild1@gmail.com>
parents: 4897
diff changeset
172 if service_info == nil or active_service.service == service_info then
a7c799a7a34b portmanager: Match service against service_info (:iter() doesn't match values)
Matthew Wild <mwild1@gmail.com>
parents: 4897
diff changeset
173 close(interface, port);
a7c799a7a34b portmanager: Match service against service_info (:iter() doesn't match values)
Matthew Wild <mwild1@gmail.com>
parents: 4897
diff changeset
174 end
4542
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
175 end
4897
1a90e5225b22 portmanager: Fix to deactivate services when they are unregistered (metatable:iter() wins)
Matthew Wild <mwild1@gmail.com>
parents: 4861
diff changeset
176 log("info", "Deactivated service '%s'", service_name or service_info.name);
4542
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
177 end
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
178
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
179 function register_service(service_name, service_info)
4607
7f45b2cb3c03 portmanager: Add unregister_service(), and allow multiple services with the same name (they get queued)
Matthew Wild <mwild1@gmail.com>
parents: 4598
diff changeset
180 table.insert(services[service_name], service_info);
4542
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
181
4607
7f45b2cb3c03 portmanager: Add unregister_service(), and allow multiple services with the same name (they get queued)
Matthew Wild <mwild1@gmail.com>
parents: 4598
diff changeset
182 if not active_services:get(service_name) then
7f45b2cb3c03 portmanager: Add unregister_service(), and allow multiple services with the same name (they get queued)
Matthew Wild <mwild1@gmail.com>
parents: 4598
diff changeset
183 log("debug", "No active service for %s, activating...", service_name);
4743
70d68e789d93 portmanager: Rename activate_service() to activate() (to match deactivate())
Matthew Wild <mwild1@gmail.com>
parents: 4742
diff changeset
184 local ok, err = activate(service_name);
4607
7f45b2cb3c03 portmanager: Add unregister_service(), and allow multiple services with the same name (they get queued)
Matthew Wild <mwild1@gmail.com>
parents: 4598
diff changeset
185 if not ok then
7f45b2cb3c03 portmanager: Add unregister_service(), and allow multiple services with the same name (they get queued)
Matthew Wild <mwild1@gmail.com>
parents: 4598
diff changeset
186 log("error", "Failed to activate service '%s': %s", service_name, err or "unknown error");
7f45b2cb3c03 portmanager: Add unregister_service(), and allow multiple services with the same name (they get queued)
Matthew Wild <mwild1@gmail.com>
parents: 4598
diff changeset
187 end
4542
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
188 end
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
189
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
190 fire_event("service-added", { name = service_name, service = service_info });
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
191 return true;
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
192 end
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
193
4607
7f45b2cb3c03 portmanager: Add unregister_service(), and allow multiple services with the same name (they get queued)
Matthew Wild <mwild1@gmail.com>
parents: 4598
diff changeset
194 function unregister_service(service_name, service_info)
4897
1a90e5225b22 portmanager: Fix to deactivate services when they are unregistered (metatable:iter() wins)
Matthew Wild <mwild1@gmail.com>
parents: 4861
diff changeset
195 log("debug", "Unregistering service: %s", service_name);
4607
7f45b2cb3c03 portmanager: Add unregister_service(), and allow multiple services with the same name (they get queued)
Matthew Wild <mwild1@gmail.com>
parents: 4598
diff changeset
196 local service_info_list = services[service_name];
7f45b2cb3c03 portmanager: Add unregister_service(), and allow multiple services with the same name (they get queued)
Matthew Wild <mwild1@gmail.com>
parents: 4598
diff changeset
197 for i, service in ipairs(service_info_list) do
7f45b2cb3c03 portmanager: Add unregister_service(), and allow multiple services with the same name (they get queued)
Matthew Wild <mwild1@gmail.com>
parents: 4598
diff changeset
198 if service == service_info then
7f45b2cb3c03 portmanager: Add unregister_service(), and allow multiple services with the same name (they get queued)
Matthew Wild <mwild1@gmail.com>
parents: 4598
diff changeset
199 table.remove(service_info_list, i);
7f45b2cb3c03 portmanager: Add unregister_service(), and allow multiple services with the same name (they get queued)
Matthew Wild <mwild1@gmail.com>
parents: 4598
diff changeset
200 end
7f45b2cb3c03 portmanager: Add unregister_service(), and allow multiple services with the same name (they get queued)
Matthew Wild <mwild1@gmail.com>
parents: 4598
diff changeset
201 end
4897
1a90e5225b22 portmanager: Fix to deactivate services when they are unregistered (metatable:iter() wins)
Matthew Wild <mwild1@gmail.com>
parents: 4861
diff changeset
202 deactivate(nil, service_info);
1a90e5225b22 portmanager: Fix to deactivate services when they are unregistered (metatable:iter() wins)
Matthew Wild <mwild1@gmail.com>
parents: 4861
diff changeset
203 if #service_info_list > 0 then -- Other services registered with this name
1a90e5225b22 portmanager: Fix to deactivate services when they are unregistered (metatable:iter() wins)
Matthew Wild <mwild1@gmail.com>
parents: 4861
diff changeset
204 activate(service_name); -- Re-activate with the next available one
4607
7f45b2cb3c03 portmanager: Add unregister_service(), and allow multiple services with the same name (they get queued)
Matthew Wild <mwild1@gmail.com>
parents: 4598
diff changeset
205 end
4612
8bb93860fe46 portmanager: Fire service-removed on unregister
Matthew Wild <mwild1@gmail.com>
parents: 4609
diff changeset
206 fire_event("service-removed", { name = service_name, service = service_info });
4607
7f45b2cb3c03 portmanager: Add unregister_service(), and allow multiple services with the same name (they get queued)
Matthew Wild <mwild1@gmail.com>
parents: 4598
diff changeset
207 end
7f45b2cb3c03 portmanager: Add unregister_service(), and allow multiple services with the same name (they get queued)
Matthew Wild <mwild1@gmail.com>
parents: 4598
diff changeset
208
4677
05d8b4099cf5 portmanager: Add get_service_at(interface, port) and close(interface, port)
Matthew Wild <mwild1@gmail.com>
parents: 4624
diff changeset
209 function close(interface, port)
05d8b4099cf5 portmanager: Add get_service_at(interface, port) and close(interface, port)
Matthew Wild <mwild1@gmail.com>
parents: 4624
diff changeset
210 local service, server = get_service_at(interface, port);
05d8b4099cf5 portmanager: Add get_service_at(interface, port) and close(interface, port)
Matthew Wild <mwild1@gmail.com>
parents: 4624
diff changeset
211 if not service then
05d8b4099cf5 portmanager: Add get_service_at(interface, port) and close(interface, port)
Matthew Wild <mwild1@gmail.com>
parents: 4624
diff changeset
212 return false, "port-not-open";
05d8b4099cf5 portmanager: Add get_service_at(interface, port) and close(interface, port)
Matthew Wild <mwild1@gmail.com>
parents: 4624
diff changeset
213 end
05d8b4099cf5 portmanager: Add get_service_at(interface, port) and close(interface, port)
Matthew Wild <mwild1@gmail.com>
parents: 4624
diff changeset
214 server:close();
05d8b4099cf5 portmanager: Add get_service_at(interface, port) and close(interface, port)
Matthew Wild <mwild1@gmail.com>
parents: 4624
diff changeset
215 active_services:remove(service.name, interface, port);
05d8b4099cf5 portmanager: Add get_service_at(interface, port) and close(interface, port)
Matthew Wild <mwild1@gmail.com>
parents: 4624
diff changeset
216 log("debug", "Removed listening service %s from [%s]:%d", service.name, interface, port);
05d8b4099cf5 portmanager: Add get_service_at(interface, port) and close(interface, port)
Matthew Wild <mwild1@gmail.com>
parents: 4624
diff changeset
217 return true;
05d8b4099cf5 portmanager: Add get_service_at(interface, port) and close(interface, port)
Matthew Wild <mwild1@gmail.com>
parents: 4624
diff changeset
218 end
05d8b4099cf5 portmanager: Add get_service_at(interface, port) and close(interface, port)
Matthew Wild <mwild1@gmail.com>
parents: 4624
diff changeset
219
05d8b4099cf5 portmanager: Add get_service_at(interface, port) and close(interface, port)
Matthew Wild <mwild1@gmail.com>
parents: 4624
diff changeset
220 function get_service_at(interface, port)
05d8b4099cf5 portmanager: Add get_service_at(interface, port) and close(interface, port)
Matthew Wild <mwild1@gmail.com>
parents: 4624
diff changeset
221 local data = active_services:search(nil, interface, port)[1][1];
05d8b4099cf5 portmanager: Add get_service_at(interface, port) and close(interface, port)
Matthew Wild <mwild1@gmail.com>
parents: 4624
diff changeset
222 return data.service, data.server;
05d8b4099cf5 portmanager: Add get_service_at(interface, port) and close(interface, port)
Matthew Wild <mwild1@gmail.com>
parents: 4624
diff changeset
223 end
05d8b4099cf5 portmanager: Add get_service_at(interface, port) and close(interface, port)
Matthew Wild <mwild1@gmail.com>
parents: 4624
diff changeset
224
4597
25d89c7d6aee portmanager: Add get_service()
Matthew Wild <mwild1@gmail.com>
parents: 4583
diff changeset
225 function get_service(service_name)
5225
079e4cb23f89 portmanager: Return first service with the specified name from get_service() (instead of the array of possible services) (thanks xnyhps)
Matthew Wild <mwild1@gmail.com>
parents: 5087
diff changeset
226 return (services[service_name] or {})[1];
4597
25d89c7d6aee portmanager: Add get_service()
Matthew Wild <mwild1@gmail.com>
parents: 4583
diff changeset
227 end
4542
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
228
4598
d2bcb959d713 portmanager: Add get_active_services()
Matthew Wild <mwild1@gmail.com>
parents: 4597
diff changeset
229 function get_active_services(...)
d2bcb959d713 portmanager: Add get_active_services()
Matthew Wild <mwild1@gmail.com>
parents: 4597
diff changeset
230 return active_services;
d2bcb959d713 portmanager: Add get_active_services()
Matthew Wild <mwild1@gmail.com>
parents: 4597
diff changeset
231 end
d2bcb959d713 portmanager: Add get_active_services()
Matthew Wild <mwild1@gmail.com>
parents: 4597
diff changeset
232
4618
1ec8122ddffe portmanager: Add get_registered_services() to the public API
Matthew Wild <mwild1@gmail.com>
parents: 4617
diff changeset
233 function get_registered_services()
1ec8122ddffe portmanager: Add get_registered_services() to the public API
Matthew Wild <mwild1@gmail.com>
parents: 4617
diff changeset
234 return services;
1ec8122ddffe portmanager: Add get_registered_services() to the public API
Matthew Wild <mwild1@gmail.com>
parents: 4617
diff changeset
235 end
1ec8122ddffe portmanager: Add get_registered_services() to the public API
Matthew Wild <mwild1@gmail.com>
parents: 4617
diff changeset
236
4542
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
237 return _M;