netproxy: an IPv6-only host is not stuck on IPv4 after one IPv6 failure (ZeHeaton23141 on X)

A remote magpie on an IPv6-only address worked, then stopped until magpie
restarted. v4Fallback marks a host:port whose IPv6 connection ended before a
byte came back and dials it over tcp4 for 30 minutes; for a host with no IPv4
that dial can only fail, so every request failed for the whole window. Now a
failed tcp4 dial clears the mark and goes on over tcp at once, and an IPv6
literal address is never marked.

Run (wsl-run, magpie-desktop; mote's disk was full): go test ./internal/netproxy,
go vet for linux, windows and darwin, gofmt. TestIPv6OnlyHostRecovers (an A
record that fails, an AAAA that serves on [::1], a prior mark) fails on the old
code and with clearV4 commented out (mark kept). Not tried: a real IPv6-only
remote magpie, the full suite, GUI tests (no GUI change). No per-remote
"IPv6 only" setting: the cause is fixed instead.
This commit is contained in:
yetone
2026-11-11 13:50:15 +08:00
parent d8b6a2f555
commit ce0cbff616
2 changed files with 95 additions and 5 deletions
+38 -5
View File
@@ -42,6 +42,9 @@ func v4Since(addr string) time.Time {
}
func markV4(addr string) {
if v6Literal(addr) {
return
}
v4Only.Lock()
defer v4Only.Unlock()
if v4Only.at == nil {
@@ -59,19 +62,49 @@ func v4Fallback(t *http.Transport) {
}
t.DialContext = func(ctx context.Context, network, addr string) (net.Conn, error) {
if network == "tcp" && !v4Since(addr).IsZero() {
network = "tcp4"
// An IPv6-only host (a remote magpie with no A record, or
// an IPv6 address typed in) has nothing to answer over
// IPv4: that dial failing clears the mark and goes on over
// tcp, so IPv6 is tried again at once, not in 30 minutes
// (ZeHeaton23141 on X).
if c, err := dial(ctx, "tcp4", addr); err == nil {
return watchV6(c, addr), nil
}
clearV4(addr)
}
c, err := dial(ctx, network, addr)
if err != nil || c == nil {
return c, err
}
if a, ok := c.RemoteAddr().(*net.TCPAddr); ok && a.IP.To4() == nil {
return &v6Conn{Conn: c, addr: addr}, nil
}
return c, nil
return watchV6(c, addr), nil
}
}
// watchV6 wraps a connection to an IPv6 address in a v6Conn.
func watchV6(c net.Conn, addr string) net.Conn {
if a, ok := c.RemoteAddr().(*net.TCPAddr); ok && a.IP.To4() == nil {
return &v6Conn{Conn: c, addr: addr}
}
return c
}
func clearV4(addr string) {
v4Only.Lock()
defer v4Only.Unlock()
delete(v4Only.at, addr)
}
// v6Literal reports whether addr's host is an IPv6 address, which has no
// IPv4 to fall back to.
func v6Literal(addr string) bool {
h, _, err := net.SplitHostPort(addr)
if err != nil {
return false
}
ip := net.ParseIP(h)
return ip != nil && ip.To4() == nil
}
// v6Conn is a connection to an IPv6 address, marking its host:port for
// IPv4 if it fails before a byte comes back.
type v6Conn struct {
+57
View File
@@ -1,7 +1,9 @@
package netproxy
import (
"bufio"
"context"
"errors"
"io"
"net"
"net/http"
@@ -75,3 +77,58 @@ func TestIPv4Fallback(t *testing.T) {
t.Fatalf("dials %s, want tcp,tcp4,tcp4", got)
}
}
// A name with an A record nobody answers and an AAAA record that served
// before (a remote magpie that is IPv6-only) is not stuck on IPv4 after
// one IPv6 connection fails: the IPv4 dial fails, the mark is cleared and
// IPv6 is tried again (ZeHeaton23141 on X).
func TestIPv6OnlyHostRecovers(t *testing.T) {
v6, err := net.Listen("tcp6", "[::1]:0")
if err != nil {
t.Skip("no ::1:", err)
}
defer v6.Close()
go func() {
for {
c, err := v6.Accept()
if err != nil {
return
}
go func() {
defer c.Close()
http.ReadRequest(bufio.NewReader(c))
io.WriteString(c, "HTTP/1.1 200 OK\r\nContent-Length: 2\r\n\r\nok")
}()
}
}()
t.Cleanup(func() { v4Only.Lock(); v4Only.at = nil; v4Only.Unlock() })
var d net.Dialer
base := &http.Transport{
Proxy: func(*http.Request) (*url.URL, error) { return nil, nil },
DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
if network == "tcp4" {
return nil, errors.New("dial tcp4: no route to host") // the A record
}
return d.DialContext(ctx, "tcp", v6.Addr().String())
},
}
addr := "magpie.example:3425"
markV4(addr) // an earlier IPv6 hiccup
c := &http.Client{Transport: Dispatch(base)}
for i := 0; i < 2; i++ {
res, err := c.Get("http://" + addr + "/")
if err != nil {
t.Fatalf("request %d: %v", i, err)
}
io.ReadAll(res.Body)
res.Body.Close()
}
if !v4Since(addr).IsZero() {
t.Fatal("IPv4 mark kept after the IPv4 dial failed")
}
// an IPv6 literal is never marked
markV4("[::1]:3425")
if !v4Since("[::1]:3425").IsZero() {
t.Fatal("IPv6 literal marked")
}
}