udphop_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package maskcompat
  2. import (
  3. "encoding/json"
  4. "testing"
  5. "github.com/xtls/xray-core/infra/conf"
  6. "github.com/xtls/xray-core/transport/internet/finalmask/udphop"
  7. )
  8. // coreUDPHop builds a finalmask's UDP masks through conf.Mask, the loader the core
  9. // calls at startup, and returns the udphop config the client would hop with, if any.
  10. func coreUDPHop(t *testing.T, finalmask map[string]any) *udphop.Config {
  11. t.Helper()
  12. raw, err := json.Marshal(finalmask)
  13. if err != nil {
  14. t.Fatalf("marshal finalmask: %v", err)
  15. }
  16. var fm conf.FinalMask
  17. if err := json.Unmarshal(raw, &fm); err != nil {
  18. t.Fatalf("decode finalmask: %v", err)
  19. }
  20. for _, mask := range fm.Udp {
  21. built, err := mask.Build(false)
  22. if err != nil {
  23. t.Fatalf("the core refuses UDP mask %q: %v", mask.Type, err)
  24. }
  25. if hop, ok := built.(*udphop.Config); ok {
  26. return hop
  27. }
  28. }
  29. return nil
  30. }
  31. func decodeFinalmask(t *testing.T, s string) map[string]any {
  32. t.Helper()
  33. var fm map[string]any
  34. if err := json.Unmarshal([]byte(s), &fm); err != nil {
  35. t.Fatalf("decode finalmask: %v", err)
  36. }
  37. return fm
  38. }
  39. func TestUpgradeLegacyUDPHop(t *testing.T) {
  40. fm := decodeFinalmask(t, `{"udp":[{"type":"salamander","settings":{"password":"p"}}],
  41. "quicParams":{"congestion":"bbr","udpHop":{"ports":"20000-20003","interval":"5-10"}}}`)
  42. if hop := coreUDPHop(t, fm); hop != nil {
  43. t.Fatal("the core hops on quicParams.udpHop again; the upgrade is no longer needed")
  44. }
  45. if !UpgradeLegacyUDPHop(fm) {
  46. t.Fatal("UpgradeLegacyUDPHop reported no change for a legacy hop")
  47. }
  48. got, _ := json.Marshal(fm)
  49. const want = `{"quicParams":{"congestion":"bbr"},"udp":[{"settings":{"password":"p"},"type":"salamander"},{"settings":{"interval":"5-10","mode":"intervalRemote","remotePorts":"20000-20003"},"type":"udphop"}]}`
  50. if string(got) != want {
  51. t.Fatalf("upgraded finalmask\n got: %s\nwant: %s", got, want)
  52. }
  53. hop := coreUDPHop(t, fm)
  54. if hop == nil || !hop.Remote || hop.Local || hop.IntervalMin != 5 || hop.IntervalMax != 10 ||
  55. len(hop.RemotePorts) != 4 || hop.RemotePorts[0] != 20000 || hop.RemotePorts[3] != 20003 {
  56. t.Fatalf("the core hops with %+v, want remote ports 20000-20003 every 5-10s", hop)
  57. }
  58. }
  59. func TestUpgradeLegacyUDPHopEdgeCases(t *testing.T) {
  60. tests := []struct {
  61. name string
  62. input string
  63. want string
  64. }{
  65. {
  66. name: "an explicit udphop mask wins over the dead key",
  67. input: `{"udp":[{"type":"udphop","settings":{"mode":"perConnRemote","remotePorts":"443"}}],"quicParams":{"udpHop":{"ports":"20000-30000"}}}`,
  68. want: `{"udp":[{"settings":{"mode":"perConnRemote","remotePorts":"443"},"type":"udphop"}]}`,
  69. },
  70. {
  71. name: "a hop without ports only loses the dead key",
  72. input: `{"quicParams":{"udpHop":{"interval":"5-10"}}}`,
  73. want: `{}`,
  74. },
  75. {
  76. name: "no interval keeps the core's 30s default",
  77. input: `{"quicParams":{"udpHop":{"ports":"443,8443"}}}`,
  78. want: `{"udp":[{"settings":{"mode":"intervalRemote","remotePorts":"443,8443"},"type":"udphop"}]}`,
  79. },
  80. }
  81. for _, tc := range tests {
  82. t.Run(tc.name, func(t *testing.T) {
  83. fm := decodeFinalmask(t, tc.input)
  84. if !UpgradeLegacyUDPHop(fm) {
  85. t.Fatal("UpgradeLegacyUDPHop reported no change")
  86. }
  87. got, _ := json.Marshal(fm)
  88. if string(got) != tc.want {
  89. t.Fatalf("upgraded finalmask\n got: %s\nwant: %s", got, tc.want)
  90. }
  91. coreUDPHop(t, fm)
  92. })
  93. }
  94. current := decodeFinalmask(t, `{"udp":[{"type":"udphop","settings":{"mode":"intervalRemote","remotePorts":"443"}}],"quicParams":{"congestion":"bbr"}}`)
  95. if UpgradeLegacyUDPHop(current) {
  96. t.Fatal("UpgradeLegacyUDPHop rewrote a finalmask without the legacy key")
  97. }
  98. }