2006-10-28 04:04:55 +02:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : mod_proxy65_stream.erl
|
|
|
|
%%% Author : Evgeniy Khramtsov <xram@jabber.ru>
|
|
|
|
%%% Purpose : Bytestream process.
|
|
|
|
%%% 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_stream).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2006-10-28 04:04:55 +02:00
|
|
|
-author('xram@jabber.ru').
|
|
|
|
|
|
|
|
-behaviour(gen_fsm).
|
|
|
|
|
|
|
|
%% gen_fsm callbacks.
|
2013-03-14 10:33:02 +01:00
|
|
|
-export([init/1, handle_event/3, handle_sync_event/4,
|
|
|
|
code_change/4, handle_info/3, terminate/3]).
|
2006-10-28 04:04:55 +02:00
|
|
|
|
|
|
|
%% gen_fsm states.
|
2013-03-14 10:33:02 +01:00
|
|
|
-export([wait_for_init/2, wait_for_auth/2,
|
|
|
|
wait_for_request/2, wait_for_activation/2,
|
|
|
|
stream_established/2]).
|
2006-10-28 04:04:55 +02:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-export([start/2, stop/1, start_link/3, activate/2,
|
2015-06-03 15:05:17 +02:00
|
|
|
relay/3, socket_type/0]).
|
2006-10-28 04:04:55 +02:00
|
|
|
|
|
|
|
-include("mod_proxy65.hrl").
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2008-07-13 21:10:01 +02:00
|
|
|
-include("ejabberd.hrl").
|
2013-04-08 11:12:54 +02:00
|
|
|
-include("logger.hrl").
|
2006-10-28 04:04:55 +02:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-define(WAIT_TIMEOUT, 60000).
|
2006-10-28 04:04:55 +02:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-record(state,
|
|
|
|
{socket :: inet:socket(),
|
|
|
|
timer = make_ref() :: reference(),
|
|
|
|
sha1 = <<"">> :: binary(),
|
|
|
|
host = <<"">> :: binary(),
|
|
|
|
auth_type = anonymous :: plain | anonymous,
|
|
|
|
shaper = none :: shaper:shaper()}).
|
2006-10-28 04:04:55 +02:00
|
|
|
|
|
|
|
%% Unused callbacks
|
|
|
|
handle_event(_Event, StateName, StateData) ->
|
|
|
|
{next_state, StateName, StateData}.
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2006-10-28 04:04:55 +02:00
|
|
|
code_change(_OldVsn, StateName, StateData, _Extra) ->
|
|
|
|
{ok, StateName, StateData}.
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2006-10-28 04:04:55 +02:00
|
|
|
%%-------------------------------
|
|
|
|
|
2009-01-12 21:03:02 +01:00
|
|
|
start({gen_tcp, Socket}, Opts1) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
{[Host], Opts} = lists:partition(fun (O) -> is_binary(O)
|
|
|
|
end,
|
|
|
|
Opts1),
|
|
|
|
Supervisor = gen_mod:get_module_proc(Host,
|
|
|
|
ejabberd_mod_proxy65_sup),
|
|
|
|
supervisor:start_child(Supervisor,
|
|
|
|
[Socket, Host, Opts]).
|
2006-10-28 04:04:55 +02:00
|
|
|
|
|
|
|
start_link(Socket, Host, Opts) ->
|
|
|
|
gen_fsm:start_link(?MODULE, [Socket, Host, Opts], []).
|
|
|
|
|
|
|
|
init([Socket, Host, Opts]) ->
|
|
|
|
process_flag(trap_exit, true),
|
2013-03-14 10:33:02 +01:00
|
|
|
AuthType = gen_mod:get_opt(auth_type, Opts,
|
|
|
|
fun(plain) -> plain;
|
|
|
|
(anonymous) -> anonymous
|
|
|
|
end, anonymous),
|
|
|
|
Shaper = gen_mod:get_opt(shaper, Opts,
|
2016-06-21 13:18:24 +02:00
|
|
|
fun acl:shaper_rules_validator/1,
|
2013-03-14 10:33:02 +01:00
|
|
|
none),
|
|
|
|
RecvBuf = gen_mod:get_opt(recbuf, Opts,
|
|
|
|
fun(I) when is_integer(I), I>0 -> I end,
|
|
|
|
8192),
|
|
|
|
SendBuf = gen_mod:get_opt(sndbuf, Opts,
|
|
|
|
fun(I) when is_integer(I), I>0 -> I end,
|
|
|
|
8192),
|
2006-10-28 04:04:55 +02:00
|
|
|
TRef = erlang:send_after(?WAIT_TIMEOUT, self(), stop),
|
2013-03-14 10:33:02 +01:00
|
|
|
inet:setopts(Socket,
|
|
|
|
[{active, true}, {recbuf, RecvBuf}, {sndbuf, SendBuf}]),
|
|
|
|
{ok, wait_for_init,
|
|
|
|
#state{host = Host, auth_type = AuthType,
|
|
|
|
socket = Socket, shaper = Shaper, timer = TRef}}.
|
2006-10-28 04:04:55 +02:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
terminate(_Reason, StateName, #state{sha1 = SHA1}) ->
|
2006-10-28 04:04:55 +02:00
|
|
|
catch mod_proxy65_sm:unregister_stream(SHA1),
|
2007-02-22 06:03:51 +01:00
|
|
|
if StateName == stream_established ->
|
2013-03-14 10:33:02 +01:00
|
|
|
?INFO_MSG("Bytestream terminated", []);
|
|
|
|
true -> ok
|
2006-10-28 04:04:55 +02:00
|
|
|
end.
|
|
|
|
|
|
|
|
%%%------------------------------
|
|
|
|
%%% API.
|
|
|
|
%%%------------------------------
|
2013-03-14 10:33:02 +01:00
|
|
|
socket_type() -> raw.
|
2006-10-28 04:04:55 +02:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
stop(StreamPid) -> StreamPid ! stop.
|
2006-10-28 04:04:55 +02:00
|
|
|
|
|
|
|
activate({P1, J1}, {P2, J2}) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
case catch {gen_fsm:sync_send_all_state_event(P1,
|
|
|
|
get_socket),
|
|
|
|
gen_fsm:sync_send_all_state_event(P2, get_socket)}
|
|
|
|
of
|
|
|
|
{S1, S2} when is_port(S1), is_port(S2) ->
|
|
|
|
P1 ! {activate, P2, S2, J1, J2},
|
|
|
|
P2 ! {activate, P1, S1, J1, J2},
|
2015-11-24 16:44:13 +01:00
|
|
|
JID1 = jid:to_string(J1),
|
|
|
|
JID2 = jid:to_string(J2),
|
2013-03-14 10:33:02 +01:00
|
|
|
?INFO_MSG("(~w:~w) Activated bytestream for ~s "
|
|
|
|
"-> ~s",
|
|
|
|
[P1, P2, JID1, JID2]),
|
|
|
|
ok;
|
|
|
|
_ -> error
|
2006-10-28 04:04:55 +02:00
|
|
|
end.
|
|
|
|
|
|
|
|
%%%-----------------------
|
|
|
|
%%% States
|
|
|
|
%%%-----------------------
|
2013-03-14 10:33:02 +01:00
|
|
|
wait_for_init(Packet,
|
|
|
|
#state{socket = Socket, auth_type = AuthType} =
|
|
|
|
StateData) ->
|
2006-10-28 04:04:55 +02:00
|
|
|
case mod_proxy65_lib:unpack_init_message(Packet) of
|
2013-03-14 10:33:02 +01:00
|
|
|
{ok, AuthMethods} ->
|
|
|
|
Method = select_auth_method(AuthType, AuthMethods),
|
|
|
|
gen_tcp:send(Socket,
|
|
|
|
mod_proxy65_lib:make_init_reply(Method)),
|
|
|
|
case Method of
|
|
|
|
?AUTH_ANONYMOUS ->
|
|
|
|
{next_state, wait_for_request, StateData};
|
|
|
|
?AUTH_PLAIN -> {next_state, wait_for_auth, StateData};
|
|
|
|
?AUTH_NO_METHODS -> {stop, normal, StateData}
|
|
|
|
end;
|
|
|
|
error -> {stop, normal, StateData}
|
2006-10-28 04:04:55 +02:00
|
|
|
end.
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
wait_for_auth(Packet,
|
|
|
|
#state{socket = Socket, host = Host} = StateData) ->
|
2006-10-28 04:04:55 +02:00
|
|
|
case mod_proxy65_lib:unpack_auth_request(Packet) of
|
2013-03-14 10:33:02 +01:00
|
|
|
{User, Pass} ->
|
2015-04-09 03:21:09 +02:00
|
|
|
Result = ejabberd_auth:check_password(User, <<"">>, Host, Pass),
|
2013-03-14 10:33:02 +01:00
|
|
|
gen_tcp:send(Socket,
|
|
|
|
mod_proxy65_lib:make_auth_reply(Result)),
|
|
|
|
case Result of
|
|
|
|
true -> {next_state, wait_for_request, StateData};
|
|
|
|
false -> {stop, normal, StateData}
|
|
|
|
end;
|
|
|
|
_ -> {stop, normal, StateData}
|
2006-10-28 04:04:55 +02:00
|
|
|
end.
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
wait_for_request(Packet,
|
|
|
|
#state{socket = Socket} = StateData) ->
|
2006-10-28 04:04:55 +02:00
|
|
|
Request = mod_proxy65_lib:unpack_request(Packet),
|
|
|
|
case Request of
|
2013-03-14 10:33:02 +01:00
|
|
|
#s5_request{sha1 = SHA1, cmd = connect} ->
|
|
|
|
case catch mod_proxy65_sm:register_stream(SHA1) of
|
|
|
|
{atomic, ok} ->
|
|
|
|
inet:setopts(Socket, [{active, false}]),
|
|
|
|
gen_tcp:send(Socket,
|
|
|
|
mod_proxy65_lib:make_reply(Request)),
|
|
|
|
{next_state, wait_for_activation,
|
|
|
|
StateData#state{sha1 = SHA1}};
|
|
|
|
_ ->
|
|
|
|
Err = mod_proxy65_lib:make_error_reply(Request),
|
|
|
|
gen_tcp:send(Socket, Err),
|
|
|
|
{stop, normal, StateData}
|
|
|
|
end;
|
|
|
|
#s5_request{cmd = udp} ->
|
|
|
|
Err = mod_proxy65_lib:make_error_reply(Request,
|
|
|
|
?ERR_COMMAND_NOT_SUPPORTED),
|
|
|
|
gen_tcp:send(Socket, Err),
|
|
|
|
{stop, normal, StateData};
|
|
|
|
_ -> {stop, normal, StateData}
|
2006-10-28 04:04:55 +02:00
|
|
|
end.
|
|
|
|
|
|
|
|
wait_for_activation(_Data, StateData) ->
|
|
|
|
{next_state, wait_for_activation, StateData}.
|
|
|
|
|
|
|
|
stream_established(_Data, StateData) ->
|
|
|
|
{next_state, stream_established, StateData}.
|
|
|
|
|
|
|
|
%%%-----------------------
|
|
|
|
%%% Callbacks processing
|
|
|
|
%%%-----------------------
|
|
|
|
|
|
|
|
%% SOCKS5 packets.
|
|
|
|
handle_info({tcp, _S, Data}, StateName, StateData)
|
2013-03-14 10:33:02 +01:00
|
|
|
when StateName /= wait_for_activation ->
|
2006-10-28 04:04:55 +02:00
|
|
|
erlang:cancel_timer(StateData#state.timer),
|
|
|
|
TRef = erlang:send_after(?WAIT_TIMEOUT, self(), stop),
|
|
|
|
gen_fsm:send_event(self(), Data),
|
2013-03-14 10:33:02 +01:00
|
|
|
{next_state, StateName, StateData#state{timer = TRef}};
|
2006-10-28 04:04:55 +02:00
|
|
|
%% Activation message.
|
|
|
|
handle_info({activate, PeerPid, PeerSocket, IJid, TJid},
|
|
|
|
wait_for_activation, StateData) ->
|
|
|
|
erlang:monitor(process, PeerPid),
|
|
|
|
erlang:cancel_timer(StateData#state.timer),
|
|
|
|
MySocket = StateData#state.socket,
|
|
|
|
Shaper = StateData#state.shaper,
|
|
|
|
Host = StateData#state.host,
|
|
|
|
MaxRate = find_maxrate(Shaper, IJid, TJid, Host),
|
2013-03-14 10:33:02 +01:00
|
|
|
spawn_link(?MODULE, relay,
|
|
|
|
[MySocket, PeerSocket, MaxRate]),
|
2007-02-22 06:03:51 +01:00
|
|
|
{next_state, stream_established, StateData};
|
2006-10-28 04:04:55 +02:00
|
|
|
%% Socket closed
|
2013-03-14 10:33:02 +01:00
|
|
|
handle_info({tcp_closed, _Socket}, _StateName,
|
|
|
|
StateData) ->
|
2006-10-28 04:04:55 +02:00
|
|
|
{stop, normal, StateData};
|
2013-03-14 10:33:02 +01:00
|
|
|
handle_info({tcp_error, _Socket, _Reason}, _StateName,
|
|
|
|
StateData) ->
|
2006-10-28 04:04:55 +02:00
|
|
|
{stop, normal, StateData};
|
|
|
|
%% Got stop message.
|
|
|
|
handle_info(stop, _StateName, StateData) ->
|
|
|
|
{stop, normal, StateData};
|
|
|
|
%% Either linked process or peer process died.
|
2013-03-14 10:33:02 +01:00
|
|
|
handle_info({'EXIT', _, _}, _StateName, StateData) ->
|
2006-10-28 04:04:55 +02:00
|
|
|
{stop, normal, StateData};
|
2013-03-14 10:33:02 +01:00
|
|
|
handle_info({'DOWN', _, _, _, _}, _StateName,
|
|
|
|
StateData) ->
|
2006-10-28 04:04:55 +02:00
|
|
|
{stop, normal, StateData};
|
|
|
|
%% Packets of no interest
|
|
|
|
handle_info(_Info, StateName, StateData) ->
|
|
|
|
{next_state, StateName, StateData}.
|
|
|
|
|
|
|
|
%% Socket request.
|
2013-03-14 10:33:02 +01:00
|
|
|
handle_sync_event(get_socket, _From,
|
|
|
|
wait_for_activation, StateData) ->
|
2006-10-28 04:04:55 +02:00
|
|
|
Socket = StateData#state.socket,
|
|
|
|
{reply, Socket, wait_for_activation, StateData};
|
2013-03-14 10:33:02 +01:00
|
|
|
handle_sync_event(_Event, _From, StateName,
|
|
|
|
StateData) ->
|
2006-10-28 04:04:55 +02:00
|
|
|
{reply, error, StateName, StateData}.
|
|
|
|
|
|
|
|
%%%-------------------------------------------------
|
|
|
|
%%% Relay Process.
|
|
|
|
%%%-------------------------------------------------
|
|
|
|
relay(MySocket, PeerSocket, Shaper) ->
|
|
|
|
case gen_tcp:recv(MySocket, 0) of
|
2013-03-14 10:33:02 +01:00
|
|
|
{ok, Data} ->
|
|
|
|
gen_tcp:send(PeerSocket, Data),
|
|
|
|
{NewShaper, Pause} = shaper:update(Shaper, byte_size(Data)),
|
|
|
|
if Pause > 0 -> timer:sleep(Pause);
|
|
|
|
true -> pass
|
|
|
|
end,
|
|
|
|
relay(MySocket, PeerSocket, NewShaper);
|
|
|
|
_ -> stopped
|
2006-10-28 04:04:55 +02:00
|
|
|
end.
|
|
|
|
|
|
|
|
%%%------------------------
|
|
|
|
%%% Auxiliary functions
|
|
|
|
%%%------------------------
|
|
|
|
select_auth_method(plain, AuthMethods) ->
|
|
|
|
case lists:member(?AUTH_PLAIN, AuthMethods) of
|
2013-03-14 10:33:02 +01:00
|
|
|
true -> ?AUTH_PLAIN;
|
|
|
|
false -> ?AUTH_NO_METHODS
|
2006-10-28 04:04:55 +02:00
|
|
|
end;
|
|
|
|
select_auth_method(anonymous, AuthMethods) ->
|
|
|
|
case lists:member(?AUTH_ANONYMOUS, AuthMethods) of
|
2013-03-14 10:33:02 +01:00
|
|
|
true -> ?AUTH_ANONYMOUS;
|
|
|
|
false -> ?AUTH_NO_METHODS
|
2006-10-28 04:04:55 +02:00
|
|
|
end.
|
|
|
|
|
|
|
|
%% Obviously, we must use shaper with maximum rate.
|
|
|
|
find_maxrate(Shaper, JID1, JID2, Host) ->
|
2013-08-12 14:25:05 +02:00
|
|
|
MaxRate1 = case acl:match_rule(Host, Shaper, JID1) of
|
|
|
|
deny -> none;
|
|
|
|
R1 -> shaper:new(R1)
|
|
|
|
end,
|
|
|
|
MaxRate2 = case acl:match_rule(Host, Shaper, JID2) of
|
|
|
|
deny -> none;
|
|
|
|
R2 -> shaper:new(R2)
|
|
|
|
end,
|
2013-03-14 10:33:02 +01:00
|
|
|
if MaxRate1 == none; MaxRate2 == none -> none;
|
|
|
|
true -> lists:max([MaxRate1, MaxRate2])
|
2006-10-28 04:04:55 +02:00
|
|
|
end.
|