feat(ipv6 probe): automatically probe whether it is IPv6

This commit is contained in:
zhouzb 2021-04-25 10:27:34 +08:00 committed by Zaiming (Stone) Shi
parent b688bcfe74
commit d9c7c72612
3 changed files with 9 additions and 45 deletions

View File

@ -53,15 +53,14 @@ translate_env(EnvName) ->
{ok, PoolSize} = application:get_env(?APP, pool_size),
{ok, ConnectTimeout} = application:get_env(?APP, connect_timeout),
URL = proplists:get_value(url, Req),
{ok, #{host := Host0,
{ok, #{host := Host,
path := Path0,
port := Port,
scheme := Scheme}} = emqx_http_lib:uri_parse(URL),
Path = path(Path0),
{Inet, Host} = parse_host(Host0),
MoreOpts = case Scheme of
http ->
[{transport_opts, [Inet]}];
[{transport_opts, [ipv6_probe]}];
https ->
CACertFile = application:get_env(?APP, cacertfile, undefined),
CertFile = application:get_env(?APP, certfile, undefined),
@ -86,7 +85,7 @@ translate_env(EnvName) ->
, {ciphers, emqx_tls_lib:default_ciphers()}
| TLSOpts
],
[{transport, ssl}, {transport_opts, [Inet | NTLSOpts]}]
[{transport, ssl}, {transport_opts, [ipv6_probe | NTLSOpts]}]
end,
PoolOpts = [{host, Host},
{port, Port},
@ -146,17 +145,6 @@ unload_hooks() ->
_ = ehttpc_sup:stop_pool('emqx_auth_http/acl_req'),
ok.
parse_host(Host) ->
case inet:parse_address(Host) of
{ok, Addr} when size(Addr) =:= 4 -> {inet, Addr};
{ok, Addr} when size(Addr) =:= 8 -> {inet6, Addr};
{error, einval} ->
case inet:getaddr(Host, inet6) of
{ok, _} -> {inet6, Host};
{error, _} -> {inet, Host}
end
end.
to_lower(Headers) ->
[{string:to_lower(K), V} || {K, V} <- Headers].

View File

@ -325,16 +325,15 @@ str(Atom) when is_atom(Atom) -> atom_to_list(Atom);
str(Bin) when is_binary(Bin) -> binary_to_list(Bin).
pool_opts(Params = #{<<"url">> := URL}, ResId) ->
{ok, #{host := Host0,
{ok, #{host := Host,
port := Port,
scheme := Scheme}} = emqx_http_lib:uri_parse(URL),
PoolSize = maps:get(<<"pool_size">>, Params, 32),
ConnectTimeout =
cuttlefish_duration:parse(str(maps:get(<<"connect_timeout">>, Params, <<"5s">>))),
{Inet, Host} = parse_host(Host0),
TransportOpts = case Scheme =:= https of
true -> [Inet | get_ssl_opts(Params, ResId)];
false -> [Inet]
true -> [ipv6_probe | get_ssl_opts(Params, ResId)];
false -> [ipv6_probe]
end,
Opts = case Scheme =:= https of
true -> [{transport_opts, TransportOpts}, {transport, ssl}];
@ -354,17 +353,6 @@ pool_name(ResId) ->
get_ssl_opts(Opts, ResId) ->
[{ssl, true}, {ssl_opts, emqx_plugin_libs_ssl:save_files_return_opts(Opts, "rules", ResId)}].
parse_host(Host) ->
case inet:parse_address(Host) of
{ok, Addr} when size(Addr) =:= 4 -> {inet, Addr};
{ok, Addr} when size(Addr) =:= 8 -> {inet6, Addr};
{error, einval} ->
case inet:getaddr(Host, inet6) of
{ok, _} -> {inet6, Host};
{error, _} -> {inet, Host}
end
end.
test_http_connect(Conf) ->
Url = fun() -> maps:get(<<"url">>, Conf) end,
try

View File

@ -41,16 +41,15 @@ stop(_State) ->
translate_env() ->
{ok, URL} = application:get_env(?APP, url),
{ok, #{host := Host0,
{ok, #{host := Host,
path := Path0,
port := Port,
scheme := Scheme}} = emqx_http_lib:uri_parse(URL),
Path = path(Path0),
{Inet, Host} = parse_host(Host0),
PoolSize = application:get_env(?APP, pool_size, 32),
MoreOpts = case Scheme of
http ->
[{transport_opts, [Inet]}];
[{transport_opts, [ipv6_probe]}];
https ->
CACertFile = application:get_env(?APP, cacertfile, undefined),
CertFile = application:get_env(?APP, certfile, undefined),
@ -75,7 +74,7 @@ translate_env() ->
, {ciphers, emqx_tls_lib:default_ciphers()}
| TLSOpts
],
[{transport, ssl}, {transport_opts, [Inet | NTLSOpts]}]
[{transport, ssl}, {transport_opts, [ipv6_probe | NTLSOpts]}]
end,
PoolOpts = [{host, Host},
{port, Port},
@ -98,14 +97,3 @@ path(Path) ->
set_content_type(Headers) ->
NHeaders = proplists:delete(<<"Content-Type">>, proplists:delete(<<"content-type">>, Headers)),
[{<<"content-type">>, <<"application/json">>} | NHeaders].
parse_host(Host) ->
case inet:parse_address(Host) of
{ok, Addr} when size(Addr) =:= 4 -> {inet, Addr};
{ok, Addr} when size(Addr) =:= 8 -> {inet6, Addr};
{error, einval} ->
case inet:getaddr(Host, inet6) of
{ok, _} -> {inet6, Host};
{error, _} -> {inet, Host}
end
end.