@@ -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,28 @@ 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)
6189 let port = self . config . port ;
@@ -68,8 +96,19 @@ impl GatewayServer {
6896 . build ( )
6997 . unwrap ( ) ;
7098
71- tracing:: info!( "Starting server at port {}" , self . config. port) ;
72- HttpServer :: new ( move || {
99+ #[ cfg( feature = "tls" ) ]
100+ let protocol = "https" ;
101+ #[ cfg( not( feature = "tls" ) ) ]
102+ let protocol = "http" ;
103+
104+ tracing:: info!(
105+ "Starting server at {}://{}:{}" ,
106+ protocol,
107+ self . config. ip,
108+ self . config. port
109+ ) ;
110+
111+ let server = HttpServer :: new ( move || {
73112 App :: new ( )
74113 . app_data ( Data :: new ( state. clone ( ) ) )
75114 . wrap ( prometheus. clone ( ) )
@@ -79,12 +118,24 @@ impl GatewayServer {
79118 . route ( "/proof/sp1" , web:: post ( ) . to ( Self :: post_proof_sp1) )
80119 . route ( "/proof/risc0" , web:: post ( ) . to ( Self :: post_proof_risc0) )
81120 . 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" ) ;
121+ } ) ;
122+
123+ #[ cfg( feature = "tls" ) ]
124+ let server = {
125+ let tls_config =
126+ Self :: load_tls_config ( & self . config . tls_cert_path , & self . config . tls_key_path )
127+ . expect ( "Failed to load TLS configuration" ) ;
128+ server
129+ . bind_rustls_0_23 ( ( self . config . ip . as_str ( ) , port) , tls_config)
130+ . expect ( "To bind socket correctly with TLS" )
131+ } ;
132+
133+ #[ cfg( not( feature = "tls" ) ) ]
134+ let server = server
135+ . bind ( ( self . config . ip . as_str ( ) , port) )
136+ . expect ( "To bind socket correctly" ) ;
137+
138+ server. run ( ) . await . expect ( "Server to never end" ) ;
88139 }
89140
90141 // Returns an OK response (code 200), no matters what receives in the request
0 commit comments