@@ -33,6 +33,7 @@ use crate::tunnel::server::handler_websocket::ws_server_upgrade;
33
33
use crate :: tunnel:: server:: reverse_tunnel:: ReverseTunnelServer ;
34
34
use crate :: tunnel:: server:: utils:: {
35
35
bad_request, extract_path_prefix, extract_tunnel_info, extract_x_forwarded_for, find_mapped_port, validate_tunnel,
36
+ HttpResponse ,
36
37
} ;
37
38
use crate :: tunnel:: tls_reloader:: TlsReloader ;
38
39
use tokio:: io:: { AsyncRead , AsyncWrite , AsyncWriteExt } ;
@@ -89,68 +90,46 @@ impl WsServer {
89
90
Pin < Box < dyn AsyncWrite + Send > > ,
90
91
bool ,
91
92
) ,
92
- Response < Either < String , BoxBody < Bytes , anyhow :: Error > > > ,
93
+ HttpResponse ,
93
94
> {
94
- match extract_x_forwarded_for ( req) {
95
- Ok ( Some ( ( x_forward_for, x_forward_for_str) ) ) => {
96
- info ! ( "Request X-Forwarded-For: {:?}" , x_forward_for) ;
97
- Span :: current ( ) . record ( "forwarded_for" , x_forward_for_str) ;
98
- client_addr. set_ip ( x_forward_for) ;
99
- }
100
- Ok ( _) => { }
101
- Err ( _err) => return Err ( bad_request ( ) ) ,
95
+ if let Some ( ( x_forward_for, x_forward_for_str) ) = extract_x_forwarded_for ( req) {
96
+ info ! ( "Request X-Forwarded-For: {x_forward_for:?}" ) ;
97
+ Span :: current ( ) . record ( "forwarded_for" , x_forward_for_str) ;
98
+ client_addr. set_ip ( x_forward_for) ;
102
99
} ;
103
100
104
- let path_prefix = match extract_path_prefix ( req) {
105
- Ok ( p) => p,
106
- Err ( _err) => return Err ( bad_request ( ) ) ,
107
- } ;
101
+ let path_prefix = extract_path_prefix ( req) ?;
108
102
109
103
if let Some ( restrict_path) = restrict_path_prefix {
110
104
if path_prefix != restrict_path {
111
105
warn ! (
112
- "Client requested upgrade path '{}' does not match upgrade path restriction '{}' (mTLS, etc.)" ,
113
- path_prefix, restrict_path
106
+ "Client requested upgrade path '{path_prefix}' does not match upgrade path restriction '{restrict_path}' (mTLS, etc.)"
114
107
) ;
115
108
return Err ( bad_request ( ) ) ;
116
109
}
117
110
}
118
111
119
- let jwt = match extract_tunnel_info ( req) {
120
- Ok ( jwt) => jwt,
121
- Err ( _err) => return Err ( bad_request ( ) ) ,
122
- } ;
112
+ let jwt = extract_tunnel_info ( req) ?;
123
113
124
114
Span :: current ( ) . record ( "id" , & jwt. claims . id ) ;
125
115
Span :: current ( ) . record ( "remote" , format ! ( "{}:{}" , jwt. claims. r, jwt. claims. rp) ) ;
126
- let remote = match RemoteAddr :: try_from ( jwt. claims ) {
127
- Ok ( remote) => remote,
128
- Err ( err) => {
129
- warn ! ( "Rejecting connection with bad tunnel info: {} {}" , err, req. uri( ) ) ;
130
- return Err ( bad_request ( ) ) ;
131
- }
132
- } ;
116
+ let remote = RemoteAddr :: try_from ( jwt. claims )
117
+ . inspect_err ( |err| warn ! ( "Rejecting connection with bad tunnel info: {err} {}" , req. uri( ) ) )
118
+ . map_err ( |_| bad_request ( ) ) ?;
133
119
134
- let restriction = match validate_tunnel ( & remote, path_prefix, & restrictions) {
135
- Some ( matched_restriction) => {
136
- info ! ( "Tunnel accepted due to matched restriction: {}" , matched_restriction. name) ;
137
- matched_restriction
138
- }
139
- None => {
140
- warn ! ( "Rejecting connection with not allowed destination: {:?}" , remote) ;
141
- return Err ( bad_request ( ) ) ;
142
- }
143
- } ;
120
+ let restriction = validate_tunnel ( & remote, path_prefix, & restrictions) . ok_or_else ( || {
121
+ warn ! ( "Rejecting connection with not allowed destination: {remote:?}" ) ;
122
+ bad_request ( )
123
+ } ) ?;
124
+ info ! ( "Tunnel accepted due to matched restriction: {}" , restriction. name) ;
144
125
145
126
let req_protocol = remote. protocol . clone ( ) ;
146
127
let inject_cookie = req_protocol. is_dynamic_reverse_tunnel ( ) ;
147
- let tunnel = match self . exec_tunnel ( restriction, remote, client_addr) . await {
148
- Ok ( ret) => ret,
149
- Err ( err) => {
150
- warn ! ( "Rejecting connection with bad upgrade request: {} {}" , err, req. uri( ) ) ;
151
- return Err ( bad_request ( ) ) ;
152
- }
153
- } ;
128
+ let tunnel = self
129
+ . exec_tunnel ( restriction, remote, client_addr)
130
+ . await
131
+ . inspect_err ( |err| warn ! ( "Rejecting connection with bad upgrade request: {err} {}" , req. uri( ) ) )
132
+ . map_err ( |_| bad_request ( ) ) ?;
154
133
155
134
let ( remote_addr, local_rx, local_tx) = tunnel;
156
135
info ! ( "connected to {:?} {}:{}" , req_protocol, remote_addr. host, remote_addr. port) ;
0 commit comments