@@ -32,7 +32,7 @@ impl Extractor for HeaderExtractor<'_> {
3232 }
3333
3434 fn keys ( & self ) -> Vec < & str > {
35- self . 0 . keys ( ) . map ( |name| name . as_str ( ) ) . collect ( )
35+ self . 0 . keys ( ) . map ( http :: HeaderName :: as_str) . collect ( )
3636 }
3737}
3838
@@ -96,37 +96,22 @@ impl ProxyService {
9696 source_identity
9797 }
9898 PolicyDecision :: Deny {
99- source_identity : Some ( source_identity ) ,
99+ source_identity,
100100 reason,
101101 } => {
102- warn ! (
103- source_identity = %source_identity,
104- source_peer_addr = %self . source_peer_addr,
105- dest_authority = %dest. authority,
106- policy_decision = "deny" ,
107- deny_reason = %reason,
108- "CONNECT denied"
109- ) ;
110- return response ( StatusCode :: FORBIDDEN , "forbidden" ) ;
111- }
112- PolicyDecision :: Deny {
113- source_identity : None ,
114- reason,
115- } => {
116- warn ! (
117- source_peer_addr = %self . source_peer_addr,
118- dest_authority = %dest. authority,
119- policy_decision = "deny" ,
120- deny_reason = %reason,
121- "CONNECT denied"
102+ log_denial (
103+ source_identity. as_deref ( ) ,
104+ self . source_peer_addr ,
105+ & dest. authority ,
106+ & reason,
122107 ) ;
123108 return response ( StatusCode :: FORBIDDEN , "forbidden" ) ;
124109 }
125110 } ;
126111
127112 // Connect to destination BEFORE returning 200 so the client knows
128113 // the tunnel is actually established.
129- let mut upstream = match TcpStream :: connect ( ( & * dest. host , dest. port ) ) . await {
114+ let upstream = match TcpStream :: connect ( ( & * dest. host , dest. port ) ) . await {
130115 Ok ( s) => s,
131116 Err ( e) => {
132117 error ! (
@@ -141,50 +126,12 @@ impl ProxyService {
141126 } ;
142127
143128 let on_upgrade = hyper:: upgrade:: on ( req) ;
144- let source_peer_addr = self . source_peer_addr ;
145-
146- let tunnel_span = tracing:: Span :: current ( ) ;
147- tokio:: spawn (
148- async move {
149- let upgraded = match on_upgrade. await {
150- Ok ( u) => u,
151- Err ( e) => {
152- warn ! (
153- source_identity = %source_identity,
154- source_peer_addr = %source_peer_addr,
155- dest_authority = %dest. authority,
156- error = %e,
157- "upgrade failed"
158- ) ;
159- return ;
160- }
161- } ;
162-
163- let mut downstream = hyper_util:: rt:: TokioIo :: new ( upgraded) ;
164-
165- match copy_bidirectional ( & mut downstream, & mut upstream) . await {
166- Ok ( ( up, down) ) => {
167- info ! (
168- source_identity = %source_identity,
169- source_peer_addr = %source_peer_addr,
170- dest_authority = %dest. authority,
171- bytes_client_to_dest = up,
172- bytes_dest_to_client = down,
173- "tunnel closed"
174- ) ;
175- }
176- Err ( e) => {
177- error ! (
178- source_identity = %source_identity,
179- source_peer_addr = %source_peer_addr,
180- dest_authority = %dest. authority,
181- error = %e,
182- "tunnel error"
183- ) ;
184- }
185- }
186- }
187- . instrument ( tunnel_span) ,
129+ spawn_tunnel (
130+ on_upgrade,
131+ upstream,
132+ source_identity,
133+ self . source_peer_addr ,
134+ dest. authority ,
188135 ) ;
189136
190137 response ( StatusCode :: OK , "" )
@@ -214,6 +161,7 @@ impl MakeProxyService {
214161 Self { policy_engine }
215162 }
216163
164+ #[ must_use]
217165 pub fn make_service (
218166 & self ,
219167 peer_certs : Vec < CertificateDer < ' static > > ,
@@ -223,12 +171,91 @@ impl MakeProxyService {
223171 }
224172}
225173
174+ #[ must_use]
226175pub fn extract_peer_certs ( conn : & ServerConnection ) -> Vec < CertificateDer < ' static > > {
227176 conn. peer_certificates ( )
228- . map ( |certs| certs . to_vec ( ) )
177+ . map ( < [ CertificateDer < ' _ > ] > :: to_vec)
229178 . unwrap_or_default ( )
230179}
231180
181+ fn log_denial (
182+ source_identity : Option < & str > ,
183+ source_peer_addr : SocketAddr ,
184+ dest_authority : & str ,
185+ reason : & str ,
186+ ) {
187+ if let Some ( source_identity) = source_identity {
188+ warn ! (
189+ source_identity = %source_identity,
190+ source_peer_addr = %source_peer_addr,
191+ dest_authority = %dest_authority,
192+ policy_decision = "deny" ,
193+ deny_reason = %reason,
194+ "CONNECT denied"
195+ ) ;
196+ } else {
197+ warn ! (
198+ source_peer_addr = %source_peer_addr,
199+ dest_authority = %dest_authority,
200+ policy_decision = "deny" ,
201+ deny_reason = %reason,
202+ "CONNECT denied"
203+ ) ;
204+ }
205+ }
206+
207+ fn spawn_tunnel (
208+ on_upgrade : hyper:: upgrade:: OnUpgrade ,
209+ mut upstream : TcpStream ,
210+ source_identity : String ,
211+ source_peer_addr : SocketAddr ,
212+ dest_authority : String ,
213+ ) {
214+ let tunnel_span = tracing:: Span :: current ( ) ;
215+ tokio:: spawn (
216+ async move {
217+ let upgraded = match on_upgrade. await {
218+ Ok ( u) => u,
219+ Err ( e) => {
220+ warn ! (
221+ source_identity = %source_identity,
222+ source_peer_addr = %source_peer_addr,
223+ dest_authority = %dest_authority,
224+ error = %e,
225+ "upgrade failed"
226+ ) ;
227+ return ;
228+ }
229+ } ;
230+
231+ let mut downstream = hyper_util:: rt:: TokioIo :: new ( upgraded) ;
232+
233+ match copy_bidirectional ( & mut downstream, & mut upstream) . await {
234+ Ok ( ( up, down) ) => {
235+ info ! (
236+ source_identity = %source_identity,
237+ source_peer_addr = %source_peer_addr,
238+ dest_authority = %dest_authority,
239+ bytes_client_to_dest = up,
240+ bytes_dest_to_client = down,
241+ "tunnel closed"
242+ ) ;
243+ }
244+ Err ( e) => {
245+ error ! (
246+ source_identity = %source_identity,
247+ source_peer_addr = %source_peer_addr,
248+ dest_authority = %dest_authority,
249+ error = %e,
250+ "tunnel error"
251+ ) ;
252+ }
253+ }
254+ }
255+ . instrument ( tunnel_span) ,
256+ ) ;
257+ }
258+
232259fn response ( status : StatusCode , message : & str ) -> Response < ProxyBody > {
233260 let body: ProxyBody = if message. is_empty ( ) {
234261 Empty :: < Bytes > :: new ( ) . boxed ( )
0 commit comments