- 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>
142 lines
4.8 KiB
Go
142 lines
4.8 KiB
Go
package bridge
|
|
|
|
import (
|
|
"context"
|
|
"crypto/tls"
|
|
"encoding/json"
|
|
"encoding/pem"
|
|
"github.com/sagernet/sing/protocol/socks"
|
|
"github.com/sagernet/sing/protocol/socks/socks5"
|
|
"net"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"sync/atomic"
|
|
"testing"
|
|
"time"
|
|
|
|
D "github.com/miekg/dns"
|
|
box "github.com/sagernet/sing-box"
|
|
"github.com/sagernet/sing-box/option"
|
|
sjson "github.com/sagernet/sing/common/json"
|
|
M "github.com/sagernet/sing/common/metadata"
|
|
)
|
|
|
|
func TestDNSProtectionRequiresExplicitResolverAndKeepsBootstrapNarrow(t *testing.T) {
|
|
c := Config{ListenPort: 12345, Feature: "proxy", Mode: "global", DNSProtection: true, Proxy: map[string]any{"type": "socks", "server": "proxy.example", "server_port": 1080}}
|
|
if _, err := c.managedJSON(); err == nil {
|
|
t.Fatal("silently chose a DNS provider")
|
|
}
|
|
c.ProxyDNS = "tls://resolver.example"
|
|
data, err := c.managedJSON()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var v map[string]any
|
|
json.Unmarshal(data, &v)
|
|
if v["dns"].(map[string]any)["final"] != "proxy-dns" || v["route"].(map[string]any)["default_domain_resolver"].(map[string]any)["server"] != "proxy-dns" {
|
|
t.Fatal("ordinary lookup escapes proxy DNS")
|
|
}
|
|
outbounds := v["outbounds"].([]any)
|
|
if outbounds[1].(map[string]any)["domain_resolver"].(map[string]any)["server"] != "bootstrap" {
|
|
t.Fatal("proxy endpoint resolution loops")
|
|
}
|
|
c.Mode = "direct"
|
|
c.Proxy = nil
|
|
c.ProxyDNS = ""
|
|
if _, err := c.managedJSON(); err != nil {
|
|
t.Fatal("direct mode required proxy DNS", err)
|
|
}
|
|
}
|
|
|
|
func TestDNSProtectionNoDirectFallbackOnProxyFailure(t *testing.T) {
|
|
// All addresses and trust are isolated test fixtures, not public DNS providers.
|
|
fixture := httptest.NewTLSServer(http.HandlerFunc(func(http.ResponseWriter, *http.Request) {}))
|
|
defer fixture.Close()
|
|
for _, fake := range []bool{false, true} {
|
|
for _, fail := range []bool{false, true} {
|
|
var proxiedQueries, bootQueries atomic.Int32
|
|
proxy, _ := socksFixture(t, func(conn net.Conn) {
|
|
if fail {
|
|
return
|
|
}
|
|
stream := tls.Server(conn, &tls.Config{Certificates: fixture.TLS.Certificates, MinVersion: tls.VersionTLS12})
|
|
dc := &D.Conn{Conn: stream}
|
|
for {
|
|
request, err := dc.ReadMsg()
|
|
if err != nil {
|
|
return
|
|
}
|
|
proxiedQueries.Add(1)
|
|
response := new(D.Msg)
|
|
response.SetReply(request)
|
|
response.Answer = []D.RR{&D.A{Hdr: D.RR_Header{Name: request.Question[0].Name, Rrtype: D.TypeA, Class: D.ClassINET, Ttl: 60}, A: net.ParseIP("203.0.113.7")}}
|
|
if dc.WriteMsg(response) != nil {
|
|
return
|
|
}
|
|
}
|
|
})
|
|
defer proxy.Close()
|
|
socket, err := net.ListenPacket("udp4", "127.0.0.1:0")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
bootstrap := &D.Server{PacketConn: socket, Handler: D.HandlerFunc(func(w D.ResponseWriter, r *D.Msg) { bootQueries.Add(1); m := new(D.Msg); m.SetReply(r); w.WriteMsg(m) })}
|
|
go bootstrap.ActivateAndServe()
|
|
defer bootstrap.Shutdown()
|
|
l, port := reservePort(t)
|
|
l.Close()
|
|
c := Config{ListenPort: port, Feature: "proxy", Mode: "rule", DefaultAction: "reject", DNS: socket.LocalAddr().String(), DNSProtection: true, FakeIP: fake,
|
|
ProxyDNS: "tls://127.0.0.1:853", Proxy: map[string]any{"type": "socks", "server": "127.0.0.1", "server_port": proxy.Addr().(*net.TCPAddr).Port}}
|
|
data, err := c.managedJSON()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
ctx := registryContext(context.Background())
|
|
opts, err := sjson.UnmarshalExtendedContext[option.Options](ctx, data)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
opts.Certificate = &option.CertificateOptions{Store: "none", Certificate: []string{string(pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: fixture.Certificate().Raw}))}}
|
|
instance, err := box.New(box.Options{Context: ctx, Options: opts})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err = instance.Start(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
target := "private-query.test"
|
|
if fake {
|
|
conn, err := net.Dial("tcp4", net.JoinHostPort("127.0.0.1", jsonNumber(int(port))))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
conn.SetDeadline(time.Now().Add(3 * time.Second))
|
|
_, err = socks.ClientHandshake5(conn, socks5.CommandConnect, M.ParseSocksaddr("1.1.1.1:53"), "", "")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
dc := &D.Conn{Conn: conn}
|
|
msg := new(D.Msg)
|
|
msg.SetQuestion("private-query.test.", D.TypeA)
|
|
if err = dc.WriteMsg(msg); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
answer, err := dc.ReadMsg()
|
|
conn.Close()
|
|
if err != nil || len(answer.Answer) != 1 {
|
|
t.Fatalf("FakeIP query: %v / %v", answer, err)
|
|
}
|
|
target = answer.Answer[0].(*D.A).A.String()
|
|
}
|
|
echoThrough(t, port, M.ParseSocksaddr(net.JoinHostPort(target, "443")), false)
|
|
instance.Close()
|
|
if bootQueries.Load() != 0 {
|
|
t.Fatal("target query leaked to bootstrap DNS")
|
|
}
|
|
if !fail && proxiedQueries.Load() == 0 {
|
|
t.Fatal("proxy DNS wasn't used")
|
|
}
|
|
}
|
|
}
|
|
}
|