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
|
|
|
%%%
|
|
|
|
%%%
|
2008-01-15 18:02:57 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2008 Process-one
|
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.
|
|
|
|
%%%
|
|
|
|
%%% 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.
|
|
|
|
-export([start_link/2]).
|
|
|
|
|
|
|
|
-include("../ejabberd.hrl").
|
|
|
|
-include("../jlib.hrl").
|
|
|
|
|
|
|
|
-define(PROCNAME, ejabberd_mod_proxy65_service).
|
|
|
|
|
|
|
|
-record(state, {
|
|
|
|
myhost,
|
|
|
|
serverhost,
|
|
|
|
name,
|
|
|
|
stream_addr,
|
|
|
|
port,
|
|
|
|
acl
|
|
|
|
}).
|
|
|
|
|
|
|
|
%% Unused callbacks.
|
|
|
|
handle_cast(_Request, State) ->
|
|
|
|
{noreply, State}.
|
|
|
|
code_change(_OldVsn, State, _Extra) ->
|
|
|
|
{ok, State}.
|
|
|
|
handle_call(_Request, _From, State) ->
|
|
|
|
{reply, ok, State}.
|
|
|
|
%%----------------
|
|
|
|
|
|
|
|
start_link(Host, Opts) ->
|
|
|
|
Proc = gen_mod:get_module_proc(Host, ?PROCNAME),
|
|
|
|
gen_server:start_link({local, Proc}, ?MODULE, [Host, Opts], []).
|
|
|
|
|
|
|
|
init([Host, Opts]) ->
|
|
|
|
{IP, State} = parse_options(Host, Opts),
|
|
|
|
NewOpts = [Host, {ip, IP} | Opts],
|
|
|
|
ejabberd_listener:add_listener(State#state.port, mod_proxy65_stream, NewOpts),
|
|
|
|
ejabberd_router:register_route(State#state.myhost),
|
|
|
|
{ok, State}.
|
|
|
|
|
|
|
|
terminate(_Reason, #state{myhost=MyHost, port=Port}) ->
|
|
|
|
catch ejabberd_listener:delete_listener(Port),
|
|
|
|
ejabberd_router:unregister_route(MyHost),
|
|
|
|
ok.
|
|
|
|
|
|
|
|
handle_info({route, From, To, {xmlelement, "iq", _, _} = Packet}, State) ->
|
|
|
|
IQ = jlib:iq_query_info(Packet),
|
|
|
|
case catch process_iq(From, IQ, State) of
|
|
|
|
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
|
|
|
|
end,
|
|
|
|
{noreply, State};
|
|
|
|
|
|
|
|
handle_info(_Info, State) ->
|
|
|
|
{noreply, State}.
|
|
|
|
|
|
|
|
%%%------------------------
|
|
|
|
%%% IQ Processing
|
|
|
|
%%%------------------------
|
|
|
|
|
|
|
|
%% disco#info request
|
2007-06-27 13:05:14 +02:00
|
|
|
process_iq(_, #iq{type = get, xmlns = ?NS_DISCO_INFO, lang = Lang} = IQ, #state{name=Name}) ->
|
2006-10-28 04:04:55 +02:00
|
|
|
IQ#iq{type = result, sub_el =
|
2007-06-27 13:05:14 +02:00
|
|
|
[{xmlelement, "query", [{"xmlns", ?NS_DISCO_INFO}], iq_disco_info(Lang, Name)}]};
|
2006-10-28 04:04:55 +02:00
|
|
|
|
|
|
|
%% disco#items request
|
|
|
|
process_iq(_, #iq{type = get, xmlns = ?NS_DISCO_ITEMS} = IQ, _) ->
|
|
|
|
IQ#iq{type = result, sub_el =
|
|
|
|
[{xmlelement, "query", [{"xmlns", ?NS_DISCO_ITEMS}], []}]};
|
|
|
|
|
|
|
|
%% vCard request
|
|
|
|
process_iq(_, #iq{type = get, xmlns = ?NS_VCARD, lang = Lang} = IQ, _) ->
|
|
|
|
IQ#iq{type = result, sub_el =
|
|
|
|
[{xmlelement, "vCard", [{"xmlns", ?NS_VCARD}], iq_vcard(Lang)}]};
|
|
|
|
|
|
|
|
%% bytestreams info request
|
|
|
|
process_iq(JID, #iq{type = get, sub_el = SubEl, xmlns = ?NS_BYTESTREAMS} = IQ,
|
|
|
|
#state{acl = ACL, stream_addr = StreamAddr, serverhost = ServerHost}) ->
|
|
|
|
case acl:match_rule(ServerHost, ACL, JID) of
|
|
|
|
allow ->
|
|
|
|
StreamHostEl = [{xmlelement, "streamhost", StreamAddr, []}],
|
|
|
|
IQ#iq{type = result, sub_el =
|
|
|
|
[{xmlelement, "query", [{"xmlns", ?NS_BYTESTREAMS}], StreamHostEl}]};
|
|
|
|
deny ->
|
|
|
|
IQ#iq{type = error, sub_el = [SubEl, ?ERR_FORBIDDEN]}
|
|
|
|
end;
|
|
|
|
|
|
|
|
%% bytestream activation request
|
|
|
|
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
|
|
|
|
allow ->
|
|
|
|
ActivateEl = xml:get_path_s(SubEl, [{elem, "activate"}]),
|
|
|
|
SID = xml:get_tag_attr_s("sid", SubEl),
|
|
|
|
case catch jlib:string_to_jid(xml:get_tag_cdata(ActivateEl)) of
|
|
|
|
TargetJID when is_record(TargetJID, jid), SID /= "",
|
|
|
|
length(SID) =< 128, TargetJID /= InitiatorJID ->
|
|
|
|
Target = jlib:jid_to_string(jlib:jid_tolower(TargetJID)),
|
|
|
|
Initiator = jlib:jid_to_string(jlib:jid_tolower(InitiatorJID)),
|
|
|
|
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 ->
|
2007-06-27 13:05:14 +02:00
|
|
|
IQ#iq{type = result, sub_el = []};
|
2006-10-28 04:04:55 +02:00
|
|
|
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;
|
2007-06-27 13:05:14 +02:00
|
|
|
_ ->
|
2006-10-28 04:04:55 +02:00
|
|
|
IQ#iq{type = error, sub_el = [SubEl, ?ERR_BAD_REQUEST]}
|
|
|
|
end;
|
|
|
|
deny ->
|
|
|
|
IQ#iq{type = error, sub_el = [SubEl, ?ERR_FORBIDDEN]}
|
|
|
|
end;
|
|
|
|
|
|
|
|
%% Unknown "set" or "get" request
|
|
|
|
process_iq(_, #iq{type=Type, sub_el=SubEl} = IQ, _) when Type==get; Type==set ->
|
|
|
|
IQ#iq{type = error, sub_el = [SubEl, ?ERR_SERVICE_UNAVAILABLE]};
|
|
|
|
|
|
|
|
%% IQ "result" or "error".
|
|
|
|
process_iq(_, _, _) ->
|
|
|
|
ok.
|
|
|
|
|
|
|
|
%%%-------------------------
|
|
|
|
%%% Auxiliary functions.
|
|
|
|
%%%-------------------------
|
|
|
|
-define(FEATURE(Feat), {xmlelement,"feature",[{"var", Feat}],[]}).
|
|
|
|
|
2007-06-27 13:05:14 +02:00
|
|
|
iq_disco_info(Lang, Name) ->
|
2006-10-28 04:04:55 +02:00
|
|
|
[{xmlelement, "identity",
|
|
|
|
[{"category", "proxy"},
|
|
|
|
{"type", "bytestreams"},
|
2007-06-27 13:05:14 +02:00
|
|
|
{"name", translate:translate(Lang, Name)}], []},
|
2006-10-28 04:04:55 +02:00
|
|
|
?FEATURE(?NS_DISCO_INFO),
|
|
|
|
?FEATURE(?NS_DISCO_ITEMS),
|
|
|
|
?FEATURE(?NS_VCARD),
|
2007-06-27 13:05:14 +02:00
|
|
|
?FEATURE(?NS_BYTESTREAMS)].
|
2006-10-28 04:04:55 +02:00
|
|
|
|
|
|
|
iq_vcard(Lang) ->
|
|
|
|
[{xmlelement, "FN", [],
|
|
|
|
[{xmlcdata, "ejabberd/mod_proxy65"}]},
|
|
|
|
{xmlelement, "URL", [],
|
|
|
|
[{xmlcdata, ?EJABBERD_URI}]},
|
|
|
|
{xmlelement, "DESC", [],
|
2007-12-15 13:02:00 +01:00
|
|
|
[{xmlcdata, translate:translate(Lang, "ejabberd SOCKS5 Bytestreams module") ++
|
2008-01-15 18:02:57 +01:00
|
|
|
"\nCopyright (c) 2003-2008 Alexey Shchepin"}]}].
|
2006-10-28 04:04:55 +02:00
|
|
|
|
|
|
|
parse_options(ServerHost, Opts) ->
|
2007-08-25 19:24:00 +02:00
|
|
|
MyHost = gen_mod:get_opt_host(ServerHost, Opts, "proxy.@HOST@"),
|
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
|
2006-11-30 14:39:05 +01:00
|
|
|
none -> get_proxy_or_domainip(ServerHost, MyHost);
|
|
|
|
Addr -> Addr
|
|
|
|
end,
|
2008-03-09 11:09:38 +01:00
|
|
|
StrIP = inet_parse:ntoa(IP),
|
2006-10-28 04:04:55 +02:00
|
|
|
StreamAddr = [{"jid", MyHost}, {"host", StrIP}, {"port", integer_to_list(Port)}],
|
|
|
|
{IP, #state{myhost = MyHost,
|
|
|
|
serverhost = ServerHost,
|
|
|
|
name = Name,
|
|
|
|
port = Port,
|
2007-06-27 13:05:14 +02:00
|
|
|
stream_addr = StreamAddr,
|
2006-10-28 04:04:55 +02:00
|
|
|
acl = ACL}}.
|
2006-11-30 14:39:05 +01:00
|
|
|
|
|
|
|
%% Return the IP of the proxy host, or if not found, the ip of the xmpp domain
|
|
|
|
get_proxy_or_domainip(ServerHost, MyHost) ->
|
|
|
|
case inet:getaddr(MyHost, inet) of
|
|
|
|
{ok, Addr} -> Addr;
|
|
|
|
{error, _} ->
|
2006-11-30 14:50:59 +01:00
|
|
|
case inet:getaddr(ServerHost, inet) of
|
2006-11-30 14:39:05 +01:00
|
|
|
{ok, Addr} -> Addr;
|
|
|
|
{error, _} -> {127,0,0,1}
|
|
|
|
end
|
2007-08-25 19:24:00 +02:00
|
|
|
end.
|