aboutsummaryrefslogtreecommitdiff
path: root/src/mod_proxy65.erl
blob: 1ccb319784f1057c18a247082f6dd15ebb96527e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
%%%----------------------------------------------------------------------
%%% File    : mod_proxy65.erl
%%% Author  : Evgeniy Khramtsov <xram@jabber.ru>
%%% Purpose : Main supervisor.
%%% Created : 12 Oct 2006 by Evgeniy Khramtsov <xram@jabber.ru>
%%%
%%%
%%% ejabberd, Copyright (C) 2002-2022   ProcessOne
%%%
%%% This program is free software; you can redistribute it and/or
%%% modify it under the terms of the GNU General Public License as
%%% published by the Free Software Foundation; either version 2 of the
%%% License, or (at your option) any later version.
%%%
%%% This program is distributed in the hope that it will be useful,
%%% but WITHOUT ANY WARRANTY; without even the implied warranty of
%%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
%%% General Public License for more details.
%%%
%%% You should have received a copy of the GNU General Public License along
%%% with this program; if not, write to the Free Software Foundation, Inc.,
%%% 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
%%%
%%%----------------------------------------------------------------------

-module(mod_proxy65).

-author('xram@jabber.ru').

-protocol({xep, 65, '1.8'}).

-behaviour(gen_mod).

-behaviour(supervisor).

%% gen_mod callbacks.
-export([start/2, stop/1, reload/3]).

%% supervisor callbacks.
-export([init/1]).

-export([start_link/1, mod_opt_type/1, mod_options/1, depends/2, mod_doc/0]).

-define(PROCNAME, ejabberd_mod_proxy65).

-include("translate.hrl").

-callback init() -> any().
-callback register_stream(binary(), pid()) -> ok | {error, any()}.
-callback unregister_stream(binary()) -> ok | {error, any()}.
-callback activate_stream(binary(), binary(), pos_integer() | infinity, node()) ->
    ok | {error, limit | conflict | notfound | term()}.

start(Host, Opts) ->
    case mod_proxy65_service:add_listener(Host, Opts) of
	{error, _} = Err ->
	    Err;
	_ ->
	    Mod = gen_mod:ram_db_mod(global, ?MODULE),
	    Mod:init(),
	    Proc = gen_mod:get_module_proc(Host, ?PROCNAME),
	    ChildSpec = {Proc, {?MODULE, start_link, [Host]},
			 transient, infinity, supervisor, [?MODULE]},
	    supervisor:start_child(ejabberd_gen_mod_sup, ChildSpec)
    end.

stop(Host) ->
    case gen_mod:is_loaded_elsewhere(Host, ?MODULE) of
	false ->
	    mod_proxy65_service:delete_listener(Host);
	true ->
	    ok
    end,
    Proc = gen_mod:get_module_proc(Host, ?PROCNAME),
    supervisor:terminate_child(ejabberd_gen_mod_sup, Proc),
    supervisor:delete_child(ejabberd_gen_mod_sup, Proc).

reload(Host, NewOpts, OldOpts) ->
    Mod = gen_mod:ram_db_mod(global, ?MODULE),
    Mod:init(),
    mod_proxy65_service:reload(Host, NewOpts, OldOpts).

start_link(Host) ->
    Proc = gen_mod:get_module_proc(Host, ?PROCNAME),
    supervisor:start_link({local, Proc}, ?MODULE, [Host]).

init([Host]) ->
    Service = {mod_proxy65_service,
	       {mod_proxy65_service, start_link, [Host]},
	       transient, 5000, worker, [mod_proxy65_service]},
    {ok, {{one_for_one, 10, 1}, [Service]}}.

depends(_Host, _Opts) ->
    [].

mod_opt_type(access) ->
    econf:acl();
mod_opt_type(hostname) ->
    econf:host();
mod_opt_type(ip) ->
    econf:ip();
mod_opt_type(name) ->
    econf:binary();
mod_opt_type(port) ->
    econf:port();
mod_opt_type(max_connections) ->
    econf:pos_int(infinity);
mod_opt_type(host) ->
    econf:host();
mod_opt_type(hosts) ->
    econf:hosts();
mod_opt_type(ram_db_type) ->
    econf:db_type(?MODULE);
mod_opt_type(server_host) ->
    econf:binary();
mod_opt_type(auth_type) ->
    econf:enum([plain, anonymous]);
mod_opt_type(recbuf) ->
    econf:pos_int();
mod_opt_type(shaper) ->
    econf:shaper();
mod_opt_type(sndbuf) ->
    econf:pos_int();
mod_opt_type(vcard) ->
    econf:vcard_temp().

mod_options(Host) ->
    [{ram_db_type, ejabberd_config:default_ram_db(Host, ?MODULE)},
     {access, all},
     {host, <<"proxy.", Host/binary>>},
     {hosts, []},
     {hostname, undefined},
     {ip, undefined},
     {port, 7777},
     {name, ?T("SOCKS5 Bytestreams")},
     {vcard, undefined},
     {max_connections, infinity},
     {auth_type, anonymous},
     {recbuf, 65536},
     {sndbuf, 65536},
     {shaper, none}].

