33//! is only capable of communicating over SSLv3
44
55use super :: { spawn_server_task, HTTP_PORT } ;
6- use crate :: api:: { headers:: X_TOKEN , proxy_http_request, AuthToken } ;
6+ use crate :: {
7+ api:: { headers:: X_TOKEN , proxy_http_request} ,
8+ ctx:: ClientContext ,
9+ } ;
710use anyhow:: Context ;
811use hyper:: {
912 body:: HttpBody , header:: HeaderValue , http:: uri:: PathAndQuery , server:: conn:: Http ,
@@ -14,7 +17,6 @@ use openssl::ssl::{Ssl, SslContext};
1417use std:: { convert:: Infallible , net:: Ipv4Addr , pin:: Pin , sync:: Arc } ;
1518use tokio:: net:: { TcpListener , TcpStream } ;
1619use tokio_openssl:: SslStream ;
17- use url:: Url ;
1820
1921/// Starts the HTTP proxy server
2022///
@@ -24,29 +26,23 @@ use url::Url;
2426/// * `context` - The SSL context to use when accepting clients
2527/// * `token` - The authentication token
2628pub async fn start_http_server (
27- http_client : reqwest:: Client ,
28- base_url : Arc < Url > ,
29+ ctx : Arc < ClientContext > ,
2930 ssl_context : SslContext ,
30- token : AuthToken ,
31- ) -> anyhow:: Result < ( ) > {
31+ ) -> std:: io:: Result < ( ) > {
3232 // Bind the local tcp socket for accepting connections
33- let listener = TcpListener :: bind ( ( Ipv4Addr :: LOCALHOST , HTTP_PORT ) )
34- . await
35- . context ( "Failed to bind listener" ) ?;
33+ let listener = TcpListener :: bind ( ( Ipv4Addr :: LOCALHOST , HTTP_PORT ) ) . await ?;
3634
3735 // Accept connections
3836 loop {
3937 let ( stream, _) = listener. accept ( ) . await ?;
4038
41- let ssl = Ssl :: new ( & ssl_context) . context ( "Failed to get ssl instance" ) ?;
42- let stream = SslStream :: new ( ssl, stream) . context ( "Failed to create ssl stream" ) ?;
39+ let ssl = Ssl :: new ( & ssl_context) . map_err ( std :: io :: Error :: other ) ?;
40+ let stream = SslStream :: new ( ssl, stream) . map_err ( std :: io :: Error :: other ) ?;
4341
44- let http_client = http_client. clone ( ) ;
45- let base_url = base_url. clone ( ) ;
46- let token = token. clone ( ) ;
42+ let ctx = ctx. clone ( ) ;
4743
4844 spawn_server_task ( async move {
49- if let Err ( err) = serve_connection ( stream, http_client , base_url , token ) . await {
45+ if let Err ( err) = serve_connection ( stream, ctx ) . await {
5046 error ! ( "Error while redirecting: {}" , err) ;
5147 }
5248 } ) ;
@@ -57,23 +53,14 @@ pub async fn start_http_server(
5753/// completes the accept stream process
5854pub async fn serve_connection (
5955 mut stream : SslStream < TcpStream > ,
60- http_client : reqwest:: Client ,
61- base_url : Arc < Url > ,
62- token : AuthToken ,
56+ ctx : Arc < ClientContext > ,
6357) -> anyhow:: Result < ( ) > {
6458 Pin :: new ( & mut stream) . accept ( ) . await ?;
6559
6660 Http :: new ( )
6761 . serve_connection (
6862 stream,
69- service_fn ( move |request| {
70- handle (
71- request,
72- http_client. clone ( ) ,
73- base_url. clone ( ) ,
74- token. clone ( ) ,
75- )
76- } ) ,
63+ service_fn ( move |request| handle ( request, ctx. clone ( ) ) ) ,
7764 )
7865 . await
7966 . context ( "Serve error" ) ?;
@@ -90,9 +77,7 @@ pub async fn serve_connection(
9077/// * `base_url` - The server base URL (Connection URL)
9178async fn handle (
9279 mut request : Request < Body > ,
93- http_client : reqwest:: Client ,
94- base_url : Arc < Url > ,
95- token : AuthToken ,
80+ ctx : Arc < ClientContext > ,
9681) -> Result < Response < Body > , Infallible > {
9782 let path_and_query = request
9883 . uri ( )
@@ -107,7 +92,7 @@ async fn handle(
10792 let path_and_query = path_and_query. strip_prefix ( '/' ) . unwrap_or ( path_and_query) ;
10893
10994 // Create the new url from the path
110- let url = match base_url. join ( path_and_query) {
95+ let url = match ctx . base_url . join ( path_and_query) {
11196 Ok ( value) => value,
11297 Err ( err) => {
11398 error ! ( "Failed to create HTTP proxy URL: {}" , err) ;
@@ -134,11 +119,11 @@ async fn handle(
134119 let mut headers = request. headers ( ) . clone ( ) ;
135120 headers. insert (
136121 X_TOKEN ,
137- HeaderValue :: from_str ( & token) . expect ( "Invalid token" ) ,
122+ HeaderValue :: from_str ( & ctx . token ) . expect ( "Invalid token" ) ,
138123 ) ;
139124
140125 // Proxy the request to the server
141- let response = match proxy_http_request ( & http_client, url, method, body, headers) . await {
126+ let response = match proxy_http_request ( & ctx . http_client , url, method, body, headers) . await {
142127 Ok ( value) => value,
143128 Err ( err) => {
144129 error ! ( "Failed to proxy HTTP request: {}" , err) ;
0 commit comments