Improve VPN video routing, profile storage and authorization reuse
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
||||
"net/netip"
|
||||
"net/url"
|
||||
"os"
|
||||
"runtime"
|
||||
"sort"
|
||||
"strings"
|
||||
)
|
||||
@@ -217,6 +218,10 @@ func resolver(value, tag, detour string) (map[string]any, error) {
|
||||
return result, nil
|
||||
}
|
||||
func (c Config) managedJSON() ([]byte, error) {
|
||||
return c.managedJSONForPlatform(runtime.GOOS)
|
||||
}
|
||||
|
||||
func (c Config) managedJSONForPlatform(platform string) ([]byte, error) {
|
||||
feature := c.Feature
|
||||
if feature == "" {
|
||||
feature = "mesh"
|
||||
@@ -515,6 +520,14 @@ func (c Config) managedJSON() ([]byte, error) {
|
||||
if c.FakeIP && !internet {
|
||||
return nil, errors.New("FakeIP requires ordinary proxy feature")
|
||||
}
|
||||
if internet && final == "proxy" && (bypassCN || mode == "global") && c.preferVideoTCPOn(platform) {
|
||||
// Keep mesh, private, GeoIP and explicit routes ahead of this policy.
|
||||
// Detect QUIC first: other UDP/443 traffic must remain usable.
|
||||
routeRules = append(routeRules,
|
||||
map[string]any{"network": "udp", "port": 443, "action": "sniff", "sniffer": []string{"quic"}},
|
||||
map[string]any{"network": "udp", "port": 443, "protocol": "quic", "domain_suffix": []string{"googlevideo.com"}, "action": "reject"},
|
||||
)
|
||||
}
|
||||
inbounds := []any{map[string]any{"type": "socks", "tag": "local", "listen": "127.0.0.1", "listen_port": c.ListenPort}}
|
||||
route := map[string]any{"rules": routeRules, "final": final}
|
||||
if len(ruleSets) > 0 {
|
||||
|
||||
@@ -0,0 +1,85 @@
|
||||
package bridge
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
M "github.com/sagernet/sing/common/metadata"
|
||||
"github.com/sagernet/sing/protocol/socks"
|
||||
"github.com/sagernet/sing/protocol/socks/socks5"
|
||||
)
|
||||
|
||||
// Isolate managed SOCKS forwarding from WAN, DNS, TLS and privileged TUN.
|
||||
// This is not a benchmark of the system VPN or a user's real proxy node.
|
||||
func BenchmarkManagedDownload(b *testing.B) {
|
||||
const size = 32 * 1024 * 1024
|
||||
chunk := make([]byte, 64*1024)
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
for remaining := size; remaining > 0; remaining -= len(chunk) {
|
||||
if _, err := w.Write(chunk); err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
}))
|
||||
defer server.Close()
|
||||
for _, managed := range []bool{false, true} {
|
||||
name := "direct"
|
||||
transport := &http.Transport{DisableCompression: true}
|
||||
var engine Engine
|
||||
if managed {
|
||||
name = "managed_socks"
|
||||
reserved, err := net.Listen("tcp4", "127.0.0.1:0")
|
||||
if err != nil {
|
||||
b.Fatal(err)
|
||||
}
|
||||
port := uint16(reserved.Addr().(*net.TCPAddr).Port)
|
||||
entry := reserved.Addr().String()
|
||||
reserved.Close()
|
||||
if err = engine.Start(context.Background(), Config{Feature: "proxy", Mode: "direct", ListenPort: port}); err != nil {
|
||||
b.Fatal(err)
|
||||
}
|
||||
transport.DialContext = func(ctx context.Context, network, address string) (net.Conn, error) {
|
||||
c, err := (&net.Dialer{}).DialContext(ctx, "tcp", entry)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
_ = c.SetDeadline(time.Now().Add(10 * time.Second))
|
||||
if _, err = socks.ClientHandshake5(c, socks5.CommandConnect, M.ParseSocksaddr(address), "", ""); err != nil {
|
||||
c.Close()
|
||||
return nil, err
|
||||
}
|
||||
_ = c.SetDeadline(time.Time{})
|
||||
return c, nil
|
||||
}
|
||||
}
|
||||
b.Run(name, func(b *testing.B) {
|
||||
client := &http.Client{Transport: transport, Timeout: 20 * time.Second}
|
||||
b.SetBytes(size)
|
||||
b.ReportAllocs()
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
r, err := client.Get(server.URL)
|
||||
if err != nil {
|
||||
b.Fatal(err)
|
||||
}
|
||||
n, err := io.Copy(io.Discard, r.Body)
|
||||
r.Body.Close()
|
||||
if err != nil {
|
||||
b.Fatal(err)
|
||||
}
|
||||
if n != size {
|
||||
b.Fatalf("short download: %d", n)
|
||||
}
|
||||
}
|
||||
})
|
||||
transport.CloseIdleConnections()
|
||||
if managed {
|
||||
_ = engine.Close()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package bridge
|
||||
|
||||
// The measured macOS VLESS/Reality Vision path benefits from video TCP fallback.
|
||||
// Share the narrowly scoped policy with Android's platform-owned TUN. Native
|
||||
// UDP proxies, custom routing policies and unrelated QUIC remain unchanged.
|
||||
func (c Config) preferVideoTCPOn(platform string) bool {
|
||||
tun := (platform == "darwin" && c.DesktopTun) || (platform == "android" && c.Tun != nil)
|
||||
if !tun || c.Proxy == nil ||
|
||||
c.Proxy["type"] != "vless" || c.Proxy["flow"] != "xtls-rprx-vision" || c.Proxy["transport"] != nil {
|
||||
return false
|
||||
}
|
||||
tls, ok := c.Proxy["tls"].(map[string]any)
|
||||
if !ok || tls["enabled"] != true {
|
||||
return false
|
||||
}
|
||||
reality, ok := tls["reality"].(map[string]any)
|
||||
return ok && reality["enabled"] == true
|
||||
}
|
||||
@@ -0,0 +1,121 @@
|
||||
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")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user