From 3d312ddcd9139552eb901f6f4716640daf7bbb99 Mon Sep 17 00:00:00 2001 From: Amir Hakimnejad Date: Thu, 5 Nov 2026 00:50:28 +0330 Subject: [PATCH] sub: keep max_early_data in Clash ws options (#1335) * sub: keep max_early_data in Clash ws options The Clash output copied early_data_header_name but not max_early_data, so Clash clients never sent early data. * sub: read max_early_data the way link generation does --- sub/clashService.go | 4 ++++ sub/clash_early_data_test.go | 38 ++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 sub/clash_early_data_test.go diff --git a/sub/clashService.go b/sub/clashService.go index 8eddc64..cfde007 100644 --- a/sub/clashService.go +++ b/sub/clashService.go @@ -351,6 +351,10 @@ func (s *ClashService) ConvertToClashMeta(outbounds *[]map[string]interface{}, b if ed, ok := transport["early_data_header_name"].(string); ok { wsOpts["early-data-header-name"] = ed } + // Without max-early-data the client ignores the header name. + if maxED, ok := transport["max_early_data"].(float64); ok && maxED > 0 { + wsOpts["max-early-data"] = int(maxED) + } if tt == "httpupgrade" { wsOpts["v2ray-http-upgrade"] = true } diff --git a/sub/clash_early_data_test.go b/sub/clash_early_data_test.go new file mode 100644 index 0000000..e665f6b --- /dev/null +++ b/sub/clash_early_data_test.go @@ -0,0 +1,38 @@ +package sub + +import ( + "encoding/json" + "testing" + + "gopkg.in/yaml.v3" +) + +func TestClashWsKeepsMaxEarlyData(t *testing.T) { + openClashTestSettings(t) + var outbound map[string]interface{} + err := json.Unmarshal([]byte(`{"type": "vless", "tag": "ws", "server": "192.0.2.1", "server_port": 443, "uuid": "00000000-0000-4000-8000-000000000001", "tls": {"enabled": true, "server_name": "ws.example"}, "transport": {"type": "ws", "path": "/socket", "max_early_data": 2048, "early_data_header_name": "Sec-WebSocket-Protocol"}}`), &outbound) + if err != nil { + t.Fatal(err) + } + outbounds := []map[string]interface{}{outbound} + result, err := (&ClashService{}).ConvertToClashMeta(&outbounds, "mode: rule\n") + if err != nil { + t.Fatal(err) + } + var config struct { + Proxies []map[string]interface{} `yaml:"proxies"` + } + if err := yaml.Unmarshal([]byte(result), &config); err != nil { + t.Fatal(err) + } + if len(config.Proxies) != 1 { + t.Fatalf("got %d proxies, want one", len(config.Proxies)) + } + wsOpts, _ := config.Proxies[0]["ws-opts"].(map[string]interface{}) + if wsOpts["max-early-data"] != 2048 { + t.Errorf("max-early-data = %v, want 2048", wsOpts["max-early-data"]) + } + if wsOpts["early-data-header-name"] != "Sec-WebSocket-Protocol" { + t.Errorf("early-data-header-name = %v", wsOpts["early-data-header-name"]) + } +}