1
0

outbound_test.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. package link
  2. import (
  3. "net/url"
  4. "strings"
  5. "testing"
  6. )
  7. func TestParseVmessLink(t *testing.T) {
  8. // vmess:// + base64 of:
  9. // {"v":"2","ps":"test","add":"1.2.3.4","port":443,"id":"uuid","aid":"0","net":"ws","type":"","host":"ex.com","path":"/","tls":"tls"}
  10. link := "vmess://eyJ2IjoiMiIsInBzIjoidGVzdCIsImFkZCI6IjEuMi4zLjQiLCJwb3J0Ijo0NDMsImlkIjoidXVpZCIsImFpZCI6IjAiLCJuZXQiOiJ3cyIsInR5cGUiOiIiLCJob3N0IjoiZXguY29tIiwicGF0aCI6Ii8iLCJ0bHMiOiJ0bHMifQ=="
  11. res, err := ParseLink(link)
  12. if err != nil {
  13. t.Fatalf("parse vmess: %v", err)
  14. }
  15. if res.Outbound["protocol"] != "vmess" {
  16. t.Errorf("expected vmess protocol, got %v", res.Outbound["protocol"])
  17. }
  18. if res.Outbound["tag"] != "test" {
  19. t.Errorf("expected tag 'test', got %v", res.Outbound["tag"])
  20. }
  21. }
  22. func TestParseVlessLink(t *testing.T) {
  23. link := "vless://[email protected]:443?type=ws&security=tls&path=/&host=ex.com#node1"
  24. res, err := ParseLink(link)
  25. if err != nil {
  26. t.Fatalf("parse vless: %v", err)
  27. }
  28. if res.Outbound["protocol"] != "vless" {
  29. t.Fatalf("bad protocol")
  30. }
  31. if res.Outbound["tag"] != "node1" {
  32. t.Errorf("tag mismatch: %v", res.Outbound["tag"])
  33. }
  34. }
  35. func TestParseVlessLink_FinalMaskQuicParamsSanitized(t *testing.T) {
  36. fm := url.QueryEscape(`{"mask":"dtls","quicParams":{"keepAlivePeriod":"10s","maxIdleTimeout":"30","initStreamReceiveWindow":524288,"maxIncomingStreams":true,"brutalUp":"100 mbps"}}`)
  37. res, err := ParseLink("vless://[email protected]:443?type=tcp&security=none&fm=" + fm + "#node1")
  38. if err != nil {
  39. t.Fatalf("parse vless with fm: %v", err)
  40. }
  41. stream, ok := res.Outbound["streamSettings"].(map[string]any)
  42. if !ok {
  43. t.Fatalf("missing streamSettings: %v", res.Outbound)
  44. }
  45. finalmask, ok := stream["finalmask"].(map[string]any)
  46. if !ok {
  47. t.Fatalf("missing finalmask: %v", stream)
  48. }
  49. if finalmask["mask"] != "dtls" {
  50. t.Errorf("mask changed: %v", finalmask["mask"])
  51. }
  52. qp, ok := finalmask["quicParams"].(map[string]any)
  53. if !ok {
  54. t.Fatalf("missing quicParams: %v", finalmask)
  55. }
  56. if got := qp["keepAlivePeriod"]; got != float64(10) {
  57. t.Errorf("keepAlivePeriod: expected 10, got %v (%T)", got, got)
  58. }
  59. if got := qp["maxIdleTimeout"]; got != float64(30) {
  60. t.Errorf("maxIdleTimeout: expected 30, got %v (%T)", got, got)
  61. }
  62. if got := qp["initStreamReceiveWindow"]; got != float64(524288) {
  63. t.Errorf("initStreamReceiveWindow: expected 524288, got %v (%T)", got, got)
  64. }
  65. if _, exists := qp["maxIncomingStreams"]; exists {
  66. t.Errorf("maxIncomingStreams should be dropped, got %v", qp["maxIncomingStreams"])
  67. }
  68. if got := qp["brutalUp"]; got != "100 mbps" {
  69. t.Errorf("brutalUp should stay a string, got %v (%T)", got, got)
  70. }
  71. }
  72. func TestParseSubscriptionBody_Base64(t *testing.T) {
  73. // base64 of the two joined links:
  74. // vless://u@h:443?type=tcp#A\nvless://u2@h2:443?type=tcp#B
  75. b64 := "dmxlc3M6Ly91QGg6NDQzP3R5cGU9dGNwI0EKdmxlc3M6Ly91MkBoMjo0NDM/dHlwZT10Y3AjQg=="
  76. obs, ids, err := ParseSubscriptionBody([]byte(b64))
  77. if err != nil {
  78. t.Fatalf("parse sub body: %v", err)
  79. }
  80. if len(obs) != 2 {
  81. t.Fatalf("expected 2 outbounds, got %d", len(obs))
  82. }
  83. if !strings.HasPrefix(ids[0], "vless:") || !strings.HasPrefix(ids[1], "vless:") {
  84. t.Errorf("bad identities: %v", ids)
  85. }
  86. }
  87. func TestSlugAndSuggest(t *testing.T) {
  88. if SlugRemark("Hello World!") != "hello-world" {
  89. t.Errorf("slug failed")
  90. }
  91. tag := SuggestTag("hk-", " SG 01 !! ", 0)
  92. if tag != "hk-sg-01" {
  93. t.Errorf("suggest tag got %q", tag)
  94. }
  95. // Non-ASCII letters/digits are preserved rather than stripped.
  96. if got := SlugRemark("Москва 🇷🇺 01"); got != "москва-01" {
  97. t.Errorf("unicode slug got %q", got)
  98. }
  99. if got := SuggestTag("ru-", "Сервер 2", 0); got != "ru-сервер-2" {
  100. t.Errorf("unicode suggest tag got %q", got)
  101. }
  102. }