Software /
code /
prosody
Annotate
plugins/mod_net_multiplex.lua @ 9405:86e3e37f25ff
mod_admin_telnet: Use prosody.hosts to be more explicit and avoid name clash with 'hosts' arguments [luacheck]
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Sun, 30 Sep 2018 14:33:42 +0200 |
parent | 7807:caf26fc419e9 |
child | 9465:876171084ea3 |
rev | line source |
---|---|
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 |
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
|
21 for service_name, services in pairs(portmanager.get_registered_services()) do |
7502
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); |
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
|
41 local listener = service.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
|
42 conn:setlistener(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
|
43 local onconnect = 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
|
44 if onconnect then onconnect(conn) 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
|
45 return listener.onincoming(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
|
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 |
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
|
55 function listener.ondisconnect(conn, err) |
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"; |
7806
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 }); |