feat: support custom bypass rules and mesh routing policies
This commit is contained in:
@@ -3,6 +3,7 @@ package bridge
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
D "github.com/miekg/dns"
|
||||
box "github.com/sagernet/sing-box"
|
||||
M "github.com/sagernet/sing/common/metadata"
|
||||
@@ -38,14 +39,14 @@ func TestBypassChinaPresetAndGlobalIsolation(t *testing.T) {
|
||||
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" {
|
||||
set := sets[1].(map[string]any)
|
||||
if len(sets) != 2 || sets[0].(map[string]any)["tag"] != "custom" || 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" {
|
||||
// custom rules/sets, ordinary private addresses and finally the China IP rule.
|
||||
if len(rules) != 8 || 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)["action"] != "reject" || rules[5].(map[string]any)["rule_set"].([]any)[0] != "custom" || rules[6].(map[string]any)["ip_is_private"] != true || rules[7].(map[string]any)["outbound"] != "direct" {
|
||||
t.Fatal("GeoIP or mesh priority broken")
|
||||
}
|
||||
if len(c.Rules) != 1 || c.RuleSets[0].Tag != "custom" {
|
||||
@@ -83,6 +84,8 @@ func TestBypassChinaPresetAndGlobalIsolation(t *testing.T) {
|
||||
// exits, so real-IP/FakeIP policy is exercised without public connections.
|
||||
func TestBypassChinaRealIPAndFakeIPRouting(t *testing.T) {
|
||||
for _, fake := range []bool{false, true} {
|
||||
mesh, mc := socksEcho(t)
|
||||
defer mesh.Close()
|
||||
direct, dc := socksEcho(t)
|
||||
defer direct.Close()
|
||||
proxy, pc := socksEcho(t)
|
||||
@@ -95,9 +98,12 @@ func TestBypassChinaRealIPAndFakeIPRouting(t *testing.T) {
|
||||
m := new(D.Msg)
|
||||
m.SetReply(r)
|
||||
ip := "203.0.113.7"
|
||||
if r.Question[0].Name == "china.test." {
|
||||
if r.Question[0].Name == "china.test." || r.Question[0].Name == "china-proxy.test." || r.Question[0].Name == "blocked.test." {
|
||||
ip = "198.51.100.7"
|
||||
}
|
||||
if r.Question[0].Name == "private-proxy.test." {
|
||||
ip = "10.99.0.1"
|
||||
}
|
||||
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)}}
|
||||
}
|
||||
@@ -107,7 +113,17 @@ func TestBypassChinaRealIPAndFakeIPRouting(t *testing.T) {
|
||||
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}}
|
||||
c := Config{ListenPort: port, Feature: "combined", MeshSOCKS: mesh.Addr().String(), MeshCIDRs: []string{"10.77.0.0/24"}, ProtectedCIDRs: []string{"10.88.0.0/24"}, 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}}
|
||||
c.Rules = []Rule{
|
||||
{Type: "ip_cidr", Value: "10.77.0.0/24", Action: "direct"},
|
||||
{Type: "ip_cidr", Value: "10.88.0.0/24", Action: "proxy"},
|
||||
{Type: "domain", Value: "china-proxy.test", Action: "proxy"},
|
||||
{Type: "domain_suffix", Value: "foreign-direct.test", Action: "direct"},
|
||||
{Type: "domain", Value: "private-proxy.test", Action: "proxy"},
|
||||
{Type: "domain", Value: "blocked.test", Action: "reject"},
|
||||
{Type: "ip_cidr", Value: "198.51.100.8/32", Action: "proxy"},
|
||||
}
|
||||
c.RuleSets = []RuleSet{{Tag: "user-direct", Enabled: true, Action: "direct", Rules: []map[string]any{{"domain": []string{"set-direct.test"}}}}}
|
||||
data, err := c.managedJSON()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
@@ -115,9 +131,17 @@ func TestBypassChinaRealIPAndFakeIPRouting(t *testing.T) {
|
||||
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"}}}}}
|
||||
for i, set := range route["rule_set"].([]any) {
|
||||
if set.(map[string]any)["tag"] == "meshport-geoip-cn" {
|
||||
route["rule_set"].([]any)[i] = 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}
|
||||
for i, outbound := range outbounds {
|
||||
if outbound.(map[string]any)["tag"] == "direct" {
|
||||
outbounds[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)
|
||||
@@ -131,7 +155,7 @@ func TestBypassChinaRealIPAndFakeIPRouting(t *testing.T) {
|
||||
if err = instance.Start(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, domain := range []string{"china.test", "foreign.test"} {
|
||||
for _, domain := range []string{"china.test", "foreign.test", "china-proxy.test", "foreign-direct.test", "private-proxy.test", "set-direct.test", "blocked.test"} {
|
||||
target := domain
|
||||
if fake {
|
||||
conn, err := net.Dial("tcp4", net.JoinHostPort("127.0.0.1", jsonNumber(int(port))))
|
||||
@@ -156,11 +180,34 @@ func TestBypassChinaRealIPAndFakeIPRouting(t *testing.T) {
|
||||
}
|
||||
target = answer.Answer[0].(*D.A).A.String()
|
||||
}
|
||||
echoThrough(t, port, M.ParseSocksaddr(net.JoinHostPort(target, "443")), true)
|
||||
echoThrough(t, port, M.ParseSocksaddr(net.JoinHostPort(target, "443")), domain != "blocked.test")
|
||||
}
|
||||
if dc.Load() != 1 || pc.Load() != 1 {
|
||||
echoThrough(t, port, M.ParseSocksaddr("198.51.100.8:443"), true)
|
||||
echoThrough(t, port, M.ParseSocksaddr("10.77.0.8:443"), true)
|
||||
echoThrough(t, port, M.ParseSocksaddr("10.88.0.8:443"), false)
|
||||
if dc.Load() != 3 || pc.Load() != 4 || mc.Load() != 1 {
|
||||
t.Fatalf("fake=%v direct=%d proxy=%d", fake, dc.Load(), pc.Load())
|
||||
}
|
||||
instance.Close()
|
||||
}
|
||||
}
|
||||
|
||||
func TestBypassCustomRuleValidationAndReservedTag(t *testing.T) {
|
||||
c := Config{ListenPort: 12345, Feature: "proxy", Mode: "bypass_cn", Proxy: map[string]any{"type": "socks", "server": "127.0.0.1", "server_port": 12346}}
|
||||
c.Rules = []Rule{{Type: "domain", Value: "*.example.com", Action: "direct"}}
|
||||
if _, err := c.managedJSON(); err == nil {
|
||||
t.Fatal("invalid custom rule was ignored")
|
||||
}
|
||||
c.Rules = nil
|
||||
c.RuleSets = []RuleSet{{Tag: "meshport-geoip-cn", Enabled: true, Action: "proxy", Rules: []map[string]any{{"domain": []string{"example.com"}}}}}
|
||||
if _, err := c.managedJSON(); err == nil {
|
||||
t.Fatal("reserved GeoIP tag accepted")
|
||||
}
|
||||
c.RuleSets = nil
|
||||
for i := 0; i < 32; i++ {
|
||||
c.RuleSets = append(c.RuleSets, RuleSet{Tag: fmt.Sprintf("user-%d", i), Enabled: true, Action: "direct", Rules: []map[string]any{{"domain": []string{"example.com"}}}})
|
||||
}
|
||||
if _, err := c.managedJSON(); err != nil {
|
||||
t.Fatal("managed set consumed a user slot", err)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user