plugins/mod_net_multiplex.lua
author Jonas Schäfer <jonas@wielicki.name>
Mon, 10 Jan 2022 18:23:54 +0100
branch0.11
changeset 12185 783056b4e448
parent 9468 876171084ea3
child 10469 09697a673015
permissions -rw-r--r--
util.xml: Do not allow doctypes, comments or processing instructions Yes. This is as bad as it sounds. CVE pending. In Prosody itself, this only affects mod_websocket, which uses util.xml to parse the <open/> frame, thus allowing unauthenticated remote DoS using Billion Laughs. However, third-party modules using util.xml may also be affected by this. This commit installs handlers which disallow the use of doctype declarations and processing instructions without any escape hatch. It, by default, also introduces such a handler for comments, however, there is a way to enable comments nontheless. This is because util.xml is used to parse human-facing data, where comments are generally a desirable feature, and also because comments are generally harmless.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4619
d5739b8b7161 mod_net_multiplex: Port multiplexing (run multiple different services on a the same port(s))... now pluggable for use with any net plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     1
module:set_global();
d5739b8b7161 mod_net_multiplex: Port multiplexing (run multiple different services on a the same port(s))... now pluggable for use with any net plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     2
d5739b8b7161 mod_net_multiplex: Port multiplexing (run multiple different services on a the same port(s))... now pluggable for use with any net plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     3
local max_buffer_len = module:get_option_number("multiplex_buffer_size", 1024);
d5739b8b7161 mod_net_multiplex: Port multiplexing (run multiple different services on a the same port(s))... now pluggable for use with any net plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     4
d5739b8b7161 mod_net_multiplex: Port multiplexing (run multiple different services on a the same port(s))... now pluggable for use with any net plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     5
local portmanager = require "core.portmanager";
d5739b8b7161 mod_net_multiplex: Port multiplexing (run multiple different services on a the same port(s))... now pluggable for use with any net plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     6
d5739b8b7161 mod_net_multiplex: Port multiplexing (run multiple different services on a the same port(s))... now pluggable for use with any net plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     7
local available_services = {};
d5739b8b7161 mod_net_multiplex: Port multiplexing (run multiple different services on a the same port(s))... now pluggable for use with any net plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     8
d5739b8b7161 mod_net_multiplex: Port multiplexing (run multiple different services on a the same port(s))... now pluggable for use with any net plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     9
local function add_service(service)
d5739b8b7161 mod_net_multiplex: Port multiplexing (run multiple different services on a the same port(s))... now pluggable for use with any net plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    10
	local multiplex_pattern = service.multiplex and service.multiplex.pattern;
d5739b8b7161 mod_net_multiplex: Port multiplexing (run multiple different services on a the same port(s))... now pluggable for use with any net plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    11
	if multiplex_pattern then
d5739b8b7161 mod_net_multiplex: Port multiplexing (run multiple different services on a the same port(s))... now pluggable for use with any net plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    12
		module:log("debug", "Adding multiplex service %q with pattern %q", service.name, multiplex_pattern);
d5739b8b7161 mod_net_multiplex: Port multiplexing (run multiple different services on a the same port(s))... now pluggable for use with any net plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    13
		available_services[service] = multiplex_pattern;
d5739b8b7161 mod_net_multiplex: Port multiplexing (run multiple different services on a the same port(s))... now pluggable for use with any net plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    14
	else
d5739b8b7161 mod_net_multiplex: Port multiplexing (run multiple different services on a the same port(s))... now pluggable for use with any net plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    15
		module:log("debug", "Service %q is not multiplex-capable", service.name);
d5739b8b7161 mod_net_multiplex: Port multiplexing (run multiple different services on a the same port(s))... now pluggable for use with any net plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    16
	end
d5739b8b7161 mod_net_multiplex: Port multiplexing (run multiple different services on a the same port(s))... now pluggable for use with any net plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    17
end
d5739b8b7161 mod_net_multiplex: Port multiplexing (run multiple different services on a the same port(s))... now pluggable for use with any net plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    18
module:hook("service-added", function (event) add_service(event.service); end);
d5739b8b7161 mod_net_multiplex: Port multiplexing (run multiple different services on a the same port(s))... now pluggable for use with any net plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    19
module:hook("service-removed", function (event)	available_services[event.service] = nil; end);
d5739b8b7161 mod_net_multiplex: Port multiplexing (run multiple different services on a the same port(s))... now pluggable for use with any net plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    20
9468
876171084ea3 mod_net_multiplex: Silence luacheck warnings
Kim Alvefur <zash@zash.se>
parents: 7810
diff changeset
    21
