@@ -31,7 +31,7 @@ mod metadata_cache;
31
31
use std:: env;
32
32
use std:: fmt:: Display ;
33
33
use std:: io:: Read ;
34
- use std:: path:: PathBuf ;
34
+ use std:: path:: { Path , PathBuf } ;
35
35
use std:: sync:: OnceLock ;
36
36
use std:: time:: Duration ;
37
37
@@ -538,11 +538,29 @@ fn handle_get_request(request: Request, config: &ProxyConfig) {
538
538
} ;
539
539
}
540
540
541
- /// Runs HTTP proxy server forever.
542
- fn main_loop ( listen_addr : & str , config : & ProxyConfig ) -> ! {
543
- info ! ( "proxy: starting HTTP server at: {listen_addr}" ) ;
541
+ /// Server listening address
542
+ enum ListenAddress {
543
+ /// IP address + port
544
+ SocketAddr ( String ) ,
545
+ /// Unix domain socket path
546
+ UnixPath ( String ) ,
547
+ }
544
548
545
- let server = Server :: http ( listen_addr) . expect ( "failed to start the HTTP server" ) ;
549
+ /// Runs HTTP proxy server forever.
550
+ fn main_loop ( listen_addr : & ListenAddress , config : & ProxyConfig ) -> ! {
551
+ let server = match listen_addr {
552
+ ListenAddress :: SocketAddr ( addr) => {
553
+ info ! ( "proxy: starting HTTP server at: {addr}" ) ;
554
+ Server :: http ( addr) . expect ( "failed to start the HTTP server" )
555
+ }
556
+ ListenAddress :: UnixPath ( path) => {
557
+ info ! ( "proxy: starting HTTP server at Unix socket {path}" ) ;
558
+ let path = Path :: new ( path) ;
559
+ // Reap stale socket files before binding.
560
+ std:: fs:: remove_file ( path) . ok ( ) ;
561
+ Server :: http_unix ( path) . expect ( "failed to start the HTTP server" )
562
+ }
563
+ } ;
546
564
547
565
// Main HTTP request accept loop.
548
566
loop {
@@ -583,6 +601,7 @@ fn usage() {
583
601
println ! ( " -h, --help print help and exit" ) ;
584
602
println ! ( " -V, --version print version and exit" ) ;
585
603
println ! ( " -L, --listen ADDRESS:PORT address and port to listen at (0.0.0.0:3080)" ) ;
604
+ println ! ( " --listen-unix PATH Unix domain socket path to listen at" ) ;
586
605
println ! ( " -U, --upstream-url URL upstream download URL (https://crates.io/)" ) ;
587
606
println ! ( " -I, --index-url URL upstream index URL (https://index.crates.io/)" ) ;
588
607
println ! ( " -S, --proxy-url URL this proxy server URL (http://localhost:3080/)" ) ;
@@ -626,7 +645,11 @@ fn main() {
626
645
verbose += 1 ;
627
646
}
628
647
629
- let listen_addr = args
648
+ let listen_addr_unix = args
649
+ . opt_value_from_str ( "--listen-unix" )
650
+ . expect ( "bad listen socket path" ) ;
651
+
652
+ let listen_addr_ip = args
630
653
. opt_value_from_str ( [ "-L" , "--listen" ] )
631
654
. expect ( "bad listen address argument" )
632
655
. unwrap_or_else ( || LISTEN_ADDRESS . to_string ( ) ) ;
@@ -703,6 +726,11 @@ fn main() {
703
726
cache_ttl,
704
727
} ;
705
728
729
+ let listen_addr = match listen_addr_unix {
730
+ Some ( unix_path) => ListenAddress :: UnixPath ( unix_path) ,
731
+ None => ListenAddress :: SocketAddr ( listen_addr_ip) ,
732
+ } ;
733
+
706
734
// Start the main HTTP server.
707
735
main_loop ( & listen_addr, & config)
708
736
}
0 commit comments