1
0

hysteria_server_description_test.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package sub
  2. import (
  3. "encoding/json"
  4. "strings"
  5. "testing"
  6. "github.com/mhsanaei/3x-ui/v3/internal/database/model"
  7. )
  8. // #6738: without the host's Description on hysteria(2):// links, Happ falls back
  9. // to its own "Hysteria | hysteria | TLS" caption on a host that also serves VLESS.
  10. func TestGenHysteriaLinkAppendsHostServerDescription(t *testing.T) {
  11. tests := map[string]struct {
  12. version int
  13. scheme string
  14. }{
  15. "hysteria v1": {version: 1, scheme: "hysteria://"},
  16. "hysteria v2": {version: 2, scheme: "hysteria2://"},
  17. }
  18. for name, tc := range tests {
  19. t.Run(name, func(t *testing.T) {
  20. host := &model.Host{
  21. Address: "hy.example.com", Port: 443,
  22. Remark: "Poland", ServerDescription: "Wi-Fi",
  23. }
  24. stream := map[string]any{
  25. "security": "tls",
  26. "externalProxy": []any{hostToExternalProxyMap(host, "hy.example.com", 443)},
  27. }
  28. rawStream, err := json.Marshal(stream)
  29. if err != nil {
  30. t.Fatalf("marshal stream settings: %v", err)
  31. }
  32. // The inbound's own `version` picks the hysteria vs hysteria2 scheme.
  33. rawSettings, err := json.Marshal(map[string]any{
  34. "version": tc.version,
  35. "clients": []any{map[string]any{"auth": "secret", "email": "user"}},
  36. })
  37. if err != nil {
  38. t.Fatalf("marshal inbound settings: %v", err)
  39. }
  40. in := &model.Inbound{
  41. Id: 920010, Listen: "203.0.113.1", Port: 443, Protocol: model.Hysteria,
  42. Remark: "hy", StreamSettings: string(rawStream), Settings: string(rawSettings),
  43. }
  44. got := (&SubService{}).genHysteriaLink(in, "user")
  45. if !strings.HasPrefix(got, tc.scheme) {
  46. t.Fatalf("link scheme changed.\n got: %s\nwant prefix: %s", got, tc.scheme)
  47. }
  48. // base64("Wi-Fi"), matching the reporter's subscription output.
  49. if !strings.HasSuffix(got, "?serverDescription=V2ktRmk=") {
  50. t.Fatalf("host serverDescription missing from fragment.\n got: %s\nwant suffix: ?serverDescription=V2ktRmk=", got)
  51. }
  52. })
  53. }
  54. }