@@ -4,6 +4,12 @@ use std::{
44 time:: { Instant , SystemTime , UNIX_EPOCH } ,
55} ;
66
7+ #[ cfg( feature = "tls" ) ]
8+ use rustls:: {
9+ pki_types:: { pem:: PemObject , CertificateDer , PrivateKeyDer } ,
10+ ServerConfig ,
11+ } ;
12+
713use actix_multipart:: form:: MultipartForm ;
814use actix_web:: {
915 web:: { self , Data } ,
@@ -56,6 +62,25 @@ impl GatewayServer {
5662 }
5763 }
5864
65+ #[ cfg( feature = "tls" ) ]
66+ fn load_tls_config ( cert_path : & str , key_path : & str ) -> Result < ServerConfig , Box < dyn std:: error:: Error > > {
67+ // Install the default crypto provider
68+ let _ = rustls:: crypto:: aws_lc_rs:: default_provider ( ) . install_default ( ) ;
69+
70+ // Load certificate chain
71+ let certs: Vec < CertificateDer > = CertificateDer :: pem_file_iter ( cert_path) ?
72+ . collect :: < Result < Vec < _ > , _ > > ( ) ?;
73+
74+ // Load private key
75+ let private_key = PrivateKeyDer :: from_pem_file ( key_path) ?;
76+
77+ let config = ServerConfig :: builder ( )
78+ . with_no_client_auth ( )
79+ . with_single_cert ( certs, private_key) ?;
80+
81+ Ok ( config)
82+ }
83+
5984 pub async fn start ( & self ) {
6085 // Note: GatewayServer is thread safe so we can just clone it (no need to add mutexes)
6186 let port = self . config . port ;
@@ -68,8 +93,14 @@ impl GatewayServer {
6893 . build ( )
6994 . unwrap ( ) ;
7095
71- tracing:: info!( "Starting server at port {}" , self . config. port) ;
72- HttpServer :: new ( move || {
96+ #[ cfg( feature = "tls" ) ]
97+ let protocol = "https" ;
98+ #[ cfg( not( feature = "tls" ) ) ]
99+ let protocol = "http" ;
100+
101+ tracing:: info!( "Starting server at {}://{}:{}" , protocol, self . config. ip, self . config. port) ;
102+
103+ let server = HttpServer :: new ( move || {
73104 App :: new ( )
74105 . app_data ( Data :: new ( state. clone ( ) ) )
75106 . wrap ( prometheus. clone ( ) )
@@ -79,12 +110,24 @@ impl GatewayServer {
79110 . route ( "/proof/sp1" , web:: post ( ) . to ( Self :: post_proof_sp1) )
80111 . route ( "/proof/risc0" , web:: post ( ) . to ( Self :: post_proof_risc0) )
81112 . route ( "/quotas/{address}" , web:: get ( ) . to ( Self :: get_quotas) )
82- } )
83- . bind ( ( self . config . ip . as_str ( ) , port) )
84- . expect ( "To bind socket correctly" )
85- . run ( )
86- . await
87- . expect ( "Server to never end" ) ;
113+ } ) ;
114+
115+ #[ cfg( feature = "tls" ) ]
116+ let server = {
117+ let tls_config = Self :: load_tls_config ( & self . config . tls_cert_path , & self . config . tls_key_path )
118+ . expect ( "Failed to load TLS configuration" ) ;
119+ server. bind_rustls_0_23 ( ( self . config . ip . as_str ( ) , port) , tls_config)
120+ . expect ( "To bind socket correctly with TLS" )
121+ } ;
122+
123+ #[ cfg( not( feature = "tls" ) ) ]
124+ let server = server. bind ( ( self . config . ip . as_str ( ) , port) )
125+ . expect ( "To bind socket correctly" ) ;
126+
127+ server
128+ . run ( )
129+ . await
130+ . expect ( "Server to never end" ) ;
88131 }
89132
90133 // Returns an OK response (code 200), no matters what receives in the request
0 commit comments