outbound_test.go 18 KB

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