Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve connection to remote #737

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
3 changes: 3 additions & 0 deletions NOTICE
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,6 @@ Copyright (c) 2009, Erlang Training and Consulting Ltd.
Copyright (C) 1998 - 2014, Daniel Stenberg, <daniel@haxx.se>, et al.

*) hackney_trace (C) 2015 under the Erlang Public LicensE

*) hackney_cidr is based on inet_cidr 1.2.1. vendored for customer purpose.
Copyright (c) 2024, Enki Multimedia , MIT License
1 change: 1 addition & 0 deletions include/hackney.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,4 @@

-define(HTTP_PROXY_ENV_VARS, ["http_proxy", "HTTP_PROXY", "all_proxy", "ALL_PROXY"]).
-define(HTTPS_PROXY_ENV_VARS, ["https_proxy", "HTTPS_PROXY", "all_proxy", "ALL_PROXY"]).
-define(HTTP_NO_PROXY_ENV_VARS, ["no_proxy", "NO_PROXY"]).
6 changes: 3 additions & 3 deletions rebar.lock
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{"1.2.0",
[{<<"certifi">>,{pkg,<<"certifi">>,<<"2.12.0">>},0},
[{<<"certifi">>,{pkg,<<"certifi">>,<<"2.13.0">>},0},
{<<"idna">>,{pkg,<<"idna">>,<<"6.1.1">>},0},
{<<"metrics">>,{pkg,<<"metrics">>,<<"1.0.1">>},0},
{<<"mimerl">>,{pkg,<<"mimerl">>,<<"1.2.0">>},0},
Expand All @@ -8,15 +8,15 @@
{<<"unicode_util_compat">>,{pkg,<<"unicode_util_compat">>,<<"0.7.0">>},0}]}.
[
{pkg_hash,[
{<<"certifi">>, <<"2D1CCA2EC95F59643862AF91F001478C9863C2AC9CB6E2F89780BFD8DE987329">>},
{<<"certifi">>, <<"E52BE248590050B2DD33B0BB274B56678F9068E67805DCA8AA8B1CCDB016BBF6">>},
{<<"idna">>, <<"8A63070E9F7D0C62EB9D9FCB360A7DE382448200FBBD1B106CC96D3D8099DF8D">>},
{<<"metrics">>, <<"25F094DEA2CDA98213CECC3AEFF09E940299D950904393B2A29D191C346A8486">>},
{<<"mimerl">>, <<"67E2D3F571088D5CFD3E550C383094B47159F3EEE8FFA08E64106CDF5E981BE3">>},
{<<"parse_trans">>, <<"6E6AA8167CB44CC8F39441D05193BE6E6F4E7C2946CB2759F015F8C56B76E5FF">>},
{<<"ssl_verify_fun">>, <<"354C321CF377240C7B8716899E182CE4890C5938111A1296ADD3EC74CF1715DF">>},
{<<"unicode_util_compat">>, <<"BC84380C9AB48177092F43AC89E4DFA2C6D62B40B8BD132B1059ECC7232F9A78">>}]},
{pkg_hash_ext,[
{<<"certifi">>, <<"EE68D85DF22E554040CDB4BE100F33873AC6051387BAF6A8F6CE82272340FF1C">>},
{<<"certifi">>, <<"8F3D9533A0F06070AFDFD5D596B32E21C6580667A492891851B0E2737BC507A1">>},
{<<"idna">>, <<"92376EB7894412ED19AC475E4A86F7B413C1B9FBB5BD16DCCD57934157944CEA">>},
{<<"metrics">>, <<"69B09ADDDC4F74A40716AE54D140F93BEB0FB8978D8636EADED0C31B6F099F16">>},
{<<"mimerl">>, <<"F278585650AA581986264638EBF698F8BB19DF297F66AD91B18910DFC6E19323">>},
Expand Down
147 changes: 130 additions & 17 deletions src/hackney.erl
Original file line number Diff line number Diff line change
Expand Up @@ -311,17 +311,18 @@ request(Method, #hackney_url{}=URL0, Headers0, Body, Options0) ->
URL = hackney_url:normalize(URL0, PathEncodeFun),

?report_trace("request", [{method, Method},
{url, URL},
{headers, Headers0},
{body, Body},
{options, Options0}]),
{url, URL},
{headers, Headers0},
{body, Body},
{options, Options0}]),

#hackney_url{transport=Transport,
host = Host,
port = Port,
user = User,
password = Password,
scheme = Scheme} = URL,
host = Host,
port = Port,
user = User,
password = Password,
scheme = Scheme} = URL,


Options = case User of
<<>> ->
Expand Down Expand Up @@ -676,14 +677,22 @@ maybe_proxy(Transport, Scheme, Host, Port, Options)
end.

maybe_proxy_from_env(Transport, _Scheme, Host, Port, Options, true) ->
?report_debug("request without proxy", []),
?report_debug("no proxy env is forced, request without proxy", []),
hackney_connect:connect(Transport, Host, Port, Options, true);
maybe_proxy_from_env(Transport, Scheme, Host, Port, Options, _) ->
case get_proxy_env(Scheme) of
{ok, Url} ->
proxy_from_url(Url, Transport, Host, Port, Options);
NoProxyEnv = get_no_proxy_env(),
case match_no_proxy_env(NoProxyEnv, Host) of
false ->
?report_debug("request with proxy", [{proxy, Url}, {host, Host}]),
proxy_from_url(Url, Transport, Host, Port, Options);
true ->
?report_debug("request without proxy", []),
hackney_connect:connect(Transport, Host, Port, Options, true)
end;
false ->
?report_debug("request without proxy", []),
?report_debug("no proxy env setup, request without proxy", []),
hackney_connect:connect(Transport, Host, Port, Options, true)
end.

Expand All @@ -705,17 +714,121 @@ proxy_from_url(Url, Transport, Host, Port, Options) ->
end
end.

get_no_proxy_env() ->
case application:get_env(hackney, no_proxy) of
undefined ->
case get_no_proxy_env(?HTTP_NO_PROXY_ENV_VARS) of
false ->
application:set_env(hackney, no_proxy, false),
false;
NoProxyEnv ->
parse_no_proxy_env(NoProxyEnv, [])
end;
{ok, NoProxyEnv} ->
NoProxyEnv
end.

get_no_proxy_env([Key | Rest]) ->
case os:getenv(Key) of
false -> get_no_proxy_env(Rest);
NoProxyStr ->
lists:usort(string:tokens(NoProxyStr, ","))
end;
get_no_proxy_env([]) ->
false.

parse_no_proxy_env(["*" | _], _Acc) ->
application:set_env(hackney, no_proxy, '*'),
'*';
parse_no_proxy_env([S | Rest], Acc) ->
try
CIDR = hackney_cidr:parse(S),
parse_no_proxy_env(Rest, [{cidr, CIDR} | Acc])
catch
_:_ ->
Labels = string:tokens(S, "."),
parse_no_proxy_env(Rest, [{host, lists:reverse(Labels)}])
end;
parse_no_proxy_env([], Acc) ->
NoProxy = lists:reverse(Acc),
application:set_env(hackney, no_proxy, NoProxy),
NoProxy.

match_no_proxy_env(false, _Host) -> false;
match_no_proxy_env('*', _Host) -> true;
match_no_proxy_env(Patterns, Host) ->
do_match_no_proxy_env(Patterns, undefined, undefined, Host).

do_match_no_proxy_env([{cidr, _CIDR} | _]=Patterns, undefined, Labels, Host) ->
Addrs = case inet:parse_address(Host) of
{ok, Addr} -> [Addr];
_ -> getaddrs(Host)
end,
do_match_no_proxy_env(Patterns, Addrs, Labels, Host);
do_match_no_proxy_env([{cidr, CIDR} | Rest], Addrs, Labels, Host) ->
case test_host_cidr(Addrs, CIDR) of
true -> true;
false -> do_match_no_proxy_env(Rest, Addrs, Labels, Host)
end;
do_match_no_proxy_env([{host, _Labels} | _] = Patterns, Addrs, undefined, Host) ->
HostLabels = string:tokens(Host, "."),
do_match_no_proxy_env(Patterns, Addrs, lists:reverse(HostLabels), Host);
do_match_no_proxy_env([{host, Labels} | Rest], Addrs, HostLabels, Host) ->
case test_host_labels(Labels, HostLabels) of
true -> true;
false -> do_match_no_proxy_env(Rest, Addrs, Labels, Host)
end;
do_match_no_proxy_env([], _, _, _) ->
false.

test_host_labels(["*" | R1], [_ | R2]) -> test_host_labels(R1, R2);
test_host_labels([ A | R1], [A | R2]) -> test_host_labels(R1, R2);
test_host_labels([], _) -> true;
test_host_labels(_, _) -> false.

test_host_cidr([Addr, Rest], CIDR) ->
case hackney_cidr:contains(CIDR, Addr) of
true -> true;
false -> test_host_cidr(Rest, CIDR)
end;
test_host_cidr([], _) ->
false.

getaddrs(Host) ->
IP4Addrs = case inet:getaddrs(Host, inet) of
{ok, Addrs} -> Addrs;
{error, nxdomain} -> []
end,
case inet:getaddrs(Host, inet6) of
{ok, IP6Addrs} -> [IP6Addrs | IP4Addrs];
{error, nxdomain} -> IP4Addrs
end.

get_proxy_env(https) ->
get_proxy_env(?HTTPS_PROXY_ENV_VARS);
case application:get_env(hackney, https_proxy) of
undefined ->
ProxyEnv = do_get_proxy_env(?HTTPS_PROXY_ENV_VARS),
application:set_env(hackney, https_proxy, ProxyEnv),
ProxyEnv;
{ok, Cached} ->
Cached
end;
get_proxy_env(S) when S =:= http; S =:= http_unix ->
get_proxy_env(?HTTP_PROXY_ENV_VARS);
case application:get_env(hackney, http_proxy) of
undefined ->
ProxyEnv = do_get_proxy_env(?HTTP_PROXY_ENV_VARS),
application:set_env(hackney, http_proxy, ProxyEnv),
ProxyEnv;
{ok, Cached} ->
Cached
end.

get_proxy_env([Var | Rest]) ->
do_get_proxy_env([Var | Rest]) ->
case os:getenv(Var) of
false -> get_proxy_env(Rest);
false -> do_get_proxy_env(Rest);
Url -> {ok, Url}
end;
get_proxy_env([]) ->
do_get_proxy_env([]) ->
false.

do_connect(ProxyHost, ProxyPort, undefined, Transport, Host, Port, Options) ->
Expand Down
20 changes: 3 additions & 17 deletions src/hackney_connection.erl
Original file line number Diff line number Diff line change
Expand Up @@ -102,27 +102,13 @@ connect_options(hackney_local_tcp, _Host, ClientOptions) ->
proplists:get_value(connect_options, ClientOptions, []);

connect_options(Transport, Host, ClientOptions) ->
ConnectOpts0 = proplists:get_value(connect_options, ClientOptions, []),

%% handle ipv6
ConnectOpts1 = case lists:member(inet, ConnectOpts0) orelse
lists:member(inet6, ConnectOpts0) of
true ->
ConnectOpts0;
false ->
case hackney_util:is_ipv6(Host) of
true ->
[inet6 | ConnectOpts0];
false ->
ConnectOpts0
end
end,
ConnectOpts = proplists:get_value(connect_options, ClientOptions, []),

case Transport of
hackney_ssl ->
ConnectOpts1 ++ ssl_opts(Host, ClientOptions);
[{ssl_options, ssl_opts(Host, ClientOptions)} | ConnectOpts];
_ ->
ConnectOpts1
ConnectOpts
end.


Expand Down
93 changes: 93 additions & 0 deletions src/hackney_happy.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
-module(hackney_happy).

-export([connect/3, connect/4]).

-include("hackney_internal.hrl").
-include_lib("kernel/include/inet.hrl").

-define(TIMEOUT, 250).
-define(CONNECT_TIMEOUT, 5000).

connect(Hostname, Port, Opts) ->
connect(Hostname, Port, Opts, ?CONNECT_TIMEOUT).

connect(Hostname, Port, Opts, Timeout) ->
do_connect(parse_address(Hostname), Port, Opts, Timeout).

do_connect(Hostname, Port, Opts, Timeout) when is_tuple(Hostname) ->
case hackney_cidr:is_ipv6(Hostname) of
true ->
?report_debug("connect using IPv6", [{hostname, Hostname}, {port, Port}]),
gen_tcp:connect(Hostname, Port, [inet6 | Opts], Timeout);
false ->
case hackney_cidr:is_ipv4(Hostname) of
true ->
?report_debug("connect using IPv4", [{hostname, Hostname}, {port, Port}]),
gen_tcp:connect(Hostname, Port, [inet | Opts], Timeout);
false ->
{error, nxdomain}
end
end;
do_connect(Hostname, Port, Opts, Timeout) ->
?report_debug("happy eyeballs, try to connect using IPv6", [{hostname, Hostname}, {port, Port}]),
Self = self(),
Addrs = getaddrs(Hostname),
Pid = spawn_link( fun() -> try_connect(Addrs, Port, Opts, Self, {error, nxdomain}) end),
MRef = erlang:monitor(process, Pid),
receive
{happy_connect, OK} ->
erlang:demonitor(MRef, [flush]),
OK;
{'DOWN', MRef, _Type, _Pid, Info} ->
{'error', {'connect_error', Info}}
after Timeout ->
erlang:demonitor(MRef, [flush]),
{error, connect_timeout}
end.

-spec parse_address(inet:ip_address() | binary() | string()) -> inet:ip_address() | string().
parse_address(IPTuple) when is_tuple(IPTuple) -> IPTuple;
parse_address(IPBin) when is_binary(IPBin) ->
parse_address(binary_to_list(IPBin));
%% IPv6 string with brackets
parse_address("[" ++ IPString) ->
parse_address(lists:sublist(IPString, length(IPString) - 1));
parse_address(IPString) ->
case inet:parse_address(IPString) of
{ok, IP} -> IP;
{error, _} -> IPString
end.

-spec getaddrs(string()) -> [{inet:ip_address(), 'inet6' | 'inet'}].
getaddrs("localhost") ->
[{{0,0,0,0,0,0,0,1}, 'inet6'}, {{127,0,0,1}, 'inet'}];
getaddrs(Name) ->
IP6Addrs = [{Addr, 'inet6'} || Addr <- getbyname(Name, 'aaaa')],
IP4Addrs = [{Addr, 'inet'} || Addr <- getbyname(Name, 'a')],
IP6Addrs ++ IP4Addrs.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👋 @benoitc

I wonder if it would make sense to "intersperse" the addresses here like [ipv6, ipv4, ipv6, ipv4, etc.]?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


getbyname(Hostname, Type) ->
case (catch inet_res:getbyname(Hostname, Type)) of
{'ok', #hostent{h_addr_list=AddrList}} -> lists:usort(AddrList);
{error, _Reason} -> [];
Else ->
%% ERLANG 22 has an issue when g matching somee DNS server messages
?report_debug("DNS error", [{hostname, Hostname}
,{type, Type}
,{error, Else}]),
[]
end.

try_connect([], _Port, _Opts, ServerPid, LastError) ->
?report_trace("happy eyeball: failed to connect", [{error, LastError}]),
ServerPid ! {hackney_happy, LastError};
try_connect([{IP, Type} | Rest], Port, Opts, ServerPid, _LastError) ->
?report_trace("try to connect", [{ip, IP}, {type, Type}]),
case gen_tcp:connect(IP, Port, [Type | Opts], ?TIMEOUT) of
{ok, Socket} = OK ->
?report_trace("success to connect", [{ip, IP}, {type, Type}]),
ok = gen_tcp:controlling_process(Socket, ServerPid),
ServerPid ! {happy_connect, OK};
Error ->
try_connect(Rest, Port, Opts, ServerPid, Error)
end.
2 changes: 1 addition & 1 deletion src/hackney_http_connect.erl
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ connect(ProxyHost, ProxyPort, Opts, Timeout)
ConnectOpts = hackney_util:filter_options(Opts, AcceptedOpts, BaseOpts),

%% connect to the proxy, and upgrade the socket if needed.
case gen_tcp:connect(ProxyHost, ProxyPort, ConnectOpts) of
case hackney_happy:connect(ProxyHost, ProxyPort, ConnectOpts) of
{ok, Socket} ->
case do_handshake(Socket, Host, Port, Opts) of
ok ->
Expand Down
8 changes: 4 additions & 4 deletions src/hackney_pool.erl
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,13 @@ checkout(Host, Port, Transport, Client) ->
Requester = self(),
try
do_checkout(Requester, Host, Port, Transport, Client)
catch _:_ ->
catch _:Error ->
?report_trace("pool: checkout failure", [{error, Error}]),
{error, checkout_failure}
end.

do_checkout(Requester, Host, _Port, Transport, #client{options=Opts,
mod_metrics=Metrics}=Client) ->
mod_metrics=Metrics}=Client) ->
ConnectTimeout = proplists:get_value(connect_timeout, Opts, 8000),
%% Fall back to using connect_timeout if checkout_timeout is not set
CheckoutTimeout = proplists:get_value(checkout_timeout, Opts, ConnectTimeout),
Expand All @@ -78,7 +79,6 @@ do_checkout(Requester, Host, _Port, Transport, #client{options=Opts,
Pool = find_pool(PoolName, Opts),
case catch gen_server:call(Pool, {checkout, Connection, Requester, RequestRef}, CheckoutTimeout) of
{ok, Socket, Owner} ->

%% stats
?report_debug("reuse a connection", [{pool, PoolName}]),
_ = metrics:update_meter(Metrics, [hackney_pool, PoolName, take_rate], 1),
Expand All @@ -105,7 +105,7 @@ do_checkout(Requester, Host, _Port, Transport, #client{options=Opts,
_ = metrics:increment_counter(Metrics, [hackney, Host, connect_timeout]),
{error, timeout};
Error ->
?report_trace("connect error", []),
?report_trace("connect error", [{pool, PoolName}, {error, Error}]),
_ = metrics:increment_counter(Metrics, [hackney, Host, connect_error]),
Error
end;
Expand Down
2 changes: 1 addition & 1 deletion src/hackney_socks5.erl
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ connect(Host, Port, Opts, Timeout) when is_list(Host), is_integer(Port),
ConnectOpts = hackney_util:filter_options(Opts, AcceptedOpts, BaseOpts),

%% connect to the socks 5 proxy
case gen_tcp:connect(ProxyHost, ProxyPort, ConnectOpts, Timeout) of
case hackney_happy:connect(ProxyHost, ProxyPort, ConnectOpts, Timeout) of
{ok, Socket} ->
case do_handshake(Socket, Host, Port, Opts) of
ok ->
Expand Down
Loading