outbound_helpers_test.go 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. package link
  2. import (
  3. "encoding/base64"
  4. "net/url"
  5. "reflect"
  6. "testing"
  7. )
  8. func TestDefaultPort(t *testing.T) {
  9. cases := []struct {
  10. in string
  11. def int
  12. want int
  13. }{
  14. {"", 443, 443},
  15. {"8080", 443, 8080},
  16. {"0", 443, 443}, // non-positive falls back
  17. {"-1", 443, 443}, // negative falls back
  18. {"abc", 443, 443}, // unparseable falls back
  19. {"65535", 443, 65535},
  20. }
  21. for _, c := range cases {
  22. if got := defaultPort(c.in, c.def); got != c.want {
  23. t.Errorf("defaultPort(%q,%d) = %d, want %d", c.in, c.def, got, c.want)
  24. }
  25. }
  26. }
  27. func TestFirstNonEmptyAndParam(t *testing.T) {
  28. if got := firstNonEmpty("a", "b"); got != "a" {
  29. t.Errorf("firstNonEmpty(a,b) = %q, want a", got)
  30. }
  31. if got := firstNonEmpty("", "b"); got != "b" {
  32. t.Errorf("firstNonEmpty(,b) = %q, want b", got)
  33. }
  34. p := url.Values{"x": {""}, "y": {"hit"}, "z": {"z"}}
  35. if got := firstParam(p, "x", "y", "z"); got != "hit" {
  36. t.Errorf("firstParam = %q, want hit (first non-empty)", got)
  37. }
  38. if got := firstParam(p, "x"); got != "" {
  39. t.Errorf("firstParam(only empty) = %q, want empty", got)
  40. }
  41. }
  42. func TestSplitComma(t *testing.T) {
  43. if got := splitComma(""); got != nil {
  44. t.Errorf("splitComma(empty) = %v, want nil", got)
  45. }
  46. if got := splitComma("a, ,b ,, c"); !reflect.DeepEqual(got, []string{"a", "b", "c"}) {
  47. t.Errorf("splitComma trim/skip = %v, want [a b c]", got)
  48. }
  49. if got := splitCommaOrDefault("", []string{"d"}); !reflect.DeepEqual(got, []string{"d"}) {
  50. t.Errorf("splitCommaOrDefault(empty) = %v, want [d]", got)
  51. }
  52. if got := splitCommaOrDefault("x,y", []string{"d"}); !reflect.DeepEqual(got, []string{"x", "y"}) {
  53. t.Errorf("splitCommaOrDefault(x,y) = %v, want [x y]", got)
  54. }
  55. }
  56. func TestPadAndBase64DecodeFlexible(t *testing.T) {
  57. if got := padBase64("abc"); got != "abc=" {
  58. t.Errorf("padBase64(abc) = %q, want abc=", got)
  59. }
  60. if got := padBase64("abcd"); got != "abcd" {
  61. t.Errorf("padBase64(abcd) = %q, want unchanged", got)
  62. }
  63. std := base64.StdEncoding.EncodeToString([]byte("aes-256-gcm:secret"))
  64. if got, err := base64DecodeFlexible(std); err != nil || got != "aes-256-gcm:secret" {
  65. t.Errorf("base64DecodeFlexible(std) = (%q,%v), want (aes-256-gcm:secret,nil)", got, err)
  66. }
  67. rawURL := base64.RawURLEncoding.EncodeToString([]byte("m:p"))
  68. if got, err := base64DecodeFlexible(rawURL); err != nil || got != "m:p" {
  69. t.Errorf("base64DecodeFlexible(rawurl) = (%q,%v), want (m:p,nil)", got, err)
  70. }
  71. if _, err := base64DecodeFlexible("!!!not!!!"); err == nil {
  72. t.Error("base64DecodeFlexible(garbage) should error")
  73. }
  74. }
  75. func TestDecodeHash(t *testing.T) {
  76. if got := decodeHash(""); got != "" {
  77. t.Errorf("decodeHash(empty) = %q, want empty", got)
  78. }
  79. if got := decodeHash("a%20b"); got != "a b" {
  80. t.Errorf("decodeHash(a%%20b) = %q, want 'a b'", got)
  81. }
  82. if got := decodeHash("plain"); got != "plain" {
  83. t.Errorf("decodeHash(plain) = %q, want plain", got)
  84. }
  85. }
  86. func TestCanonicalQuery_SortsKeys(t *testing.T) {
  87. // unsorted input must come out key-sorted for a stable identity
  88. got := canonicalQuery(url.Values{"c": {"3"}, "a": {"1"}, "b": {"2"}})
  89. if got != "a=1&b=2&c=3" {
  90. t.Fatalf("canonicalQuery = %q, want a=1&b=2&c=3", got)
  91. }
  92. }
  93. // stream navigates res.Outbound["streamSettings"][key] as a map.
  94. func streamSub(t *testing.T, res *ParseResult, key string) map[string]any {
  95. t.Helper()
  96. ss, _ := res.Outbound["streamSettings"].(map[string]any)
  97. m, ok := ss[key].(map[string]any)
  98. if !ok {
  99. t.Fatalf("streamSettings.%s missing/not a map: %#v", key, ss)
  100. }
  101. return m
  102. }
  103. func TestParse_RealitySecurityMapped(t *testing.T) {
  104. res, err := ParseLink("vless://[email protected]:443?type=tcp&security=reality&pbk=PBK&sid=SID&sni=SNI&fp=firefox&spx=%2Fspx&pqv=PQV")
  105. if err != nil {
  106. t.Fatalf("parse: %v", err)
  107. }
  108. re := streamSub(t, res, "realitySettings")
  109. for k, want := range map[string]string{"publicKey": "PBK", "shortId": "SID", "serverName": "SNI", "fingerprint": "firefox", "spiderX": "/spx", "mldsa65Verify": "PQV"} {
  110. if re[k] != want {
  111. t.Errorf("realitySettings[%q] = %v, want %q", k, re[k], want)
  112. }
  113. }
  114. }
  115. func TestParse_TLSSecurityMapped(t *testing.T) {
  116. res, err := ParseLink("trojan://[email protected]:443?type=tcp&security=tls&sni=SNI&fp=chrome&alpn=h2,http/1.1&ech=ECH&pcs=PCS")
  117. if err != nil {
  118. t.Fatalf("parse: %v", err)
  119. }
  120. tls := streamSub(t, res, "tlsSettings")
  121. if tls["serverName"] != "SNI" || tls["fingerprint"] != "chrome" || tls["echConfigList"] != "ECH" || tls["pinnedPeerCertSha256"] != "PCS" {
  122. t.Errorf("tlsSettings fields = %#v", tls)
  123. }
  124. if alpn, _ := tls["alpn"].([]string); !reflect.DeepEqual(alpn, []string{"h2", "http/1.1"}) {
  125. t.Errorf("alpn = %#v, want [h2 http/1.1]", tls["alpn"])
  126. }
  127. }
  128. func TestParse_WSAndGRPCTransport(t *testing.T) {
  129. ws, err := ParseLink("vless://[email protected]:443?type=ws&host=H&path=%2Fwspath")
  130. if err != nil {
  131. t.Fatalf("parse ws: %v", err)
  132. }
  133. wss := streamSub(t, ws, "wsSettings")
  134. if wss["host"] != "H" || wss["path"] != "/wspath" {
  135. t.Errorf("wsSettings = %#v, want host=H path=/wspath", wss)
  136. }
  137. grpc, err := ParseLink("vless://[email protected]:443?type=grpc&serviceName=svc&authority=auth&mode=multi")
  138. if err != nil {
  139. t.Fatalf("parse grpc: %v", err)
  140. }
  141. gs := streamSub(t, grpc, "grpcSettings")
  142. if gs["serviceName"] != "svc" || gs["authority"] != "auth" || gs["multiMode"] != true {
  143. t.Errorf("grpcSettings = %#v, want serviceName=svc authority=auth multiMode=true", gs)
  144. }
  145. }
  146. func TestParse_XhttpExtraAndSnakeCaseFields(t *testing.T) {
  147. q := url.Values{}
  148. q.Set("type", "xhttp")
  149. q.Set("encryption", "none")
  150. q.Set("security", "none")
  151. q.Set("mode", "auto")
  152. q.Set("x_padding_bytes", "1-50")
  153. q.Set("extra", `{"mode":"auto","xPaddingBytes":"1-50","scMaxEachPostBytes":"1000000"}`)
  154. res, err := ParseLink("vless://[email protected]:443?" + q.Encode() + "#r")
  155. if err != nil {
  156. t.Fatalf("parse: %v", err)
  157. }
  158. xh := streamSub(t, res, "xhttpSettings")
  159. if xh["xPaddingBytes"] != "1-50" {
  160. t.Errorf("xPaddingBytes = %v, want 1-50 (dropped from the snake_case/extra payload the emitter writes)", xh["xPaddingBytes"])
  161. }
  162. if xh["scMaxEachPostBytes"] != "1000000" {
  163. t.Errorf("scMaxEachPostBytes = %v, want 1000000 (dropped from the extra blob)", xh["scMaxEachPostBytes"])
  164. }
  165. }
  166. func TestParse_VmessWSPathWithoutHostKey(t *testing.T) {
  167. // Some generators omit "host" when it equals the address; the path must
  168. // still be honored (matching the TS parser), not dropped to the default.
  169. inner := `{"v":"2","add":"h","port":443,"id":"11111111-2222-4333-8444-555555555555","net":"ws","path":"/api","tls":"tls"}`
  170. link := "vmess://" + base64.StdEncoding.EncodeToString([]byte(inner))
  171. res, err := ParseLink(link)
  172. if err != nil {
  173. t.Fatalf("parse: %v", err)
  174. }
  175. wss := streamSub(t, res, "wsSettings")
  176. if wss["path"] != "/api" {
  177. t.Errorf("wsSettings path = %v, want /api (dropped when host key absent)", wss["path"])
  178. }
  179. }
  180. func TestParse_Hysteria2VerifyPeerCertByName(t *testing.T) {
  181. res, err := ParseLink("hysteria2://[email protected]:443?security=tls&sni=decoy.com&vcn=real-cert.com#r")
  182. if err != nil {
  183. t.Fatalf("parse: %v", err)
  184. }
  185. tls := streamSub(t, res, "tlsSettings")
  186. if tls["verifyPeerCertByName"] != "real-cert.com" {
  187. t.Errorf("verifyPeerCertByName = %v, want real-cert.com (vcn param ignored)", tls["verifyPeerCertByName"])
  188. }
  189. }
  190. func TestParse_TCPHTTPHeader(t *testing.T) {
  191. res, err := ParseLink("vless://[email protected]:443?type=tcp&headerType=http&host=ex.com&path=%2F")
  192. if err != nil {
  193. t.Fatalf("parse: %v", err)
  194. }
  195. tcp := streamSub(t, res, "tcpSettings")
  196. header, _ := tcp["header"].(map[string]any)
  197. if header["type"] != "http" {
  198. t.Errorf("tcp header type = %v, want http", header["type"])
  199. }
  200. }
  201. func TestParseVless_CoreFields(t *testing.T) {
  202. res, err := ParseLink("vless://[email protected]:8443?type=tcp&security=none&flow=xtls-rprx-vision#tag1")
  203. if err != nil {
  204. t.Fatalf("parse: %v", err)
  205. }
  206. st, _ := res.Outbound["settings"].(map[string]any)
  207. if st["address"] != "9.9.9.9" || st["port"] != 8443 || st["id"] != "the-uuid" || st["flow"] != "xtls-rprx-vision" {
  208. t.Errorf("vless settings = %#v", st)
  209. }
  210. }
  211. func TestParseTrojanAndSS_CoreFields(t *testing.T) {
  212. tr, err := ParseLink("trojan://[email protected]:443?type=tcp&security=tls#tj")
  213. if err != nil {
  214. t.Fatalf("parse trojan: %v", err)
  215. }
  216. srv := tr.Outbound["settings"].(map[string]any)["servers"].([]any)[0].(map[string]any)
  217. if srv["address"] != "t.com" || srv["port"] != 443 || srv["password"] != "secret" {
  218. t.Errorf("trojan server = %#v", srv)
  219. }
  220. ssLink := "ss://" + base64.StdEncoding.EncodeToString([]byte("aes-256-gcm:sspass")) + "@s.com:8388#ss1"
  221. ss, err := ParseLink(ssLink)
  222. if err != nil {
  223. t.Fatalf("parse ss: %v", err)
  224. }
  225. ssrv := ss.Outbound["settings"].(map[string]any)["servers"].([]any)[0].(map[string]any)
  226. if ssrv["address"] != "s.com" || ssrv["port"] != 8388 || ssrv["password"] != "sspass" || ssrv["method"] != "aes-256-gcm" {
  227. t.Errorf("ss server = %#v", ssrv)
  228. }
  229. }