122 lines
3.9 KiB
Go
122 lines
3.9 KiB
Go
package bridge
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/sagernet/sing-box/adapter"
|
|
"github.com/sagernet/sing-box/log"
|
|
"github.com/sagernet/sing-box/option"
|
|
R "github.com/sagernet/sing-box/route/rule"
|
|
sjson "github.com/sagernet/sing/common/json"
|
|
M "github.com/sagernet/sing/common/metadata"
|
|
)
|
|
|
|
func videoConfig() Config {
|
|
return Config{ListenPort: 12345, Feature: "proxy", Mode: "global", DesktopTun: true, PhysicalInterface: "en0",
|
|
Proxy: map[string]any{"type": "vless", "server": "proxy.example", "server_port": 443,
|
|
"uuid": "00000000-0000-4000-8000-000000000001", "flow": "xtls-rprx-vision",
|
|
"tls": map[string]any{"enabled": true, "server_name": "example.com",
|
|
"reality": map[string]any{"enabled": true, "public_key": strings.Repeat("A", 43)}}}}
|
|
}
|
|
|
|
func TestVideoFallbackMatchesOnlyYouTubeQUIC(t *testing.T) {
|
|
for _, platform := range []string{"darwin", "android"} {
|
|
for _, mode := range []string{"global", "bypass_cn"} {
|
|
t.Run(platform+"/"+mode, func(t *testing.T) {
|
|
c := videoConfig()
|
|
c.Mode = mode
|
|
if platform == "android" {
|
|
c.DesktopTun = false
|
|
c.Tun = &TunConfig{FD: 42, Name: "tun0", Interface: "wlan0", Index: 1}
|
|
}
|
|
checkVideoRule(t, c, platform)
|
|
})
|
|
}
|
|
}
|
|
}
|
|
|
|
func checkVideoRule(t *testing.T, c Config, platform string) {
|
|
t.Helper()
|
|
data, err := c.managedJSONForPlatform(platform)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
ctx := registryContext(context.Background())
|
|
opts, err := sjson.UnmarshalExtendedContext[option.Options](ctx, data)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
rules := opts.Route.Rules
|
|
if len(rules) < 2 || rules[len(rules)-2].DefaultOptions.Action != "sniff" {
|
|
t.Fatal("QUIC detection missing before fallback")
|
|
}
|
|
if _, err := R.NewRule(ctx, log.NewNOPFactory().Logger(), rules[len(rules)-2], true); err != nil {
|
|
t.Fatalf("QUIC sniffer unavailable: %v", err)
|
|
}
|
|
reject, err := R.NewRule(ctx, log.NewNOPFactory().Logger(), rules[len(rules)-1], true)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, tc := range []struct {
|
|
domain, protocol, network string
|
|
port uint16
|
|
want bool
|
|
}{
|
|
{"rr3---cdn.googlevideo.com", "quic", "udp", 443, true},
|
|
{"googlevideo.com", "quic", "udp", 443, true},
|
|
{"other.example", "quic", "udp", 443, false},
|
|
{"evilgooglevideo.com", "quic", "udp", 443, false},
|
|
{"rr3---cdn.googlevideo.com", "stun", "udp", 443, false},
|
|
{"rr3---cdn.googlevideo.com", "", "udp", 443, false},
|
|
{"rr3---cdn.googlevideo.com", "tls", "tcp", 443, false},
|
|
{"rr3---cdn.googlevideo.com", "quic", "udp", 8443, false},
|
|
} {
|
|
m := adapter.InboundContext{Domain: tc.domain, Protocol: tc.protocol, Network: tc.network,
|
|
Destination: M.Socksaddr{Fqdn: tc.domain, Port: tc.port}}
|
|
if got := reject.Match(&m); got != tc.want {
|
|
t.Fatalf("%+v: match=%v", tc, got)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestVideoFallbackDoesNotChangeOtherTransportsOrModes(t *testing.T) {
|
|
for _, mutate := range []func(*Config){
|
|
func(c *Config) { c.DesktopTun = false },
|
|
func(c *Config) { c.Mode = "direct" },
|
|
func(c *Config) { c.Mode = "rule" },
|
|
func(c *Config) { c.Proxy["flow"] = "" },
|
|
func(c *Config) { c.Proxy["tls"].(map[string]any)["reality"].(map[string]any)["enabled"] = false },
|
|
} {
|
|
c := videoConfig()
|
|
mutate(&c)
|
|
data, err := c.managedJSON()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if strings.Contains(string(data), `"sniffer"`) {
|
|
t.Fatal("fallback changed unrelated mode/transport")
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestAndroidVideoPolicyRequiresPlatformTunAndVision(t *testing.T) {
|
|
c := videoConfig()
|
|
c.DesktopTun = false
|
|
if c.preferVideoTCPOn("android") {
|
|
t.Fatal("application-only proxy enabled TUN policy")
|
|
}
|
|
c.Tun = &TunConfig{FD: 42, Name: "tun0", Interface: "wlan0", Index: 1}
|
|
if !c.preferVideoTCPOn("android") {
|
|
t.Fatal("Android platform TUN missing video policy")
|
|
}
|
|
if c.preferVideoTCPOn("linux") || c.preferVideoTCPOn("darwin") {
|
|
t.Fatal("policy leaked to other platforms")
|
|
}
|
|
c.Proxy["flow"] = ""
|
|
if c.preferVideoTCPOn("android") {
|
|
t.Fatal("non-Vision transport changed")
|
|
}
|
|
}
|