xray_xhttp_session_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. package service
  2. import (
  3. "encoding/json"
  4. "testing"
  5. "github.com/mhsanaei/3x-ui/v3/internal/util/json_util"
  6. )
  7. // xray-core v26.6.22 (#6258) renamed the XHTTP session keys with no fallback.
  8. // The lift must rewrite stored configs at config-generation time so pre-upgrade
  9. // inbounds/outbounds keep working without a manual re-save.
  10. func TestLiftXhttpSessionIDKeys(t *testing.T) {
  11. t.Run("lifts legacy keys and drops them", func(t *testing.T) {
  12. stream := map[string]any{
  13. "xhttpSettings": map[string]any{
  14. "sessionPlacement": "cookie",
  15. "sessionKey": "x_session",
  16. },
  17. }
  18. if !liftXhttpSessionIDKeys(stream) {
  19. t.Fatal("expected changed=true")
  20. }
  21. xhttp := stream["xhttpSettings"].(map[string]any)
  22. if xhttp["sessionIDPlacement"] != "cookie" || xhttp["sessionIDKey"] != "x_session" {
  23. t.Fatalf("renamed keys missing: %#v", xhttp)
  24. }
  25. if _, ok := xhttp["sessionPlacement"]; ok {
  26. t.Fatal("legacy sessionPlacement still present")
  27. }
  28. if _, ok := xhttp["sessionKey"]; ok {
  29. t.Fatal("legacy sessionKey still present")
  30. }
  31. })
  32. t.Run("keeps an explicit new key over the legacy one", func(t *testing.T) {
  33. stream := map[string]any{
  34. "xhttpSettings": map[string]any{
  35. "sessionPlacement": "cookie",
  36. "sessionIDPlacement": "header",
  37. },
  38. }
  39. liftXhttpSessionIDKeys(stream)
  40. xhttp := stream["xhttpSettings"].(map[string]any)
  41. if xhttp["sessionIDPlacement"] != "header" {
  42. t.Fatalf("explicit new key was overwritten: %v", xhttp["sessionIDPlacement"])
  43. }
  44. })
  45. t.Run("no-op without xhttpSettings or legacy keys", func(t *testing.T) {
  46. if liftXhttpSessionIDKeys(map[string]any{"wsSettings": map[string]any{}}) {
  47. t.Fatal("expected no change for non-xhttp stream")
  48. }
  49. if liftXhttpSessionIDKeys(map[string]any{"xhttpSettings": map[string]any{"path": "/"}}) {
  50. t.Fatal("expected no change when no legacy keys present")
  51. }
  52. })
  53. }
  54. func TestLiftOutboundsXhttpSessionIDKeys(t *testing.T) {
  55. raw := json_util.RawMessage(`[{"protocol":"vless","streamSettings":{"network":"xhttp","xhttpSettings":{"sessionKey":"x_session","sessionPlacement":"query"}}}]`)
  56. out := liftOutboundsXhttpSessionIDKeys(raw)
  57. var parsed []map[string]any
  58. if err := json.Unmarshal(out, &parsed); err != nil {
  59. t.Fatalf("unmarshal rewritten outbounds: %v", err)
  60. }
  61. xhttp := parsed[0]["streamSettings"].(map[string]any)["xhttpSettings"].(map[string]any)
  62. if xhttp["sessionIDKey"] != "x_session" || xhttp["sessionIDPlacement"] != "query" {
  63. t.Fatalf("outbound keys not lifted: %#v", xhttp)
  64. }
  65. if _, ok := xhttp["sessionKey"]; ok {
  66. t.Fatal("legacy sessionKey survived in outbound")
  67. }
  68. // Unchanged input must return byte-identical output (no spurious hot-reload).
  69. clean := json_util.RawMessage(`[{"protocol":"freedom"}]`)
  70. if got := liftOutboundsXhttpSessionIDKeys(clean); string(got) != string(clean) {
  71. t.Fatalf("clean outbounds were rewritten: %s", got)
  72. }
  73. }