Skip to content

Commit f5facd8

Browse files
committed
extend Conduit_lwt_unix.endp with TLS tunnel
1 parent 7c00878 commit f5facd8

File tree

2 files changed

+27
-17
lines changed

2 files changed

+27
-17
lines changed

src/conduit-lwt-unix/conduit_lwt_unix.ml

+10-6
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ type vchan_flow = { domid : int; port : string } [@@deriving sexp]
141141

142142
type flow =
143143
| TCP of tcp_flow
144-
| Tunnel
144+
| Tunnel of string * ic * oc
145145
| Domain_socket of domain_flow
146146
| Vchan of vchan_flow
147147
[@@deriving sexp]
@@ -294,10 +294,10 @@ let connect_with_tls_native ~ctx (`Hostname hostname, `IP ip, `Port port) =
294294

295295
let connect_with_tls_tunnel ~ctx (`Hostname hostname, ic, oc) =
296296
certificates ~ctx >>= fun certificates ->
297-
let hostname = domain_name hostname in
297+
let host = domain_name hostname in
298298
Conduit_lwt_tls.Client.tunnel ?certificates
299-
~authenticator:ctx.tls_authenticator hostname (ic, oc)
300-
>|= fun (ic, oc) -> (Tunnel, ic, oc)
299+
~authenticator:ctx.tls_authenticator host (ic, oc)
300+
>|= fun (ic', oc') -> (Tunnel (hostname, ic, oc), ic', oc')
301301

302302
let connect_with_openssl ~ctx (`Hostname host_addr, `IP ip, `Port port) =
303303
let sa = Unix.ADDR_INET (Ipaddr_unix.to_inet_addr ip, port) in
@@ -427,15 +427,17 @@ let serve ?backlog ?timeout ?stop ~on_exn ~(ctx : ctx) ~(mode : server) callback
427427
let fn s = Sockaddr_server.init ~on:(`Socket s) ?timeout ?stop callback in
428428
Conduit_lwt_launchd.activate fn name
429429

430+
type endp = [ Conduit.endp | `TLS_tunnel of string * ic * oc ] [@@deriving sexp]
431+
430432
let endp_of_flow = function
431433
| TCP { ip; port; _ } -> `TCP (ip, port)
432-
| Tunnel -> `Unknown "TLS tunnel"
434+
| Tunnel (hostname, ic, oc) -> `TLS_tunnel (hostname, ic, oc)
433435
| Domain_socket { path; _ } -> `Unix_domain_socket path
434436
| Vchan { domid; port } -> `Vchan_direct (domid, port)
435437

436438
(** Use the configuration of the server to interpret how to handle a particular
437439
endpoint from the resolver into a concrete implementation of type [client] *)
438-
let endp_to_client ~ctx:_ (endp : Conduit.endp) : client Lwt.t =
440+
let endp_to_client ~ctx:_ (endp : [< endp ]) : client Lwt.t =
439441
match endp with
440442
| `TCP (ip, port) -> Lwt.return (`TCP (`IP ip, `Port port))
441443
| `Unix_domain_socket file -> Lwt.return (`Unix_domain_socket (`File file))
@@ -449,6 +451,8 @@ let endp_to_client ~ctx:_ (endp : Conduit.endp) : client Lwt.t =
449451
Printf.ksprintf failwith
450452
"TLS to non-TCP currently unsupported: host=%s endp=%s" host
451453
(Sexplib0.Sexp.to_string_hum (Conduit.sexp_of_endp endp))
454+
| `TLS_tunnel (host, ic, oc) ->
455+
Lwt.return (`TLS_tunnel (`Hostname host, ic, oc))
452456
| `Unknown err -> failwith ("resolution failed: " ^ err)
453457

454458
let endp_to_server ~ctx (endp : Conduit.endp) =

src/conduit-lwt-unix/conduit_lwt_unix.mli

+17-11
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,13 @@ type client_tls_config =
2626
[@@deriving sexp]
2727
(** Configuration fragment for a TLS client connecting to a remote endpoint *)
2828

29+
type 'a io = 'a Lwt.t
30+
type ic = (Lwt_io.input_channel[@sexp.opaque]) [@@deriving sexp]
31+
type oc = (Lwt_io.output_channel[@sexp.opaque]) [@@deriving sexp]
32+
2933
type client =
3034
[ `TLS of client_tls_config
31-
| `TLS_tunnel of
32-
[ `Hostname of string ] * Lwt_io.input_channel * Lwt_io.output_channel
35+
| `TLS_tunnel of [ `Hostname of string ] * ic * oc
3336
| `TLS_native of client_tls_config
3437
(** Force use of native OCaml TLS stack to connect.*)
3538
| `OpenSSL of client_tls_config
@@ -105,10 +108,6 @@ type server =
105108
the {{:http://mirage.github.io/ocaml-launchd/launchd/} ocaml-launchd}
106109
documentation for more. *)
107110

108-
type 'a io = 'a Lwt.t
109-
type ic = (Lwt_io.input_channel[@sexp.opaque]) [@@deriving sexp]
110-
type oc = (Lwt_io.output_channel[@sexp.opaque]) [@@deriving sexp]
111-
112111
type tcp_flow = private {
113112
fd : Lwt_unix.file_descr; [@sexp.opaque]
114113
ip : Ipaddr.t;
@@ -131,7 +130,7 @@ type vchan_flow = private { domid : int; port : string } [@@deriving sexp_of]
131130
transport method. *)
132131
type flow = private
133132
| TCP of tcp_flow
134-
| Tunnel
133+
| Tunnel of string * ic * oc
135134
| Domain_socket of domain_flow
136135
| Vchan of vchan_flow
137136
[@@deriving sexp_of]
@@ -207,11 +206,18 @@ val set_max_active : int -> unit
207206
accepted. When the limit is hit accept blocks until another server
208207
connection is closed. *)
209208

210-
val endp_of_flow : flow -> Conduit.endp
211-
(** [endp_of_flow flow] retrieves the original {!Conduit.endp} from the
212-
established [flow] *)
209+
type endp =
210+
[ Conduit.endp
211+
| `TLS_tunnel of string * ic * oc
212+
(** Wrap in a TLS channel over an existing [Lwt_io.channel] connection,
213+
[hostname,input_channel,output_channel] *) ]
214+
[@@deriving sexp]
215+
216+
val endp_of_flow : flow -> endp
217+
(** [endp_of_flow flow] retrieves the original {!endp} from the established
218+
[flow] *)
213219

214-
val endp_to_client : ctx:ctx -> Conduit.endp -> client io
220+
val endp_to_client : ctx:ctx -> [< endp ] -> client io
215221
(** [endp_to_client ~ctx endp] converts an [endp] into a a concrete connection
216222
mechanism of type [client] *)
217223

0 commit comments

Comments
 (0)