- Lab app: proxy panel, node editor, configuration and detail pages, responsive layout, and related tests - Add meshport-socks crate and sing-box bridge scripts/docs - Add singbox_flutter binding and meshport_client_core package - FFI, VPN helper (unified mac path), and Android/macOS runner updates - Docs for architecture, client runtime, UI design, and validation - UI design output artifacts under outputs/ui Co-Authored-By: Claude Haiku 5.5 <noreply@anthropic.com>
293 lines
9.6 KiB
Go
293 lines
9.6 KiB
Go
package bridge
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"io"
|
|
"net"
|
|
"net/netip"
|
|
"sync/atomic"
|
|
"testing"
|
|
"time"
|
|
|
|
D "github.com/miekg/dns"
|
|
M "github.com/sagernet/sing/common/metadata"
|
|
"github.com/sagernet/sing/protocol/socks"
|
|
"github.com/sagernet/sing/protocol/socks/socks5"
|
|
)
|
|
|
|
// A real SOCKS5 fixture echoes through the selected outlet; a request counter
|
|
// distinguishes route selection from mere successful engine startup.
|
|
func socksEcho(t *testing.T) (net.Listener, *atomic.Int32) {
|
|
return socksFixture(t, func(c net.Conn) { io.Copy(c, c) })
|
|
}
|
|
func socksFixture(t *testing.T, handler func(net.Conn)) (net.Listener, *atomic.Int32) {
|
|
l, _ := reservePort(t)
|
|
counter := &atomic.Int32{}
|
|
go func() {
|
|
for {
|
|
c, e := l.Accept()
|
|
if e != nil {
|
|
return
|
|
}
|
|
go func() {
|
|
defer c.Close()
|
|
c.SetDeadline(time.Now().Add(5 * time.Second))
|
|
var greeting [2]byte
|
|
if _, e := io.ReadFull(c, greeting[:]); e != nil {
|
|
return
|
|
}
|
|
methods := make([]byte, int(greeting[1]))
|
|
if _, e := io.ReadFull(c, methods); e != nil {
|
|
return
|
|
}
|
|
c.Write([]byte{5, 0})
|
|
var req [4]byte
|
|
if _, e := io.ReadFull(c, req[:]); e != nil {
|
|
return
|
|
}
|
|
n := 4
|
|
if req[3] == 3 {
|
|
var size [1]byte
|
|
if _, e := io.ReadFull(c, size[:]); e != nil {
|
|
return
|
|
}
|
|
n = int(size[0])
|
|
} else if req[3] == 4 {
|
|
n = 16
|
|
}
|
|
if _, e := io.ReadFull(c, make([]byte, n+2)); e != nil {
|
|
return
|
|
}
|
|
counter.Add(1)
|
|
c.Write([]byte{5, 0, 0, 1, 127, 0, 0, 1, 0, 1})
|
|
handler(c)
|
|
}()
|
|
}
|
|
}()
|
|
return l, counter
|
|
}
|
|
func echoThrough(t *testing.T, port uint16, target M.Socksaddr, want bool) {
|
|
t.Helper()
|
|
c, e := net.Dial("tcp4", net.JoinHostPort("127.0.0.1", jsonNumber(int(port))))
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
defer c.Close()
|
|
c.SetDeadline(time.Now().Add(3 * time.Second))
|
|
_, e = socks.ClientHandshake5(c, socks5.CommandConnect, target, "", "")
|
|
if !want {
|
|
if e == nil {
|
|
c.Write([]byte("x"))
|
|
var b [1]byte
|
|
n, readErr := c.Read(b[:])
|
|
if n != 0 || readErr == nil {
|
|
t.Fatal("protected target passed")
|
|
}
|
|
if ne, ok := readErr.(net.Error); ok && ne.Timeout() {
|
|
t.Fatal("rejection timed out")
|
|
}
|
|
}
|
|
return
|
|
}
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
c.Write([]byte("route-echo"))
|
|
result := make([]byte, 10)
|
|
if _, e := io.ReadFull(c, result); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
if string(result) != "route-echo" {
|
|
t.Fatal(string(result))
|
|
}
|
|
}
|
|
func jsonNumber(n int) string { raw, _ := json.Marshal(n); return string(raw) }
|
|
func TestModesAndMeshPriority(t *testing.T) {
|
|
proxy, count := socksEcho(t)
|
|
defer proxy.Close()
|
|
mesh, meshCount := socksEcho(t)
|
|
defer mesh.Close()
|
|
direct, _ := reservePort(t)
|
|
defer direct.Close()
|
|
go func() {
|
|
for {
|
|
c, e := direct.Accept()
|
|
if e != nil {
|
|
return
|
|
}
|
|
go func() { defer c.Close(); io.Copy(c, c) }()
|
|
}
|
|
}()
|
|
for _, mode := range []string{"rule", "global", "direct"} {
|
|
t.Run(mode, func(t *testing.T) {
|
|
entry, port := reservePort(t)
|
|
entry.Close()
|
|
var engine Engine
|
|
config := Config{ListenPort: port, Feature: "combined", Mode: mode, MeshSOCKS: mesh.Addr().String(), MeshCIDRs: []string{"10.77.0.0/24"}, ProtectedCIDRs: []string{"10.88.0.0/24"}, Proxy: map[string]any{"type": "socks", "version": "5", "server": "127.0.0.1", "server_port": proxy.Addr().(*net.TCPAddr).Port}, Rules: []Rule{{Type: "ip_cidr", Value: "127.0.0.0/8", Action: "direct"}}}
|
|
if e := engine.Start(context.Background(), config); e != nil {
|
|
t.Fatalf("%v: %v", e, engine.startupCause)
|
|
}
|
|
defer engine.Stop()
|
|
before := count.Load()
|
|
beforeMesh := meshCount.Load()
|
|
echoThrough(t, port, M.SocksaddrFrom(netip.MustParseAddr("127.0.0.1"), uint16(direct.Addr().(*net.TCPAddr).Port)), true)
|
|
expected := int32(0)
|
|
if mode == "global" {
|
|
expected = 1
|
|
}
|
|
if count.Load()-before != expected {
|
|
t.Fatal("wrong internet outlet")
|
|
}
|
|
echoThrough(t, port, M.ParseSocksaddr("10.77.0.2:1234"), true)
|
|
if meshCount.Load()-beforeMesh != 1 {
|
|
t.Fatal("mesh priority lost")
|
|
}
|
|
before = count.Load()
|
|
echoThrough(t, port, M.ParseSocksaddr("10.88.0.2:1234"), false)
|
|
if count.Load() != before {
|
|
t.Fatal("protected destination leaked to proxy")
|
|
}
|
|
})
|
|
}
|
|
}
|
|
func TestDNSAndDomainRule(t *testing.T) {
|
|
upstream, count := socksEcho(t)
|
|
defer upstream.Close()
|
|
dnsSock, e := net.ListenPacket("udp4", "127.0.0.1:0")
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
dnsServer := &D.Server{PacketConn: dnsSock, Handler: D.HandlerFunc(func(w D.ResponseWriter, r *D.Msg) {
|
|
m := new(D.Msg)
|
|
m.SetReply(r)
|
|
m.Answer = []D.RR{&D.A{Hdr: D.RR_Header{Name: r.Question[0].Name, Rrtype: D.TypeA, Class: D.ClassINET, Ttl: 60}, A: net.ParseIP("203.0.113.7")}}
|
|
w.WriteMsg(m)
|
|
})}
|
|
go dnsServer.ActivateAndServe()
|
|
defer dnsServer.Shutdown()
|
|
entry, port := reservePort(t)
|
|
entry.Close()
|
|
var engine Engine
|
|
config := Config{ListenPort: port, Feature: "proxy", Mode: "rule", DNS: dnsSock.LocalAddr().String(), DefaultAction: "reject", Proxy: map[string]any{"type": "socks", "version": "5", "server": "127.0.0.1", "server_port": upstream.Addr().(*net.TCPAddr).Port}, Rules: []Rule{{Type: "domain_suffix", Value: "example.test", Action: "proxy"}}}
|
|
if e := engine.Start(context.Background(), config); e != nil {
|
|
t.Fatalf("%v: %v", e, engine.startupCause)
|
|
}
|
|
defer engine.Stop()
|
|
echoThrough(t, port, M.ParseSocksaddr("sub.example.test:1234"), true)
|
|
if count.Load() != 1 {
|
|
t.Fatal("domain rule not selected")
|
|
}
|
|
echoThrough(t, port, M.ParseSocksaddr("blocked.invalid:1234"), false)
|
|
if count.Load() != 1 {
|
|
t.Fatal("default reject bypassed")
|
|
}
|
|
}
|
|
func TestImportedNodeCannotReplaceManagedDialer(t *testing.T) {
|
|
for _, field := range []string{"detour", "bind_interface", "routing_mark", "domain_resolver", "tag", "route"} {
|
|
node := map[string]any{"type": "socks", "server": "example.test", "server_port": 1080, field: "unmanaged"}
|
|
if _, e := proxyNode(node); e == nil {
|
|
t.Fatal("injected field accepted", field)
|
|
}
|
|
}
|
|
var engine Engine
|
|
config := Config{ListenPort: 10818, Feature: "proxy", Mode: "direct", Tun: &TunConfig{FD: 123, Name: "tun0", Interface: "eth0", Index: 2}}
|
|
if e := engine.Start(context.Background(), config); e == nil {
|
|
engine.Stop()
|
|
t.Fatal("unprotected platform TUN accepted")
|
|
}
|
|
}
|
|
|
|
func TestWithdrawnMeshDNSIsRejectedWithoutFallback(t *testing.T) {
|
|
upstream, count := socksEcho(t)
|
|
defer upstream.Close()
|
|
entry, port := reservePort(t)
|
|
entry.Close()
|
|
var engine Engine
|
|
config := Config{ListenPort: port, Feature: "combined", Mode: "global", MeshSOCKS: "127.0.0.1:9", ProtectedCIDRs: []string{"10.77.0.0/24"}, MeshDNS: []MeshDNS{{Suffix: "mesh.test", Server: "10.77.0.2"}}, Proxy: map[string]any{"type": "socks", "server": "127.0.0.1", "server_port": upstream.Addr().(*net.TCPAddr).Port}}
|
|
if e := engine.Start(context.Background(), config); e != nil {
|
|
t.Fatalf("%v: %v", e, engine.startupCause)
|
|
}
|
|
defer engine.Stop()
|
|
echoThrough(t, port, M.ParseSocksaddr("secret.mesh.test:1234"), false)
|
|
if count.Load() != 0 {
|
|
t.Fatal("mesh DNS failed open to proxy")
|
|
}
|
|
}
|
|
|
|
func TestFakeIPMappingAndMeshExclusion(t *testing.T) {
|
|
dnsSock, err := net.ListenPacket("udp4", "127.0.0.1:0")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
dnsServer := &D.Server{PacketConn: dnsSock, Handler: D.HandlerFunc(func(w D.ResponseWriter, r *D.Msg) {
|
|
m := new(D.Msg)
|
|
m.SetReply(r)
|
|
m.Answer = []D.RR{&D.A{Hdr: D.RR_Header{Name: r.Question[0].Name, Rrtype: D.TypeA, Class: D.ClassINET, Ttl: 60}, A: net.ParseIP("203.0.113.7")}}
|
|
w.WriteMsg(m)
|
|
})}
|
|
go dnsServer.ActivateAndServe()
|
|
defer dnsServer.Shutdown()
|
|
proxy, count := socksEcho(t)
|
|
defer proxy.Close()
|
|
listener, port := reservePort(t)
|
|
listener.Close()
|
|
engine := &Engine{}
|
|
config := Config{ListenPort: port, Feature: "combined", Mode: "global", MeshSOCKS: "127.0.0.1:9", DNS: dnsSock.LocalAddr().String(), ProtectedCIDRs: []string{"10.77.0.0/24"}, Proxy: map[string]any{"type": "socks", "server": "127.0.0.1", "server_port": proxy.Addr().(*net.TCPAddr).Port}, FakeIP: true, MeshDNS: []MeshDNS{{Suffix: "mesh.test", Server: "10.77.0.2"}}}
|
|
if e := engine.Start(context.Background(), config); e != nil {
|
|
t.Fatalf("%v: %v", e, engine.startupCause)
|
|
}
|
|
defer engine.Close()
|
|
query := func(domain string) *D.Msg {
|
|
t.Helper()
|
|
c, err := net.Dial("tcp4", net.JoinHostPort("127.0.0.1", jsonNumber(int(port))))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer c.Close()
|
|
c.SetDeadline(time.Now().Add(3 * time.Second))
|
|
_, err = socks.ClientHandshake5(c, socks5.CommandConnect, M.ParseSocksaddr("1.1.1.1:53"), "", "")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
conn := &D.Conn{Conn: c}
|
|
msg := new(D.Msg)
|
|
msg.SetQuestion(D.Fqdn(domain), D.TypeA)
|
|
if e := conn.WriteMsg(msg); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
reply, e := conn.ReadMsg()
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
return reply
|
|
}
|
|
reply := query("public.test")
|
|
if len(reply.Answer) != 1 {
|
|
t.Fatalf("missing FakeIP answer: %v", reply)
|
|
}
|
|
address, ok := reply.Answer[0].(*D.A)
|
|
if !ok || !netip.MustParsePrefix("198.18.0.0/15").Contains(netip.MustParseAddr(address.A.String())) {
|
|
t.Fatal("not a FakeIP answer")
|
|
}
|
|
echoThrough(t, port, M.ParseSocksaddr(net.JoinHostPort(address.A.String(), "443")), true)
|
|
if count.Load() != 1 {
|
|
t.Fatal("FakeIP connection did not use selected proxy")
|
|
}
|
|
if err := engine.Stop(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := engine.Start(context.Background(), config); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
echoThrough(t, port, M.ParseSocksaddr(net.JoinHostPort(address.A.String(), "443")), true)
|
|
if count.Load() != 2 {
|
|
t.Fatal("FakeIP mapping lost across managed rebuild")
|
|
}
|
|
// Known withdrawn mesh domains must reject before the generic FakeIP rule.
|
|
reply = query("host.mesh.test")
|
|
if len(reply.Answer) != 0 || reply.Rcode == D.RcodeSuccess {
|
|
t.Fatal("withdrawn mesh domain leaked to FakeIP")
|
|
}
|
|
}
|