Changeset

4622:9eccb5a27989

Merge with Zash
author Matthew Wild <mwild1@gmail.com>
date Thu, 15 Mar 2012 03:19:35 +0000
parents 4620:e9dc6ae68c69 (diff) 4621:0445a543ae57 (current diff)
children 4623:403b56b78018
files plugins/s2s/mod_s2s.lua
diffstat 9 files changed, 175 insertions(+), 31 deletions(-) [+]
line wrap: on
line diff
--- a/core/moduleapi.lua	Sun Mar 11 21:37:55 2012 +0100
+++ b/core/moduleapi.lua	Thu Mar 15 03:19:35 2012 +0000
@@ -22,6 +22,7 @@
 
 local prosody = prosody;
 local hosts = prosody.hosts;
+local core_post_stanza = prosody.core_post_stanza;
 
 -- Registry of shared module data
 local shared_data = setmetatable({}, { __mode = "v" });
@@ -50,6 +51,7 @@
 	local _log = logger.init("mod_"..self.name);
 	self.log = function (self, ...) return _log(...); end;
 	self._log = _log;
+	self.global = true;
 end
 
 function api:add_feature(xmlns)
@@ -291,4 +293,22 @@
 	end
 end
 
+function api:provides(name, item)
+	if not item then item = self.environment; end
+	if not item.name then
+		local item_name = module.name;
+		-- Strip a provider prefix to find the item name
+		-- (e.g. "auth_foo" -> "foo" for an auth provider)
+		if item_name:find(name.."_", 1, true) == 1 then
+			item_name = item_name:sub(#name+2);
+		end
+		item.name = item_name;
+	end
+	self:add_item(name, item);
+end
+
+function api:send(stanza)
+	return core_post_stanza(hosts[self.host], stanza);
+end
+
 return api;
--- a/core/modulemanager.lua	Sun Mar 11 21:37:55 2012 +0100
+++ b/core/modulemanager.lua	Thu Mar 15 03:19:35 2012 +0000
@@ -101,11 +101,12 @@
 	end
 	
 	if mod.module.items then -- remove items
+		local events = (host == "*" and prosody.events) or hosts[host].events;
 		for key,t in pairs(mod.module.items) do
 			for i = #t,1,-1 do
 				local value = t[i];
 				t[i] = nil;
-				hosts[host].events.fire_event("item-removed/"..key, {source = mod.module, item = value});
+				events.fire_event("item-removed/"..key, {source = mod.module, item = value});
 			end
 		end
 	end
@@ -122,6 +123,7 @@
 	
 	if not modulemap[host] then
 		modulemap[host] = {};
+		hosts[host].modules = modulemap[host];
 	end
 	
 	if modulemap[host][module_name] then
@@ -147,8 +149,6 @@
 	api_instance.environment = pluginenv;
 	
 	setfenv(mod, pluginenv);
-	hosts[host].modules = modulemap[host];
-	modulemap[host][module_name] = pluginenv;
 	
 	local ok, err = pcall(mod);
 	if ok then
@@ -160,15 +160,18 @@
 			end
 		end
 
-		-- Use modified host, if the module set one
-		if api_instance.host == "*" and host ~= "*" then
-			modulemap[host][module_name] = nil;
-			modulemap["*"][module_name] = pluginenv;
-			api_instance:set_global();
+		modulemap[pluginenv.module.host][module_name] = pluginenv;
+		if pluginenv.module.host == "*" then
+			if not pluginenv.module.global then -- COMPAT w/pre-0.9
+				log("warn", "mod_%s: Setting module.host = '*' deprecated, call module:set_global() instead", module_name);
+				api_instance:set_global();
+			end
+		else
+			hosts[host].modules[module_name] = pluginenv;
 		end
-	else
+	end
+	if not ok then
 		log("error", "Error initializing module '%s' on '%s': %s", module_name, host, err or "nil");
-		do_unload_module(api_instance.host, module_name); -- Ignore error, module may be partially-loaded
 	end
 	return ok and pluginenv, err;
 end
--- a/core/portmanager.lua	Sun Mar 11 21:37:55 2012 +0100
+++ b/core/portmanager.lua	Thu Mar 15 03:19:35 2012 +0000
@@ -13,8 +13,8 @@
 
 --- Private state
 
--- service_name -> service_info
-local services = {};
+-- service_name -> { service_info, ... }
+local services = setmetatable({}, { __index = function (t, k) rawset(t, k, {}); return rawget(t, k); end });
 
 -- service_name, interface (string), port (number)
 local active_services = multitable.new();
@@ -52,28 +52,42 @@
 
 module("portmanager", package.seeall);
 
+prosody.events.add_handler("item-added/net-provider", function (event)
+	local item = event.item;
+	register_service(item.name, item);
+end);
+prosody.events.add_handler("item-removed/net-provider", function (event)
+	local item = event.item;
+	unregister_service(item.name, item);
+end);
+
 --- Public API
 
 function activate_service(service_name)
-	local service_info = services[service_name];
+	local service_info = services[service_name][1];
 	if not service_info then
 		return nil, "Unknown service: "..service_name;
 	end
+	
+	local listener = service_info.listener;
 
-	local bind_interfaces = set.new(config.get("*", service_name.."_interfaces")
-		or config.get("*", service_name.."_interface") -- COMPAT w/pre-0.9
+	local config_prefix = (service_info.config_prefix or service_name).."_";
+	if config_prefix == "_" then
+		config_prefix = "";
+	end
+
+	local bind_interfaces = set.new(config.get("*", config_prefix.."interfaces")
+		or config.get("*", config_prefix.."interface") -- COMPAT w/pre-0.9
 		or (service_info.private and default_local_interfaces)
 		or config.get("*", "interfaces")
 		or config.get("*", "interface") -- COMPAT w/pre-0.9
-		or service_info.default_interface -- COMPAT w/pre0.9
+		or listener.default_interface -- COMPAT w/pre0.9
 		or default_interfaces);
 	
-	local bind_ports = set.new(config.get("*", service_name.."_ports")
-		or (service_info.multiplex and config.get("*", "ports"))
+	local bind_ports = set.new(config.get("*", config_prefix.."ports")
 		or service_info.default_ports
-		or {service_info.default_port});
+		or {listener.default_port}); -- COMPAT w/pre-0.9
 
-	local listener = service_info.listener;
 	local mode = listener.default_mode or "*a";
 	local ssl;
 	if service_info.encryption == "ssl" then
@@ -85,8 +99,8 @@
 	
 	for interface in bind_interfaces do
 		for port in bind_ports do
-			if not service_info.multiplex and #active_services:search(nil, interface, port) > 0 then
-				log("error", "Multiple services configured to listen on the same port: %s, %s", table.concat(active_services:search(nil, interface, port), ", "), service_name);
+			if #active_services:search(nil, interface, port) > 0 then
+				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>");
 			else
 				local handler, err = server.addserver(interface, port, listener, mode, ssl);
 				if not handler then
@@ -102,6 +116,7 @@
 		end
 	end
 	log("info", "Activated service '%s'", service_name);
+	return true;
 end
 
 function deactivate(service_name)
@@ -118,16 +133,36 @@
 end
 
 function register_service(service_name, service_info)
-	services[service_name] = service_info;
+	table.insert(services[service_name], service_info);
 
-	if not active_services[service_name] then
-		activate_service(service_name);
+	if not active_services:get(service_name) then
+		log("debug", "No active service for %s, activating...", service_name);
+		local ok, err = activate_service(service_name);
+		if not ok then
+			log("error", "Failed to activate service '%s': %s", service_name, err or "unknown error");
+		end
 	end
 	
 	fire_event("service-added", { name = service_name, service = service_info });
 	return true;
 end
 
+function unregister_service(service_name, service_info)
+	local service_info_list = services[service_name];
+	for i, service in ipairs(service_info_list) do
+		if service == service_info then
+			table.remove(service_info_list, i);
+		end
+	end
+	if active_services[service_name] == service_info then
+		deactivate(service_name);
+		if #service_info_list > 0 then -- Other services registered with this name
+			activate(service_name); -- Re-activate with the next available one
+		end
+	end
+	fire_event("service-removed", { name = service_name, service = service_info });
+end
+
 function get_service(service_name)
 	return services[service_name];
 end
@@ -136,4 +171,8 @@
 	return active_services;
 end
 
+function get_registered_services()
+	return services;
+end
+
 return _M;
--- a/plugins/mod_auth_internal_hashed.lua	Sun Mar 11 21:37:55 2012 +0100
+++ b/plugins/mod_auth_internal_hashed.lua	Thu Mar 15 03:19:35 2012 +0000
@@ -54,7 +54,7 @@
 
 function new_hashpass_provider(host)
 	local provider = { name = "internal_hashed" };
-	log("debug", "initializing hashpass authentication provider for host '%s'", host);
+	log("debug", "initializing internal_hashed authentication provider for host '%s'", host);
 
 	function provider.test_password(username, password)
 		local credentials = datamanager.load(username, host, "accounts") or {};
--- a/plugins/mod_auth_internal_plain.lua	Sun Mar 11 21:37:55 2012 +0100
+++ b/plugins/mod_auth_internal_plain.lua	Thu Mar 15 03:19:35 2012 +0000
@@ -23,7 +23,7 @@
 
 function new_default_provider(host)
 	local provider = { name = "internal_plain" };
-	log("debug", "initializing default authentication provider for host '%s'", host);
+	log("debug", "initializing internal_plain authentication provider for host '%s'", host);
 
 	function provider.test_password(username, password)
 		log("debug", "test password '%s' for user %s at host %s", password, username, module.host);
--- a/plugins/mod_c2s.lua	Sun Mar 11 21:37:55 2012 +0100
+++ b/plugins/mod_c2s.lua	Thu Mar 15 03:19:35 2012 +0000
@@ -220,15 +220,20 @@
 	sessions[conn] = session;
 end
 
-portmanager.register_service("c2s", {
+module:add_item("net-provider", {
+	name = "c2s";
 	listener = listener;
 	default_port = 5222;
 	encryption = "starttls";
 });
 
-portmanager.register_service("legacy_ssl", {
+module:add_item("net-provider", {
+	name = "legacy_ssl";
 	listener = listener;
 	encryption = "ssl";
+	multiplex = {
+		pattern = "^<.*:stream.*%sxmlns%s*=%s*(['\"])jabber:client%1.*>";
+	};
 });
 
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/plugins/mod_net_multiplex.lua	Thu Mar 15 03:19:35 2012 +0000
@@ -0,0 +1,70 @@
+module:set_global();
+
+local max_buffer_len = module:get_option_number("multiplex_buffer_size", 1024);
+
+local portmanager = require "core.portmanager";
+
+local available_services = {};
+
+local function add_service(service)
+	local multiplex_pattern = service.multiplex and service.multiplex.pattern;
+	if multiplex_pattern then
+		module:log("debug", "Adding multiplex service %q with pattern %q", service.name, multiplex_pattern);
+		available_services[service] = multiplex_pattern;
+	else
+		module:log("debug", "Service %q is not multiplex-capable", service.name);
+	end
+end
+module:hook("service-added", function (event) add_service(event.service); end);
+module:hook("service-removed", function (event)	available_services[event.service] = nil; end);
+
+for service_name, services in pairs(portmanager.get_registered_services()) do
+	for i, service in ipairs(services) do
+		add_service(service);
+	end
+end
+
+local buffers = {};
+
+local listener = { default_mode = "*a" };
+
+function listener.onconnect()
+end
+
+function listener.onincoming(conn, data)
+	if not data then return; end
+	local buf = buffers[conn];
+	buffers[conn] = nil;
+	buf = buf and buf..data or data;
+	for service, multiplex_pattern in pairs(available_services) do
+		if buf:match(multiplex_pattern) then
+			module:log("debug", "Routing incoming connection to %s", service.name);
+			local listener = service.listener;
+			conn:setlistener(listener);
+			local onconnect = listener.onconnect;
+			if onconnect then onconnect(conn) end
+			return listener.onincoming(conn, buf);
+		end
+	end
+	if #buf > max_buffer_len then -- Give up
+		conn:close();
+	else
+		buffers[conn] = buf;
+	end
+end
+
+function listener.ondisconnect(conn, err)
+	buffers[conn] = nil; -- warn if no buffer?
+end
+
+module:add_item("net-provider", {
+	name = "multiplex";
+	config_prefix = "";
+	listener = listener;
+});
+
+module:provides("net", {
+	name = "multiplex_ssl";
+	config_prefix = "ssl";
+	listener = listener;
+});
--- a/plugins/s2s/mod_s2s.lua	Sun Mar 11 21:37:55 2012 +0100
+++ b/plugins/s2s/mod_s2s.lua	Thu Mar 15 03:19:35 2012 +0000
@@ -464,9 +464,13 @@
 
 s2sout.set_listener(listener);
 
-require "core.portmanager".register_service("s2s", {
+module:add_item("net-provider", {
+	name = "s2s";
 	listener = listener;
 	default_port = 5269;
 	encryption = "starttls";
+	multiplex = {
+		pattern = "^<.*:stream.*%sxmlns%s*=%s*(['\"])jabber:server%1.*>";
+	};
 });
 
--- a/plugins/s2s/s2sout.lib.lua	Sun Mar 11 21:37:55 2012 +0100
+++ b/plugins/s2s/s2sout.lib.lua	Thu Mar 15 03:19:35 2012 +0000
@@ -316,7 +316,10 @@
 	if event.name ~= "s2s" then return end
 
 	local s2s_sources = portmanager.get_active_services():get("s2s");
-
+	if not s2s_sources then
+		module:log("warn", "s2s not listening on any ports, outgoing connections may fail");
+		return;
+	end
 	for source, _ in pairs(s2s_sources) do
 		if source == "*" or source == "0.0.0.0" then
 			if not socket.local_addresses then