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
|
|
|
%%%
|
|
|
|
%%%
|
2010-01-12 17:15:16 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2010 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-19 15:47:33 +01:00
|
|
|
%%%
|
2007-12-24 14:57:53 +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., 59 Temple Place, Suite 330, Boston, MA
|
|
|
|
%%% 02111-1307 USA
|
|
|
|
%%%
|
2006-10-28 04:04:55 +02:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(mod_proxy65_service).
|
|
|
|
-author('xram@jabber.ru').
|
|
|
|
|
|
|
|
-behaviour(gen_server).
|
|
|
|
|
|
|
|
%% gen_server callbacks.
|
|
|
|
-export([init/1,
|
|
|
|
handle_info/2,
|
|
|
|
handle_call/3,
|
|
|
|
handle_cast/2,
|
|
|
|
terminate/2,
|
|
|
|
code_change/3
|
|
|
|
]).
|
|
|
|
|
|
|
|
%% API.
|
2008-10-13 12:11:19 +02:00
|
|
|
-export([start_link/2, add_listener/2, delete_listener/1]).
|
2006-10-28 04:04:55 +02:00
|
|
|
|
2008-12-03 15:34:43 +01:00
|
|
|
-include_lib("exmpp/include/exmpp.hrl").
|
|
|
|
|
2008-07-15 10:45:05 +02:00
|
|
|
-include("ejabberd.hrl").
|
2006-10-28 04:04:55 +02:00
|
|
|
|
|
|
|
-define(PROCNAME, ejabberd_mod_proxy65_service).
|
|
|
|
|
|
|
|
-record(state, {
|
|
|
|
myhost,
|
|
|
|
serverhost,
|
|
|
|
name,
|
|
|
|
stream_addr,
|
|
|
|
port,
|
2009-01-19 16:27:07 +01:00
|
|
|
ip,
|
2006-10-28 04:04:55 +02:00
|
|
|
acl
|
|
|
|
}).
|
|
|
|
|
2008-10-13 12:11:19 +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),
|
|
|
|
gen_server:start_link({local, Proc}, ?MODULE, [Host, Opts], []).
|
|
|
|
|
|
|
|
init([Host, Opts]) ->
|
2009-01-19 16:27:07 +01:00
|
|
|
State = parse_options(Host, Opts),
|
2006-10-28 04:04:55 +02:00
|
|
|
ejabberd_router:register_route(State#state.myhost),
|
|
|
|
{ok, State}.
|
|
|
|
|
2008-10-13 12:11:19 +02:00
|
|
|
terminate(_Reason, #state{myhost=MyHost}) ->
|
2006-10-28 04:04:55 +02:00
|
|
|
ejabberd_router:unregister_route(MyHost),
|
|
|
|
ok.
|
|
|
|
|
2008-12-03 15:34:43 +01:00
|
|
|
handle_info({route, From, To, Packet}, State) when ?IS_IQ(Packet) ->
|
|
|
|
IQ_Rec = exmpp_iq:xmlel_to_iq(Packet),
|
|
|
|
case catch process_iq(From, IQ_Rec, State) of
|
|
|
|
Result when ?IS_IQ_RECORD(Result) ->
|
|
|
|
ejabberd_router:route(To, From,
|
|
|
|
exmpp_iq:iq_to_xmlel(Result, To, From));
|
2006-10-28 04:04:55 +02:00
|
|
|
{'EXIT', Reason} ->
|
|
|
|
?ERROR_MSG("Error when processing IQ stanza: ~p", [Reason]),
|
2008-12-03 15:34:43 +01:00
|
|
|
Err = exmpp_iq:error(Packet, 'internal-server-error'),
|
2006-10-28 04:04:55 +02:00
|
|
|
ejabberd_router:route(To, From, Err);
|
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end,
|
|
|
|
{noreply, State};
|
|
|
|
handle_info(_Info, State) ->
|
|
|
|
{noreply, State}.
|
|
|
|
|
2009-01-19 16:27:07 +01:00
|
|
|
handle_call(get_port_ip, _From, State) ->
|
|
|
|
{reply, {port_ip, State#state.port, State#state.ip}, State};
|
2008-10-13 12:11:19 +02:00
|
|
|
handle_call(_Request, _From, State) ->
|
|
|
|
{reply, ok, State}.
|
|
|
|
|
|
|
|
handle_cast(_Request, State) ->
|
|
|
|
{noreply, State}.
|
|
|
|
|
|
|
|
code_change(_OldVsn, State, _Extra) ->
|
|
|
|
{ok, State}.
|
|
|
|
|
|
|
|
%%%------------------------
|
|
|
|
%%% Listener management
|
|
|
|
%%%------------------------
|
|
|
|
|
|
|
|
add_listener(Host, Opts) ->
|
2009-01-19 16:27:07 +01:00
|
|
|
State = parse_options(Host, Opts),
|
|
|
|
NewOpts = [Host | Opts],
|
|
|
|
ejabberd_listener:add_listener({State#state.port, State#state.ip}, mod_proxy65_stream, NewOpts).
|
2008-10-13 12:11:19 +02:00
|
|
|
|
|
|
|
delete_listener(Host) ->
|
|
|
|
Proc = gen_mod:get_module_proc(Host, ?PROCNAME),
|
2009-01-19 16:27:07 +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-13 12:11:19 +02:00
|
|
|
|
2006-10-28 04:04:55 +02:00
|
|
|
%%%------------------------
|
|
|
|
%%% IQ Processing
|
|
|
|
%%%------------------------
|
|
|
|
|
|
|
|
%% disco#info request
|
2009-07-17 22:47:40 +02:00
|
|
|
process_iq(_, #iq{type = get, ns = ?NS_DISCO_INFO, lang = Lang} = IQ_Rec,
|
|
|
|
#state{name=Name, serverhost = ServerHost}) ->
|
|
|
|
ServerHostB = list_to_binary(ServerHost),
|
|
|
|
Info = ejabberd_hooks:run_fold(
|
|
|
|
disco_info, ServerHostB, [], [ServerHost, ?MODULE, <<>>, ""]),
|
2008-12-03 15:34:43 +01:00
|
|
|
Result = #xmlel{ns = ?NS_DISCO_INFO, name = 'query',
|
2009-07-17 22:47:40 +02:00
|
|
|
children = iq_disco_info(Lang, Name)++Info},
|
2008-12-03 15:34:43 +01:00
|
|
|
exmpp_iq:result(IQ_Rec, Result);
|
2006-10-28 04:04:55 +02:00
|
|
|
|
|
|
|
%% disco#items request
|
2008-12-03 15:34:43 +01:00
|
|
|
process_iq(_, #iq{type = get, ns = ?NS_DISCO_ITEMS} = IQ_Rec, _) ->
|
|
|
|
Result = #xmlel{ns = ?NS_DISCO_ITEMS, name = 'query',
|
|
|
|
children = []},
|
|
|
|
exmpp_iq:result(IQ_Rec, Result);
|
2006-10-28 04:04:55 +02:00
|
|
|
|
|
|
|
%% vCard request
|
2008-12-03 15:34:43 +01:00
|
|
|
process_iq(_, #iq{type = get, ns = ?NS_VCARD, lang = Lang} = IQ_Rec, _) ->
|
|
|
|
Result = #xmlel{ns = ?NS_VCARD, name = 'vCard',
|
|
|
|
children = iq_vcard(Lang)},
|
|
|
|
exmpp_iq:result(IQ_Rec, Result);
|
2006-10-28 04:04:55 +02:00
|
|
|
|
|
|
|
%% bytestreams info request
|
2008-12-03 15:34:43 +01:00
|
|
|
process_iq(JID, #iq{type = get, ns = ?NS_BYTESTREAMS} = IQ_Rec,
|
2006-10-28 04:04:55 +02:00
|
|
|
#state{acl = ACL, stream_addr = StreamAddr, serverhost = ServerHost}) ->
|
|
|
|
case acl:match_rule(ServerHost, ACL, JID) of
|
|
|
|
allow ->
|
2008-12-03 15:34:43 +01:00
|
|
|
StreamHostEl = [#xmlel{ns = ?NS_BYTESTREAMS, name = 'streamhost',
|
|
|
|
attrs = StreamAddr}],
|
|
|
|
Result = #xmlel{ns = ?NS_BYTESTREAMS, name = 'query',
|
|
|
|
children = StreamHostEl},
|
|
|
|
exmpp_iq:result(IQ_Rec, Result);
|
2006-10-28 04:04:55 +02:00
|
|
|
deny ->
|
2008-12-03 15:34:43 +01:00
|
|
|
exmpp_iq:error(IQ_Rec, 'forbidden')
|
2006-10-28 04:04:55 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
%% bytestream activation request
|
2008-12-03 15:34:43 +01:00
|
|
|
process_iq(InitiatorJID, #iq{type = set, payload = SubEl, ns = ?NS_BYTESTREAMS} = IQ_Rec,
|
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
|
|
|
|
allow ->
|
2008-12-03 15:34:43 +01:00
|
|
|
ActivateEl = exmpp_xml:get_path(SubEl, [{element, 'activate'}]),
|
2009-01-21 14:34:26 +01:00
|
|
|
SID = exmpp_xml:get_attribute_as_list(SubEl, 'sid', ""),
|
2010-06-09 07:56:33 +02:00
|
|
|
case catch exmpp_jid:parse(exmpp_xml:get_cdata_as_list(ActivateEl)) of
|
2008-12-03 15:34:43 +01:00
|
|
|
TargetJID when ?IS_JID(TargetJID), SID /= "",
|
2006-10-28 04:04:55 +02:00
|
|
|
length(SID) =< 128, TargetJID /= InitiatorJID ->
|
2009-06-01 18:53:48 +02:00
|
|
|
Target = exmpp_jid:prep_to_list(TargetJID),
|
|
|
|
Initiator = exmpp_jid:prep_to_list(InitiatorJID),
|
2006-10-28 04:04:55 +02:00
|
|
|
SHA1 = sha:sha(SID ++ Initiator ++ Target),
|
2007-06-27 13:05:14 +02:00
|
|
|
case mod_proxy65_sm:activate_stream(SHA1, InitiatorJID, TargetJID, ServerHost) of
|
2006-10-28 04:04:55 +02:00
|
|
|
ok ->
|
2008-12-03 15:34:43 +01:00
|
|
|
exmpp_iq:result(IQ_Rec);
|
2006-10-28 04:04:55 +02:00
|
|
|
false ->
|
2008-12-03 15:34:43 +01:00
|
|
|
exmpp_iq:error(IQ_Rec, 'item-not-found');
|
2006-10-28 04:04:55 +02:00
|
|
|
limit ->
|
2008-12-03 15:34:43 +01:00
|
|
|
exmpp_iq:error(IQ_Rec, 'resource-constraint');
|
2006-10-28 04:04:55 +02:00
|
|
|
conflict ->
|
2008-12-03 15:34:43 +01:00
|
|
|
exmpp_iq:error(IQ_Rec, 'conflict');
|
2006-10-28 04:04:55 +02:00
|
|
|
_ ->
|
2008-12-03 15:34:43 +01:00
|
|
|
exmpp_iq:error(IQ_Rec, 'internal-server-error')
|
2006-10-28 04:04:55 +02:00
|
|
|
end;
|
2007-06-27 13:05:14 +02:00
|
|
|
_ ->
|
2008-12-03 15:34:43 +01:00
|
|
|
exmpp_iq:error(IQ_Rec, 'bad-request')
|
2006-10-28 04:04:55 +02:00
|
|
|
end;
|
|
|
|
deny ->
|
2008-12-03 15:34:43 +01:00
|
|
|
exmpp_iq:error(IQ_Rec, 'forbidden')
|
2006-10-28 04:04:55 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
%% Unknown "set" or "get" request
|
2008-12-03 15:34:43 +01:00
|
|
|
process_iq(_, #iq{kind=request} = IQ_Rec, _) ->
|
|
|
|
exmpp_iq:error(IQ_Rec, 'service-unavailable');
|
2006-10-28 04:04:55 +02:00
|
|
|
|
|
|
|
%% IQ "result" or "error".
|
|
|
|
process_iq(_, _, _) ->
|
|
|
|
ok.
|
|
|
|
|
|
|
|
%%%-------------------------
|
|
|
|
%%% Auxiliary functions.
|
|
|
|
%%%-------------------------
|
2008-12-03 15:34:43 +01:00
|
|
|
-define(FEATURE(Feat), #xmlel{ns = ?NS_DISCO_INFO, name = 'feature',
|
2009-01-21 14:34:26 +01:00
|
|
|
attrs = [?XMLATTR('var', Feat)]}).
|
2006-10-28 04:04:55 +02:00
|
|
|
|
2007-06-27 13:05:14 +02:00
|
|
|
iq_disco_info(Lang, Name) ->
|
2008-12-03 15:34:43 +01:00
|
|
|
[#xmlel{ns = ?NS_DISCO_INFO, name = 'identity', attrs =
|
2009-01-21 14:34:26 +01:00
|
|
|
[?XMLATTR('category', <<"proxy">>),
|
|
|
|
?XMLATTR('type', <<"bytestreams">>),
|
|
|
|
?XMLATTR('name', translate:translate(Lang, Name))]},
|
2008-12-03 15:34:43 +01:00
|
|
|
?FEATURE(?NS_DISCO_INFO_s),
|
|
|
|
?FEATURE(?NS_VCARD_s),
|
|
|
|
?FEATURE(?NS_BYTESTREAMS_s)].
|
2006-10-28 04:04:55 +02:00
|
|
|
|
|
|
|
iq_vcard(Lang) ->
|
2008-12-03 15:34:43 +01:00
|
|
|
[#xmlel{ns = ?NS_VCARD, name = 'FN', children =
|
|
|
|
[#xmlcdata{cdata = <<"ejabberd/mod_proxy65">>}]},
|
|
|
|
#xmlel{ns = ?NS_VCARD, name = 'URL', children =
|
|
|
|
[#xmlcdata{cdata = list_to_binary(?EJABBERD_URI)}]},
|
|
|
|
#xmlel{ns = ?NS_VCARD, name = 'DESC', children =
|
|
|
|
[#xmlcdata{cdata = list_to_binary(translate:translate(Lang, "ejabberd SOCKS5 Bytestreams module") ++
|
2010-01-12 17:15:16 +01:00
|
|
|
"\nCopyright (c) 2003-2010 Alexey Shchepin")}]}].
|
2006-10-28 04:04:55 +02:00
|
|
|
|
|
|
|
parse_options(ServerHost, Opts) ->
|
2010-07-22 18:42:18 +02:00
|
|
|
MyHost = gen_mod:expand_host_name(ServerHost, Opts, "proxy"),
|
2006-10-28 04:04:55 +02:00
|
|
|
Port = gen_mod:get_opt(port, Opts, 7777),
|
|
|
|
ACL = gen_mod:get_opt(access, Opts, all),
|
|
|
|
Name = gen_mod:get_opt(name, Opts, "SOCKS5 Bytestreams"),
|
|
|
|
IP = case gen_mod:get_opt(ip, Opts, none) of
|
2009-03-09 15:44:46 +01:00
|
|
|
none -> get_my_ip();
|
|
|
|
Addr -> Addr
|
|
|
|
end,
|
2010-02-17 15:58:53 +01:00
|
|
|
HostName = case gen_mod:get_opt(hostname, Opts, none) of
|
|
|
|
none ->
|
|
|
|
inet_parse:ntoa(IP);
|
|
|
|
HostAddr when is_tuple(HostAddr) ->
|
|
|
|
inet_parse:ntoa(HostAddr);
|
|
|
|
HostNameStr ->
|
|
|
|
HostNameStr
|
|
|
|
end,
|
|
|
|
StreamAddr = [?XMLATTR('jid', MyHost), ?XMLATTR('host', HostName),
|
|
|
|
?XMLATTR('port', Port)],
|
2009-01-19 16:27:07 +01:00
|
|
|
#state{myhost = MyHost,
|
2009-03-09 15:44:46 +01:00
|
|
|
serverhost = ServerHost,
|
|
|
|
name = Name,
|
|
|
|
port = Port,
|
|
|
|
ip = IP,
|
|
|
|
stream_addr = StreamAddr,
|
|
|
|
acl = ACL}.
|
|
|
|
|
|
|
|
get_my_ip() ->
|
|
|
|
{ok, MyHostName} = inet:gethostname(),
|
|
|
|
case inet:getaddr(MyHostName, inet) of
|
2006-11-30 14:39:05 +01:00
|
|
|
{ok, Addr} -> Addr;
|
2009-03-09 15:44:46 +01:00
|
|
|
{error, _} -> {127,0,0,1}
|
2007-08-25 19:24:00 +02:00
|
|
|
end.
|