outbound_test.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  1. package link
  2. import (
  3. "encoding/base64"
  4. "encoding/json"
  5. "net/url"
  6. "strings"
  7. "testing"
  8. )
  9. func TestParseVmessLink(t *testing.T) {
  10. // vmess:// + base64 of:
  11. // {"v":"2","ps":"test","add":"1.2.3.4","port":443,"id":"uuid","aid":"0","net":"ws","type":"","host":"ex.com","path":"/","tls":"tls"}
  12. link := "vmess://eyJ2IjoiMiIsInBzIjoidGVzdCIsImFkZCI6IjEuMi4zLjQiLCJwb3J0Ijo0NDMsImlkIjoidXVpZCIsImFpZCI6IjAiLCJuZXQiOiJ3cyIsInR5cGUiOiIiLCJob3N0IjoiZXguY29tIiwicGF0aCI6Ii8iLCJ0bHMiOiJ0bHMifQ=="
  13. res, err := ParseLink(link)
  14. if err != nil {
  15. t.Fatalf("parse vmess: %v", err)
  16. }
  17. if res.Outbound["protocol"] != "vmess" {
  18. t.Errorf("expected vmess protocol, got %v", res.Outbound["protocol"])
  19. }
  20. if res.Outbound["tag"] != "test" {
  21. t.Errorf("expected tag 'test', got %v", res.Outbound["tag"])
  22. }
  23. }
  24. func TestParseVlessLink(t *testing.T) {
  25. link := "vless://[email protected]:443?type=ws&security=tls&path=/&host=ex.com#node1"
  26. res, err := ParseLink(link)
  27. if err != nil {
  28. t.Fatalf("parse vless: %v", err)
  29. }
  30. if res.Outbound["protocol"] != "vless" {
  31. t.Fatalf("bad protocol")
  32. }
  33. if res.Outbound["tag"] != "node1" {
  34. t.Errorf("tag mismatch: %v", res.Outbound["tag"])
  35. }
  36. }
  37. func TestParseVlessLink_FinalMaskQuicParamsSanitized(t *testing.T) {
  38. fm := url.QueryEscape(`{"mask":"dtls","quicParams":{"keepAlivePeriod":"10s","maxIdleTimeout":"30","initStreamReceiveWindow":524288,"maxIncomingStreams":true,"brutalUp":"100 mbps"}}`)
  39. res, err := ParseLink("vless://[email protected]:443?type=tcp&security=none&fm=" + fm + "#node1")
  40. if err != nil {
  41. t.Fatalf("parse vless with fm: %v", err)
  42. }
  43. stream, ok := res.Outbound["streamSettings"].(map[string]any)
  44. if !ok {
  45. t.Fatalf("missing streamSettings: %v", res.Outbound)
  46. }
  47. finalmask, ok := stream["finalmask"].(map[string]any)
  48. if !ok {
  49. t.Fatalf("missing finalmask: %v", stream)
  50. }
  51. if finalmask["mask"] != "dtls" {
  52. t.Errorf("mask changed: %v", finalmask["mask"])
  53. }
  54. qp, ok := finalmask["quicParams"].(map[string]any)
  55. if !ok {
  56. t.Fatalf("missing quicParams: %v", finalmask)
  57. }
  58. if got := qp["keepAlivePeriod"]; got != int64(10) {
  59. t.Errorf("keepAlivePeriod: expected 10, got %v (%T)", got, got)
  60. }
  61. if got := qp["maxIdleTimeout"]; got != int64(30) {
  62. t.Errorf("maxIdleTimeout: expected 30, got %v (%T)", got, got)
  63. }
  64. if got := qp["initStreamReceiveWindow"]; got != int64(524288) {
  65. t.Errorf("initStreamReceiveWindow: expected 524288, got %v (%T)", got, got)
  66. }
  67. if _, exists := qp["maxIncomingStreams"]; exists {
  68. t.Errorf("maxIncomingStreams should be dropped, got %v", qp["maxIncomingStreams"])
  69. }
  70. if got := qp["brutalUp"]; got != "100 mbps" {
  71. t.Errorf("brutalUp should stay a string, got %v (%T)", got, got)
  72. }
  73. }
  74. func TestSanitizeFinalMaskQuicParams_ClampsAndRejects(t *testing.T) {
  75. cases := []struct {
  76. name string
  77. key string
  78. in any
  79. want any
  80. }{
  81. {"infinite string dropped", "keepAlivePeriod", "inf", nil},
  82. {"nan string dropped", "keepAlivePeriod", "NaN", nil},
  83. {"negative dropped", "maxStreamReceiveWindow", float64(-5), nil},
  84. {"negative duration dropped", "keepAlivePeriod", "-10s", nil},
  85. {"absurd magnitude dropped", "initConnectionReceiveWindow", float64(1e30), nil},
  86. {"keepAlive clamped up", "keepAlivePeriod", "1s", int64(2)},
  87. {"keepAlive clamped down", "keepAlivePeriod", "90s", int64(60)},
  88. {"idle clamped up", "maxIdleTimeout", float64(1), int64(4)},
  89. {"idle clamped down", "maxIdleTimeout", "10m", int64(120)},
  90. {"streams clamped up", "maxIncomingStreams", float64(4), int64(8)},
  91. {"zero means unset and survives", "maxIdleTimeout", float64(0), int64(0)},
  92. {"window passes through", "initStreamReceiveWindow", float64(524288), int64(524288)},
  93. }
  94. for _, c := range cases {
  95. t.Run(c.name, func(t *testing.T) {
  96. parsed := map[string]any{"quicParams": map[string]any{c.key: c.in}}
  97. sanitizeFinalMaskQuicParams(parsed)
  98. qp := parsed["quicParams"].(map[string]any)
  99. got, exists := qp[c.key]
  100. if c.want == nil {
  101. if exists {
  102. t.Fatalf("%s: expected key dropped, got %v (%T)", c.key, got, got)
  103. }
  104. return
  105. }
  106. if !exists || got != c.want {
  107. t.Fatalf("%s: expected %v, got %v (%T)", c.key, c.want, got, got)
  108. }
  109. })
  110. }
  111. }
  112. func salamanderPassword(t *testing.T, res *ParseResult) (string, bool) {
  113. t.Helper()
  114. stream, ok := res.Outbound["streamSettings"].(map[string]any)
  115. if !ok {
  116. t.Fatalf("missing streamSettings: %v", res.Outbound)
  117. }
  118. finalmask, ok := stream["finalmask"].(map[string]any)
  119. if !ok {
  120. return "", false
  121. }
  122. udp, ok := finalmask["udp"].([]any)
  123. if !ok {
  124. return "", false
  125. }
  126. for _, m := range udp {
  127. mask, _ := m.(map[string]any)
  128. if mask == nil || mask["type"] != "salamander" {
  129. continue
  130. }
  131. settings, _ := mask["settings"].(map[string]any)
  132. pw, _ := settings["password"].(string)
  133. return pw, true
  134. }
  135. return "", false
  136. }
  137. func finalmaskUDP(t *testing.T, res *ParseResult) []any {
  138. t.Helper()
  139. stream, _ := res.Outbound["streamSettings"].(map[string]any)
  140. finalmask, _ := stream["finalmask"].(map[string]any)
  141. udp, _ := finalmask["udp"].([]any)
  142. return udp
  143. }
  144. func hopMask(t *testing.T, res *ParseResult) (map[string]any, bool) {
  145. t.Helper()
  146. for _, rawMask := range finalmaskUDP(t, res) {
  147. mask, _ := rawMask.(map[string]any)
  148. if maskType, _ := mask["type"].(string); maskType == "udphop" {
  149. settings, _ := mask["settings"].(map[string]any)
  150. return settings, true
  151. }
  152. }
  153. return nil, false
  154. }
  155. func hopPorts(t *testing.T, res *ParseResult) (string, bool) {
  156. t.Helper()
  157. settings, ok := hopMask(t, res)
  158. if !ok {
  159. return "", false
  160. }
  161. ports, _ := settings["remotePorts"].(string)
  162. return ports, true
  163. }
  164. func TestParseHysteria2_Obfs(t *testing.T) {
  165. cases := []struct {
  166. name string
  167. query string
  168. wantPw string
  169. wantSet bool
  170. }{
  171. {"standard", "obfs=salamander&obfs-password=s3cr3t", "s3cr3t", true},
  172. {"snake-case alias", "obfs=salamander&obfs_password=aliaspw", "aliaspw", true},
  173. {"camel-case alias", "obfs=salamander&obfsPassword=camelpw", "camelpw", true},
  174. {"case-insensitive type", "obfs=Salamander&obfs-password=mixed", "mixed", true},
  175. {"no obfs", "sni=ex.com", "", false},
  176. {"obfs without password", "obfs=salamander", "", false},
  177. {"unknown obfs type", "obfs=random&obfs-password=x", "", false},
  178. }
  179. for _, c := range cases {
  180. t.Run(c.name, func(t *testing.T) {
  181. res, err := ParseLink("hysteria2://[email protected]:443?security=tls&" + c.query + "#node")
  182. if err != nil {
  183. t.Fatalf("parse hysteria2: %v", err)
  184. }
  185. if res.Outbound["protocol"] != "hysteria" {
  186. t.Fatalf("bad protocol: %v", res.Outbound["protocol"])
  187. }
  188. pw, ok := salamanderPassword(t, res)
  189. if ok != c.wantSet {
  190. t.Fatalf("salamander mask present = %v, want %v (stream: %v)", ok, c.wantSet, res.Outbound["streamSettings"])
  191. }
  192. if pw != c.wantPw {
  193. t.Errorf("salamander password: got %q, want %q", pw, c.wantPw)
  194. }
  195. })
  196. }
  197. }
  198. func TestParseHysteria2_ObfsFinalMaskPrecedence(t *testing.T) {
  199. cases := []struct {
  200. name string
  201. fm string
  202. obfsPw string
  203. wantPw string
  204. wantUDPLen int
  205. }{
  206. {
  207. name: "fm password wins over obfs",
  208. fm: `{"udp":[{"type":"salamander","settings":{"password":"fromfm"}}]}`,
  209. obfsPw: "fromobfs",
  210. wantPw: "fromfm",
  211. wantUDPLen: 1,
  212. },
  213. {
  214. name: "obfs fills password-less fm mask",
  215. fm: `{"udp":[{"type":"salamander","settings":{}}]}`,
  216. obfsPw: "fromobfs",
  217. wantPw: "fromobfs",
  218. wantUDPLen: 1,
  219. },
  220. {
  221. name: "obfs appends alongside a non-salamander mask",
  222. fm: `{"udp":[{"type":"mkcp-legacy","settings":{"header":"srtp"}}]}`,
  223. obfsPw: "fromobfs",
  224. wantPw: "fromobfs",
  225. wantUDPLen: 2,
  226. },
  227. }
  228. for _, c := range cases {
  229. t.Run(c.name, func(t *testing.T) {
  230. link := "hysteria2://[email protected]:443?security=tls&fm=" + url.QueryEscape(c.fm) +
  231. "&obfs=salamander&obfs-password=" + c.obfsPw + "#node"
  232. res, err := ParseLink(link)
  233. if err != nil {
  234. t.Fatalf("parse hysteria2: %v", err)
  235. }
  236. pw, ok := salamanderPassword(t, res)
  237. if !ok {
  238. t.Fatalf("salamander mask missing: %v", res.Outbound["streamSettings"])
  239. }
  240. if pw != c.wantPw {
  241. t.Errorf("salamander password: got %q, want %q", pw, c.wantPw)
  242. }
  243. if udp := finalmaskUDP(t, res); len(udp) != c.wantUDPLen {
  244. t.Errorf("udp mask count: got %d, want %d (%v)", len(udp), c.wantUDPLen, udp)
  245. }
  246. })
  247. }
  248. }
  249. func TestParseHysteria2_Mport(t *testing.T) {
  250. cases := []struct {
  251. name string
  252. query string
  253. wantPorts string
  254. wantHop bool
  255. }{
  256. {"standard mport", "mport=20000-50000", "20000-50000", true},
  257. {"no mport", "sni=ex.com", "", false},
  258. {
  259. name: "fm udphop mask wins over mport",
  260. query: "mport=1-2&fm=" + url.QueryEscape(`{"udp":[{"type":"udphop","settings":{"mode":"intervalremote","interval":"7-9","remotePorts":"30000-40000"}}]}`),
  261. wantPorts: "30000-40000",
  262. wantHop: true,
  263. },
  264. {
  265. name: "legacy fm quicParams.udpHop no longer suppresses mport",
  266. query: "mport=1-2&fm=" + url.QueryEscape(`{"quicParams":{"udpHop":{"ports":"30000-40000","interval":"7-9"}}}`),
  267. wantPorts: "1-2",
  268. wantHop: true,
  269. },
  270. }
  271. for _, c := range cases {
  272. t.Run(c.name, func(t *testing.T) {
  273. res, err := ParseLink("hysteria2://[email protected]:443?security=tls&" + c.query + "#node")
  274. if err != nil {
  275. t.Fatalf("parse hysteria2: %v", err)
  276. }
  277. ports, ok := hopPorts(t, res)
  278. if ok != c.wantHop {
  279. t.Fatalf("udpHop present = %v, want %v (stream: %v)", ok, c.wantHop, res.Outbound["streamSettings"])
  280. }
  281. if ports != c.wantPorts {
  282. t.Errorf("hop ports: got %q, want %q", ports, c.wantPorts)
  283. }
  284. })
  285. }
  286. }
  287. // xray-core 26.9.9 rejects a udphop mask whose mode is empty or unknown, so
  288. // the mport importer must emit a mode the core's UDPHop.Build() accepts.
  289. func TestParseHysteria2_MportEmitsCoreAcceptedMask(t *testing.T) {
  290. res, err := ParseLink("hysteria2://[email protected]:443?security=tls&mport=20000-50000#node")
  291. if err != nil {
  292. t.Fatalf("parse hysteria2: %v", err)
  293. }
  294. settings, ok := hopMask(t, res)
  295. if !ok {
  296. t.Fatalf("no udphop mask (stream: %v)", res.Outbound["streamSettings"])
  297. }
  298. if got, _ := settings["mode"].(string); got != "intervalremote" {
  299. t.Errorf("mode = %q, want %q", got, "intervalremote")
  300. }
  301. if got, _ := settings["interval"].(string); got != "5-10" {
  302. t.Errorf("interval = %q, want %q", got, "5-10")
  303. }
  304. stream, _ := res.Outbound["streamSettings"].(map[string]any)
  305. finalmask, _ := stream["finalmask"].(map[string]any)
  306. if quicParams, ok := finalmask["quicParams"].(map[string]any); ok {
  307. if _, dead := quicParams["udpHop"]; dead {
  308. t.Error("importer still writes the quicParams.udpHop key the core ignores")
  309. }
  310. }
  311. }
  312. func TestParseShadowsocks(t *testing.T) {
  313. modernUser := base64.StdEncoding.EncodeToString([]byte("aes-256-gcm:secretpass"))
  314. legacyBody := base64.StdEncoding.EncodeToString([]byte("aes-256-gcm:[email protected]:8388"))
  315. cases := []struct {
  316. name string
  317. link string
  318. host string
  319. port int
  320. method string
  321. pass string
  322. }{
  323. {
  324. name: "modern",
  325. link: "ss://" + modernUser + "@1.2.3.4:8388#node",
  326. host: "1.2.3.4",
  327. port: 8388,
  328. method: "aes-256-gcm",
  329. pass: "secretpass",
  330. },
  331. {
  332. name: "modern with plugin query",
  333. link: "ss://" + modernUser + "@1.2.3.4:8388?plugin=v2ray-plugin#node",
  334. host: "1.2.3.4",
  335. port: 8388,
  336. method: "aes-256-gcm",
  337. pass: "secretpass",
  338. },
  339. {
  340. name: "modern sip002 slash query",
  341. link: "ss://" + modernUser + "@1.2.3.4:8388/?plugin=obfs-local%3Bobfs%3Dhttp#node",
  342. host: "1.2.3.4",
  343. port: 8388,
  344. method: "aes-256-gcm",
  345. pass: "secretpass",
  346. },
  347. {
  348. name: "legacy",
  349. link: "ss://" + legacyBody + "#node",
  350. host: "1.2.3.4",
  351. port: 8388,
  352. method: "aes-256-gcm",
  353. pass: "secretpass",
  354. },
  355. {
  356. name: "base64url userinfo with plugin and trailing slash",
  357. link: "ss://" + base64.RawURLEncoding.EncodeToString([]byte("aes-128-gcm:pa+ss/word")) + "@1.2.3.4:8388/?plugin=obfs-local%3Bobfs%3Dhttp#node",
  358. host: "1.2.3.4",
  359. port: 8388,
  360. method: "aes-128-gcm",
  361. pass: "pa+ss/word",
  362. },
  363. {
  364. name: "sip022 percent-encoded userinfo",
  365. link: "ss://2022-blake3-aes-256-gcm:YctPZ6U7xPPcU%2Bgp3u%2B0tx%2FtRizJN9K8y%2BuKlW2qjlI%[email protected]:8888#Example3",
  366. host: "example.com",
  367. port: 8888,
  368. method: "2022-blake3-aes-256-gcm",
  369. pass: "YctPZ6U7xPPcU+gp3u+0tx/tRizJN9K8y+uKlW2qjlI=",
  370. },
  371. {
  372. name: "sip022 dual-key password with type query preserves inner colon",
  373. link: "ss://2022-blake3-aes-256-gcm:AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA%3D:BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB%[email protected]:9999?type=tcp#node",
  374. host: "1.2.3.4",
  375. port: 9999,
  376. method: "2022-blake3-aes-256-gcm",
  377. pass: "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=:BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB=",
  378. },
  379. }
  380. for _, c := range cases {
  381. t.Run(c.name, func(t *testing.T) {
  382. res, err := ParseLink(c.link)
  383. if err != nil {
  384. t.Fatalf("parse ss: %v", err)
  385. }
  386. if res.Outbound["protocol"] != "shadowsocks" {
  387. t.Fatalf("protocol = %v, want shadowsocks", res.Outbound["protocol"])
  388. }
  389. srv := res.Outbound["settings"].(map[string]any)["servers"].([]any)[0].(map[string]any)
  390. if srv["address"] != c.host {
  391. t.Errorf("address = %v, want %v", srv["address"], c.host)
  392. }
  393. if srv["port"] != c.port {
  394. t.Errorf("port = %v, want %v", srv["port"], c.port)
  395. }
  396. if srv["method"] != c.method {
  397. t.Errorf("method = %v, want %v", srv["method"], c.method)
  398. }
  399. if srv["password"] != c.pass {
  400. t.Errorf("password = %v, want %v", srv["password"], c.pass)
  401. }
  402. })
  403. }
  404. }
  405. func TestParseShadowsocksTLSQueryRoundTrip(t *testing.T) {
  406. user := base64.RawURLEncoding.EncodeToString([]byte("chacha20-ietf-poly1305:secretpass"))
  407. link := "ss://" + user + "@example.com:443?alpn=h2%2Chttp%2F1.1&fp=firefox&security=tls&sni=example.com&type=tcp#user"
  408. res, err := ParseLink(link)
  409. if err != nil {
  410. t.Fatalf("parse ss tls: %v", err)
  411. }
  412. srv := res.Outbound["settings"].(map[string]any)["servers"].([]any)[0].(map[string]any)
  413. if srv["address"] != "example.com" || srv["port"] != 443 {
  414. t.Fatalf("server = %v", srv)
  415. }
  416. if srv["method"] != "chacha20-ietf-poly1305" || srv["password"] != "secretpass" {
  417. t.Fatalf("creds = %v", srv)
  418. }
  419. stream, ok := res.Outbound["streamSettings"].(map[string]any)
  420. if !ok {
  421. t.Fatalf("missing streamSettings: %v", res.Outbound)
  422. }
  423. if stream["network"] != "tcp" {
  424. t.Errorf("network = %v, want tcp", stream["network"])
  425. }
  426. if stream["security"] != "tls" {
  427. t.Errorf("security = %v, want tls", stream["security"])
  428. }
  429. tls, ok := stream["tlsSettings"].(map[string]any)
  430. if !ok {
  431. t.Fatalf("missing tlsSettings: %v", stream)
  432. }
  433. if tls["serverName"] != "example.com" {
  434. t.Errorf("sni = %v, want example.com", tls["serverName"])
  435. }
  436. if tls["fingerprint"] != "firefox" {
  437. t.Errorf("fp = %v, want firefox", tls["fingerprint"])
  438. }
  439. alpn, _ := tls["alpn"].([]string)
  440. if len(alpn) != 2 || alpn[0] != "h2" || alpn[1] != "http/1.1" {
  441. t.Errorf("alpn = %v, want [h2 http/1.1]", alpn)
  442. }
  443. }
  444. func TestParseShadowsocksBadPort(t *testing.T) {
  445. user := base64.StdEncoding.EncodeToString([]byte("aes-256-gcm:secretpass"))
  446. cases := map[string]string{
  447. "modern": "ss://" + user + "@1.2.3.4:notaport#node",
  448. "legacy": "ss://" + base64.StdEncoding.EncodeToString([]byte("aes-256-gcm:[email protected]:notaport")) + "#node",
  449. }
  450. for name, link := range cases {
  451. t.Run(name, func(t *testing.T) {
  452. if _, err := ParseLink(link); err == nil {
  453. t.Errorf("expected parse error for non-numeric port, got nil")
  454. }
  455. })
  456. }
  457. }
  458. func TestParseSubscriptionBody_Base64(t *testing.T) {
  459. // base64 of the two joined links:
  460. // vless://u@h:443?type=tcp#A\nvless://u2@h2:443?type=tcp#B
  461. b64 := "dmxlc3M6Ly91QGg6NDQzP3R5cGU9dGNwI0EKdmxlc3M6Ly91MkBoMjo0NDM/dHlwZT10Y3AjQg=="
  462. obs, ids, err := ParseSubscriptionBody([]byte(b64))
  463. if err != nil {
  464. t.Fatalf("parse sub body: %v", err)
  465. }
  466. if len(obs) != 2 {
  467. t.Fatalf("expected 2 outbounds, got %d", len(obs))
  468. }
  469. if !strings.HasPrefix(ids[0], "vless:") || !strings.HasPrefix(ids[1], "vless:") {
  470. t.Errorf("bad identities: %v", ids)
  471. }
  472. }
  473. func TestSlugAndSuggest(t *testing.T) {
  474. if SlugRemark("Hello World!") != "hello-world" {
  475. t.Errorf("slug failed")
  476. }
  477. tag := SuggestTag("hk-", " SG 01 !! ", 0)
  478. if tag != "hk-sg-01" {
  479. t.Errorf("suggest tag got %q", tag)
  480. }
  481. // Non-ASCII letters/digits are preserved rather than stripped.
  482. if got := SlugRemark("Москва 🇷🇺 01"); got != "москва-01" {
  483. t.Errorf("unicode slug got %q", got)
  484. }
  485. if got := SuggestTag("ru-", "Сервер 2", 0); got != "ru-сервер-2" {
  486. t.Errorf("unicode suggest tag got %q", got)
  487. }
  488. }
  489. // The obfs-local plugin the panel exports carries the only description of
  490. // shadowsocks tcp/http obfuscation, so it has to become that header.
  491. func TestParseShadowsocksObfsLocalPlugin(t *testing.T) {
  492. user := base64.RawURLEncoding.EncodeToString([]byte("aes-256-gcm:secretpass"))
  493. const httpObfs = "obfs-local;obfs=http;obfs-host=obfs.example.com"
  494. for _, tc := range []struct {
  495. name, query, wantHeader, wantHost string
  496. }{
  497. {"http obfs becomes the tcp header", "plugin=" + url.QueryEscape(httpObfs), "http", "obfs.example.com"},
  498. {"unencoded separators map the same way", "plugin=" + httpObfs, "http", "obfs.example.com"},
  499. {"tls obfs has no xray header", "plugin=" + url.QueryEscape("obfs-local;obfs=tls"), "none", ""},
  500. {"an unrelated plugin is left alone", "plugin=v2ray-plugin", "none", ""},
  501. } {
  502. t.Run(tc.name, func(t *testing.T) {
  503. res, err := ParseLink("ss://" + user + "@1.2.3.4:8388/?" + tc.query + "#node")
  504. if err != nil {
  505. t.Fatalf("parse ss: %v", err)
  506. }
  507. raw, err := json.Marshal(res.Outbound["streamSettings"])
  508. if err != nil {
  509. t.Fatalf("marshal stream: %v", err)
  510. }
  511. var stream map[string]any
  512. _ = json.Unmarshal(raw, &stream)
  513. tcp, _ := stream["tcpSettings"].(map[string]any)
  514. header, _ := tcp["header"].(map[string]any)
  515. if header == nil || header["type"] != tc.wantHeader {
  516. t.Fatalf("header = %v, want type %q", header, tc.wantHeader)
  517. }
  518. request, _ := header["request"].(map[string]any)
  519. headers, _ := request["headers"].(map[string]any)
  520. hosts, _ := headers["Host"].([]any)
  521. got := ""
  522. if len(hosts) > 0 {
  523. got, _ = hosts[0].(string)
  524. }
  525. if got != tc.wantHost {
  526. t.Errorf("host = %q, want %q", got, tc.wantHost)
  527. }
  528. })
  529. }
  530. }