hot_diff_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. package xray
  2. import (
  3. "os"
  4. "strings"
  5. "testing"
  6. xuilogger "github.com/mhsanaei/3x-ui/v3/internal/logger"
  7. "github.com/mhsanaei/3x-ui/v3/internal/util/json_util"
  8. "github.com/op/go-logging"
  9. )
  10. func TestMain(m *testing.M) {
  11. // ComputeHotDiff logs the section that blocks a hot apply; the package
  12. // logger must exist before any test exercises a blocked path.
  13. xuilogger.InitLogger(logging.ERROR)
  14. os.Exit(m.Run())
  15. }
  16. func makeHotConfig() *Config {
  17. return &Config{
  18. LogConfig: json_util.RawMessage(`{"loglevel":"warning"}`),
  19. RouterConfig: json_util.RawMessage(`{"domainStrategy":"AsIs","rules":[{"type":"field","inboundTag":["api"],"outboundTag":"api"}]}`),
  20. OutboundConfigs: json_util.RawMessage(`[{"protocol":"freedom","tag":"direct"},{"protocol":"blackhole","tag":"blocked"}]`),
  21. Policy: json_util.RawMessage(`{}`),
  22. API: json_util.RawMessage(`{"services":["HandlerService","StatsService","RoutingService"],"tag":"api"}`),
  23. Stats: json_util.RawMessage(`{}`),
  24. Metrics: json_util.RawMessage(`{}`),
  25. InboundConfigs: []InboundConfig{
  26. {
  27. Port: 62789,
  28. Protocol: "tunnel",
  29. Tag: "api",
  30. Listen: json_util.RawMessage(`"127.0.0.1"`),
  31. Settings: json_util.RawMessage(`{}`),
  32. },
  33. {
  34. Port: 1080,
  35. Protocol: "vless",
  36. Tag: "inbound-1080",
  37. Listen: json_util.RawMessage(`"0.0.0.0"`),
  38. Settings: json_util.RawMessage(`{"clients":[]}`),
  39. },
  40. },
  41. }
  42. }
  43. func TestComputeHotDiff_NoChanges(t *testing.T) {
  44. diff, ok := ComputeHotDiff(makeHotConfig(), makeHotConfig())
  45. if !ok {
  46. t.Fatal("identical configs must be hot-appliable")
  47. }
  48. if !diff.Empty() {
  49. t.Fatalf("identical configs must produce an empty diff, got %+v", diff)
  50. }
  51. }
  52. func TestComputeHotDiff_FormattingOnlyChangeIsEmptyDiff(t *testing.T) {
  53. oldCfg := makeHotConfig()
  54. newCfg := makeHotConfig()
  55. // Reformat every section the way a frontend textarea save would.
  56. newCfg.LogConfig = json_util.RawMessage("{\n \"loglevel\": \"warning\"\n}")
  57. newCfg.Policy = json_util.RawMessage("{ }")
  58. newCfg.API = json_util.RawMessage("{\n \"services\": [\"HandlerService\", \"StatsService\", \"RoutingService\"],\n \"tag\": \"api\"\n}")
  59. newCfg.OutboundConfigs = json_util.RawMessage("[\n {\"protocol\": \"freedom\", \"tag\": \"direct\"},\n {\"protocol\": \"blackhole\", \"tag\": \"blocked\"}\n]")
  60. newCfg.InboundConfigs[1].Settings = json_util.RawMessage("{\n \"clients\": []\n}")
  61. diff, ok := ComputeHotDiff(oldCfg, newCfg)
  62. if !ok {
  63. t.Fatal("formatting-only change must be hot-appliable")
  64. }
  65. if len(diff.RemovedInboundTags) != 0 || len(diff.AddedInbounds) != 0 ||
  66. len(diff.RemovedOutboundTags) != 0 || len(diff.AddedOutbounds) != 0 {
  67. t.Fatalf("formatting-only change must produce no handler ops, got %+v", diff)
  68. }
  69. }
  70. func TestComputeHotDiff_CanonicalEquality(t *testing.T) {
  71. // Key reorder in a static section (the DNS editor rebuilds the object on
  72. // save) must not read as a change.
  73. oldCfg := makeHotConfig()
  74. oldCfg.DNSConfig = json_util.RawMessage(`{"servers":["1.1.1.1"],"queryStrategy":"UseIP","tag":"dns-in"}`)
  75. newCfg := makeHotConfig()
  76. newCfg.DNSConfig = json_util.RawMessage(`{"tag":"dns-in","queryStrategy":"UseIP","servers":["1.1.1.1"]}`)
  77. diff, ok := ComputeHotDiff(oldCfg, newCfg)
  78. if !ok || !diff.Empty() {
  79. t.Fatalf("dns key reorder must be an empty hot diff, ok=%v diff=%+v", ok, diff)
  80. }
  81. // Explicit null and an absent section are the same thing.
  82. newCfg = makeHotConfig()
  83. newCfg.FakeDNS = json_util.RawMessage(`null`)
  84. diff, ok = ComputeHotDiff(makeHotConfig(), newCfg)
  85. if !ok || !diff.Empty() {
  86. t.Fatalf("fakedns null vs absent must be an empty hot diff, ok=%v diff=%+v", ok, diff)
  87. }
  88. // A real DNS change still forces a restart — there is no reload API.
  89. newCfg = makeHotConfig()
  90. newCfg.DNSConfig = json_util.RawMessage(`{"servers":["8.8.8.8"]}`)
  91. if _, ok := ComputeHotDiff(makeHotConfig(), newCfg); ok {
  92. t.Fatal("real dns change must force a restart")
  93. }
  94. // Large integers keep full precision during normalization: two values
  95. // that only differ past float64 precision must still read as a change.
  96. oldCfg = makeHotConfig()
  97. oldCfg.Policy = json_util.RawMessage(`{"big":9007199254740993}`)
  98. newCfg = makeHotConfig()
  99. newCfg.Policy = json_util.RawMessage(`{"big":9007199254740992}`)
  100. if _, ok := ComputeHotDiff(oldCfg, newCfg); ok {
  101. t.Fatal("values differing past float64 precision must not compare equal")
  102. }
  103. // Reordered keys inside the first (default) outbound must not force a
  104. // restart — the form editor rebuilds the object on save.
  105. oldCfg = makeHotConfig()
  106. oldCfg.OutboundConfigs = json_util.RawMessage(`[{"protocol":"freedom","settings":{"domainStrategy":"AsIs"},"tag":"direct"},{"protocol":"blackhole","tag":"blocked"}]`)
  107. newCfg = makeHotConfig()
  108. newCfg.OutboundConfigs = json_util.RawMessage(`[{"tag":"direct","settings":{"domainStrategy":"AsIs"},"protocol":"freedom"},{"protocol":"blackhole","tag":"blocked"}]`)
  109. diff, ok = ComputeHotDiff(oldCfg, newCfg)
  110. if !ok || !diff.Empty() {
  111. t.Fatalf("first outbound key reorder must be an empty hot diff, ok=%v diff=%+v", ok, diff)
  112. }
  113. }
  114. func TestComputeHotDiff_StaticSectionChangeNeedsRestart(t *testing.T) {
  115. newCfg := makeHotConfig()
  116. newCfg.LogConfig = json_util.RawMessage(`{"loglevel":"debug"}`)
  117. if _, ok := ComputeHotDiff(makeHotConfig(), newCfg); ok {
  118. t.Fatal("log change must force a restart")
  119. }
  120. newCfg = makeHotConfig()
  121. newCfg.DNSConfig = json_util.RawMessage(`{"servers":["1.1.1.1"]}`)
  122. if _, ok := ComputeHotDiff(makeHotConfig(), newCfg); ok {
  123. t.Fatal("dns change must force a restart")
  124. }
  125. newCfg = makeHotConfig()
  126. newCfg.Observatory = json_util.RawMessage(`{"subjectSelector":["wg"]}`)
  127. if _, ok := ComputeHotDiff(makeHotConfig(), newCfg); ok {
  128. t.Fatal("observatory change must force a restart")
  129. }
  130. }
  131. func TestComputeHotDiff_InboundAddRemoveChange(t *testing.T) {
  132. oldCfg := makeHotConfig()
  133. newCfg := makeHotConfig()
  134. // change existing
  135. newCfg.InboundConfigs[1].Settings = json_util.RawMessage(`{"clients":[{"email":"a"}]}`)
  136. // add new
  137. newCfg.InboundConfigs = append(newCfg.InboundConfigs, InboundConfig{
  138. Port: 2080, Protocol: "vmess", Tag: "inbound-2080",
  139. Settings: json_util.RawMessage(`{}`),
  140. })
  141. diff, ok := ComputeHotDiff(oldCfg, newCfg)
  142. if !ok {
  143. t.Fatal("inbound-only change must be hot-appliable")
  144. }
  145. if len(diff.RemovedInboundTags) != 1 || diff.RemovedInboundTags[0] != "inbound-1080" {
  146. t.Fatalf("expected changed inbound to be removed, got %v", diff.RemovedInboundTags)
  147. }
  148. if len(diff.AddedInbounds) != 2 {
  149. t.Fatalf("expected re-add + new add, got %d", len(diff.AddedInbounds))
  150. }
  151. if diff.RoutingConfig != nil || len(diff.AddedOutbounds) != 0 || len(diff.RemovedOutboundTags) != 0 {
  152. t.Fatalf("unexpected non-inbound operations: %+v", diff)
  153. }
  154. }
  155. func TestComputeHotDiff_ApiInboundChangeNeedsRestart(t *testing.T) {
  156. newCfg := makeHotConfig()
  157. newCfg.InboundConfigs[0].Port = 62790
  158. if _, ok := ComputeHotDiff(makeHotConfig(), newCfg); ok {
  159. t.Fatal("api inbound change must force a restart")
  160. }
  161. }
  162. func TestComputeHotDiff_OutboundChangeAndReorder(t *testing.T) {
  163. oldCfg := makeHotConfig()
  164. newCfg := makeHotConfig()
  165. // change a non-first outbound + add one
  166. newCfg.OutboundConfigs = json_util.RawMessage(`[{"protocol":"freedom","tag":"direct"},{"protocol":"blackhole","settings":{},"tag":"blocked"},{"protocol":"socks","tag":"warp"}]`)
  167. diff, ok := ComputeHotDiff(oldCfg, newCfg)
  168. if !ok {
  169. t.Fatal("outbound-only change must be hot-appliable")
  170. }
  171. if len(diff.RemovedOutboundTags) != 1 || diff.RemovedOutboundTags[0] != "blocked" {
  172. t.Fatalf("expected changed outbound to be removed, got %v", diff.RemovedOutboundTags)
  173. }
  174. if len(diff.AddedOutbounds) != 2 {
  175. t.Fatalf("expected re-add + new add, got %d", len(diff.AddedOutbounds))
  176. }
  177. for _, raw := range diff.AddedOutbounds {
  178. if !strings.Contains(string(raw), `"tag"`) {
  179. t.Fatalf("added outbound JSON must be the raw element, got %s", raw)
  180. }
  181. }
  182. // pure reorder of non-first outbounds must be a no-op
  183. reordered := makeHotConfig()
  184. reordered.OutboundConfigs = json_util.RawMessage(`[{"protocol":"freedom","tag":"direct"},{"protocol":"socks","tag":"warp"},{"protocol":"blackhole","tag":"blocked"}]`)
  185. base := makeHotConfig()
  186. base.OutboundConfigs = json_util.RawMessage(`[{"protocol":"freedom","tag":"direct"},{"protocol":"blackhole","tag":"blocked"},{"protocol":"socks","tag":"warp"}]`)
  187. diff, ok = ComputeHotDiff(base, reordered)
  188. if !ok || !diff.Empty() {
  189. t.Fatalf("reorder of non-first outbounds must be an empty hot diff, ok=%v diff=%+v", ok, diff)
  190. }
  191. }
  192. func TestComputeHotDiff_FirstOutboundChangeNeedsRestart(t *testing.T) {
  193. newCfg := makeHotConfig()
  194. // change the default (first) outbound content
  195. newCfg.OutboundConfigs = json_util.RawMessage(`[{"protocol":"freedom","settings":{"domainStrategy":"UseIP"},"tag":"direct"},{"protocol":"blackhole","tag":"blocked"}]`)
  196. if _, ok := ComputeHotDiff(makeHotConfig(), newCfg); ok {
  197. t.Fatal("changing the default outbound must force a restart")
  198. }
  199. // swap which outbound comes first
  200. newCfg = makeHotConfig()
  201. newCfg.OutboundConfigs = json_util.RawMessage(`[{"protocol":"blackhole","tag":"blocked"},{"protocol":"freedom","tag":"direct"}]`)
  202. if _, ok := ComputeHotDiff(makeHotConfig(), newCfg); ok {
  203. t.Fatal("changing the first outbound must force a restart")
  204. }
  205. }
  206. func TestComputeHotDiff_TaglessOutboundNeedsRestart(t *testing.T) {
  207. newCfg := makeHotConfig()
  208. newCfg.OutboundConfigs = json_util.RawMessage(`[{"protocol":"freedom","tag":"direct"},{"protocol":"blackhole"}]`)
  209. if _, ok := ComputeHotDiff(makeHotConfig(), newCfg); ok {
  210. t.Fatal("tagless outbound must force a restart")
  211. }
  212. }
  213. func TestComputeHotDiff_RoutingRulesChange(t *testing.T) {
  214. newCfg := makeHotConfig()
  215. newCfg.RouterConfig = json_util.RawMessage(`{"domainStrategy":"AsIs","rules":[{"type":"field","inboundTag":["api"],"outboundTag":"api"},{"type":"field","ip":["geoip:private"],"outboundTag":"blocked"}]}`)
  216. diff, ok := ComputeHotDiff(makeHotConfig(), newCfg)
  217. if !ok {
  218. t.Fatal("rules-only routing change must be hot-appliable")
  219. }
  220. if diff.RoutingConfig == nil {
  221. t.Fatal("routing diff must carry the new routing section")
  222. }
  223. // balancers are reloadable too
  224. newCfg = makeHotConfig()
  225. newCfg.RouterConfig = json_util.RawMessage(`{"domainStrategy":"AsIs","rules":[],"balancers":[{"tag":"b1","selector":["wg"]}]}`)
  226. if _, ok := ComputeHotDiff(makeHotConfig(), newCfg); !ok {
  227. t.Fatal("balancer-only routing change must be hot-appliable")
  228. }
  229. }
  230. func TestComputeHotDiff_RoutingStrategyChangeNeedsRestart(t *testing.T) {
  231. newCfg := makeHotConfig()
  232. newCfg.RouterConfig = json_util.RawMessage(`{"domainStrategy":"IPIfNonMatch","rules":[{"type":"field","inboundTag":["api"],"outboundTag":"api"}]}`)
  233. if _, ok := ComputeHotDiff(makeHotConfig(), newCfg); ok {
  234. t.Fatal("domainStrategy change must force a restart")
  235. }
  236. }