Skip to content

Commit

Permalink
feat: try IPv6 when dialing IPv4 loopback (#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
deansheather authored May 1, 2024
1 parent f586aa4 commit d8a4721
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions wgengine/netstack/netstack.go
Original file line number Diff line number Diff line change
Expand Up @@ -1000,8 +1000,24 @@ func (ns *Impl) forwardTCP(getClient func(...tcpip.SettableSocketOption) *gonet.
var stdDialer net.Dialer
server, err := stdDialer.DialContext(ctx, "tcp", dialAddrStr)
if err != nil {
ns.logf("netstack: could not connect to local server at %s: %v", dialAddr.String(), err)
return
// Coder: Retry with loopback IPv6 if the dial was for 127.0.0.1.
if dialAddr.Addr().Is4() && dialAddr.Addr().String() == "127.0.0.1" {
ipv6DialAddr := netip.AddrPortFrom(netip.IPv6Loopback(), dialAddr.Port())
server, err = stdDialer.DialContext(ctx, "tcp", ipv6DialAddr.String())
if err == nil {
if debugNetstack() {
ns.logf("[coder] netstack: successful IPv4 loopback => IPv6 loopback redirect: original = %s, new = %s", dialAddrStr, ipv6DialAddr.String())
}
dialAddr = ipv6DialAddr
dialAddrStr = ipv6DialAddr.String()
} else {
ns.logf("netstack: could not connect to local server at %s (or %s)", dialAddrStr, ipv6DialAddr.String(), err)
return
}
} else {
ns.logf("netstack: could not connect to local server at %s: %v", dialAddr.String(), err)
return
}
}
defer server.Close()

Expand Down

0 comments on commit d8a4721

Please sign in to comment.