feat(ipv6 probe): automatically probe whether it is IPv6
This commit is contained in:
parent
b688bcfe74
commit
d9c7c72612
|
@ -53,15 +53,14 @@ translate_env(EnvName) ->
|
||||||
{ok, PoolSize} = application:get_env(?APP, pool_size),
|
{ok, PoolSize} = application:get_env(?APP, pool_size),
|
||||||
{ok, ConnectTimeout} = application:get_env(?APP, connect_timeout),
|
{ok, ConnectTimeout} = application:get_env(?APP, connect_timeout),
|
||||||
URL = proplists:get_value(url, Req),
|
URL = proplists:get_value(url, Req),
|
||||||
{ok, #{host := Host0,
|
{ok, #{host := Host,
|
||||||
path := Path0,
|
path := Path0,
|
||||||
port := Port,
|
port := Port,
|
||||||
scheme := Scheme}} = emqx_http_lib:uri_parse(URL),
|
scheme := Scheme}} = emqx_http_lib:uri_parse(URL),
|
||||||
Path = path(Path0),
|
Path = path(Path0),
|
||||||
{Inet, Host} = parse_host(Host0),
|
|
||||||
MoreOpts = case Scheme of
|
MoreOpts = case Scheme of
|
||||||
http ->
|
http ->
|
||||||
[{transport_opts, [Inet]}];
|
[{transport_opts, [ipv6_probe]}];
|
||||||
https ->
|
https ->
|
||||||
CACertFile = application:get_env(?APP, cacertfile, undefined),
|
CACertFile = application:get_env(?APP, cacertfile, undefined),
|
||||||
CertFile = application:get_env(?APP, certfile, undefined),
|
CertFile = application:get_env(?APP, certfile, undefined),
|
||||||
|
@ -86,7 +85,7 @@ translate_env(EnvName) ->
|
||||||
, {ciphers, emqx_tls_lib:default_ciphers()}
|
, {ciphers, emqx_tls_lib:default_ciphers()}
|
||||||
| TLSOpts
|
| TLSOpts
|
||||||
],
|
],
|
||||||
[{transport, ssl}, {transport_opts, [Inet | NTLSOpts]}]
|
[{transport, ssl}, {transport_opts, [ipv6_probe | NTLSOpts]}]
|
||||||
end,
|
end,
|
||||||
PoolOpts = [{host, Host},
|
PoolOpts = [{host, Host},
|
||||||
{port, Port},
|
{port, Port},
|
||||||
|
@ -146,17 +145,6 @@ unload_hooks() ->
|
||||||
_ = ehttpc_sup:stop_pool('emqx_auth_http/acl_req'),
|
_ = ehttpc_sup:stop_pool('emqx_auth_http/acl_req'),
|
||||||
ok.
|
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) ->
|
to_lower(Headers) ->
|
||||||
[{string:to_lower(K), V} || {K, V} <- Headers].
|
[{string:to_lower(K), V} || {K, V} <- Headers].
|
||||||
|
|
||||||
|
|
|
@ -325,16 +325,15 @@ str(Atom) when is_atom(Atom) -> atom_to_list(Atom);
|
||||||
str(Bin) when is_binary(Bin) -> binary_to_list(Bin).
|
str(Bin) when is_binary(Bin) -> binary_to_list(Bin).
|
||||||
|
|
||||||
pool_opts(Params = #{<<"url">> := URL}, ResId) ->
|
pool_opts(Params = #{<<"url">> := URL}, ResId) ->
|
||||||
{ok, #{host := Host0,
|
{ok, #{host := Host,
|
||||||
port := Port,
|
port := Port,
|
||||||
scheme := Scheme}} = emqx_http_lib:uri_parse(URL),
|
scheme := Scheme}} = emqx_http_lib:uri_parse(URL),
|
||||||
PoolSize = maps:get(<<"pool_size">>, Params, 32),
|
PoolSize = maps:get(<<"pool_size">>, Params, 32),
|
||||||
ConnectTimeout =
|
ConnectTimeout =
|
||||||
cuttlefish_duration:parse(str(maps:get(<<"connect_timeout">>, Params, <<"5s">>))),
|
cuttlefish_duration:parse(str(maps:get(<<"connect_timeout">>, Params, <<"5s">>))),
|
||||||
{Inet, Host} = parse_host(Host0),
|
|
||||||
TransportOpts = case Scheme =:= https of
|
TransportOpts = case Scheme =:= https of
|
||||||
true -> [Inet | get_ssl_opts(Params, ResId)];
|
true -> [ipv6_probe | get_ssl_opts(Params, ResId)];
|
||||||
false -> [Inet]
|
false -> [ipv6_probe]
|
||||||
end,
|
end,
|
||||||
Opts = case Scheme =:= https of
|
Opts = case Scheme =:= https of
|
||||||
true -> [{transport_opts, TransportOpts}, {transport, ssl}];
|
true -> [{transport_opts, TransportOpts}, {transport, ssl}];
|
||||||
|
@ -354,17 +353,6 @@ pool_name(ResId) ->
|
||||||
get_ssl_opts(Opts, ResId) ->
|
get_ssl_opts(Opts, ResId) ->
|
||||||
[{ssl, true}, {ssl_opts, emqx_plugin_libs_ssl:save_files_return_opts(Opts, "rules", 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) ->
|
test_http_connect(Conf) ->
|
||||||
Url = fun() -> maps:get(<<"url">>, Conf) end,
|
Url = fun() -> maps:get(<<"url">>, Conf) end,
|
||||||
try
|
try
|
||||||
|
|
|
@ -41,16 +41,15 @@ stop(_State) ->
|
||||||
|
|
||||||
translate_env() ->
|
translate_env() ->
|
||||||
{ok, URL} = application:get_env(?APP, url),
|
{ok, URL} = application:get_env(?APP, url),
|
||||||
{ok, #{host := Host0,
|
{ok, #{host := Host,
|
||||||
path := Path0,
|
path := Path0,
|
||||||
port := Port,
|
port := Port,
|
||||||
scheme := Scheme}} = emqx_http_lib:uri_parse(URL),
|
scheme := Scheme}} = emqx_http_lib:uri_parse(URL),
|
||||||
Path = path(Path0),
|
Path = path(Path0),
|
||||||
{Inet, Host} = parse_host(Host0),
|
|
||||||
PoolSize = application:get_env(?APP, pool_size, 32),
|
PoolSize = application:get_env(?APP, pool_size, 32),
|
||||||
MoreOpts = case Scheme of
|
MoreOpts = case Scheme of
|
||||||
http ->
|
http ->
|
||||||
[{transport_opts, [Inet]}];
|
[{transport_opts, [ipv6_probe]}];
|
||||||
https ->
|
https ->
|
||||||
CACertFile = application:get_env(?APP, cacertfile, undefined),
|
CACertFile = application:get_env(?APP, cacertfile, undefined),
|
||||||
CertFile = application:get_env(?APP, certfile, undefined),
|
CertFile = application:get_env(?APP, certfile, undefined),
|
||||||
|
@ -75,7 +74,7 @@ translate_env() ->
|
||||||
, {ciphers, emqx_tls_lib:default_ciphers()}
|
, {ciphers, emqx_tls_lib:default_ciphers()}
|
||||||
| TLSOpts
|
| TLSOpts
|
||||||
],
|
],
|
||||||
[{transport, ssl}, {transport_opts, [Inet | NTLSOpts]}]
|
[{transport, ssl}, {transport_opts, [ipv6_probe | NTLSOpts]}]
|
||||||
end,
|
end,
|
||||||
PoolOpts = [{host, Host},
|
PoolOpts = [{host, Host},
|
||||||
{port, Port},
|
{port, Port},
|
||||||
|
@ -98,14 +97,3 @@ path(Path) ->
|
||||||
set_content_type(Headers) ->
|
set_content_type(Headers) ->
|
||||||
NHeaders = proplists:delete(<<"Content-Type">>, proplists:delete(<<"content-type">>, Headers)),
|
NHeaders = proplists:delete(<<"Content-Type">>, proplists:delete(<<"content-type">>, Headers)),
|
||||||
[{<<"content-type">>, <<"application/json">>} | NHeaders].
|
[{<<"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.
|
|
||||||
|
|
Loading…
Reference in New Issue