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.
150 lines
4.4 KiB
Go
150 lines
4.4 KiB
Go
package bridge
|
|
|
|
import (
|
|
"context"
|
|
M "github.com/sagernet/sing/common/metadata"
|
|
"github.com/sagernet/sing/protocol/socks"
|
|
"github.com/sagernet/sing/protocol/socks/socks5"
|
|
"io"
|
|
"net"
|
|
"net/netip"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func reservePort(t *testing.T) (net.Listener, uint16) {
|
|
t.Helper()
|
|
l, err := net.Listen("tcp4", "127.0.0.1:0")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return l, uint16(l.Addr().(*net.TCPAddr).Port)
|
|
}
|
|
|
|
func TestUnconfiguredTargetsAndDomainsAreRejected(t *testing.T) {
|
|
bridgeListener, _ := reservePort(t)
|
|
defer bridgeListener.Close()
|
|
entry, port := reservePort(t)
|
|
entry.Close()
|
|
var engine Engine
|
|
if err := engine.Start(context.Background(), Config{ListenPort: port, MeshSOCKS: bridgeListener.Addr().String(), MeshCIDRs: []string{"10.77.0.0/24"}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer engine.Stop()
|
|
for _, destination := range []M.Socksaddr{
|
|
M.SocksaddrFrom(netip.MustParseAddr("127.0.0.1"), uint16(bridgeListener.Addr().(*net.TCPAddr).Port)),
|
|
M.SocksaddrFrom(netip.MustParseAddr("10.78.0.1"), 1234),
|
|
M.ParseSocksaddr("must-not-resolve.invalid:1234"),
|
|
} {
|
|
conn, err := net.Dial("tcp4", entry.Addr().String())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
conn.SetDeadline(time.Now().Add(3 * time.Second))
|
|
_, err = socks.ClientHandshake5(conn, socks5.CommandConnect, destination, "", "")
|
|
conn.Close()
|
|
if err == nil {
|
|
t.Fatal("unconfigured target accepted:", destination)
|
|
}
|
|
if timeout, ok := err.(net.Error); ok && timeout.Timeout() {
|
|
t.Fatal("rejection timed out:", destination)
|
|
}
|
|
}
|
|
bridgeListener.(*net.TCPListener).SetDeadline(time.Now().Add(100 * time.Millisecond))
|
|
if conn, err := bridgeListener.Accept(); err == nil {
|
|
conn.Close()
|
|
t.Fatal("rejected request reached mesh bridge")
|
|
}
|
|
}
|
|
|
|
func TestConfigurationRejectsUnsupportedEndpointsAndPrefixes(t *testing.T) {
|
|
base := Config{ListenPort: 1080, MeshSOCKS: "127.0.0.1:1234", MeshCIDRs: []string{"10.77.0.0/24"}}
|
|
for _, endpoint := range []string{"example.com:1080", "0.0.0.0:1080", "10.0.0.1:1080", "127.0.0.1:0", "[::1]:1080"} {
|
|
c := base
|
|
c.MeshSOCKS = endpoint
|
|
if _, err := c.managedJSON(); err == nil {
|
|
t.Errorf("accepted endpoint %s", endpoint)
|
|
}
|
|
}
|
|
for _, prefix := range []string{"0.0.0.0/0", "10.0.0.0/8", "1.1.1.0/24", "127.0.0.0/24", "fd00::/64", "invalid"} {
|
|
c := base
|
|
c.MeshCIDRs = []string{prefix}
|
|
if _, err := c.managedJSON(); err == nil {
|
|
t.Errorf("accepted prefix %s", prefix)
|
|
}
|
|
}
|
|
c := base
|
|
c.MeshCIDRs = nil
|
|
if _, err := c.managedJSON(); err == nil {
|
|
t.Error("accepted empty projection")
|
|
}
|
|
}
|
|
|
|
func TestEngineLifecycleAndFailedStartRecovery(t *testing.T) {
|
|
occupied, port := reservePort(t)
|
|
defer occupied.Close()
|
|
c := Config{ListenPort: port, MeshSOCKS: "127.0.0.1:1234", MeshCIDRs: []string{"10.77.0.11/24", "10.77.0.0/24"}}
|
|
var engine Engine
|
|
if engine.Snapshot().State != "Stopped" {
|
|
t.Fatal("initial state")
|
|
}
|
|
if err := engine.Start(context.Background(), c); err == nil {
|
|
t.Fatal("started on occupied port")
|
|
} else if !strings.Contains(err.Error(), "listen_port_in_use") {
|
|
t.Fatalf("missing safe occupied-port diagnostic: %v", err)
|
|
}
|
|
if engine.Snapshot().State != "Failed" {
|
|
t.Fatal("failed start state")
|
|
}
|
|
occupied.Close()
|
|
if err := engine.Start(context.Background(), c); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer engine.Stop()
|
|
if engine.Snapshot().State != "Running" {
|
|
t.Fatal("running state")
|
|
}
|
|
if engine.Start(context.Background(), c) == nil {
|
|
t.Fatal("duplicate start accepted")
|
|
}
|
|
conn, err := net.Dial("tcp4", occupied.Addr().String())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer conn.Close()
|
|
conn.SetDeadline(time.Now().Add(3 * time.Second))
|
|
if _, err := conn.Write([]byte{5, 1, 0}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var reply [2]byte
|
|
if _, err := io.ReadFull(conn, reply[:]); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if reply != [2]byte{5, 0} {
|
|
t.Fatal("unexpected negotiation", reply)
|
|
}
|
|
if err := engine.Stop(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := engine.Stop(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if engine.Snapshot().State != "Stopped" {
|
|
t.Fatal("stop state")
|
|
}
|
|
if n, err := conn.Read(reply[:]); n != 0 {
|
|
t.Fatal("active connection retained after stop")
|
|
} else if timeout, ok := err.(net.Error); ok && timeout.Timeout() {
|
|
t.Fatal("active connection only closed by client timeout")
|
|
}
|
|
l, err := net.Listen("tcp4", occupied.Addr().String())
|
|
if err != nil {
|
|
t.Fatal("listener leaked:", err)
|
|
}
|
|
l.Close()
|
|
if err := engine.Start(context.Background(), c); err != nil {
|
|
t.Fatal("restart:", err)
|
|
}
|
|
}
|