Skip to content

Commit

Permalink
Upgrade to ranch-2.1
Browse files Browse the repository at this point in the history
  • Loading branch information
surik committed Apr 30, 2022
1 parent d2d5e3c commit b42f644
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 20 deletions.
2 changes: 2 additions & 0 deletions rebar.config
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
%% -*- mode: erlang -*-
%% ex: ft=erlang

{plugins, [rebar3_hex]}.

{deps, [
ranch
]}.
Expand Down
8 changes: 5 additions & 3 deletions rebar.lock
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{"1.1.0",
[{<<"ranch">>,{pkg,<<"ranch">>,<<"1.7.1">>},0}]}.
{"1.2.0",
[{<<"ranch">>,{pkg,<<"ranch">>,<<"2.1.0">>},0}]}.
[
{pkg_hash,[
{<<"ranch">>, <<"6B1FAB51B49196860B733A49C07604465A47BDB78AA10C1C16A3D199F7F8C881">>}]}
{<<"ranch">>, <<"2261F9ED9574DCFCC444106B9F6DA155E6E540B2F82BA3D42B339B93673B72A3">>}]},
{pkg_hash_ext,[
{<<"ranch">>, <<"244EE3FA2A6175270D8E1FC59024FD9DBC76294A321057DE8F803B1479E76916">>}]}
].
8 changes: 4 additions & 4 deletions src/tunnerl_app.erl
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ start(_StartType, _StartArgs) ->
Proto = application:get_env(tunnerl, protocols, 1080),
Handler = application:get_env(tunnerl, handler, tunnerl_handler_dummy),

TransportOpts = [{port, Port}, {ip, IP}],
Opts = [{auth, Auth},
{protocols, Proto},
TransportOpts = #{socket_opts => [{port, Port}, {ip, IP}], num_acceptors => NumAcceptors},
Opts = [{auth, Auth},
{protocols, Proto},
{handler, Handler}],

{ok, _} = ranch:start_listener(tunnerl, NumAcceptors, ranch_tcp,
{ok, _} = ranch:start_listener(tunnerl, ranch_tcp,
TransportOpts,
tunnerl_socks_protocol, Opts),

Expand Down
28 changes: 15 additions & 13 deletions src/tunnerl_socks_protocol.erl
Original file line number Diff line number Diff line change
Expand Up @@ -3,45 +3,45 @@
-behavior(ranch_protocol).

%% ranch_protocol callbacks
-export([start_link/4,
init/4]).
-export([start_link/3,
init/3]).

-export([connect/3,
-export([connect/3,
pretty_address/1]).
-export([loop/1]).

-include("tunnerl.hrl").

start_link(Ref, Socket, Transport, Opts) ->
Pid = spawn_link(?MODULE, init, [Ref, Socket, Transport, Opts]),
start_link(Ref, Transport, Opts) ->
Pid = spawn_link(?MODULE, init, [Ref, Transport, Opts]),
{ok, Pid}.

init(Ref, Socket, Transport, Opts) ->
init(Ref, Transport, Opts) ->
Handler = proplists:get_value(handler, Opts, tunnerl_handler_dummy),
Protocols = proplists:get_value(protocols, Opts),
ok = ranch:accept_ack(Ref),
{ok, Socket} = ranch:handshake(Ref),
{ok, {Addr, Port}} = inet:peername(Socket),
State = #state{socks4 = lists:member(socks4, Protocols),
socks5 = lists:member(socks5, Protocols),
handler = Handler,
transport = Transport,
transport = Transport,
client_ip = Addr,
client_port = Port,
incoming_socket = Socket},
{ok, <<Version>>} = Transport:recv(Socket, 1, ?TIMEOUT),
case Version of
?VERSION5 -> loop(tunnerl_socks5:process(State));
?VERSION4 -> loop(tunnerl_socks4:process(State));
_ ->
_ ->
Transport:close(Socket)
end.

loop(#state{transport = Transport,
incoming_socket = ISocket,
loop(#state{transport = Transport,
incoming_socket = ISocket,
outgoing_socket = OSocket} = State) ->
inet:setopts(ISocket, [{active, once}]),
OSocket /= undefined andalso inet:setopts(OSocket, [{active, once}]),
{OK, Closed, Error} = Transport:messages(),
{OK, Closed, Error, _Passive} = Transport:messages(),
receive
{OK, ISocket, Data} ->
Transport:send(OSocket, Data),
Expand All @@ -60,7 +60,9 @@ loop(#state{transport = Transport,
Transport:close(OSocket);
{Error, _OSocket, _Reason} ->
% TODO: inform handler
Transport:close(ISocket)
Transport:close(ISocket);
Other ->
io:format("~p~n", [Other])
end;
loop(_) -> ok.

Expand Down

0 comments on commit b42f644

Please sign in to comment.