- 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>
167 lines
6.0 KiB
Go
167 lines
6.0 KiB
Go
package bridge
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
D "github.com/miekg/dns"
|
|
box "github.com/sagernet/sing-box"
|
|
M "github.com/sagernet/sing/common/metadata"
|
|
"net"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/sagernet/sing/protocol/socks"
|
|
"github.com/sagernet/sing/protocol/socks/socks5"
|
|
|
|
"github.com/sagernet/sing-box/option"
|
|
sjson "github.com/sagernet/sing/common/json"
|
|
)
|
|
|
|
func TestBypassChinaPresetAndGlobalIsolation(t *testing.T) {
|
|
for _, fake := range []bool{false, true} {
|
|
c := Config{ListenPort: 12345, Feature: "combined", Mode: "bypass_cn", FakeIP: fake,
|
|
MeshSOCKS: "127.0.0.1:12346", MeshCIDRs: []string{"10.77.0.0/24"}, ProtectedCIDRs: []string{"10.88.0.0/24"},
|
|
DefaultAction: "reject", Proxy: map[string]any{"type": "socks", "server": "127.0.0.1", "server_port": 12347},
|
|
Rules: []Rule{{Type: "ip_cidr", Value: "0.0.0.0/0", Action: "reject"}},
|
|
RuleSets: []RuleSet{{Tag: "custom", Enabled: true, Action: "reject", Rules: []map[string]any{{"ip_cidr": []string{"0.0.0.0/0"}}}}}}
|
|
data, err := c.managedJSON()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err = sjson.UnmarshalExtendedContext[option.Options](registryContext(context.Background()), data); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var result map[string]any
|
|
json.Unmarshal(data, &result)
|
|
route := result["route"].(map[string]any)
|
|
if route["final"] != "proxy" {
|
|
t.Fatal("preset did not force proxy fallback")
|
|
}
|
|
sets := route["rule_set"].([]any)
|
|
set := sets[0].(map[string]any)
|
|
if len(sets) != 1 || set["tag"] != "meshport-geoip-cn" || set["format"] != "binary" || set["download_detour"] != "proxy" || set["update_interval"] != "24h" {
|
|
t.Fatal("managed GeoIP update missing")
|
|
}
|
|
rules := route["rules"].([]any)
|
|
// DNS hijack/real-IP resolution precede mesh, withdrawn-prefix protection,
|
|
// ordinary private addresses and finally the China IP rule.
|
|
if len(rules) != 6 || rules[1].(map[string]any)["action"] != "resolve" || rules[2].(map[string]any)["outbound"] != "meshport" || rules[3].(map[string]any)["action"] != "reject" || rules[4].(map[string]any)["ip_is_private"] != true || rules[5].(map[string]any)["outbound"] != "direct" {
|
|
t.Fatal("GeoIP or mesh priority broken")
|
|
}
|
|
if len(c.Rules) != 1 || c.RuleSets[0].Tag != "custom" {
|
|
t.Fatal("preset modified saved custom rules")
|
|
}
|
|
c.Mode = "global"
|
|
data, err = c.managedJSON()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
json.Unmarshal(data, &result)
|
|
route = result["route"].(map[string]any)
|
|
if _, ok := route["rule_set"]; ok {
|
|
t.Fatal("global mode still loads GeoIP")
|
|
}
|
|
if len(route["rules"].([]any)) != 4 || route["final"] != "proxy" {
|
|
t.Fatal("global mode contains ordinary bypass rules")
|
|
}
|
|
c.Mode = "bypass_cn"
|
|
c.Proxy = nil
|
|
if _, err = c.managedJSON(); err == nil {
|
|
t.Fatal("preset accepted missing proxy node")
|
|
}
|
|
c.Feature = "mesh"
|
|
c.MeshDNS = nil
|
|
if _, err = c.managedJSON(); fake && err == nil {
|
|
t.Fatal("mesh-only accepted FakeIP")
|
|
} else if !fake && err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
}
|
|
|
|
// Use documentation IPs and isolated SOCKS counters in place of physical
|
|
// exits, so real-IP/FakeIP policy is exercised without public connections.
|
|
func TestBypassChinaRealIPAndFakeIPRouting(t *testing.T) {
|
|
for _, fake := range []bool{false, true} {
|
|
direct, dc := socksEcho(t)
|
|
defer direct.Close()
|
|
proxy, pc := socksEcho(t)
|
|
defer proxy.Close()
|
|
socket, err := net.ListenPacket("udp4", "127.0.0.1:0")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
server := &D.Server{PacketConn: socket, Handler: D.HandlerFunc(func(w D.ResponseWriter, r *D.Msg) {
|
|
m := new(D.Msg)
|
|
m.SetReply(r)
|
|
ip := "203.0.113.7"
|
|
if r.Question[0].Name == "china.test." {
|
|
ip = "198.51.100.7"
|
|
}
|
|
if r.Question[0].Qtype == D.TypeA {
|
|
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(ip)}}
|
|
}
|
|
w.WriteMsg(m)
|
|
})}
|
|
go server.ActivateAndServe()
|
|
defer server.Shutdown()
|
|
listener, port := reservePort(t)
|
|
listener.Close()
|
|
c := Config{ListenPort: port, Feature: "proxy", Mode: "bypass_cn", FakeIP: fake, DNS: socket.LocalAddr().String(), 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)
|
|
}
|
|
var generated map[string]any
|
|
json.Unmarshal(data, &generated)
|
|
route := generated["route"].(map[string]any)
|
|
route["rule_set"] = []any{map[string]any{"type": "inline", "tag": "meshport-geoip-cn", "rules": []any{map[string]any{"ip_cidr": []string{"198.51.100.0/24"}}}}}
|
|
outbounds := generated["outbounds"].([]any)
|
|
outbounds[0] = map[string]any{"type": "socks", "tag": "direct", "server": "127.0.0.1", "server_port": direct.Addr().(*net.TCPAddr).Port}
|
|
data, _ = json.Marshal(generated)
|
|
ctx := registryContext(context.Background())
|
|
options, err := sjson.UnmarshalExtendedContext[option.Options](ctx, data)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
instance, err := box.New(box.Options{Context: ctx, Options: options})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err = instance.Start(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, domain := range []string{"china.test", "foreign.test"} {
|
|
target := domain
|
|
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)
|
|
}
|
|
dnsConn := &D.Conn{Conn: conn}
|
|
msg := new(D.Msg)
|
|
msg.SetQuestion(D.Fqdn(domain), D.TypeA)
|
|
if err = dnsConn.WriteMsg(msg); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
answer, err := dnsConn.ReadMsg()
|
|
conn.Close()
|
|
if err != nil || len(answer.Answer) != 1 {
|
|
t.Fatalf("FakeIP answer: %v / %v", answer, err)
|
|
}
|
|
target = answer.Answer[0].(*D.A).A.String()
|
|
}
|
|
echoThrough(t, port, M.ParseSocksaddr(net.JoinHostPort(target, "443")), true)
|
|
}
|
|
if dc.Load() != 1 || pc.Load() != 1 {
|
|
t.Fatalf("fake=%v direct=%d proxy=%d", fake, dc.Load(), pc.Load())
|
|
}
|
|
instance.Close()
|
|
}
|
|
}
|