@@ -14,19 +14,13 @@ use hyper::header::{
1414 HeaderValue , ACCESS_CONTROL_ALLOW_HEADERS , ACCESS_CONTROL_ALLOW_METHODS ,
1515 ACCESS_CONTROL_ALLOW_ORIGIN , CONTENT_LENGTH , CONTENT_TYPE ,
1616} ;
17- use hyper:: server:: conn:: http1;
1817use hyper:: { Method , Request , Response } ;
1918use hyper_rustls:: builderstates:: WantsSchemes ;
2019use hyper_rustls:: { HttpsConnector , HttpsConnectorBuilder } ;
2120use hyper_util:: client:: legacy:: connect:: HttpConnector ;
2221use hyper_util:: client:: legacy:: Client ;
23- use hyper_util:: rt:: { TokioExecutor , TokioIo } ;
24- use hyper_util:: service:: TowerToHyperService ;
25- use tokio:: io:: { AsyncRead , AsyncWrite } ;
26- #[ cfg( unix) ]
27- use tokio:: net:: UnixListener ;
28- use tokio_util:: net:: Listener ;
29- use tracing:: { error, instrument} ;
22+ use hyper_util:: rt:: TokioExecutor ;
23+ use tracing:: instrument;
3024
3125pub mod error;
3226#[ cfg( not( feature = "_test-util" ) ) ]
@@ -45,47 +39,6 @@ pub mod bootstrap;
4539pub const EXPECTED_MEDIA_TYPE : HeaderValue = HeaderValue :: from_static ( "message/ohttp-req" ) ;
4640pub const DEFAULT_GATEWAY : & str = "https://payjo.in" ;
4741
48- #[ instrument]
49- #[ cfg( unix) ]
50- pub async fn listen_socket (
51- socket_path : & str ,
52- gateway_origin : GatewayUri ,
53- ) -> Result < tokio:: task:: JoinHandle < Result < ( ) , BoxError > > , BoxError > {
54- let listener = UnixListener :: bind ( socket_path) ?;
55- tracing:: info!( "OHTTP relay listening on socket: {}" , socket_path) ;
56- let sentinel_tag = SentinelTag :: new ( [ 0u8 ; 32 ] ) ;
57- ohttp_relay ( listener, RelayConfig :: new_with_default_client ( gateway_origin, sentinel_tag) ) . await
58- }
59-
60- #[ instrument]
61- #[ cfg( not( unix) ) ]
62- pub async fn listen_socket (
63- socket_path : & str ,
64- gateway_origin : GatewayUri ,
65- ) -> Result < tokio:: task:: JoinHandle < Result < ( ) , BoxError > > , BoxError > {
66- // Keep API parity across targets while making the limitation explicit at runtime.
67- let _ = ( socket_path, gateway_origin) ;
68- Err ( std:: io:: Error :: new (
69- std:: io:: ErrorKind :: Unsupported ,
70- "UNIX_SOCKET is only supported on unix targets; use PORT instead" ,
71- )
72- . into ( ) )
73- }
74-
75- #[ cfg( feature = "_test-util" ) ]
76- pub async fn listen_tcp_on_free_port (
77- default_gateway : GatewayUri ,
78- root_store : rustls:: RootCertStore ,
79- ) -> Result < ( u16 , tokio:: task:: JoinHandle < Result < ( ) , BoxError > > ) , BoxError > {
80- let listener = tokio:: net:: TcpListener :: bind ( "[::]:0" ) . await ?;
81- let port = listener. local_addr ( ) ?. port ( ) ;
82- println ! ( "OHTTP relay binding to port {}" , listener. local_addr( ) ?) ;
83- let sentinel_tag = SentinelTag :: new ( [ 0u8 ; 32 ] ) ;
84- let config = RelayConfig :: new ( default_gateway, root_store, sentinel_tag) ;
85- let handle = ohttp_relay ( listener, config) . await ?;
86- Ok ( ( port, handle) )
87- }
88-
8942#[ derive( Debug ) ]
9043struct RelayConfig {
9144 default_gateway : GatewayUri ,
@@ -116,8 +69,6 @@ pub struct Service {
11669}
11770
11871impl Service {
119- fn from_config ( config : Arc < RelayConfig > ) -> Self { Self { config } }
120-
12172 pub async fn new ( sentinel_tag : SentinelTag ) -> Self {
12273 // The default gateway is hardcoded because it is obsolete and required only for backwards
12374 // compatibility.
@@ -131,10 +82,12 @@ impl Service {
13182
13283 #[ cfg( feature = "_test-util" ) ]
13384 pub async fn new_with_roots (
134- root_store : rustls:: RootCertStore ,
13585 sentinel_tag : SentinelTag ,
86+ root_store : rustls:: RootCertStore ,
87+ default_gateway : Option < GatewayUri > ,
13688 ) -> Self {
137- let gateway_origin = GatewayUri :: from_str ( DEFAULT_GATEWAY ) . expect ( "valid gateway uri" ) ;
89+ let gateway_origin = default_gateway
90+ . unwrap_or_else ( || GatewayUri :: from_str ( DEFAULT_GATEWAY ) . expect ( "valid gateway uri" ) ) ;
13891 let config = RelayConfig :: new ( gateway_origin, root_store, sentinel_tag) ;
13992 config. prober . assert_opt_in ( & config. default_gateway ) . await ;
14093 Self { config : Arc :: new ( config) }
@@ -197,38 +150,6 @@ impl From<rustls::RootCertStore> for HttpClient {
197150 }
198151}
199152
200- #[ instrument( skip( listener) ) ]
201- async fn ohttp_relay < L > (
202- mut listener : L ,
203- config : RelayConfig ,
204- ) -> Result < tokio:: task:: JoinHandle < Result < ( ) , BoxError > > , BoxError >
205- where
206- L : Listener + Unpin + Send + ' static ,
207- L :: Io : AsyncRead + AsyncWrite + Unpin + Send + ' static ,
208- {
209- config. prober . assert_opt_in ( & config. default_gateway ) . await ;
210-
211- let config = Arc :: new ( config) ;
212-
213- let handle = tokio:: spawn ( async move {
214- while let Ok ( ( stream, _) ) = listener. accept ( ) . await {
215- let service = Service :: from_config ( config. clone ( ) ) ;
216- let io = TokioIo :: new ( stream) ;
217- tokio:: spawn ( async move {
218- let hyper_service = TowerToHyperService :: new ( service) ;
219- if let Err ( err) =
220- http1:: Builder :: new ( ) . serve_connection ( io, hyper_service) . with_upgrades ( ) . await
221- {
222- error ! ( "Error serving connection: {:?}" , err) ;
223- }
224- } ) ;
225- }
226- Ok ( ( ) )
227- } ) ;
228-
229- Ok ( handle)
230- }
231-
232153#[ instrument]
233154async fn serve_ohttp_relay < B > (
234155 req : Request < B > ,
0 commit comments