for _, services in pairs(portmanager.get_registered_services()) do
7505
021d2b844c51 mod_net_multiplex: remove unused one-letter loop variable [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 6380
diff changeset
    22
	for _, service in ipairs(services) do
4619
d5739b8b7161 mod_net_multiplex: Port multiplexing (run multiple different services on a the same port(s))... now pluggable for use with any net plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    23
		add_service(service);
d5739b8b7161 mod_net_multiplex: Port multiplexing (run multiple different services on a the same port(s))... now pluggable for use with any net plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    24
	end
d5739b8b7161 mod_net_multiplex: Port multiplexing (run multiple different services on a the same port(s))... now pluggable for use with any net plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    25
end
d5739b8b7161 mod_net_multiplex: Port multiplexing (run multiple different services on a the same port(s))... now pluggable for use with any net plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    26
d5739b8b7161 mod_net_multiplex: Port multiplexing (run multiple different services on a the same port(s))... now pluggable for use with any net plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    27
local buffers = {};
d5739b8b7161 mod_net_multiplex: Port multiplexing (run multiple different services on a the same port(s))... now pluggable for use with any net plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    28
d5739b8b7161 mod_net_multiplex: Port multiplexing (run multiple different services on a the same port(s))... now pluggable for use with any net plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    29
local listener = { default_mode = "*a" };
d5739b8b7161 mod_net_multiplex: Port multiplexing (run multiple different services on a the same port(s))... now pluggable for use with any net plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    30
d5739b8b7161 mod_net_multiplex: Port multiplexing (run multiple different services on a the same port(s))... now pluggable for use with any net plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    31
function listener.onconnect()
d5739b8b7161 mod_net_multiplex: Port multiplexing (run multiple different services on a the same port(s))... now pluggable for use with any net plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    32
end
d5739b8b7161 mod_net_multiplex: Port multiplexing (run multiple different services on a the same port(s))... now pluggable for use with any net plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    33
d5739b8b7161 mod_net_multiplex: Port multiplexing (run multiple different services on a the same port(s))... now pluggable for use with any net plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    34
function listener.onincoming(conn, data)
d5739b8b7161 mod_net_multiplex: Port multiplexing (run multiple different services on a the same port(s))... now pluggable for use with any net plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    35
	if not data then return; end
d5739b8b7161 mod_net_multiplex: Port multiplexing (run multiple different services on a the same port(s))... now pluggable for use with any net plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    36
	local buf = buffers[conn];
d5739b8b7161 mod_net_multiplex: Port multiplexing (run multiple different services on a the same port(s))... now pluggable for use with any net plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    37
	buf = buf and buf..data or data;
d5739b8b7161 mod_net_multiplex: Port multiplexing (run multiple different services on a the same port(s))... now pluggable for use with any net plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    38
	for service, multiplex_pattern in pairs(available_services) do
d5739b8b7161 mod_net_multiplex: Port multiplexing (run multiple different services on a the same port(s))... now pluggable for use with any net plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    39
		if buf:match(multiplex_pattern) then
d5739b8b7161 mod_net_multiplex: Port multiplexing (run multiple different services on a the same port(s))... now pluggable for use with any net plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    40
			module:log("debug", "Routing incoming connection to %s", service.name);
9468
876171084ea3 mod_net_multiplex: Silence luacheck warnings
Kim Alvefur <zash@zash.se>
parents: 7810
diff changeset
    41
			local next_listener = service.listener;
876171084ea3 mod_net_multiplex: Silence luacheck warnings
Kim Alvefur <zash@zash.se>
parents: 7810
diff changeset
    42
			conn:setlistener(next_listener);
876171084ea3 mod_net_multiplex: Silence luacheck warnings
Kim Alvefur <zash@zash.se>
parents: 7810
diff changeset
    43
			local onconnect = next_listener.onconnect;
4619
d5739b8b7161 mod_net_multiplex: Port multiplexing (run multiple different services on a the same port(s))... now pluggable for use with any net plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    44
			if onconnect then onconnect(conn) end
9468
876171084ea3 mod_net_multiplex: Silence luacheck warnings
Kim Alvefur <zash@zash.se>
parents: 7810
diff changeset
    45
			return next_listener.onincoming(conn, buf);
4619
d5739b8b7161 mod_net_multiplex: Port multiplexing (run multiple different services on a the same port(s))... now pluggable for use with any net plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    46
		end
d5739b8b7161 mod_net_multiplex: Port multiplexing (run multiple different services on a the same port(s))... now pluggable for use with any net plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    47
	end
d5739b8b7161 mod_net_multiplex: Port multiplexing (run multiple different services on a the same port(s))... now pluggable for use with any net plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    48
	if #buf > max_buffer_len then -- Give up
d5739b8b7161 mod_net_multiplex: Port multiplexing (run multiple different services on a the same port(s))... now pluggable for use with any net plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    49
		conn:close();
d5739b8b7161 mod_net_multiplex: Port multiplexing (run multiple different services on a the same port(s))... now pluggable for use with any net plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    50
	else
d5739b8b7161 mod_net_multiplex: Port multiplexing (run multiple different services on a the same port(s))... now pluggable for use with any net plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    51
		buffers[conn] = buf;
d5739b8b7161 mod_net_multiplex: Port multiplexing (run multiple different services on a the same port(s))... now pluggable for use with any net plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    52
	end
d5739b8b7161 mod_net_multiplex: Port multiplexing (run multiple different services on a the same port(s))... now pluggable for use with any net plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    53
end
d5739b8b7161 mod_net_multiplex: Port multiplexing (run multiple different services on a the same port(s))... now pluggable for use with any net plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    54
9468
876171084ea3 mod_net_multiplex: Silence luacheck warnings
Kim Alvefur <zash@zash.se>
parents: 7810
diff changeset
    55
function listener.ondisconnect(conn)
4619
d5739b8b7161 mod_net_multiplex: Port multiplexing (run multiple different services on a the same port(s))... now pluggable for use with any net plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    56
	buffers[conn] = nil; -- warn if no buffer?
d5739b8b7161 mod_net_multiplex: Port multiplexing (run multiple different services on a the same port(s))... now pluggable for use with any net plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    57
end
d5739b8b7161 mod_net_multiplex: Port multiplexing (run multiple different services on a the same port(s))... now pluggable for use with any net plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    58
6380
4220ffb87b22 net.http, net.http.server, mod_c2s, mod_s2s, mod_component, mod_admin_telnet, mod_net_multiplex: Add ondetach to release connection from 'sessions' table (or equivalent)
Matthew Wild <mwild1@gmail.com>
parents: 5120
diff changeset
    59
listener.ondetach = listener.ondisconnect;
4220ffb87b22 net.http, net.http.server, mod_c2s, mod_s2s, mod_component, mod_admin_telnet, mod_net_multiplex: Add ondetach to release connection from 'sessions' table (or equivalent)
Matthew Wild <mwild1@gmail.com>
parents: 5120
diff changeset
    60
5120
bcabea740c00 mod_{admin_telnet,c2s,component,http,net_multiplex,s2s}: Use module:provides() instead of module:add_item().
Waqas Hussain <waqas20@gmail.com>
parents: 4619
diff changeset
    61
module:provides("net", {
4619
d5739b8b7161 mod_net_multiplex: Port multiplexing (run multiple different services on a the same port(s))... now pluggable for use with any net plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    62
	name = "multiplex";
d5739b8b7161 mod_net_multiplex: Port multiplexing (run multiple different services on a the same port(s))... now pluggable for use with any net plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    63
	config_prefix = "";
d5739b8b7161 mod_net_multiplex: Port multiplexing (run multiple different services on a the same port(s))... now pluggable for use with any net plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    64
	listener = listener;
d5739b8b7161 mod_net_multiplex: Port multiplexing (run multiple different services on a the same port(s))... now pluggable for use with any net plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    65
});
d5739b8b7161 mod_net_multiplex: Port multiplexing (run multiple different services on a the same port(s))... now pluggable for use with any net plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    66
d5739b8b7161 mod_net_multiplex: Port multiplexing (run multiple different services on a the same port(s))... now pluggable for use with any net plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    67
module:provides("net", {
d5739b8b7161 mod_net_multiplex: Port multiplexing (run multiple different services on a the same port(s))... now pluggable for use with any net plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    68
	name = "multiplex_ssl";
d5739b8b7161 mod_net_multiplex: Port multiplexing (run multiple different services on a the same port(s))... now pluggable for use with any net plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    69
	config_prefix = "ssl";
7809
00bca79ae778 mod_net_multiplex: Enable SSL on the SSL port (fixes #803)
Kim Alvefur <zash@zash.se>
parents: 6380
diff changeset
    70
	encryption = "ssl";
4619
d5739b8b7161 mod_net_multiplex: Port multiplexing (run multiple different services on a the same port(s))... now pluggable for use with any net plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    71
	listener = listener;
d5739b8b7161 mod_net_multiplex: Port multiplexing (run multiple different services on a the same port(s))... now pluggable for use with any net plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    72
});