Files
shenandshen c7cc2aa865 Fix VPN internet connectivity and simplify client setup
Initialize Android physical interfaces and preserve macOS scoped routes. Add redacted diagnostics, default DNS, ordered auto-save, simplified controls, scanner rotation settings, and native regression coverage. Restore vendored build sources and document validation and stability checks.
2026-10-12 06:51:25 -07:00

104 lines
2.8 KiB
Go

package bridge
import (
"context"
"io"
"net"
"sync/atomic"
"testing"
"time"
"github.com/sagernet/sing-box/adapter"
"github.com/sagernet/sing-box/common/dialer"
"github.com/sagernet/sing-box/log"
"github.com/sagernet/sing-box/option"
"github.com/sagernet/sing-box/route"
"github.com/sagernet/sing/common/control"
M "github.com/sagernet/sing/common/metadata"
"github.com/sagernet/sing/service"
)
// Exercise sing-box's real interface selection, which otherwise fails with
// "no available network interface" even though the host reports a default.
func TestHostPlatformInitialPhysicalNetwork(t *testing.T) {
interfaces, err := net.Interfaces()
if err != nil {
t.Fatal(err)
}
var physical net.Interface
for _, candidate := range interfaces {
if candidate.Flags&net.FlagUp != 0 && candidate.Flags&net.FlagLoopback == 0 {
physical = candidate
break
}
}
if physical.Index == 0 {
t.Skip("No active physical interface")
}
var protected atomic.Int32
p := &hostPlatform{
monitor: &hostMonitor{physical: &control.Interface{Name: physical.Name, Index: physical.Index, MTU: physical.MTU, Flags: physical.Flags}},
protect: func(fd int) error {
if fd < 0 {
t.Fatal("Invalid protected fd")
}
protected.Add(1)
return nil
},
}
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
ctx = service.ContextWith[adapter.PlatformInterface](ctx, p)
manager, err := route.NewNetworkManager(ctx, log.NewNOPFactory().Logger(), option.RouteOptions{AutoDetectInterface: true}, option.DNSOptions{})
if err != nil {
t.Fatal(err)
}
if err = p.Initialize(manager); err != nil {
t.Fatal(err)
}
ctx = service.ContextWith[adapter.NetworkManager](ctx, manager)
d, err := dialer.NewDefault(ctx, option.DialerOptions{})
if err != nil {
t.Fatal(err)
}
listener, err := net.Listen("tcp4", "127.0.0.1:0")
if err != nil {
t.Fatal(err)
}
defer listener.Close()
go func() {
for {
c, err := listener.Accept()
if err != nil {
return
}
go func() { defer c.Close(); _, _ = io.Copy(c, c) }()
}
}()
conn, err := d.DialContext(ctx, "tcp", M.ParseSocksaddr(listener.Addr().String()))
if err != nil {
t.Fatal(err)
}
defer conn.Close()
_ = conn.SetDeadline(time.Now().Add(3 * time.Second))
if _, err = conn.Write([]byte("protected TCP")); err != nil {
t.Fatal(err)
}
data := make([]byte, len("protected TCP"))
if _, err = io.ReadFull(conn, data); err != nil {
t.Fatal(err)
}
if string(data) != "protected TCP" || protected.Load() == 0 {
t.Fatal("TCP bypassed socket protection or echo failed")
}
before := protected.Load()
udp, err := d.ListenPacket(ctx, M.ParseSocksaddr("127.0.0.1:12345"))
if err != nil {
t.Fatal(err)
}
defer udp.Close()
if protected.Load() <= before {
t.Fatal("UDP bypassed socket protection")
}
}