@@ -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,9 +62,31 @@ impl GatewayServer {
5662 }
5763 }
5864
65+ #[ cfg( feature = "tls" ) ]
66+ fn load_tls_config (
67+ cert_path : & str ,
68+ key_path : & str ,
69+ ) -> Result < ServerConfig , Box < dyn std:: error:: Error > > {
70+ // Install the default crypto provider
71+ let _ = rustls:: crypto:: aws_lc_rs:: default_provider ( ) . install_default ( ) ;
72+
73+ // Load certificate chain
74+ let certs: Vec < CertificateDer > =
75+ CertificateDer :: pem_file_iter ( cert_path) ?. collect :: < Result < Vec < _ > , _ > > ( ) ?;
76+
77+ // Load private key
78+ let private_key = PrivateKeyDer :: from_pem_file ( key_path) ?;
79+
80+ let config = ServerConfig :: builder ( )
81+ . with_no_client_auth ( )
82+ . with_single_cert ( certs, private_key) ?;
83+
84+ Ok ( config)
85+ }
86+
5987 pub async fn start ( & self ) {
6088 // Note: GatewayServer is thread safe so we can just clone it (no need to add mutexes)
61- let port = self . config . port ;
89+ let http_port = self . config . port ;
6290 let state = self . clone ( ) ;
6391
6492 // Note: This creates a new Prometheus server different from the one created in GatewayServer::new. The created
@@ -68,8 +96,7 @@ impl GatewayServer {
6896 . build ( )
6997 . unwrap ( ) ;
7098
71- tracing:: info!( "Starting server at port {}" , self . config. port) ;
72- HttpServer :: new ( move || {
99+ let server = HttpServer :: new ( move || {
73100 App :: new ( )
74101 . app_data ( Data :: new ( state. clone ( ) ) )
75102 . wrap ( prometheus. clone ( ) )
@@ -79,12 +106,37 @@ impl GatewayServer {
79106 . route ( "/proof/sp1" , web:: post ( ) . to ( Self :: post_proof_sp1) )
80107 . route ( "/proof/risc0" , web:: post ( ) . to ( Self :: post_proof_risc0) )
81108 . 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" ) ;
109+ } ) ;
110+
111+ tracing:: info!(
112+ "Starting HTTP server at http://{}:{}" ,
113+ self . config. ip,
114+ http_port
115+ ) ;
116+
117+ let server = server
118+ . bind ( ( self . config . ip . as_str ( ) , http_port) )
119+ . expect ( "To bind HTTP socket correctly" ) ;
120+
121+ #[ cfg( feature = "tls" ) ]
122+ let server = {
123+ let tls_port = self . config . tls_port ;
124+ tracing:: info!(
125+ "Starting HTTPS server at https://{}:{}" ,
126+ self . config. ip,
127+ tls_port
128+ ) ;
129+
130+ let tls_config =
131+ Self :: load_tls_config ( & self . config . tls_cert_path , & self . config . tls_key_path )
132+ . expect ( "Failed to load TLS configuration" ) ;
133+
134+ server
135+ . bind_rustls_0_23 ( ( self . config . ip . as_str ( ) , tls_port) , tls_config)
136+ . expect ( "To bind HTTPS socket correctly with TLS" )
137+ } ;
138+
139+ server. run ( ) . await . expect ( "Server to never end" ) ;
88140 }
89141
90142 // Returns an OK response (code 200), no matters what receives in the request
0 commit comments