subService_test.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781
  1. package sub
  2. import (
  3. "encoding/base64"
  4. "encoding/json"
  5. "strings"
  6. "testing"
  7. "github.com/mhsanaei/3x-ui/v3/database/model"
  8. )
  9. func TestSubscriptionExpiryFromClient(t *testing.T) {
  10. const now = int64(1_700_000_000_000)
  11. const oneDayMs = int64(86_400_000)
  12. if got := subscriptionExpiryFromClient(now, 0); got != 0 {
  13. t.Fatalf("zero expiry should stay zero, got %d", got)
  14. }
  15. if got := subscriptionExpiryFromClient(now, 1_700_000_000_000); got != 1_700_000_000_000 {
  16. t.Fatalf("positive expiry should pass through, got %d", got)
  17. }
  18. if got := subscriptionExpiryFromClient(now, -oneDayMs); got != now+oneDayMs {
  19. t.Fatalf("delayed-start expiry should be now+|value|, got %d, want %d", got, now+oneDayMs)
  20. }
  21. if a, b := subscriptionExpiryFromClient(now, -oneDayMs), subscriptionExpiryFromClient(now, -oneDayMs); a != b {
  22. t.Fatalf("same now+value should be deterministic across calls, got %d vs %d (#4545 review)", a, b)
  23. }
  24. }
  25. func TestFindClientIndex(t *testing.T) {
  26. clients := []model.Client{
  27. {Email: "[email protected]"},
  28. {Email: "[email protected]"},
  29. {Email: "[email protected]"},
  30. }
  31. if got := findClientIndex(clients, "[email protected]"); got != 1 {
  32. t.Fatalf("findClientIndex middle = %d, want 1", got)
  33. }
  34. if got := findClientIndex(clients, "[email protected]"); got != 0 {
  35. t.Fatalf("findClientIndex first = %d, want 0", got)
  36. }
  37. if got := findClientIndex(clients, "[email protected]"); got != -1 {
  38. t.Fatalf("findClientIndex missing = %d, want -1", got)
  39. }
  40. if got := findClientIndex(nil, "x"); got != -1 {
  41. t.Fatalf("findClientIndex on nil slice = %d, want -1", got)
  42. }
  43. }
  44. func TestIsRoutableHost(t *testing.T) {
  45. routable := []string{"example.com", "sub.example.com", "10.0.0.1", "192.168.1.5", "1.2.3.4", "2001:db8::1"}
  46. for _, v := range routable {
  47. if !isRoutableHost(v) {
  48. t.Fatalf("isRoutableHost(%q) = false, want true", v)
  49. }
  50. }
  51. notRoutable := []string{"", "0.0.0.0", "::", "::0", "127.0.0.1", "127.0.0.2", "::1", "[::1]"}
  52. for _, v := range notRoutable {
  53. if isRoutableHost(v) {
  54. t.Fatalf("isRoutableHost(%q) = true, want false", v)
  55. }
  56. }
  57. }
  58. func TestResolveInboundAddress(t *testing.T) {
  59. const reqHost = "sub.example.com"
  60. // A subscriber reaches the panel through reqHost; the inbound's own
  61. // bind Listen IP (loopback, private, or even a public secondary IP) is
  62. // a server-side detail and must never become the link's connect host.
  63. t.Run("bind listen IP must not leak into the link host", func(t *testing.T) {
  64. s := &SubService{address: reqHost}
  65. for _, listen := range []string{"127.0.0.1", "10.0.0.5", "192.168.1.10", "1.2.3.4", "0.0.0.0", "::", "::0", ""} {
  66. ib := &model.Inbound{Listen: listen}
  67. if got := s.resolveInboundAddress(ib); got != reqHost {
  68. t.Fatalf("listen %q: address = %q, want %q (subscriber host, not bind IP)", listen, got, reqHost)
  69. }
  70. }
  71. })
  72. t.Run("node-managed inbound uses the node address", func(t *testing.T) {
  73. id := 7
  74. s := &SubService{
  75. address: reqHost,
  76. nodesByID: map[int]*model.Node{7: {Id: 7, Address: "node7.example.com"}},
  77. }
  78. ib := &model.Inbound{NodeID: &id, Listen: "1.2.3.4"}
  79. if got := s.resolveInboundAddress(ib); got != "node7.example.com" {
  80. t.Fatalf("node-managed address = %q, want node7.example.com", got)
  81. }
  82. })
  83. t.Run("node id with no known node falls back to subscriber host", func(t *testing.T) {
  84. id := 9
  85. s := &SubService{address: reqHost, nodesByID: map[int]*model.Node{}}
  86. ib := &model.Inbound{NodeID: &id, Listen: "10.0.0.1"}
  87. if got := s.resolveInboundAddress(ib); got != reqHost {
  88. t.Fatalf("unknown-node address = %q, want subscriber host %q", got, reqHost)
  89. }
  90. })
  91. }
  92. func TestUnmarshalStreamSettings(t *testing.T) {
  93. got := unmarshalStreamSettings(`{"network":"ws","wsSettings":{"path":"/api"}}`)
  94. if got["network"] != "ws" {
  95. t.Fatalf("network = %v, want ws", got["network"])
  96. }
  97. ws, ok := got["wsSettings"].(map[string]any)
  98. if !ok || ws["path"] != "/api" {
  99. t.Fatalf("wsSettings = %v, want map with path=/api", got["wsSettings"])
  100. }
  101. }
  102. func TestUnmarshalStreamSettings_InvalidJSON(t *testing.T) {
  103. if got := unmarshalStreamSettings("not json"); got != nil {
  104. t.Fatalf("invalid JSON should produce nil map, got %#v", got)
  105. }
  106. }
  107. func TestSearchHost_StringValue(t *testing.T) {
  108. headers := map[string]any{"Host": "example.com"}
  109. if got := searchHost(headers); got != "example.com" {
  110. t.Fatalf("searchHost = %q, want example.com", got)
  111. }
  112. }
  113. func TestSearchHost_CaseInsensitiveKey(t *testing.T) {
  114. headers := map[string]any{"host": "example.com"}
  115. if got := searchHost(headers); got != "example.com" {
  116. t.Fatalf("searchHost = %q, want example.com", got)
  117. }
  118. headers2 := map[string]any{"HOST": "example.com"}
  119. if got := searchHost(headers2); got != "example.com" {
  120. t.Fatalf("searchHost uppercase = %q, want example.com", got)
  121. }
  122. }
  123. func TestSearchHost_ArrayValue(t *testing.T) {
  124. headers := map[string]any{"Host": []any{"first.example.com", "second.example.com"}}
  125. if got := searchHost(headers); got != "first.example.com" {
  126. t.Fatalf("searchHost array = %q, want first.example.com", got)
  127. }
  128. }
  129. func TestSearchHost_EmptyArray(t *testing.T) {
  130. headers := map[string]any{"Host": []any{}}
  131. if got := searchHost(headers); got != "" {
  132. t.Fatalf("searchHost empty array = %q, want empty", got)
  133. }
  134. }
  135. func TestSearchHost_NoHostKey(t *testing.T) {
  136. headers := map[string]any{"X-Other": "value"}
  137. if got := searchHost(headers); got != "" {
  138. t.Fatalf("searchHost no host = %q, want empty", got)
  139. }
  140. }
  141. func TestSearchHost_NotAMap(t *testing.T) {
  142. if got := searchHost("not a map"); got != "" {
  143. t.Fatalf("searchHost non-map = %q, want empty", got)
  144. }
  145. if got := searchHost(nil); got != "" {
  146. t.Fatalf("searchHost nil = %q, want empty", got)
  147. }
  148. }
  149. func TestSearchKey_FoundAtTopLevel(t *testing.T) {
  150. data := map[string]any{"foo": 42, "bar": "x"}
  151. got, ok := searchKey(data, "foo")
  152. if !ok {
  153. t.Fatal("expected to find foo")
  154. }
  155. if got != 42 {
  156. t.Fatalf("got %v, want 42", got)
  157. }
  158. }
  159. func TestSearchKey_FoundInNested(t *testing.T) {
  160. data := map[string]any{
  161. "outer": map[string]any{
  162. "inner": map[string]any{
  163. "target": "hit",
  164. },
  165. },
  166. }
  167. got, ok := searchKey(data, "target")
  168. if !ok {
  169. t.Fatal("expected to find target in nested map")
  170. }
  171. if got != "hit" {
  172. t.Fatalf("got %v, want hit", got)
  173. }
  174. }
  175. func TestSearchKey_FoundInsideArray(t *testing.T) {
  176. data := map[string]any{
  177. "list": []any{
  178. map[string]any{"other": 1},
  179. map[string]any{"needle": "found"},
  180. },
  181. }
  182. got, ok := searchKey(data, "needle")
  183. if !ok {
  184. t.Fatal("expected to find needle in array element")
  185. }
  186. if got != "found" {
  187. t.Fatalf("got %v, want found", got)
  188. }
  189. }
  190. func TestSearchKey_NotFound(t *testing.T) {
  191. data := map[string]any{"foo": "bar"}
  192. if _, ok := searchKey(data, "missing"); ok {
  193. t.Fatal("expected ok=false for missing key")
  194. }
  195. }
  196. func TestSearchKey_OnScalar(t *testing.T) {
  197. if _, ok := searchKey(42, "anything"); ok {
  198. t.Fatal("expected ok=false searching on a scalar")
  199. }
  200. }
  201. func TestBuildXhttpExtra_IncludesClientSideFieldsWhenPresent(t *testing.T) {
  202. extra := buildXhttpExtra(map[string]any{
  203. "path": "/xhttp",
  204. "host": "example.com",
  205. "mode": "packet-up",
  206. "xPaddingBytes": "100-1000",
  207. "uplinkHTTPMethod": "GET",
  208. "uplinkChunkSize": float64(4096),
  209. "noGRPCHeader": true,
  210. "scMinPostsIntervalMs": "20-40",
  211. "xmux": map[string]any{
  212. "maxConcurrency": "16-32",
  213. "hMaxRequestTimes": "600-900",
  214. "hMaxReusableSecs": "1800-3000",
  215. "hKeepAlivePeriod": float64(15),
  216. },
  217. "downloadSettings": map[string]any{
  218. "network": "xhttp",
  219. },
  220. "headers": map[string]any{
  221. "Host": "ignored.example.com",
  222. "X-Forwarded": "1",
  223. "X-Test-Empty": "",
  224. },
  225. })
  226. if extra["path"] != nil || extra["host"] != nil {
  227. t.Fatalf("path/host should stay top-level, got extra %#v", extra)
  228. }
  229. for _, key := range []string{
  230. "xPaddingBytes",
  231. "uplinkHTTPMethod",
  232. "uplinkChunkSize",
  233. "noGRPCHeader",
  234. "scMinPostsIntervalMs",
  235. "xmux",
  236. "downloadSettings",
  237. } {
  238. if _, ok := extra[key]; !ok {
  239. t.Fatalf("extra missing %q: %#v", key, extra)
  240. }
  241. }
  242. if _, ok := extra["mode"]; ok {
  243. t.Fatalf("mode should stay as a top-level query parameter, got extra %#v", extra)
  244. }
  245. headers, ok := extra["headers"].(map[string]any)
  246. if !ok {
  247. t.Fatalf("headers = %#v, want map", extra["headers"])
  248. }
  249. if _, ok := headers["Host"]; ok {
  250. t.Fatalf("headers should not include Host: %#v", headers)
  251. }
  252. if headers["X-Forwarded"] != "1" {
  253. t.Fatalf("headers[X-Forwarded] = %#v, want 1", headers["X-Forwarded"])
  254. }
  255. }
  256. func TestBuildXhttpExtra_LeavesDefaultClientSideFieldsOut(t *testing.T) {
  257. extra := buildXhttpExtra(map[string]any{
  258. "uplinkHTTPMethod": "",
  259. "uplinkChunkSize": float64(0),
  260. "noGRPCHeader": false,
  261. "xmux": map[string]any{},
  262. "downloadSettings": map[string]any{},
  263. })
  264. if extra != nil {
  265. t.Fatalf("default-only xhttp extra = %#v, want nil", extra)
  266. }
  267. }
  268. func TestCloneStringMap(t *testing.T) {
  269. src := map[string]string{"a": "1", "b": "2"}
  270. dst := cloneStringMap(src)
  271. if len(dst) != len(src) {
  272. t.Fatalf("clone length = %d, want %d", len(dst), len(src))
  273. }
  274. for k, v := range src {
  275. if dst[k] != v {
  276. t.Fatalf("clone[%q] = %q, want %q", k, dst[k], v)
  277. }
  278. }
  279. dst["a"] = "changed"
  280. if src["a"] == "changed" {
  281. t.Fatal("modifying clone leaked into source")
  282. }
  283. }
  284. func TestCloneStringMap_Empty(t *testing.T) {
  285. dst := cloneStringMap(map[string]string{})
  286. if dst == nil {
  287. t.Fatal("clone of empty map should not be nil")
  288. }
  289. if len(dst) != 0 {
  290. t.Fatalf("clone of empty map should be empty, got %v", dst)
  291. }
  292. }
  293. func TestGetHostFromXFH_HostOnly(t *testing.T) {
  294. got, err := getHostFromXFH("example.com")
  295. if err != nil {
  296. t.Fatalf("unexpected error: %v", err)
  297. }
  298. if got != "example.com" {
  299. t.Fatalf("got %q, want example.com", got)
  300. }
  301. }
  302. func TestGetHostFromXFH_HostWithPort(t *testing.T) {
  303. got, err := getHostFromXFH("example.com:8443")
  304. if err != nil {
  305. t.Fatalf("unexpected error: %v", err)
  306. }
  307. if got != "example.com" {
  308. t.Fatalf("got %q, want example.com", got)
  309. }
  310. }
  311. func TestGetHostFromXFH_IPv6WithPort(t *testing.T) {
  312. got, err := getHostFromXFH("[2606:4700::1111]:443")
  313. if err != nil {
  314. t.Fatalf("unexpected error: %v", err)
  315. }
  316. if got != "2606:4700::1111" {
  317. t.Fatalf("got %q, want 2606:4700::1111", got)
  318. }
  319. }
  320. func TestGetHostFromXFH_BadHostPort(t *testing.T) {
  321. if _, err := getHostFromXFH("example.com:8443:9999"); err == nil {
  322. t.Fatal("expected error for malformed host:port")
  323. }
  324. }
  325. func TestReadPositiveInt(t *testing.T) {
  326. cases := []struct {
  327. name string
  328. in any
  329. wantVal int
  330. wantOk bool
  331. }{
  332. {"int_positive", int(5), 5, true},
  333. {"int_zero", int(0), 0, false},
  334. {"int_negative", int(-3), -3, false},
  335. {"int32_positive", int32(7), 7, true},
  336. {"int64_positive", int64(99), 99, true},
  337. {"float64_positive", float64(12), 12, true},
  338. {"float64_zero", float64(0.0), 0, false},
  339. {"float64_negative", float64(-1.5), -1, false},
  340. {"float32_positive", float32(3), 3, true},
  341. {"string", "not a number", 0, false},
  342. {"nil", nil, 0, false},
  343. }
  344. for _, c := range cases {
  345. t.Run(c.name, func(t *testing.T) {
  346. gotVal, gotOk := readPositiveInt(c.in)
  347. if gotVal != c.wantVal || gotOk != c.wantOk {
  348. t.Fatalf("readPositiveInt(%v) = (%d, %v), want (%d, %v)", c.in, gotVal, gotOk, c.wantVal, c.wantOk)
  349. }
  350. })
  351. }
  352. }
  353. func TestSetStringParam(t *testing.T) {
  354. p := map[string]string{"existing": "value"}
  355. setStringParam(p, "new", "hello")
  356. if p["new"] != "hello" {
  357. t.Fatalf("missing key after set: %v", p)
  358. }
  359. setStringParam(p, "existing", "")
  360. if _, ok := p["existing"]; ok {
  361. t.Fatalf("empty value should delete the key, got %v", p)
  362. }
  363. }
  364. func TestSetIntParam(t *testing.T) {
  365. p := map[string]string{"existing": "10"}
  366. setIntParam(p, "n", 42)
  367. if p["n"] != "42" {
  368. t.Fatalf("set positive int: got %v", p)
  369. }
  370. setIntParam(p, "existing", 0)
  371. if _, ok := p["existing"]; ok {
  372. t.Fatalf("zero value should delete the key, got %v", p)
  373. }
  374. p["other"] = "5"
  375. setIntParam(p, "other", -1)
  376. if _, ok := p["other"]; ok {
  377. t.Fatalf("negative value should delete the key, got %v", p)
  378. }
  379. }
  380. func TestSetStringField(t *testing.T) {
  381. f := map[string]any{"existing": "value"}
  382. setStringField(f, "new", "hello")
  383. if f["new"] != "hello" {
  384. t.Fatalf("missing key after set: %v", f)
  385. }
  386. setStringField(f, "existing", "")
  387. if _, ok := f["existing"]; ok {
  388. t.Fatalf("empty value should delete the key, got %v", f)
  389. }
  390. }
  391. func TestSetIntField(t *testing.T) {
  392. f := map[string]any{"existing": 10}
  393. setIntField(f, "n", 7)
  394. if f["n"] != 7 {
  395. t.Fatalf("set positive int: got %v", f)
  396. }
  397. setIntField(f, "existing", 0)
  398. if _, ok := f["existing"]; ok {
  399. t.Fatalf("zero value should delete the key, got %v", f)
  400. }
  401. }
  402. func TestBuildVmessLink(t *testing.T) {
  403. obj := map[string]any{
  404. "v": "2",
  405. "ps": "remark",
  406. "add": "example.com",
  407. "port": 443,
  408. "net": "tcp",
  409. }
  410. link := buildVmessLink(obj)
  411. if !strings.HasPrefix(link, "vmess://") {
  412. t.Fatalf("missing vmess:// prefix: %q", link)
  413. }
  414. payload := strings.TrimPrefix(link, "vmess://")
  415. decoded, err := base64.StdEncoding.DecodeString(payload)
  416. if err != nil {
  417. t.Fatalf("base64 decode failed: %v", err)
  418. }
  419. var roundTrip map[string]any
  420. if err := json.Unmarshal(decoded, &roundTrip); err != nil {
  421. t.Fatalf("decoded payload is not JSON: %v\n%s", err, decoded)
  422. }
  423. if roundTrip["add"] != "example.com" {
  424. t.Fatalf("round-trip add = %v, want example.com", roundTrip["add"])
  425. }
  426. if roundTrip["ps"] != "remark" {
  427. t.Fatalf("round-trip ps = %v, want remark", roundTrip["ps"])
  428. }
  429. }
  430. func TestCloneVmessShareObj_CopiesEverythingByDefault(t *testing.T) {
  431. base := map[string]any{
  432. "v": "2",
  433. "sni": "example.com",
  434. "alpn": "h2",
  435. "fp": "chrome",
  436. "net": "tcp",
  437. }
  438. out := cloneVmessShareObj(base, "tls")
  439. for _, key := range []string{"sni", "alpn", "fp", "net", "v"} {
  440. if _, ok := out[key]; !ok {
  441. t.Fatalf("expected key %q to be preserved when security=tls, got %v", key, out)
  442. }
  443. }
  444. }
  445. func TestCloneVmessShareObj_NoneStripsTLSOnlyKeys(t *testing.T) {
  446. base := map[string]any{
  447. "v": "2",
  448. "sni": "example.com",
  449. "alpn": "h2",
  450. "fp": "chrome",
  451. "net": "tcp",
  452. }
  453. out := cloneVmessShareObj(base, "none")
  454. for _, key := range []string{"sni", "alpn", "fp"} {
  455. if _, ok := out[key]; ok {
  456. t.Fatalf("security=none should strip %q, got %v", key, out)
  457. }
  458. }
  459. if out["v"] != "2" || out["net"] != "tcp" {
  460. t.Fatalf("non-TLS keys should remain, got %v", out)
  461. }
  462. }
  463. func TestApplyExternalProxyTLSParams_UsesProxyDomainAndOverrides(t *testing.T) {
  464. params := map[string]string{
  465. "security": "tls",
  466. "sni": "origin.example.com",
  467. "fp": "firefox",
  468. "alpn": "h2",
  469. }
  470. ep := map[string]any{
  471. "dest": "proxy.example.com",
  472. "sni": "tls.example.com",
  473. "fingerprint": "chrome",
  474. "alpn": []any{"h3", "h2"},
  475. }
  476. applyExternalProxyTLSParams(ep, params, "tls")
  477. if params["sni"] != "tls.example.com" {
  478. t.Fatalf("sni = %q, want tls.example.com", params["sni"])
  479. }
  480. if params["fp"] != "chrome" {
  481. t.Fatalf("fp = %q, want chrome", params["fp"])
  482. }
  483. if params["alpn"] != "h3,h2" {
  484. t.Fatalf("alpn = %q, want h3,h2", params["alpn"])
  485. }
  486. }
  487. func TestApplyExternalProxyTLSParams_PreservesUpstreamSNI(t *testing.T) {
  488. // External-proxy entry has no SNI of its own; its dest must not
  489. // clobber the upstream tlsSettings.serverName already written into
  490. // params. Regression: the dest fallback used to overwrite "222" with
  491. // "111" whenever an operator set forceTls=same and left the proxy's
  492. // SNI field blank.
  493. params := map[string]string{"security": "tls", "sni": "real.example.com"}
  494. ep := map[string]any{"dest": "proxy.example.com"}
  495. applyExternalProxyTLSParams(ep, params, "tls")
  496. if params["sni"] != "real.example.com" {
  497. t.Fatalf("sni = %q, want upstream sni preserved (real.example.com)", params["sni"])
  498. }
  499. }
  500. func TestApplyExternalProxyTLSParams_ExplicitSNIOverridesUpstream(t *testing.T) {
  501. params := map[string]string{"security": "tls", "sni": "real.example.com"}
  502. ep := map[string]any{"dest": "proxy.example.com", "sni": "edge.example.com"}
  503. applyExternalProxyTLSParams(ep, params, "tls")
  504. if params["sni"] != "edge.example.com" {
  505. t.Fatalf("sni = %q, want edge.example.com", params["sni"])
  506. }
  507. }
  508. func TestApplyExternalProxyTLSToStream_DoesNotLeakAcrossProxies(t *testing.T) {
  509. stream := map[string]any{
  510. "security": "tls",
  511. "tlsSettings": map[string]any{
  512. "serverName": "upstream.example.com",
  513. },
  514. }
  515. proxies := []map[string]any{
  516. {"dest": "a.example.com", "sni": "a-sni.example.com", "fingerprint": "chrome", "alpn": []any{"h3"}},
  517. {"dest": "b.example.com"},
  518. }
  519. results := make([]map[string]any, 0, len(proxies))
  520. for _, ep := range proxies {
  521. working := cloneStreamForExternalProxy(stream)
  522. applyExternalProxyTLSToStream(ep, working, "tls")
  523. ts := working["tlsSettings"].(map[string]any)
  524. snapshot := map[string]any{
  525. "serverName": ts["serverName"],
  526. "fingerprint": ts["fingerprint"],
  527. "alpn": ts["alpn"],
  528. }
  529. results = append(results, snapshot)
  530. }
  531. if results[0]["serverName"] != "a-sni.example.com" || results[0]["fingerprint"] != "chrome" {
  532. t.Fatalf("proxy A snapshot = %v", results[0])
  533. }
  534. // Proxy B has no SNI of its own — the upstream tlsSettings serverName
  535. // must remain in place (no dest fallback) and no fingerprint/alpn
  536. // must leak from proxy A.
  537. if results[1]["serverName"] != "upstream.example.com" {
  538. t.Fatalf("proxy B serverName = %v, want upstream.example.com preserved", results[1]["serverName"])
  539. }
  540. if results[1]["fingerprint"] != nil {
  541. t.Fatalf("proxy B should inherit no fingerprint, got %v (leaked from A)", results[1]["fingerprint"])
  542. }
  543. if results[1]["alpn"] != nil {
  544. t.Fatalf("proxy B should inherit no alpn, got %v (leaked from A)", results[1]["alpn"])
  545. }
  546. }
  547. func TestApplyExternalProxyTLSParams_DoesNotApplyForNone(t *testing.T) {
  548. params := map[string]string{
  549. "security": "none",
  550. "sni": "origin.example.com",
  551. }
  552. ep := map[string]any{
  553. "dest": "proxy.example.com",
  554. "fingerprint": "chrome",
  555. "alpn": []any{"h3"},
  556. }
  557. applyExternalProxyTLSParams(ep, params, "none")
  558. if params["sni"] != "origin.example.com" {
  559. t.Fatalf("sni should not change for security=none, got %q", params["sni"])
  560. }
  561. if _, ok := params["fp"]; ok {
  562. t.Fatalf("fp should not be set for security=none, got %v", params)
  563. }
  564. if _, ok := params["alpn"]; ok {
  565. t.Fatalf("alpn should not be set for security=none, got %v", params)
  566. }
  567. }
  568. func TestExtractKcpShareFields_Defaults(t *testing.T) {
  569. stream := map[string]any{}
  570. got := extractKcpShareFields(stream)
  571. if got.headerType != "none" {
  572. t.Fatalf("default headerType = %q, want none", got.headerType)
  573. }
  574. if got.seed != "" || got.mtu != 0 || got.tti != 0 {
  575. t.Fatalf("default kcpShareFields should be zero except headerType, got %+v", got)
  576. }
  577. }
  578. func TestExtractKcpShareFields_ReadsAllFields(t *testing.T) {
  579. stream := map[string]any{
  580. "kcpSettings": map[string]any{
  581. "header": map[string]any{"type": "wechat-video"},
  582. "seed": "secret-seed",
  583. "mtu": float64(1350),
  584. "tti": float64(50),
  585. },
  586. }
  587. got := extractKcpShareFields(stream)
  588. if got.headerType != "wechat-video" {
  589. t.Fatalf("headerType = %q, want wechat-video", got.headerType)
  590. }
  591. if got.seed != "secret-seed" {
  592. t.Fatalf("seed = %q, want secret-seed", got.seed)
  593. }
  594. if got.mtu != 1350 {
  595. t.Fatalf("mtu = %d, want 1350", got.mtu)
  596. }
  597. if got.tti != 50 {
  598. t.Fatalf("tti = %d, want 50", got.tti)
  599. }
  600. }
  601. func TestExtractKcpShareFields_FinalMaskLegacyHeader(t *testing.T) {
  602. stream := map[string]any{
  603. "finalmask": map[string]any{
  604. "udp": []any{
  605. map[string]any{
  606. "type": "mkcp-legacy",
  607. "settings": map[string]any{"header": "wechat", "value": ""},
  608. },
  609. },
  610. },
  611. }
  612. got := extractKcpShareFields(stream)
  613. if got.headerType != "wechat-video" {
  614. t.Fatalf("headerType = %q, want wechat-video", got.headerType)
  615. }
  616. if got.seed != "" {
  617. t.Fatalf("seed = %q, want empty for header mask", got.seed)
  618. }
  619. }
  620. func TestExtractKcpShareFields_FinalMaskLegacySeed(t *testing.T) {
  621. stream := map[string]any{
  622. "finalmask": map[string]any{
  623. "udp": []any{
  624. map[string]any{
  625. "type": "mkcp-legacy",
  626. "settings": map[string]any{"header": "", "value": "obfs-pass"},
  627. },
  628. },
  629. },
  630. }
  631. got := extractKcpShareFields(stream)
  632. if got.headerType != "none" {
  633. t.Fatalf("headerType = %q, want none for empty-header legacy mask", got.headerType)
  634. }
  635. if got.seed != "obfs-pass" {
  636. t.Fatalf("seed = %q, want obfs-pass", got.seed)
  637. }
  638. }
  639. func TestKcpShareFields_ApplyToParams(t *testing.T) {
  640. params := map[string]string{}
  641. kcpShareFields{headerType: "wechat-video", seed: "s", mtu: 1350, tti: 50}.applyToParams(params)
  642. if params["headerType"] != "wechat-video" {
  643. t.Fatalf("headerType param = %q", params["headerType"])
  644. }
  645. if params["seed"] != "s" {
  646. t.Fatalf("seed param = %q", params["seed"])
  647. }
  648. if params["mtu"] != "1350" {
  649. t.Fatalf("mtu param = %q", params["mtu"])
  650. }
  651. if params["tti"] != "50" {
  652. t.Fatalf("tti param = %q", params["tti"])
  653. }
  654. }
  655. func TestKcpShareFields_ApplyToParams_NoneHeaderNotAdded(t *testing.T) {
  656. params := map[string]string{}
  657. kcpShareFields{headerType: "none"}.applyToParams(params)
  658. if _, ok := params["headerType"]; ok {
  659. t.Fatalf("headerType=none should not be added, got %v", params)
  660. }
  661. }
  662. func TestMarshalFinalMask_EmptyReturnsFalse(t *testing.T) {
  663. if _, ok := marshalFinalMask(map[string]any{}); ok {
  664. t.Fatal("expected ok=false for empty finalmask")
  665. }
  666. if _, ok := marshalFinalMask(nil); ok {
  667. t.Fatal("expected ok=false for nil finalmask")
  668. }
  669. }
  670. func TestMarshalFinalMask_WithContent(t *testing.T) {
  671. fm := map[string]any{
  672. "tcp": []any{
  673. map[string]any{"type": "fragment"},
  674. },
  675. }
  676. out, ok := marshalFinalMask(fm)
  677. if !ok {
  678. t.Fatal("expected ok=true for finalmask with valid tcp mask")
  679. }
  680. if !strings.Contains(out, `"tcp"`) {
  681. t.Fatalf("marshaled finalmask missing tcp key: %s", out)
  682. }
  683. if !strings.Contains(out, "fragment") {
  684. t.Fatalf("marshaled finalmask missing mask type: %s", out)
  685. }
  686. }
  687. func TestMarshalFinalMask_UnknownTypeIsDropped(t *testing.T) {
  688. fm := map[string]any{
  689. "tcp": []any{
  690. map[string]any{"type": "not-a-real-mask"},
  691. },
  692. }
  693. if _, ok := marshalFinalMask(fm); ok {
  694. t.Fatal("unknown mask types should be dropped, leaving nothing to marshal")
  695. }
  696. }
  697. func TestHasFinalMaskContent(t *testing.T) {
  698. if hasFinalMaskContent(nil) {
  699. t.Fatal("nil should not count as content")
  700. }
  701. if hasFinalMaskContent(map[string]any{}) {
  702. t.Fatal("empty map should not count as content")
  703. }
  704. if !hasFinalMaskContent(map[string]any{"x": 1}) {
  705. t.Fatal("non-empty map should count as content")
  706. }
  707. }