1
0

outbound_helpers_test.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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_TCPHTTPHeader(t *testing.T) {
  147. res, err := ParseLink("vless://[email protected]:443?type=tcp&headerType=http&host=ex.com&path=%2F")
  148. if err != nil {
  149. t.Fatalf("parse: %v", err)
  150. }
  151. tcp := streamSub(t, res, "tcpSettings")
  152. header, _ := tcp["header"].(map[string]any)
  153. if header["type"] != "http" {
  154. t.Errorf("tcp header type = %v, want http", header["type"])
  155. }
  156. }
  157. func TestParseVless_CoreFields(t *testing.T) {
  158. res, err := ParseLink("vless://[email protected]:8443?type=tcp&security=none&flow=xtls-rprx-vision#tag1")
  159. if err != nil {
  160. t.Fatalf("parse: %v", err)
  161. }
  162. st, _ := res.Outbound["settings"].(map[string]any)
  163. if st["address"] != "9.9.9.9" || st["port"] != 8443 || st["id"] != "the-uuid" || st["flow"] != "xtls-rprx-vision" {
  164. t.Errorf("vless settings = %#v", st)
  165. }
  166. }
  167. func TestParseTrojanAndSS_CoreFields(t *testing.T) {
  168. tr, err := ParseLink("trojan://[email protected]:443?type=tcp&security=tls#tj")
  169. if err != nil {
  170. t.Fatalf("parse trojan: %v", err)
  171. }
  172. srv := tr.Outbound["settings"].(map[string]any)["servers"].([]any)[0].(map[string]any)
  173. if srv["address"] != "t.com" || srv["port"] != 443 || srv["password"] != "secret" {
  174. t.Errorf("trojan server = %#v", srv)
  175. }
  176. ssLink := "ss://" + base64.StdEncoding.EncodeToString([]byte("aes-256-gcm:sspass")) + "@s.com:8388#ss1"
  177. ss, err := ParseLink(ssLink)
  178. if err != nil {
  179. t.Fatalf("parse ss: %v", err)
  180. }
  181. ssrv := ss.Outbound["settings"].(map[string]any)["servers"].([]any)[0].(map[string]any)
  182. if ssrv["address"] != "s.com" || ssrv["port"] != 8388 || ssrv["password"] != "sspass" || ssrv["method"] != "aes-256-gcm" {
  183. t.Errorf("ss server = %#v", ssrv)
  184. }
  185. }