@@ -376,15 +376,15 @@ func (h *tcpHandler) handle(px ipn.Proxy, gconn *netstack.GTCPConn, src, target
376376 }
377377 }
378378
379- bindAddr := makeAnyAddrPort ( src )
379+ var bindAddr netip. AddrPort
380380 eim := settings .EndpointIndependentMapping .Load ()
381381 portfwd := settings .PortForward .Load ()
382- maybeDialBind := eim || portfwd
383382
384- if eim {
385- if nataddr := h .natLookup (pidstr (px ), src , target ); nataddr .IsValid () {
386- bindAddr = nataddr
387- }
383+ if portfwd { // port forwarding overriden by eim
384+ bindAddr = makeAnyAddrPort (src )
385+ }
386+ if eim { // bindAddr may be invalid
387+ bindAddr = h .natLookup (pidstr (px ), src , target )
388388 }
389389
390390 var pc protect.Conn
@@ -397,16 +397,17 @@ func (h *tcpHandler) handle(px ipn.Proxy, gconn *netstack.GTCPConn, src, target
397397 smm .ID , pidstr (px ), src , gconn .LocalAddr (), bindAddr , targetstr , smm .UID )
398398 }
399399
400+ dialbindOK := false
400401 // github.com/google/gvisor/blob/5ba35f516b5c2/test/benchmarks/tcp/tcp_proxy.go#L359
401402 // ref: stackoverflow.com/questions/63656117
402403 // ref: stackoverflow.com/questions/40328025
403- if maybeDialBind {
404+ if bindAddr . IsValid () {
404405 pc , err = px .Dialer ().DialBind ("tcp" , bindAddr .String (), targetstr )
405- maybeDialBind = err == nil
406- logwif (! maybeDialBind )("tcp: %s dialbind ok? %t (%s [%s] => %s via %s); err? %v" ,
407- smm .ID , maybeDialBind , src , bindAddr , targetstr , pidstr (px ), err )
406+ dialbindOK = err == nil
407+ logwif (! dialbindOK )("tcp: %s dialbind ok? %t (%s [%s] => %s via %s); err? %v" ,
408+ smm .ID , dialbindOK , src , bindAddr , targetstr , pidstr (px ), err )
408409 }
409- if ! maybeDialBind {
410+ if ! dialbindOK {
410411 pc , err = px .Dialer ().Dial ("tcp" , targetstr )
411412 }
412413 if err == nil {
@@ -437,7 +438,7 @@ func (h *tcpHandler) handle(px ipn.Proxy, gconn *netstack.GTCPConn, src, target
437438 if err != nil {
438439 clos (pc )
439440 log .W ("tcp: err dialing %s proxy(%s) %v [%v] => %v (bind? %t) for %s: %v" ,
440- smm .ID , smm .PID , src , bindAddr , smm .Target , maybeDialBind , smm .UID , err )
441+ smm .ID , smm .PID , src , bindAddr , smm .Target , dialbindOK , smm .UID , err )
441442 return cont , err
442443 }
443444
0 commit comments