diff options
author | Loïc Hoguin <[email protected]> | 2019-04-19 13:09:50 +0200 |
---|---|---|
committer | Loïc Hoguin <[email protected]> | 2019-04-19 13:09:50 +0200 |
commit | eecb5e76eca90ebbd97f24e79282444fa27bf508 (patch) | |
tree | cbcbe5ef7d156912621546d14d0375a660e8dcdf /src | |
parent | 6d0ea34ebe7cac66e3f25a018883c104c7fc31b6 (diff) | |
download | gun-eecb5e76eca90ebbd97f24e79282444fa27bf508.tar.gz gun-eecb5e76eca90ebbd97f24e79282444fa27bf508.tar.bz2 gun-eecb5e76eca90ebbd97f24e79282444fa27bf508.zip |
Integrate gun_tls_proxy into gun proper
Still need to add ALPN support and to wait before trying to send
data on a proxied TLS connection that didn't complete its handshake.
Diffstat (limited to 'src')
-rw-r--r-- | src/gun.erl | 29 | ||||
-rw-r--r-- | src/gun_http.erl | 15 | ||||
-rw-r--r-- | src/gun_tls_proxy.erl | 36 |
3 files changed, 60 insertions, 20 deletions
diff --git a/src/gun.erl b/src/gun.erl index 1fd7e37..2f023e1 100644 --- a/src/gun.erl +++ b/src/gun.erl @@ -173,12 +173,13 @@ owner_ref :: reference(), host :: inet:hostname() | inet:ip_address(), port :: inet:port_number(), + origin_scheme :: binary(), origin_host :: inet:hostname() | inet:ip_address(), origin_port :: inet:port_number(), intermediaries = [] :: [intermediary()], opts :: opts(), keepalive_ref :: undefined | reference(), - socket :: undefined | inet:socket() | ssl:sslsocket(), + socket :: undefined | inet:socket() | ssl:sslsocket() | pid(), transport :: module(), messages :: {atom(), atom(), atom()}, protocol :: module(), @@ -292,6 +293,7 @@ info(ServerPid) -> socket=Socket, transport=Transport, protocol=Protocol, + origin_scheme=OriginScheme, origin_host=OriginHost, origin_port=OriginPort, intermediaries=Intermediaries @@ -299,10 +301,15 @@ info(ServerPid) -> {ok, {SockIP, SockPort}} = Transport:sockname(Socket), #{ socket => Socket, - transport => Transport:name(), + transport => case OriginScheme of + <<"http">> -> tcp; + <<"https">> -> tls + end, protocol => Protocol:name(), sock_ip => SockIP, sock_port => SockPort, + %% @todo Add origin_scheme to documentation/tests. + origin_scheme => OriginScheme, origin_host => OriginHost, origin_port => OriginPort, %% Intermediaries are listed in the order data goes through them. @@ -685,14 +692,16 @@ start_link(Owner, Host, Port, Opts) -> init({Owner, Host, Port, Opts}) -> Retry = maps:get(retry, Opts, 5), - Transport = case maps:get(transport, Opts, default_transport(Port)) of - tcp -> gun_tcp; - tls -> gun_tls + OriginTransport = maps:get(transport, Opts, default_transport(Port)), + {OriginScheme, Transport} = case OriginTransport of + tcp -> {<<"http">>, gun_tcp}; + tls -> {<<"https">>, gun_tls} end, OwnerRef = monitor(process, Owner), State = #state{owner=Owner, owner_ref=OwnerRef, - host=Host, port=Port, origin_host=Host, origin_port=Port, - opts=Opts, transport=Transport, messages=Transport:messages()}, + host=Host, port=Port, origin_scheme=OriginScheme, + origin_host=Host, origin_port=Port, opts=Opts, + transport=Transport, messages=Transport:messages()}, {ok, not_connected, State, {next_event, internal, {retries, Retry}}}. @@ -901,7 +910,7 @@ commands([{state, ProtoState}|Tail], State) -> %% Order is important: the origin must be changed before %% the transport and/or protocol in order to keep track %% of the intermediaries properly. -commands([{origin, _Scheme, Host, Port, Type}|Tail], +commands([{origin, Scheme, Host, Port, Type}|Tail], State=#state{transport=Transport, protocol=Protocol, origin_host=IntermediateHost, origin_port=IntermediatePort, intermediaries=Intermediaries}) -> @@ -912,8 +921,8 @@ commands([{origin, _Scheme, Host, Port, Type}|Tail], transport => Transport:name(), protocol => Protocol:name() }, - commands(Tail, State#state{origin_host=Host, origin_port=Port, - intermediaries=[Info|Intermediaries]}); + commands(Tail, State#state{origin_scheme=Scheme, + origin_host=Host, origin_port=Port, intermediaries=[Info|Intermediaries]}); commands([{switch_transport, Transport, Socket}|Tail], State) -> commands(Tail, active(State#state{socket=Socket, transport=Transport, messages=Transport:messages()})); diff --git a/src/gun_http.erl b/src/gun_http.erl index 719307c..efcea35 100644 --- a/src/gun_http.erl +++ b/src/gun_http.erl @@ -202,8 +202,8 @@ handle(Data, State=#http_state{in={body, Length}, connection=Conn}) -> end end. -handle_head(Data, State=#http_state{socket=Socket, version=ClientVersion, - content_handlers=Handlers0, connection=Conn, +handle_head(Data, State=#http_state{socket=Socket, transport=Transport, + version=ClientVersion, content_handlers=Handlers0, connection=Conn, streams=[Stream=#stream{ref=StreamRef, reply_to=ReplyTo, method=Method, is_alive=IsAlive}|Tail]}) -> {Version, Status, _, Rest} = cow_http:parse_status_line(Data), @@ -226,6 +226,17 @@ handle_head(Data, State=#http_state{socket=Socket, version=ClientVersion, NewHost = maps:get(host, Destination), NewPort = maps:get(port, Destination), case Destination of + #{transport := tls} when Transport =:= gun_tls -> + TLSOpts = maps:get(tls_opts, Destination, []), + TLSTimeout = maps:get(tls_handshake_timeout, Destination, infinity), + {ok, ProxyPid} = gun_tls_proxy:start_link(NewHost, NewPort, + TLSOpts, TLSTimeout, Socket, gun_tls), + [{state, State2#http_state{socket=ProxyPid, transport=gun_tls_proxy}}, + {origin, <<"https">>, NewHost, NewPort, connect}, + {switch_transport, gun_tls_proxy, ProxyPid}]; + %% @todo Might also need to switch protocol, but gotta wait + %% @todo for the TLS connection to be established first. + %% @todo Should have a gun_tls_proxy event indicating connection success. #{transport := tls} -> TLSOpts = maps:get(tls_opts, Destination, []), TLSTimeout = maps:get(tls_handshake_timeout, Destination, infinity), diff --git a/src/gun_tls_proxy.erl b/src/gun_tls_proxy.erl index 591411b..23737ab 100644 --- a/src/gun_tls_proxy.erl +++ b/src/gun_tls_proxy.erl @@ -102,9 +102,19 @@ start_link(Host, Port, Opts, Timeout, OutSocket, OutTransport) -> ?DEBUG_LOG("host ~0p port ~0p opts ~0p timeout ~0p out_socket ~0p out_transport ~0p", [Host, Port, Opts, Timeout, OutSocket, OutTransport]), - gen_server:start_link(?MODULE, - {self(), Host, Port, Opts, Timeout, OutSocket, OutTransport}, - []). + + case gen_server:start_link(?MODULE, + {self(), Host, Port, Opts, Timeout, OutSocket, OutTransport}, + []) of + {ok, Pid} when is_port(OutSocket) -> + gen_tcp:controlling_process(OutSocket, Pid), + {ok, Pid}; + {ok, Pid} when not is_pid(OutSocket) -> + ssl:controlling_process(OutSocket, Pid), + {ok, Pid}; + Other -> + Other + end. %% gun_tls_proxy_cb interface. @@ -114,11 +124,21 @@ cb_controlling_process(Pid, ControllingPid) -> cb_send(Pid, Data) -> ?DEBUG_LOG("pid ~0p data ~0p", [Pid, Data]), - gen_server:call(Pid, {?FUNCTION_NAME, Data}). + try + gen_server:call(Pid, {?FUNCTION_NAME, Data}) + catch + exit:{noproc, _} -> + {error, closed} + end. cb_setopts(Pid, Opts) -> ?DEBUG_LOG("pid ~0p opts ~0p", [Pid, Opts]), - gen_server:call(Pid, {?FUNCTION_NAME, Opts}). + try + gen_server:call(Pid, {?FUNCTION_NAME, Opts}) + catch + exit:{noproc, _} -> + {error, einval} + end. %% Transport. @@ -156,6 +176,7 @@ close(Pid) -> gen_server:call(Pid, ?FUNCTION_NAME). %% gen_server. +%% @todo Probably need to gen_statem it to avoid trying to send stuff before being connected. init({OwnerPid, Host, Port, Opts, Timeout, OutSocket, OutTransport}) -> if @@ -244,6 +265,8 @@ handle_cast(Msg={connect_proc, Error}, State) -> {stop, Error, State}; handle_cast(Msg={cb_controlling_process, ProxyPid}, State) -> ?DEBUG_LOG("msg ~0p state ~0p", [Msg, State]), + %% We link so that the ssl process terminates when we do. + link(ProxyPid), {noreply, State#state{proxy_pid=ProxyPid}}; handle_cast(Msg={setopts, Opts}, State) -> ?DEBUG_LOG("msg ~0p state ~0p", [Msg, State]), @@ -342,7 +365,6 @@ tcp_test() -> ssl:start(), {ok, Socket} = gen_tcp:connect("google.com", 443, [binary, {active, false}]), {ok, ProxyPid1} = start_link("google.com", 443, [], 5000, Socket, gen_tcp), - gen_tcp:controlling_process(Socket, ProxyPid1), timer:sleep(500), send(ProxyPid1, <<"GET / HTTP/1.1\r\nHost: google.com\r\n\r\n">>), timer:sleep(1000), @@ -355,7 +377,6 @@ ssl_test() -> {ok, Socket} = ssl:connect("localhost", Port, [binary, {active, false}]), timer:sleep(500), {ok, ProxyPid1} = start_link("google.com", 443, [], 5000, Socket, ssl), - ssl:controlling_process(Socket, ProxyPid1), timer:sleep(500), send(ProxyPid1, <<"GET / HTTP/1.1\r\nHost: google.com\r\n\r\n">>), timer:sleep(1000), @@ -369,7 +390,6 @@ ssl2_test() -> {ok, Socket} = ssl:connect("localhost", Port2, [binary, {active, false}]), timer:sleep(500), {ok, ProxyPid1} = start_link("localhost", Port1, [], 5000, Socket, ssl), - ssl:controlling_process(Socket, ProxyPid1), timer:sleep(500), {ok, ProxyPid2} = start_link("google.com", 443, [], 5000, ProxyPid1, ?MODULE), timer:sleep(500), |