outbound_test.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 != int64(10) {
  57. t.Errorf("keepAlivePeriod: expected 10, got %v (%T)", got, got)
  58. }
  59. if got := qp["maxIdleTimeout"]; got != int64(30) {
  60. t.Errorf("maxIdleTimeout: expected 30, got %v (%T)", got, got)
  61. }
  62. if got := qp["initStreamReceiveWindow"]; got != int64(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 TestSanitizeFinalMaskQuicParams_ClampsAndRejects(t *testing.T) {
  73. cases := []struct {
  74. name string
  75. key string
  76. in any
  77. want any
  78. }{
  79. {"infinite string dropped", "keepAlivePeriod", "inf", nil},
  80. {"nan string dropped", "keepAlivePeriod", "NaN", nil},
  81. {"negative dropped", "maxStreamReceiveWindow", float64(-5), nil},
  82. {"negative duration dropped", "keepAlivePeriod", "-10s", nil},
  83. {"absurd magnitude dropped", "initConnectionReceiveWindow", float64(1e30), nil},
  84. {"keepAlive clamped up", "keepAlivePeriod", "1s", int64(2)},
  85. {"keepAlive clamped down", "keepAlivePeriod", "90s", int64(60)},
  86. {"idle clamped up", "maxIdleTimeout", float64(1), int64(4)},
  87. {"idle clamped down", "maxIdleTimeout", "10m", int64(120)},
  88. {"streams clamped up", "maxIncomingStreams", float64(4), int64(8)},
  89. {"zero means unset and survives", "maxIdleTimeout", float64(0), int64(0)},
  90. {"window passes through", "initStreamReceiveWindow", float64(524288), int64(524288)},
  91. }
  92. for _, c := range cases {
  93. t.Run(c.name, func(t *testing.T) {
  94. parsed := map[string]any{"quicParams": map[string]any{c.key: c.in}}
  95. sanitizeFinalMaskQuicParams(parsed)
  96. qp := parsed["quicParams"].(map[string]any)
  97. got, exists := qp[c.key]
  98. if c.want == nil {
  99. if exists {
  100. t.Fatalf("%s: expected key dropped, got %v (%T)", c.key, got, got)
  101. }
  102. return
  103. }
  104. if !exists || got != c.want {
  105. t.Fatalf("%s: expected %v, got %v (%T)", c.key, c.want, got, got)
  106. }
  107. })
  108. }
  109. }
  110. func TestParseSubscriptionBody_Base64(t *testing.T) {
  111. // base64 of the two joined links:
  112. // vless://u@h:443?type=tcp#A\nvless://u2@h2:443?type=tcp#B
  113. b64 := "dmxlc3M6Ly91QGg6NDQzP3R5cGU9dGNwI0EKdmxlc3M6Ly91MkBoMjo0NDM/dHlwZT10Y3AjQg=="
  114. obs, ids, err := ParseSubscriptionBody([]byte(b64))
  115. if err != nil {
  116. t.Fatalf("parse sub body: %v", err)
  117. }
  118. if len(obs) != 2 {
  119. t.Fatalf("expected 2 outbounds, got %d", len(obs))
  120. }
  121. if !strings.HasPrefix(ids[0], "vless:") || !strings.HasPrefix(ids[1], "vless:") {
  122. t.Errorf("bad identities: %v", ids)
  123. }
  124. }
  125. func TestSlugAndSuggest(t *testing.T) {
  126. if SlugRemark("Hello World!") != "hello-world" {
  127. t.Errorf("slug failed")
  128. }
  129. tag := SuggestTag("hk-", " SG 01 !! ", 0)
  130. if tag != "hk-sg-01" {
  131. t.Errorf("suggest tag got %q", tag)
  132. }
  133. // Non-ASCII letters/digits are preserved rather than stripped.
  134. if got := SlugRemark("Москва 🇷🇺 01"); got != "москва-01" {
  135. t.Errorf("unicode slug got %q", got)
  136. }
  137. if got := SuggestTag("ru-", "Сервер 2", 0); got != "ru-сервер-2" {
  138. t.Errorf("unicode suggest tag got %q", got)
  139. }
  140. }