salamander_uri_test.go 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. package sub
  2. import (
  3. "reflect"
  4. "strconv"
  5. "strings"
  6. "testing"
  7. "github.com/mhsanaei/3x-ui/v3/internal/database/model"
  8. "github.com/mhsanaei/3x-ui/v3/internal/logger"
  9. )
  10. func TestExtraSalamanderKeys(t *testing.T) {
  11. if got := extraSalamanderKeys(map[string]any{"password": "pw"}, false); len(got) != 0 {
  12. t.Fatalf("expressible settings reported extras: %v", got)
  13. }
  14. // packetSize exports as the v2rayN gecko fields when expressed; a truly
  15. // unexpressible key always is. An inexpressible packetSize stays extra.
  16. in := map[string]any{"password": "pw", "headerType": "dns"}
  17. if got, want := extraSalamanderKeys(in, false), []string{"headerType"}; !reflect.DeepEqual(got, want) {
  18. t.Fatalf("extraSalamanderKeys = %v, want %v", got, want)
  19. }
  20. full := map[string]any{"password": "pw", "packetSize": "512-1200", "headerType": "dns"}
  21. if got, want := extraSalamanderKeys(full, true), []string{"headerType"}; !reflect.DeepEqual(got, want) {
  22. t.Fatalf("expressed packetSize not excluded: %v, want %v", got, want)
  23. }
  24. if got, want := extraSalamanderKeys(full, false), []string{"headerType", "packetSize"}; !reflect.DeepEqual(got, want) {
  25. t.Fatalf("unexpressed packetSize not reported: %v, want %v", got, want)
  26. }
  27. }
  28. func TestGenHysteriaLinkWarnsOnceForUnsupportedSalamanderSettings(t *testing.T) {
  29. makeInbound := func(id int, settings string) *model.Inbound {
  30. return &model.Inbound{
  31. Id: id, Listen: "203.0.113.1", Port: 443, Protocol: model.Hysteria,
  32. Settings: `{"version":2,"clients":[{"auth":"secret","email":"user"}]}`,
  33. StreamSettings: `{"security":"tls","finalmask":{"udp":[{"type":"salamander","settings":` + settings + `}]}}`,
  34. }
  35. }
  36. countWarnings := func(id int) int {
  37. needle := "inbound " + strconv.Itoa(id) + ": salamander settings"
  38. count := 0
  39. for _, line := range logger.GetLogs(100, "warning") {
  40. if strings.Contains(line, needle) {
  41. count++
  42. }
  43. }
  44. return count
  45. }
  46. const standardID = 910001
  47. (&SubService{}).genHysteriaLink(makeInbound(standardID, `{"password":"pw"}`), "user")
  48. if got := countWarnings(standardID); got != 0 {
  49. t.Fatalf("password-only warning count = %d, want 0", got)
  50. }
  51. const unsupportedID = 910002
  52. in := makeInbound(unsupportedID, `{"password":"pw","headerType":"dns"}`)
  53. (&SubService{}).genHysteriaLink(in, "user")
  54. (&SubService{}).genHysteriaLink(in, "user")
  55. if got := countWarnings(unsupportedID); got != 1 {
  56. t.Fatalf("unsupported-settings warning count = %d, want 1", got)
  57. }
  58. }
  59. // A mask with BOTH an expressible packetSize and another key must still warn
  60. // about the leftover key while emitting the gecko URI.
  61. func TestGenHysteriaLinkGeckoStillWarnsOnExtraKeys(t *testing.T) {
  62. in := &model.Inbound{
  63. Id: 910003, Listen: "203.0.113.1", Port: 443, Protocol: model.Hysteria,
  64. Settings: `{"version":2,"clients":[{"auth":"secret","email":"user"}]}`,
  65. StreamSettings: `{"security":"tls","finalmask":{"udp":[{"type":"salamander","settings":{"password":"pw","packetSize":"512-1200","headerType":"dns"}}]}}`,
  66. }
  67. got := (&SubService{}).genHysteriaLink(in, "user")
  68. if !strings.Contains(got, "obfs=gecko") {
  69. t.Fatalf("gecko not emitted for valid packetSize:\n %s", got)
  70. }
  71. needle := "inbound 910003: salamander settings"
  72. found := 0
  73. for _, line := range logger.GetLogs(100, "warning") {
  74. if strings.Contains(line, needle) {
  75. found++
  76. }
  77. }
  78. if found == 0 {
  79. t.Fatal("leftover salamander key did not warn alongside the gecko export")
  80. }
  81. }