@@ -178,32 +178,6 @@ let test_interception () =
178
178
Lwt. return ()
179
179
end
180
180
181
- (* Test that port 3128 passes through to an upstream proxy *)
182
- let test_proxy_passthrough () =
183
- Host.Main. run begin
184
- let request =
185
- Cohttp.Request. make
186
- (Uri. make ~scheme: " http" ~host: " dave.recoil.org" ~path: " /" () )
187
- in
188
- intercept ~pcap: " test_proxy_passthrough.pcap" ~port: 3128 request >> = fun result ->
189
- Log. info (fun f ->
190
- f " original was: %s"
191
- (Sexplib.Sexp. to_string_hum (Cohttp.Request. sexp_of_t request)));
192
- Log. info (fun f ->
193
- f " proxied was: %s"
194
- (Sexplib.Sexp. to_string_hum (Cohttp.Request. sexp_of_t result)));
195
- Alcotest. check Alcotest. string " resource"
196
- request.Cohttp.Request. resource
197
- result.Cohttp.Request. resource;
198
- Alcotest. check Alcotest. string " method"
199
- (Cohttp.Code. string_of_method request.Cohttp.Request. meth)
200
- (Cohttp.Code. string_of_method result.Cohttp.Request. meth);
201
- Alcotest. check Alcotest. string " version"
202
- (Cohttp.Code. string_of_version request.Cohttp.Request. version)
203
- (Cohttp.Code. string_of_version result.Cohttp.Request. version);
204
- Lwt. return ()
205
- end
206
-
207
181
(* Test that a relative URI becomes absolute *)
208
182
let test_uri_relative () =
209
183
Host.Main. run begin
@@ -293,6 +267,72 @@ let test_user_agent_preserved () =
293
267
294
268
let err_flush e = Fmt. kstrf failwith " %a" Incoming.C. pp_write_error e
295
269
270
+ let test_proxy_passthrough () =
271
+ let forwarded, forwarded_u = Lwt. task () in
272
+ Host.Main. run begin
273
+ Slirp_stack. with_stack ~pcap: " test_proxy_passthrough.pcap" (fun _ stack ->
274
+ with_server (fun flow ->
275
+ let ic = Incoming.C. create flow in
276
+ (* read something *)
277
+ Incoming.C. read_some ~len: 5 ic
278
+ >> = function
279
+ | Ok `Eof -> failwith " test_proxy_passthrough: read_some returned Eof"
280
+ | Error _ -> failwith " test_proxy_passthrough: read_some returned Error"
281
+ | Ok (`Data buf ) ->
282
+ let txt = Cstruct. to_string buf in
283
+ Alcotest. check Alcotest. string " message" " hello" txt;
284
+ let response = " there" in
285
+ (* write something *)
286
+ Incoming.C. write_string ic response 0 (String. length response);
287
+ Incoming.C. flush ic
288
+ >> = function
289
+ | Error _ -> failwith " test_proxy_passthrough: flush returned error"
290
+ | Ok () ->
291
+ Lwt. wakeup_later forwarded_u () ;
292
+ Lwt. return_unit
293
+ ) (fun server ->
294
+ let json =
295
+ Ezjsonm. from_string (" { \" http\" : \" 127.0.0.1:" ^
296
+ (string_of_int server.Server. port) ^ " \" }" )
297
+ in
298
+ Slirp_stack.Slirp_stack.Debug. update_http_json json ()
299
+ >> = function
300
+ | Error (`Msg m ) -> failwith (" Failed to enable HTTP proxy: " ^ m)
301
+ | Ok () ->
302
+ let open Slirp_stack in
303
+ Client.TCPV4. create_connection (Client. tcpv4 stack.t) (primary_dns_ip, 3128 )
304
+ >> = function
305
+ | Error _ ->
306
+ Log. err (fun f -> f " Failed to connect to %s:3128" (Ipaddr.V4. to_string primary_dns_ip));
307
+ failwith " test_proxy_passthrough: connect failed"
308
+ | Ok flow ->
309
+ Log. info (fun f -> f " Connected to %s:3128" (Ipaddr.V4. to_string primary_dns_ip));
310
+ let oc = Outgoing.C. create flow in
311
+ let request = " hello" in
312
+ Outgoing.C. write_string oc request 0 (String. length request);
313
+ Outgoing.C. flush oc
314
+ >> = function
315
+ | Error _ -> failwith " test_proxy_passthrough: client flush returned error"
316
+ | Ok () ->
317
+ Outgoing.C. read_some ~len: 5 oc
318
+ >> = function
319
+ | Ok `Eof -> failwith " test_proxy_passthrough: client read_some returned Eof"
320
+ | Error _ -> failwith " test_proxy_passthrough: client read_some returned Error"
321
+ | Ok (`Data buf ) ->
322
+ let txt = Cstruct. to_string buf in
323
+ Alcotest. check Alcotest. string " message" " there" txt;
324
+ Lwt. pick [
325
+ (Host.Time. sleep_ns (Duration. of_sec 100 ) > |= fun () ->
326
+ `Timeout );
327
+ (forwarded >> = fun x -> Lwt. return (`Result x))
328
+ ]
329
+ )
330
+ > |= function
331
+ | `Timeout -> failwith " HTTP proxy failed"
332
+ | `Result x -> x
333
+ )
334
+ end
335
+
296
336
let test_http_connect () =
297
337
let test_dst_ip = Ipaddr.V4. of_string_exn " 1.2.3.4" in
298
338
Host.Main. run begin
@@ -387,13 +427,13 @@ let test_http_connect () =
387
427
| Error (`Msg m ) -> failwith (" Failed to enable HTTP proxy: " ^ m)
388
428
| Ok () ->
389
429
let open Slirp_stack in
390
- Client.TCPV4. create_connection (Client. tcpv4 stack.t) (Ipaddr.V4. localhost , 3128 )
430
+ Client.TCPV4. create_connection (Client. tcpv4 stack.t) (primary_dns_ip , 3128 )
391
431
>> = function
392
432
| Error _ ->
393
- Log. err (fun f -> f " Failed to connect to localhost :3128" );
433
+ Log. err (fun f -> f " Failed to connect to %s :3128" ( Ipaddr.V4. to_string primary_dns_ip) );
394
434
failwith " test_proxy_connect: connect failed"
395
435
| Ok flow ->
396
- Log. info (fun f -> f " Connected to localhost:80 " );
436
+ Log. info (fun f -> f " Connected to %s:3128 " ( Ipaddr.V4. to_string primary_dns_ip) );
397
437
let oc = Outgoing.C. create flow in
398
438
let request =
399
439
let connect = Cohttp.Request. make ~meth: `CONNECT (Uri. make () ) in
@@ -440,13 +480,13 @@ let test_http_connect () =
440
480
Host.Main. run begin
441
481
Slirp_stack. with_stack ~pcap: " test_http_proxy_connect_fail.pcap" (fun _ stack ->
442
482
let open Slirp_stack in
443
- Client.TCPV4. create_connection (Client. tcpv4 stack.t) (Ipaddr.V4. localhost , 3128 )
483
+ Client.TCPV4. create_connection (Client. tcpv4 stack.t) (primary_dns_ip , 3128 )
444
484
>> = function
445
485
| Error _ ->
446
- Log. err (fun f -> f " Failed to connect to localhost :3128" );
486
+ Log. err (fun f -> f " Failed to connect to %s :3128" ( Ipaddr.V4. to_string primary_dns_ip) );
447
487
failwith " test_proxy_connect_fail: connect failed"
448
488
| Ok flow ->
449
- Log. info (fun f -> f " Connected to localhost:80 " );
489
+ Log. info (fun f -> f " Connected to %s:3128 " ( Ipaddr.V4. to_string primary_dns_ip) );
450
490
let oc = Outgoing.C. create flow in
451
491
let request =
452
492
let connect = Cohttp.Request. make ~meth: `CONNECT (Uri. make () ) in
@@ -475,13 +515,13 @@ let test_http_connect () =
475
515
Host.Main. run begin
476
516
Slirp_stack. with_stack ~pcap: " test_http_proxy_get_dns.pcap" (fun _ stack ->
477
517
let open Slirp_stack in
478
- Client.TCPV4. create_connection (Client. tcpv4 stack.t) (Ipaddr.V4. localhost , 3128 )
518
+ Client.TCPV4. create_connection (Client. tcpv4 stack.t) (primary_dns_ip , 3128 )
479
519
>> = function
480
520
| Error _ ->
481
- Log. err (fun f -> f " Failed to connect to localhost :3128" );
521
+ Log. err (fun f -> f " Failed to connect to %s :3128" ( Ipaddr.V4. to_string primary_dns_ip) );
482
522
failwith " test_proxy_get_dns: connect failed"
483
523
| Ok flow ->
484
- Log. info (fun f -> f " Connected to localhost:80 " );
524
+ Log. info (fun f -> f " Connected to %s:3128 " ( Ipaddr.V4. to_string primary_dns_ip) );
485
525
let oc = Outgoing.C. create flow in
486
526
let host = " does.not.exist.recoil.org" in
487
527
let request = Cohttp.Request. make ~meth: `GET (Uri. make ~host () ) in
@@ -506,13 +546,13 @@ let test_http_connect () =
506
546
Host.Main. run begin
507
547
Slirp_stack. with_stack ~pcap: " test_http_proxy_get.pcap" (fun _ stack ->
508
548
let open Slirp_stack in
509
- Client.TCPV4. create_connection (Client. tcpv4 stack.t) (Ipaddr.V4. localhost , 3128 )
549
+ Client.TCPV4. create_connection (Client. tcpv4 stack.t) (primary_dns_ip , 3128 )
510
550
>> = function
511
551
| Error _ ->
512
- Log. err (fun f -> f " Failed to connect to localhost :3128" );
552
+ Log. err (fun f -> f " Failed to connect to %s :3128" ( Ipaddr.V4. to_string primary_dns_ip) );
513
553
failwith " test_proxy_get: connect failed"
514
554
| Ok flow ->
515
- Log. info (fun f -> f " Connected to localhost:80 " );
555
+ Log. info (fun f -> f " Connected to %s:3128 " ( Ipaddr.V4. to_string primary_dns_ip) );
516
556
let oc = Outgoing.C. create flow in
517
557
let host = " dave.recoil.org" in
518
558
let request = Cohttp.Request. make ~meth: `GET (Uri. make ~host () ) in
0 commit comments