mod_doc() ->
    #{desc =>
          ?T("This module implements "
             "https://xmpp.org/extensions/xep-0065.html"
             "[XEP-0065: SOCKS5 Bytestreams]. It allows ejabberd "
             "to act as a file transfer proxy between two XMPP clients."),
      opts =>
          [{host,
            #{desc => ?T("Deprecated. Use 'hosts' instead.")}},
           {hosts,
            #{value => ?T("[Host, ...]"),
              desc =>
                  ?T("This option defines the Jabber IDs of the service. "
                     "If the 'hosts' option is not specified, the only Jabber ID will "
                     "be the hostname of the virtual host with the prefix \"proxy.\". "
                     "The keyword '@HOST@' is replaced with the real virtual host name.")}},
           {name,
            #{value => ?T("Name"),
              desc =>
                  ?T("The value of the service name. This name is only visible in some "
                     "clients that support https://xmpp.org/extensions/xep-0030.html"
                     "[XEP-0030: Service Discovery]. The default is \"SOCKS5 Bytestreams\".")}},
           {access,
            #{value => ?T("AccessName"),
              desc =>
                  ?T("Defines an access rule for file transfer initiators. "
                     "The default value is 'all'. You may want to restrict "
                     "access to the users of your server only, in order to "
                     "avoid abusing your proxy by the users of remote "
                     "servers.")}},
           {ram_db_type,
            #{value => "mnesia | redis | sql",
              desc =>
                  ?T("Same as top-level _`default_ram_db`_ option, "
                     "but applied to this module only.")}},
           {ip,
            #{value => ?T("IPAddress"),
              desc =>
                  ?T("This option specifies which network interface to listen "
                     "for. The default value is an IP address of the service's "
                     "DNS name, or, if fails, '127.0.0.1'.")}},
           {hostname,
            #{value => ?T("Host"),
              desc =>
                  ?T("Defines a hostname offered by the proxy when "
                     "establishing a session with clients. This is useful "
                     "when you run the proxy behind a NAT. The keyword "
                     "'@HOST@' is replaced with the virtual host name. "
                     "The default is to use the value of 'ip' option. "
                     "Examples: 'proxy.mydomain.org', '200.150.100.50'.")}},
           {port,
            #{value => "1..65535",
              desc =>
                  ?T("A port number to listen for incoming connections. "
                     "The default value is '7777'.")}},
           {auth_type,
            #{value => "anonymous | plain",
              desc =>
                  ?T("SOCKS5 authentication type. "
                     "The default value is 'anonymous'. "
                     "If set to 'plain', ejabberd will use "
                     "authentication backend as it would "
                     "for SASL PLAIN.")}},
           {max_connections,
            #{value => "pos_integer() | infinity",
              desc =>
                  ?T("Maximum number of active connections per file transfer "
                     "initiator. The default value is 'infinity'.")}},
           {shaper,
            #{value => ?T("Shaper"),
              desc =>
                  ?T("This option defines a shaper for the file transfer peers. "
                     "A shaper with the maximum bandwidth will be selected. "
                     "The default is 'none', i.e. no shaper.")}},
           {recbuf,
            #{value => ?T("Size"),
              desc =>
                  ?T("A size of the buffer for incoming packets. "
                     "If you define a shaper, set the value of this "
                     "option to the size of the shaper in order "
                     "to avoid traffic spikes in file transfers. "
                     "The default value is '65536' bytes.")}},
           {sndbuf,
            #{value => ?T("Size"),
              desc =>
                  ?T("A size of the buffer for outgoing packets. "
                     "If you define a shaper, set the value of this "
                     "option to the size of the shaper in order "
                     "to avoid traffic spikes in file transfers. "
                     "The default value is '65536' bytes.")}},
           {vcard,
            #{value => ?T("vCard"),
              desc =>
                  ?T("A custom vCard of the service that will be displayed "
                     "by some XMPP clients in Service Discovery. The value of "
                     "'vCard' is a YAML map constructed from an XML representation "
                     "of vCard. Since the representation has no attributes, "
                     "the mapping is straightforward."),
              example =>
                  [{?T("For example, the following XML representation of vCard:"),
                    ["<vCard xmlns='vcard-temp'>",
                     "  <FN>Conferences</FN>",
                     "  <ADR>",
                     "    <WORK/>",
                     "    <STREET>Elm Street</STREET>",
                     "  </ADR>",
                     "</vCard>"]},
                   {?T("will be translated to:"),
                    ["vcard:",
                     "  fn: Conferences",
                     "  adr:",
                     "    -",
                     "      work: true",
                     "      street: Elm Street"]}]}}],
      example =>
          ["acl:",
           "  admin:",
           "    user: admin@example.org",
           "  proxy_users:",
           "    server: example.org",
           "",
           "access_rules:",
           "  proxy65_access:",
           "    allow: proxy_users",
           "",
           "shaper_rules:",
           "  proxy65_shaper:",
           "    none: admin",
           "  proxyrate: proxy_users",
           "",
           "shaper:",
           "  proxyrate: 10240",
           "",
           "modules:",
           "  ...",
           "  mod_proxy65:",
           "    host: proxy1.example.org",
           "    name: \"File Transfer Proxy\"",
           "    ip: 200.150.100.1",
           "    port: 7778",
           "    max_connections: 5",
           "    access: proxy65_access",
           "    shaper: proxy65_shaper",
           "    recbuf: 10240",
           "    sndbuf: 10240",
           "  ..."]}.