@@ -180,6 +180,8 @@ allowed_ip=%s/128`,
180
180
}, exists , nil
181
181
}
182
182
183
+ type ipPortKey struct {}
184
+
183
185
func (api * API ) handleTunnelMW (next http.Handler ) http.Handler {
184
186
return http .HandlerFunc (func (rw http.ResponseWriter , r * http.Request ) {
185
187
ctx := r .Context ()
@@ -210,36 +212,28 @@ func (api *API) handleTunnelMW(next http.Handler) http.Handler {
210
212
return
211
213
}
212
214
213
- dialCtx , dialCancel := context .WithTimeout (ctx , api .Options .PeerDialTimeout )
214
- defer dialCancel ()
215
-
216
- nc , err := api .wgNet .DialContextTCPAddrPort (dialCtx , netip .AddrPortFrom (ip , tunnelsdk .TunnelPort ))
217
- if err != nil {
218
- httpapi .Write (ctx , rw , http .StatusBadGateway , tunnelsdk.Response {
219
- Message : "Failed to dial peer." ,
220
- Detail : err .Error (),
221
- })
222
- return
223
- }
224
-
225
215
span := trace .SpanFromContext (ctx )
226
216
span .SetAttributes (attribute .Bool ("proxy_request" , true ))
227
217
218
+ // The transport on the reverse proxy uses this ctx value to know which
219
+ // IP to dial. See tunneld.go.
220
+ ctx = context .WithValue (ctx , ipPortKey {}, netip .AddrPortFrom (ip , tunnelsdk .TunnelPort ))
221
+ r = r .WithContext (ctx )
222
+
228
223
rp := httputil.ReverseProxy {
224
+ // This can only happen when it fails to dial.
225
+ ErrorHandler : func (w http.ResponseWriter , r * http.Request , err error ) {
226
+ httpapi .Write (ctx , rw , http .StatusBadGateway , tunnelsdk.Response {
227
+ Message : "Failed to dial peer." ,
228
+ Detail : err .Error (),
229
+ })
230
+ },
229
231
Director : func (rp * http.Request ) {
230
232
rp .URL .Scheme = "http"
231
233
rp .URL .Host = r .Host
232
234
rp .Host = r .Host
233
235
},
234
- Transport : & http.Transport {
235
- DialContext : func (ctx context.Context , network , addr string ) (net.Conn , error ) {
236
- return & tracingConnWrapper {
237
- Conn : nc ,
238
- span : span ,
239
- ctx : ctx ,
240
- }, nil
241
- },
242
- },
236
+ Transport : api .transport ,
243
237
}
244
238
245
239
span .End ()
0 commit comments