2006-10-28 04:04:55 +02:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : mod_proxy65_service.erl
|
|
|
|
%%% Author : Evgeniy Khramtsov <xram@jabber.ru>
|
|
|
|
%%% Purpose : SOCKS5 Bytestreams XMPP service.
|
|
|
|
%%% Created : 12 Oct 2006 by Evgeniy Khramtsov <xram@jabber.ru>
|
2007-12-24 14:57:53 +01:00
|
|
|
%%%
|
|
|
|
%%%
|
2016-01-13 12:29:14 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2016 ProcessOne
|
2007-12-24 14:57:53 +01:00
|
|
|
%%%
|
|
|
|
%%% 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.
|
2009-01-12 15:44:42 +01:00
|
|
|
%%%
|
2014-02-22 11:27:40 +01:00
|
|
|
%%% 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.
|
2007-12-24 14:57:53 +01:00
|
|
|
%%%
|
2006-10-28 04:04:55 +02:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(mod_proxy65_service).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2006-10-28 04:04:55 +02:00
|
|
|
-author('xram@jabber.ru').
|
|
|
|
|
|
|
|
-behaviour(gen_server).
|
|
|
|
|
|
|
|
%% gen_server callbacks.
|
2013-03-14 10:33:02 +01:00
|
|
|
-export([init/1, handle_info/2, handle_call/3,
|
|
|
|
handle_cast/2, terminate/2, code_change/3]).
|
2006-10-28 04:04:55 +02:00
|
|
|
|
2015-06-01 14:38:27 +02:00
|
|
|
-export([start_link/2, add_listener/2,
|
2015-06-03 15:05:17 +02:00
|
|
|
transform_module_options/1, delete_listener/1]).
|
2006-10-28 04:04:55 +02:00
|
|
|
|
2008-07-13 21:10:01 +02:00
|
|
|
-include("ejabberd.hrl").
|
2013-04-08 11:12:54 +02:00
|
|
|
-include("logger.hrl").
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2008-07-13 21:10:01 +02:00
|
|
|
-include("jlib.hrl").
|
2006-10-28 04:04:55 +02:00
|
|
|
|
|
|
|
-define(PROCNAME, ejabberd_mod_proxy65_service).
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-record(state,
|
|
|
|
{myhost = <<"">> :: binary(),
|
|
|
|
serverhost = <<"">> :: binary(),
|
|
|
|
name = <<"">> :: binary(),
|
|
|
|
stream_addr = [] :: [attr()],
|
|
|
|
port = 0 :: inet:port_number(),
|
|
|
|
ip = {127,0,0,1} :: inet:ip_address(),
|
|
|
|
acl = none :: atom()}).
|
2008-10-12 13:17:35 +02:00
|
|
|
|
|
|
|
%%%------------------------
|
|
|
|
%%% gen_server callbacks
|
|
|
|
%%%------------------------
|
2006-10-28 04:04:55 +02:00
|
|
|
|
|
|
|
start_link(Host, Opts) ->
|
|
|
|
Proc = gen_mod:get_module_proc(Host, ?PROCNAME),
|
2013-03-14 10:33:02 +01:00
|
|
|
gen_server:start_link({local, Proc}, ?MODULE,
|
|
|
|
[Host, Opts], []).
|
2006-10-28 04:04:55 +02:00
|
|
|
|
|
|
|
init([Host, Opts]) ->
|
2009-01-12 21:03:02 +01:00
|
|
|
State = parse_options(Host, Opts),
|
2006-10-28 04:04:55 +02:00
|
|
|
ejabberd_router:register_route(State#state.myhost),
|
|
|
|
{ok, State}.
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
terminate(_Reason, #state{myhost = MyHost}) ->
|
|
|
|
ejabberd_router:unregister_route(MyHost), ok.
|
2006-10-28 04:04:55 +02:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
handle_info({route, From, To,
|
|
|
|
#xmlel{name = <<"iq">>} = Packet},
|
|
|
|
State) ->
|
2006-10-28 04:04:55 +02:00
|
|
|
IQ = jlib:iq_query_info(Packet),
|
|
|
|
case catch process_iq(From, IQ, State) of
|
2013-03-14 10:33:02 +01:00
|
|
|
Result when is_record(Result, iq) ->
|
|
|
|
ejabberd_router:route(To, From, jlib:iq_to_xml(Result));
|
|
|
|
{'EXIT', Reason} ->
|
|
|
|
?ERROR_MSG("Error when processing IQ stanza: ~p",
|
|
|
|
[Reason]),
|
|
|
|
Err = jlib:make_error_reply(Packet,
|
|
|
|
?ERR_INTERNAL_SERVER_ERROR),
|
|
|
|
ejabberd_router:route(To, From, Err);
|
|
|
|
_ -> ok
|
2006-10-28 04:04:55 +02:00
|
|
|
end,
|
|
|
|
{noreply, State};
|
2013-03-14 10:33:02 +01:00
|
|
|
handle_info(_Info, State) -> {noreply, State}.
|
2006-10-28 04:04:55 +02:00
|
|
|
|
2009-01-12 21:03:02 +01:00
|
|
|
handle_call(get_port_ip, _From, State) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
{reply, {port_ip, State#state.port, State#state.ip},
|
|
|
|
State};
|
2008-10-12 13:17:35 +02:00
|
|
|
handle_call(_Request, _From, State) ->
|
|
|
|
{reply, ok, State}.
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
handle_cast(_Request, State) -> {noreply, State}.
|
2008-10-12 13:17:35 +02:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
code_change(_OldVsn, State, _Extra) -> {ok, State}.
|
2008-10-12 13:17:35 +02:00
|
|
|
|
|
|
|
%%%------------------------
|
|
|
|
%%% Listener management
|
|
|
|
%%%------------------------
|
|
|
|
|
|
|
|
add_listener(Host, Opts) ->
|
2009-01-12 21:03:02 +01:00
|
|
|
State = parse_options(Host, Opts),
|
|
|
|
NewOpts = [Host | Opts],
|
2013-03-14 10:33:02 +01:00
|
|
|
ejabberd_listener:add_listener({State#state.port,
|
|
|
|
State#state.ip},
|
|
|
|
mod_proxy65_stream, NewOpts).
|
2008-10-12 13:17:35 +02:00
|
|
|
|
|
|
|
delete_listener(Host) ->
|
|
|
|
Proc = gen_mod:get_module_proc(Host, ?PROCNAME),
|
2013-03-14 10:33:02 +01:00
|
|
|
{port_ip, Port, IP} = gen_server:call(Proc,
|
|
|
|
get_port_ip),
|
|
|
|
catch ejabberd_listener:delete_listener({Port, IP},
|
|
|
|
mod_proxy65_stream).
|
2008-10-12 13:17:35 +02:00
|
|
|
|
2006-10-28 04:04:55 +02:00
|
|
|
%%%------------------------
|
|
|
|
%%% IQ Processing
|
|
|
|
%%%------------------------
|
|
|
|
|
|
|
|
%% disco#info request
|
2013-03-14 10:33:02 +01:00
|
|
|
process_iq(_,
|
|
|
|
#iq{type = get, xmlns = ?NS_DISCO_INFO, lang = Lang} =
|
|
|
|
IQ,
|
|
|
|
#state{name = Name, serverhost = ServerHost}) ->
|
|
|
|
Info = ejabberd_hooks:run_fold(disco_info, ServerHost,
|
|
|
|
[], [ServerHost, ?MODULE, <<"">>, <<"">>]),
|
|
|
|
IQ#iq{type = result,
|
|
|
|
sub_el =
|
|
|
|
[#xmlel{name = <<"query">>,
|
|
|
|
attrs = [{<<"xmlns">>, ?NS_DISCO_INFO}],
|
|
|
|
children = iq_disco_info(Lang, Name) ++ Info}]};
|
2006-10-28 04:04:55 +02:00
|
|
|
%% disco#items request
|
2013-03-14 10:33:02 +01:00
|
|
|
process_iq(_,
|
|
|
|
#iq{type = get, xmlns = ?NS_DISCO_ITEMS} = IQ, _) ->
|
|
|
|
IQ#iq{type = result,
|
|
|
|
sub_el =
|
|
|
|
[#xmlel{name = <<"query">>,
|
|
|
|
attrs = [{<<"xmlns">>, ?NS_DISCO_ITEMS}],
|
|
|
|
children = []}]};
|
2006-10-28 04:04:55 +02:00
|
|
|
%% vCard request
|
2013-03-14 10:33:02 +01:00
|
|
|
process_iq(_,
|
|
|
|
#iq{type = get, xmlns = ?NS_VCARD, lang = Lang} = IQ,
|
|
|
|
_) ->
|
|
|
|
IQ#iq{type = result,
|
|
|
|
sub_el =
|
|
|
|
[#xmlel{name = <<"vCard">>,
|
|
|
|
attrs = [{<<"xmlns">>, ?NS_VCARD}],
|
|
|
|
children = iq_vcard(Lang)}]};
|
2006-10-28 04:04:55 +02:00
|
|
|
%% bytestreams info request
|
2013-03-14 10:33:02 +01:00
|
|
|
process_iq(JID,
|
|
|
|
#iq{type = get, sub_el = SubEl,
|
|
|
|
xmlns = ?NS_BYTESTREAMS} =
|
|
|
|
IQ,
|
|
|
|
#state{acl = ACL, stream_addr = StreamAddr,
|
|
|
|
serverhost = ServerHost}) ->
|
2006-10-28 04:04:55 +02:00
|
|
|
case acl:match_rule(ServerHost, ACL, JID) of
|
2013-03-14 10:33:02 +01:00
|
|
|
allow ->
|
|
|
|
StreamHostEl = [#xmlel{name = <<"streamhost">>,
|
|
|
|
attrs = StreamAddr, children = []}],
|
|
|
|
IQ#iq{type = result,
|
|
|
|
sub_el =
|
|
|
|
[#xmlel{name = <<"query">>,
|
|
|
|
attrs = [{<<"xmlns">>, ?NS_BYTESTREAMS}],
|
|
|
|
children = StreamHostEl}]};
|
|
|
|
deny ->
|
|
|
|
IQ#iq{type = error, sub_el = [SubEl, ?ERR_FORBIDDEN]}
|
2006-10-28 04:04:55 +02:00
|
|
|
end;
|
|
|
|
%% bytestream activation request
|
2013-03-14 10:33:02 +01:00
|
|
|
process_iq(InitiatorJID,
|
|
|
|
#iq{type = set, sub_el = SubEl,
|
|
|
|
xmlns = ?NS_BYTESTREAMS} =
|
|
|
|
IQ,
|
2007-06-27 13:05:14 +02:00
|
|
|
#state{acl = ACL, serverhost = ServerHost}) ->
|
2006-10-28 04:04:55 +02:00
|
|
|
case acl:match_rule(ServerHost, ACL, InitiatorJID) of
|
2013-03-14 10:33:02 +01:00
|
|
|
allow ->
|
|
|
|
ActivateEl = xml:get_path_s(SubEl,
|
|
|
|
[{elem, <<"activate">>}]),
|
|
|
|
SID = xml:get_tag_attr_s(<<"sid">>, SubEl),
|
|
|
|
case catch
|
2015-11-24 16:44:13 +01:00
|
|
|
jid:from_string(xml:get_tag_cdata(ActivateEl))
|
2013-03-14 10:33:02 +01:00
|
|
|
of
|
|
|
|
TargetJID
|
|
|
|
when is_record(TargetJID, jid), SID /= <<"">>,
|
|
|
|
byte_size(SID) =< 128, TargetJID /= InitiatorJID ->
|
|
|
|
Target =
|
2015-11-24 16:44:13 +01:00
|
|
|
jid:to_string(jid:tolower(TargetJID)),
|
2013-03-14 10:33:02 +01:00
|
|
|
Initiator =
|
2015-11-24 16:44:13 +01:00
|
|
|
jid:to_string(jid:tolower(InitiatorJID)),
|
2013-06-20 10:40:44 +02:00
|
|
|
SHA1 = p1_sha:sha(<<SID/binary, Initiator/binary, Target/binary>>),
|
2013-03-14 10:33:02 +01:00
|
|
|
case mod_proxy65_sm:activate_stream(SHA1, InitiatorJID,
|
|
|
|
TargetJID, ServerHost)
|
|
|
|
of
|
|
|
|
ok -> IQ#iq{type = result, sub_el = []};
|
|
|
|
false ->
|
|
|
|
IQ#iq{type = error,
|
|
|
|
sub_el = [SubEl, ?ERR_ITEM_NOT_FOUND]};
|
|
|
|
limit ->
|
|
|
|
IQ#iq{type = error,
|
|
|
|
sub_el = [SubEl, ?ERR_RESOURCE_CONSTRAINT]};
|
|
|
|
conflict ->
|
|
|
|
IQ#iq{type = error, sub_el = [SubEl, ?ERR_CONFLICT]};
|
|
|
|
_ ->
|
|
|
|
IQ#iq{type = error,
|
|
|
|
sub_el = [SubEl, ?ERR_INTERNAL_SERVER_ERROR]}
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
IQ#iq{type = error, sub_el = [SubEl, ?ERR_BAD_REQUEST]}
|
|
|
|
end;
|
|
|
|
deny ->
|
|
|
|
IQ#iq{type = error, sub_el = [SubEl, ?ERR_FORBIDDEN]}
|
2006-10-28 04:04:55 +02:00
|
|
|
end;
|
|
|
|
%% Unknown "set" or "get" request
|
2013-03-14 10:33:02 +01:00
|
|
|
process_iq(_, #iq{type = Type, sub_el = SubEl} = IQ, _)
|
|
|
|
when Type == get; Type == set ->
|
|
|
|
IQ#iq{type = error,
|
|
|
|
sub_el = [SubEl, ?ERR_SERVICE_UNAVAILABLE]};
|
2006-10-28 04:04:55 +02:00
|
|
|
%% IQ "result" or "error".
|
2013-03-14 10:33:02 +01:00
|
|
|
process_iq(_, _, _) -> ok.
|
2006-10-28 04:04:55 +02:00
|
|
|
|
|
|
|
%%%-------------------------
|
|
|
|
%%% Auxiliary functions.
|
|
|
|
%%%-------------------------
|
2013-03-14 10:33:02 +01:00
|
|
|
-define(FEATURE(Feat),
|
|
|
|
#xmlel{name = <<"feature">>,
|
|
|
|
attrs = [{<<"var">>, Feat}], children = []}).
|
2006-10-28 04:04:55 +02:00
|
|
|
|
2007-06-27 13:05:14 +02:00
|
|
|
iq_disco_info(Lang, Name) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
[#xmlel{name = <<"identity">>,
|
|
|
|
attrs =
|
|
|
|
[{<<"category">>, <<"proxy">>},
|
|
|
|
{<<"type">>, <<"bytestreams">>},
|
|
|
|
{<<"name">>, translate:translate(Lang, Name)}],
|
|
|
|
children = []},
|
|
|
|
?FEATURE((?NS_DISCO_INFO)), ?FEATURE((?NS_VCARD)),
|
|
|
|
?FEATURE((?NS_BYTESTREAMS))].
|
2006-10-28 04:04:55 +02:00
|
|
|
|
|
|
|
iq_vcard(Lang) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
[#xmlel{name = <<"FN">>, attrs = [],
|
|
|
|
children = [{xmlcdata, <<"ejabberd/mod_proxy65">>}]},
|
|
|
|
#xmlel{name = <<"URL">>, attrs = [],
|
|
|
|
children = [{xmlcdata, ?EJABBERD_URI}]},
|
|
|
|
#xmlel{name = <<"DESC">>, attrs = [],
|
|
|
|
children =
|
|
|
|
[{xmlcdata,
|
|
|
|
<<(translate:translate(Lang,
|
|
|
|
<<"ejabberd SOCKS5 Bytestreams module">>))/binary,
|
2016-01-13 12:29:14 +01:00
|
|
|
"\nCopyright (c) 2003-2016 ProcessOne">>}]}].
|
2006-10-28 04:04:55 +02:00
|
|
|
|
|
|
|
parse_options(ServerHost, Opts) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
MyHost = gen_mod:get_opt_host(ServerHost, Opts,
|
|
|
|
<<"proxy.@HOST@">>),
|
|
|
|
Port = gen_mod:get_opt(port, Opts,
|
|
|
|
fun(P) when is_integer(P), P>0, P<65536 -> P end,
|
|
|
|
7777),
|
|
|
|
ACL = gen_mod:get_opt(access, Opts, fun(A) when is_atom(A) -> A end,
|
|
|
|
all),
|
|
|
|
Name = gen_mod:get_opt(name, Opts, fun iolist_to_binary/1,
|
|
|
|
<<"SOCKS5 Bytestreams">>),
|
|
|
|
IP = gen_mod:get_opt(ip, Opts,
|
2013-08-12 14:25:05 +02:00
|
|
|
fun(S) ->
|
|
|
|
{ok, Addr} = inet_parse:address(
|
|
|
|
binary_to_list(
|
|
|
|
iolist_to_binary(S))),
|
2013-03-14 10:33:02 +01:00
|
|
|
Addr
|
|
|
|
end, get_my_ip()),
|
|
|
|
HostName = gen_mod:get_opt(hostname, Opts,
|
2013-08-12 14:25:05 +02:00
|
|
|
fun iolist_to_binary/1,
|
|
|
|
jlib:ip_to_list(IP)),
|
2013-03-14 10:33:02 +01:00
|
|
|
StreamAddr = [{<<"jid">>, MyHost},
|
|
|
|
{<<"host">>, HostName},
|
|
|
|
{<<"port">>, jlib:integer_to_binary(Port)}],
|
|
|
|
#state{myhost = MyHost, serverhost = ServerHost,
|
|
|
|
name = Name, port = Port, ip = IP,
|
|
|
|
stream_addr = StreamAddr, acl = ACL}.
|
2009-02-27 04:22:40 +01:00
|
|
|
|
2013-08-12 14:25:05 +02:00
|
|
|
transform_module_options(Opts) ->
|
|
|
|
lists:map(
|
|
|
|
fun({ip, IP}) when is_tuple(IP) ->
|
|
|
|
{ip, jlib:ip_to_list(IP)};
|
|
|
|
({hostname, IP}) when is_tuple(IP) ->
|
|
|
|
{hostname, jlib:ip_to_list(IP)};
|
|
|
|
(Opt) ->
|
|
|
|
Opt
|
|
|
|
end, Opts).
|
|
|
|
|
2009-02-27 04:22:40 +01:00
|
|
|
get_my_ip() ->
|
|
|
|
{ok, MyHostName} = inet:gethostname(),
|
|
|
|
case inet:getaddr(MyHostName, inet) of
|
2013-03-14 10:33:02 +01:00
|
|
|
{ok, Addr} -> Addr;
|
|
|
|
{error, _} -> {127, 0, 0, 1}
|
2007-08-25 19:24:00 +02:00
|
|
|
end.
|