129 lines
4.5 KiB
Go
129 lines
4.5 KiB
Go
package bridge
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net"
|
|
"testing"
|
|
|
|
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 TestMeshPoliciesNeverFallBackToInternet(t *testing.T) {
|
|
for _, mode := range []string{"rule", "bypass_cn"} {
|
|
for _, state := range []string{"active", "withdrawn", "proxy-only", "mesh-default"} {
|
|
if mode == "bypass_cn" && state == "mesh-default" {
|
|
continue
|
|
}
|
|
t.Run(mode+"/"+state, func(t *testing.T) {
|
|
mesh, mc := socksEcho(t)
|
|
defer mesh.Close()
|
|
proxy, pc := socksEcho(t)
|
|
defer proxy.Close()
|
|
direct, dc := socksEcho(t)
|
|
defer direct.Close()
|
|
dnsSocket, err := net.ListenPacket("udp4", "127.0.0.1:0")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
dnsServer := &D.Server{PacketConn: dnsSocket, Handler: D.HandlerFunc(func(w D.ResponseWriter, r *D.Msg) {
|
|
m := new(D.Msg)
|
|
m.SetReply(r)
|
|
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("203.0.113.33")}}
|
|
}
|
|
w.WriteMsg(m)
|
|
})}
|
|
go dnsServer.ActivateAndServe()
|
|
defer dnsServer.Shutdown()
|
|
entry, port := reservePort(t)
|
|
entry.Close()
|
|
c := Config{ListenPort: port, Feature: "combined", Mode: mode, DefaultAction: "direct", DNS: dnsSocket.LocalAddr().String(),
|
|
MeshSOCKS: mesh.Addr().String(), MeshCIDRs: []string{"10.77.0.0/24"}, 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},
|
|
Rules: []Rule{{Type: "ip_cidr", Value: "10.77.0.0/24", Action: "mesh"}, {Type: "ip_cidr", Value: "10.44.0.0/24", Action: "mesh"}, {Type: "domain_suffix", Value: "mesh-policy.test", Action: "mesh"}},
|
|
RuleSets: []RuleSet{{Tag: "group-subnets", Enabled: true, Action: "mesh", Rules: []map[string]any{{"ip_cidr": []string{"10.55.0.0/24"}}}}},
|
|
}
|
|
if state == "withdrawn" {
|
|
c.MeshCIDRs = nil
|
|
}
|
|
if state == "proxy-only" {
|
|
c.Feature = "proxy"
|
|
c.MeshCIDRs = nil
|
|
c.ProtectedCIDRs = nil
|
|
}
|
|
if state == "mesh-default" {
|
|
c.DefaultAction = "mesh"
|
|
}
|
|
data, err := c.managedJSON()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var generated map[string]any
|
|
if err = json.Unmarshal(data, &generated); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
route := generated["route"].(map[string]any)
|
|
if sets, ok := route["rule_set"].([]any); ok {
|
|
for i, set := range sets {
|
|
if set.(map[string]any)["tag"] == "meshport-geoip-cn" {
|
|
sets[i] = map[string]any{"type": "inline", "tag": "meshport-geoip-cn", "rules": []any{map[string]any{"ip_cidr": []string{"198.51.100.0/24"}}}}
|
|
}
|
|
}
|
|
}
|
|
for i, out := range generated["outbounds"].([]any) {
|
|
if out.(map[string]any)["tag"] == "direct" {
|
|
generated["outbounds"].([]any)[i] = 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)
|
|
}
|
|
defer instance.Close()
|
|
if err = instance.Start(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
active := state == "active" || state == "mesh-default"
|
|
echoThrough(t, port, M.ParseSocksaddr("10.77.0.8:443"), active)
|
|
for _, target := range []string{"10.44.0.8:443", "10.55.0.8:443", "host.mesh-policy.test:443"} {
|
|
echoThrough(t, port, M.ParseSocksaddr(target), false)
|
|
}
|
|
echoThrough(t, port, M.ParseSocksaddr("203.0.113.9:443"), state != "mesh-default")
|
|
wantMesh := int32(0)
|
|
if active {
|
|
wantMesh = 1
|
|
}
|
|
wantProxy, wantDirect := int32(0), int32(0)
|
|
if state != "mesh-default" {
|
|
if mode == "bypass_cn" {
|
|
wantProxy = 1
|
|
} else {
|
|
wantDirect = 1
|
|
}
|
|
}
|
|
if mc.Load() != wantMesh || pc.Load() != wantProxy || dc.Load() != wantDirect {
|
|
t.Fatalf("unexpected outlet: mesh=%d proxy=%d direct=%d", mc.Load(), pc.Load(), dc.Load())
|
|
}
|
|
if active {
|
|
mesh.Close()
|
|
echoThrough(t, port, M.ParseSocksaddr("10.77.0.8:443"), false)
|
|
if pc.Load() != wantProxy || dc.Load() != wantDirect {
|
|
t.Fatal("unavailable mesh bridge fell back to internet")
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
}
|