214 lines
8.6 KiB
Go
214 lines
8.6 KiB
Go
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"
|
|
"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[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,
|
|
// 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" {
|
|
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} {
|
|
mesh, mc := socksEcho(t)
|
|
defer mesh.Close()
|
|
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." || 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)}}
|
|
}
|
|
w.WriteMsg(m)
|
|
})}
|
|
go server.ActivateAndServe()
|
|
defer server.Shutdown()
|
|
listener, port := reservePort(t)
|
|
listener.Close()
|
|
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)
|
|
}
|
|
var generated map[string]any
|
|
json.Unmarshal(data, &generated)
|
|
route := generated["route"].(map[string]any)
|
|
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)
|
|
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)
|
|
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", "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))))
|
|
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")), domain != "blocked.test")
|
|
}
|
|
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)
|
|
}
|
|
}
|