xray_bind_conflict_test.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. package service
  2. import (
  3. "encoding/json"
  4. "strconv"
  5. "strings"
  6. "testing"
  7. "github.com/mhsanaei/3x-ui/v3/internal/amneziawgnet"
  8. "github.com/mhsanaei/3x-ui/v3/internal/database"
  9. "github.com/mhsanaei/3x-ui/v3/internal/database/model"
  10. "github.com/mhsanaei/3x-ui/v3/internal/xray"
  11. )
  12. // configFromInbounds builds the config the way the panel does, from raw JSON:
  13. // the probe is only worth anything if it parses what is really written.
  14. func configFromInbounds(t *testing.T, inbounds string) *xray.Config {
  15. t.Helper()
  16. var cfg xray.Config
  17. if err := json.Unmarshal([]byte(`{"inbounds":[`+inbounds+`]}`), &cfg); err != nil {
  18. t.Fatalf("build config: %v", err)
  19. }
  20. return &cfg
  21. }
  22. func TestBindConflicts(t *testing.T) {
  23. const (
  24. relay = `{"listen":"127.0.0.1","port":65101,"protocol":"socks","tag":"relay","settings":{"auth":"password","udp":true,"accounts":[]}}`
  25. user = `{"listen":"0.0.0.0","port":65101,"protocol":"vless","tag":"user","streamSettings":{"network":"tcp"}}`
  26. )
  27. cases := []struct {
  28. name string
  29. inbounds string
  30. running string
  31. want int
  32. }{
  33. {
  34. "same listen, port and tcp",
  35. `{"listen":"0.0.0.0","port":443,"protocol":"vless","tag":"a"},
  36. {"listen":"0.0.0.0","port":443,"protocol":"vmess","tag":"b"}`,
  37. ``, 1,
  38. },
  39. {
  40. "tcp and udp on one port are legal",
  41. `{"listen":"0.0.0.0","port":443,"protocol":"vless","tag":"a"},
  42. {"listen":"0.0.0.0","port":443,"protocol":"hysteria","tag":"b"}`,
  43. ``, 0,
  44. },
  45. {
  46. "kcp moves vless to udp and frees the port",
  47. `{"listen":"0.0.0.0","port":443,"protocol":"vless","tag":"a","streamSettings":{"network":"kcp"}},
  48. {"listen":"0.0.0.0","port":443,"protocol":"vless","tag":"b","streamSettings":{"network":"tcp"}}`,
  49. ``, 0,
  50. },
  51. {
  52. "wildcard listen overlaps a loopback one",
  53. `{"listen":"0.0.0.0","port":8443,"protocol":"vless","tag":"a"},
  54. {"listen":"127.0.0.1","port":8443,"protocol":"trojan","tag":"b"}`,
  55. ``, 1,
  56. },
  57. {
  58. "absent listen means wildcard",
  59. `{"port":8443,"protocol":"vless","tag":"a"},
  60. {"listen":"127.0.0.1","port":8443,"protocol":"trojan","tag":"b"}`,
  61. ``, 1,
  62. },
  63. {
  64. "distinct loopback addresses do not overlap",
  65. `{"listen":"127.0.0.1","port":8443,"protocol":"vless","tag":"a"},
  66. {"listen":"127.0.0.2","port":8443,"protocol":"vless","tag":"b"}`,
  67. ``, 0,
  68. },
  69. {
  70. "port zero is not a bind",
  71. `{"listen":"0.0.0.0","port":0,"protocol":"tunnel","tag":"a"},
  72. {"listen":"0.0.0.0","port":0,"protocol":"vless","tag":"b"}`,
  73. ``, 0,
  74. },
  75. {
  76. "clean config",
  77. `{"listen":"0.0.0.0","port":443,"protocol":"vless","tag":"a"},
  78. {"listen":"127.0.0.1","port":62789,"protocol":"tunnel","tag":"api"},
  79. ` + relay,
  80. ``, 0,
  81. },
  82. {
  83. // The relay the AmneziaWG family lives on: loopback tcp+udp, on the
  84. // same port as a user inbound's tcp.
  85. "amneziawg relay against a tcp inbound",
  86. relay + "," + user, ``, 1,
  87. },
  88. {
  89. "amneziawg relay against a udp inbound",
  90. relay + `,{"listen":"0.0.0.0","port":65101,"protocol":"hysteria","tag":"user"}`,
  91. ``, 1,
  92. },
  93. {
  94. // Proves the relay's transports come from settings.udp and not from a
  95. // blanket "loopback owns everything" rule.
  96. "socks bridge without udp coexists with a udp inbound",
  97. `{"listen":"127.0.0.1","port":65101,"protocol":"socks","tag":"bridge","settings":{"auth":"noauth"}},
  98. {"listen":"0.0.0.0","port":65101,"protocol":"hysteria","tag":"user"}`,
  99. ``, 0,
  100. },
  101. {
  102. "reserved api inbound against a user inbound",
  103. `{"listen":"127.0.0.1","port":62789,"protocol":"tunnel","tag":"api","settings":{"rewriteAddress":"127.0.0.1"}},
  104. {"listen":"0.0.0.0","port":62789,"protocol":"vless","tag":"user"}`,
  105. ``, 1,
  106. },
  107. {
  108. // The core is running this pair right now, so whatever a static read
  109. // says about it, it binds: an established setup is never refused.
  110. "collision the running config already serves",
  111. relay + "," + user, relay + "," + user, 0,
  112. },
  113. {
  114. "collision the running config does not have",
  115. relay + "," + user, user, 1,
  116. },
  117. {
  118. // `::` and `0.0.0.0` on one port is what bindv6only=1 makes legal, so the
  119. // running config excuses it -- but moving one onto the other is not it.
  120. "excused pair whose listen changed into a real collision",
  121. `{"listen":"0.0.0.0","port":443,"protocol":"vless","tag":"a"},
  122. {"listen":"0.0.0.0","port":443,"protocol":"vless","tag":"b"}`,
  123. `{"listen":"::","port":443,"protocol":"vless","tag":"a"},
  124. {"listen":"0.0.0.0","port":443,"protocol":"vless","tag":"b"}`,
  125. 1,
  126. },
  127. {
  128. // The same pair of sockets is the same evidence, whichever order the
  129. // generator happened to emit them in.
  130. "excused pair with its listens swapped stays excused",
  131. `{"listen":"0.0.0.0","port":443,"protocol":"vless","tag":"a"},
  132. {"listen":"::","port":443,"protocol":"vless","tag":"b"}`,
  133. `{"listen":"::","port":443,"protocol":"vless","tag":"a"},
  134. {"listen":"0.0.0.0","port":443,"protocol":"vless","tag":"b"}`,
  135. 0,
  136. },
  137. {
  138. "same pair on another port is still new",
  139. `{"listen":"127.0.0.1","port":65102,"protocol":"socks","tag":"relay","settings":{"auth":"password","udp":true,"accounts":[]}},
  140. {"listen":"0.0.0.0","port":65102,"protocol":"vless","tag":"user","streamSettings":{"network":"tcp"}}`,
  141. relay + "," + user, 1,
  142. },
  143. }
  144. for _, tc := range cases {
  145. t.Run(tc.name, func(t *testing.T) {
  146. var running *xray.Config
  147. if tc.running != "" {
  148. running = configFromInbounds(t, tc.running)
  149. }
  150. got := bindConflicts(configFromInbounds(t, tc.inbounds), running)
  151. if len(got) != tc.want {
  152. t.Fatalf("bindConflicts = %v, want %d conflict(s)", got, tc.want)
  153. }
  154. for _, c := range got {
  155. if c.tagA == "" || c.tagB == "" || c.tagA == c.tagB {
  156. t.Fatalf("a conflict must name both tags, got %+v", c)
  157. }
  158. }
  159. })
  160. }
  161. }
  162. func TestBindConflicts_MessageNamesBothSides(t *testing.T) {
  163. conflicts := bindConflicts(configFromInbounds(t, `
  164. {"listen":"127.0.0.1","port":65101,"protocol":"socks","tag":"relay","settings":{"auth":"password","udp":true,"accounts":[]}},
  165. {"listen":"0.0.0.0","port":65101,"protocol":"vless","tag":"user","streamSettings":{"network":"tcp"}}`), nil)
  166. if len(conflicts) != 1 {
  167. t.Fatalf("want exactly one conflict, got %v", conflicts)
  168. }
  169. msg := conflicts[0].String()
  170. for _, want := range []string{`"relay"`, `"user"`, "127.0.0.1", "65101"} {
  171. if !strings.Contains(msg, want) {
  172. t.Fatalf("conflict message %q must contain %q", msg, want)
  173. }
  174. }
  175. }
  176. // The probe must read what the panel really emits: the AmneziaWG relay is a
  177. // loopback "socks" inbound whose udp flag lives in settings, not streamSettings.
  178. func TestBindConflicts_GeneratedConfig(t *testing.T) {
  179. cases := []struct {
  180. name string
  181. portOff int
  182. running bool
  183. want int
  184. }{
  185. {"user inbound on the relay port", 0, false, 1},
  186. {"already running config", 0, true, 0},
  187. {"user inbound on a free port", 1, false, 0},
  188. }
  189. for _, tc := range cases {
  190. t.Run(tc.name, func(t *testing.T) {
  191. setupSettingTestDB(t)
  192. seedInboundConflict(t, "awg-1", "0.0.0.0", 51820, model.AmneziaWG, ``, amneziawgRoutedSettings)
  193. var awg model.Inbound
  194. if err := database.GetDB().Where("tag = ?", "awg-1").First(&awg).Error; err != nil {
  195. t.Fatalf("read seeded row: %v", err)
  196. }
  197. relayPort := amneziawgnet.SOCKSPortForInbound(awg.Id)
  198. seedInboundConflict(t, "user", "0.0.0.0", relayPort+tc.portOff, model.VLESS, `{"network":"tcp"}`, `{}`)
  199. svc := &XrayService{}
  200. cfg, err := svc.GetXrayConfig()
  201. if err != nil {
  202. t.Fatalf("GetXrayConfig: %v", err)
  203. }
  204. running := (*xray.Config)(nil)
  205. if tc.running {
  206. if running, err = svc.GetXrayConfig(); err != nil {
  207. t.Fatalf("second GetXrayConfig: %v", err)
  208. }
  209. }
  210. got := bindConflicts(cfg, running)
  211. if len(got) != tc.want {
  212. t.Fatalf("bindConflicts = %v, want %d", got, tc.want)
  213. }
  214. if tc.want == 0 {
  215. return
  216. }
  217. msg := got[0].String()
  218. for _, want := range []string{`"awg-1"`, `"user"`, strconv.Itoa(relayPort)} {
  219. if !strings.Contains(msg, want) {
  220. t.Fatalf("conflict message %q must contain %q", msg, want)
  221. }
  222. }
  223. })
  224. }
  225. }