Skip to content

Commit

Permalink
adjust
Browse files Browse the repository at this point in the history
  • Loading branch information
yueyoum committed Apr 20, 2015
1 parent b10f7b6 commit cf8af82
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 16 deletions.
2 changes: 1 addition & 1 deletion client/src/mpc_http_acceptor_sup.erl
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ start_link() ->
{ok, LSock} = gen_tcp:listen(Port, [binary,
{ip, {0, 0, 0, 0}},
{reuseaddr, true},
{active, once},
{active, false},
{backlog, 256}]),

supervisor:start_link({local, ?SERVER}, ?MODULE, [LSock]).
Expand Down
13 changes: 9 additions & 4 deletions client/src/mpc_http_child.erl
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,13 @@ start_link(Socket) ->
%% @end
%%--------------------------------------------------------------------
init([Socket]) ->
{ok, Key} = application:get_env(make_proxy_client, key),
{ok, #state{key=Key, socket=Socket}}.
case inet:setopts(Socket, [{active, once}]) of
ok ->
{ok, Key} = application:get_env(make_proxy_client, key),
{ok, #state{key=Key, socket=Socket}};
{error, Reason} ->
{stop, Reason}
end.

%%--------------------------------------------------------------------
%% @private
Expand Down Expand Up @@ -96,7 +101,7 @@ handle_cast({http_new, Socket, Request}, #state{key = Key, socket = Socket} = St
{ok, Target, NormalizedReqeust} ->
ok = gen_tcp:send(Remote, mp_crypto:encrypt(Key, Target)),
ok = gen_tcp:send(Remote, mp_crypto:encrypt(Key, NormalizedReqeust)),
inet:setopts(Socket, [{active, once}]),
ok = inet:setopts(Socket, [{active, once}]),
{noreply, State#state{remote = Remote}, ?TIMEOUT};
{error, Reason} ->
{stop, Reason, State}
Expand Down Expand Up @@ -154,7 +159,7 @@ handle_info({tcp, Socket, Response}, #state{key=Key, socket=Client} = State) whe
{ok, RealData} = mp_crypto:decrypt(Key, Response),
case gen_tcp:send(Client, RealData) of
ok ->
inet:setopts(Socket, [{active, once}]),
ok = inet:setopts(Socket, [{active, once}]),
{noreply, State, ?TIMEOUT};
{error, Error} ->
{stop, Error, State}
Expand Down
8 changes: 4 additions & 4 deletions client/src/mpc_socks5_child.erl
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ handle_cast(_Info, State) ->
handle_info(timeout, #state{key=Key, socket=Socket, started = false} = State) ->
case start_process(Socket, Key) of
{ok, Remote} ->
inet:setopts(Socket, [{active, once}]),
inet:setopts(Remote, [{active, once}]),
ok = inet:setopts(Socket, [{active, once}]),
ok = inet:setopts(Remote, [{active, once}]),
{noreply, State#state{socket=Socket, remote=Remote, started = true}, ?TIMEOUT};
{error, Error} ->
{stop, Error, State}
Expand All @@ -113,7 +113,7 @@ handle_info(timeout, #state{started = true} = State)->
handle_info({tcp, Socket, Request}, #state{key=Key, socket=Socket, remote=Remote} = State) ->
case gen_tcp:send(Remote, mp_crypto:encrypt(Key, Request)) of
ok ->
inet:setopts(Socket, [{active, once}]),
ok = inet:setopts(Socket, [{active, once}]),
{noreply, State, ?TIMEOUT};
{error, Error} ->
{stop, Error, State}
Expand All @@ -124,7 +124,7 @@ handle_info({tcp, Socket, Response}, #state{key=Key, socket=Client, remote=Socke
{ok, RealData} = mp_crypto:decrypt(Key, Response),
case gen_tcp:send(Client, RealData) of
ok ->
inet:setopts(Socket, [{active, once}]),
ok = inet:setopts(Socket, [{active, once}]),
{noreply, State, ?TIMEOUT};
{error, Error} ->
{stop, Error, State}
Expand Down
2 changes: 1 addition & 1 deletion server/src/mp_acceptor_sup.erl
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ start_link() ->
{ok, LSock} = gen_tcp:listen(Port, [binary,
{ip, {0, 0, 0, 0}},
{reuseaddr, true},
{active, once},
{active, false},
{packet, 4},
{backlog, 256}]),

Expand Down
17 changes: 11 additions & 6 deletions server/src/mp_child.erl
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,13 @@ start_link(Socket) ->
%% @end
%%--------------------------------------------------------------------
init([Socket]) ->
{ok, Key} = application:get_env(make_proxy_server, key),
{ok, #state{key=Key, socket = Socket}, ?TIMEOUT}.
case inet:setopts(Socket, [{active, once}]) of
ok ->
{ok, Key} = application:get_env(make_proxy_server, key),
{ok, #state{key=Key, socket = Socket}, ?TIMEOUT};
{error, Reason} ->
{stop, Reason}
end.

%%--------------------------------------------------------------------
%% @private
Expand Down Expand Up @@ -105,8 +110,8 @@ handle_info(timeout, #state{socket=Socket} = State) when is_port(Socket) ->
handle_info({tcp, Socket, Request}, #state{key = Key, socket = Socket, remote = undefined} = State) ->
case connect_to_remote(Request, Key) of
{ok, Remote} ->
inet:setopts(Socket, [{active, once}]),
inet:setopts(Remote, [{active, once}]),
ok = inet:setopts(Socket, [{active, once}]),
ok = inet:setopts(Remote, [{active, once}]),
{noreply, State#state{remote=Remote}, ?TIMEOUT};
{error, Error} ->
{stop, Error, State}
Expand All @@ -117,7 +122,7 @@ handle_info({tcp, Socket, Request}, #state{key=Key, socket=Socket, remote=Remote
{ok, RealData} = mp_crypto:decrypt(Key, Request),
case gen_tcp:send(Remote, RealData) of
ok ->
inet:setopts(Socket, [{active, once}]),
ok = inet:setopts(Socket, [{active, once}]),
{noreply, State, ?TIMEOUT};
{error, Error} ->
{stop, Error, State}
Expand All @@ -127,7 +132,7 @@ handle_info({tcp, Socket, Request}, #state{key=Key, socket=Socket, remote=Remote
handle_info({tcp, Socket, Response}, #state{key=Key, socket=Client, remote=Socket} = State) ->
case gen_tcp:send(Client, mp_crypto:encrypt(Key, Response)) of
ok ->
inet:setopts(Socket, [{active, once}]),
ok = inet:setopts(Socket, [{active, once}]),
{noreply, State, ?TIMEOUT};
{error, Error} ->
{stop, Error, State}
Expand Down

0 comments on commit cf8af82

Please sign in to comment.