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
|
|
|
%%%
|
2019-01-08 22:53:27 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2019 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').
|
|
|
|
|
2017-08-05 19:58:21 +02:00
|
|
|
-behaviour(p1_fsm).
|
2018-09-17 10:21:02 +02:00
|
|
|
-behaviour(ejabberd_listener).
|
2006-10-28 04:04:55 +02:00
|
|
|
|
|
|
|
%% 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.
|
2018-09-17 10:21:02 +02:00
|
|
|
-export([accepting/2, wait_for_init/2, wait_for_auth/2,
|
2013-03-14 10:33:02 +01:00
|
|
|
wait_for_request/2, wait_for_activation/2,
|
|
|
|
stream_established/2]).
|
2006-10-28 04:04:55 +02:00
|
|
|
|
2019-04-01 15:53:28 +02:00
|
|
|
-export([start/3, stop/1, start_link/3, activate/2,
|
2019-06-14 11:33:26 +02:00
|
|
|
relay/3, accept/1, listen_options/0]).
|
2006-10-28 04:04:55 +02:00
|
|
|
|
|
|
|
-include("mod_proxy65.hrl").
|
2013-03-14 10:33:02 +01:00
|
|
|
|
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,
|
2018-07-05 08:31:55 +02:00
|
|
|
shaper = none :: ejabberd_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
|
|
|
%%-------------------------------
|
|
|
|
|
2019-06-14 11:33:26 +02:00
|
|
|
start(gen_tcp, Socket, Opts) ->
|
|
|
|
Host = proplists:get_value(server_host, Opts),
|
|
|
|
p1_fsm:start(?MODULE, [Socket, Host], []).
|
|
|
|
|
|
|
|
start_link(gen_tcp, Socket, Opts) ->
|
|
|
|
Host = proplists:get_value(server_host, Opts),
|
|
|
|
p1_fsm:start_link(?MODULE, [Socket, Host], []).
|
|
|
|
|
|
|
|
init([Socket, Host]) ->
|
2006-10-28 04:04:55 +02:00
|
|
|
process_flag(trap_exit, true),
|
2019-06-14 11:33:26 +02:00
|
|
|
AuthType = mod_proxy65_opt:auth_type(Host),
|
|
|
|
Shaper = mod_proxy65_opt:shaper(Host),
|
|
|
|
RecvBuf = mod_proxy65_opt:recbuf(Host),
|
|
|
|
SendBuf = mod_proxy65_opt:sndbuf(Host),
|
2006-10-28 04:04:55 +02:00
|
|
|
TRef = erlang:send_after(?WAIT_TIMEOUT, self(), stop),
|
2018-09-17 10:21:02 +02:00
|
|
|
inet:setopts(Socket, [{recbuf, RecvBuf}, {sndbuf, SendBuf}]),
|
|
|
|
{ok, accepting,
|
2013-03-14 10:33:02 +01:00
|
|
|
#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}) ->
|
2017-01-16 13:28:11 +01:00
|
|
|
Mod = gen_mod:ram_db_mod(global, mod_proxy65),
|
|
|
|
Mod:unregister_stream(SHA1),
|
2007-02-22 06:03:51 +01:00
|
|
|
if StateName == stream_established ->
|
2017-01-17 21:06:55 +01:00
|
|
|
?INFO_MSG("(~w) Bytestream terminated", [self()]);
|
2013-03-14 10:33:02 +01:00
|
|
|
true -> ok
|
2006-10-28 04:04:55 +02:00
|
|
|
end.
|
|
|
|
|
|
|
|
%%%------------------------------
|
|
|
|
%%% API.
|
|
|
|
%%%------------------------------
|
2018-09-17 10:21:02 +02:00
|
|
|
accept(StreamPid) ->
|
|
|
|
p1_fsm:send_event(StreamPid, accept).
|
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}) ->
|
2017-08-05 19:58:21 +02:00
|
|
|
case catch {p1_fsm:sync_send_all_state_event(P1,
|
2013-03-14 10:33:02 +01:00
|
|
|
get_socket),
|
2017-08-05 19:58:21 +02:00
|
|
|
p1_fsm:sync_send_all_state_event(P2, get_socket)}
|
2013-03-14 10:33:02 +01:00
|
|
|
of
|
|
|
|
{S1, S2} when is_port(S1), is_port(S2) ->
|
|
|
|
P1 ! {activate, P2, S2, J1, J2},
|
|
|
|
P2 ! {activate, P1, S1, J1, J2},
|
2017-02-26 08:07:12 +01:00
|
|
|
JID1 = jid:encode(J1),
|
|
|
|
JID2 = jid:encode(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
|
|
|
|
%%%-----------------------
|
2018-09-17 10:21:02 +02:00
|
|
|
accepting(accept, State) ->
|
|
|
|
inet:setopts(State#state.socket, [{active, true}]),
|
|
|
|
{next_state, wait_for_init, State}.
|
|
|
|
|
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} ->
|
2017-01-16 13:28:11 +01:00
|
|
|
Mod = gen_mod:ram_db_mod(global, mod_proxy65),
|
|
|
|
case Mod:register_stream(SHA1, self()) of
|
|
|
|
ok ->
|
2013-03-14 10:33:02 +01:00
|
|
|
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 ->
|
2018-07-17 20:50:58 +02:00
|
|
|
misc:cancel_timer(StateData#state.timer),
|
2006-10-28 04:04:55 +02:00
|
|
|
TRef = erlang:send_after(?WAIT_TIMEOUT, self(), stop),
|
2017-08-05 19:58:21 +02:00
|
|
|
p1_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),
|
2018-07-17 20:50:58 +02:00
|
|
|
misc:cancel_timer(StateData#state.timer),
|
2006-10-28 04:04:55 +02:00
|
|
|
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
|
2018-07-03 12:44:58 +02:00
|
|
|
{ok, Data} ->
|
|
|
|
case gen_tcp:send(PeerSocket, Data) of
|
|
|
|
ok ->
|
2018-07-05 08:31:55 +02:00
|
|
|
{NewShaper, Pause} = ejabberd_shaper:update(Shaper, byte_size(Data)),
|
2018-07-03 12:44:58 +02:00
|
|
|
if Pause > 0 -> timer:sleep(Pause);
|
|
|
|
true -> pass
|
|
|
|
end,
|
|
|
|
relay(MySocket, PeerSocket, NewShaper);
|
|
|
|
{error, _} = Err ->
|
|
|
|
Err
|
|
|
|
end;
|
|
|
|
{error, _} = Err ->
|
|
|
|
Err
|
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) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
R1 = ejabberd_shaper:match(Host, Shaper, JID1),
|
|
|
|
R2 = ejabberd_shaper:match(Host, Shaper, JID2),
|
|
|
|
R = case ejabberd_shaper:get_max_rate(R1) >= ejabberd_shaper:get_max_rate(R2) of
|
|
|
|
true -> R1;
|
|
|
|
false -> R2
|
|
|
|
end,
|
|
|
|
ejabberd_shaper:new(R).
|
2018-01-23 08:54:52 +01:00
|
|
|
|
|
|
|
listen_options() ->
|
2019-06-14 11:33:26 +02:00
|
|
|
[].
|