1
0

hot_diff.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  1. package xray
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "github.com/mhsanaei/3x-ui/v3/internal/logger"
  6. "github.com/mhsanaei/3x-ui/v3/internal/util/json_util"
  7. )
  8. // HotDiff describes the gRPC API operations needed to bring a running Xray
  9. // instance from one generated config to another without restarting the
  10. // process. It only covers the sections Xray can reload at runtime: inbounds,
  11. // outbounds and routing rules/balancers.
  12. type HotDiff struct {
  13. RemovedInboundTags []string
  14. AddedInbounds [][]byte
  15. RemovedUsers []UserOp
  16. AddedUsers []UserOp
  17. RemovedOutboundTags []string
  18. AddedOutbounds [][]byte
  19. RoutingConfig []byte // full new routing section; nil when unchanged
  20. }
  21. // UserOp is a per-user AlterInbound operation; User is nil for removals.
  22. type UserOp struct {
  23. Tag string
  24. Protocol string
  25. Email string
  26. User map[string]any
  27. }
  28. // Empty reports whether the diff contains no operations.
  29. func (d *HotDiff) Empty() bool {
  30. return len(d.RemovedInboundTags) == 0 &&
  31. len(d.AddedInbounds) == 0 &&
  32. len(d.RemovedUsers) == 0 &&
  33. len(d.AddedUsers) == 0 &&
  34. len(d.RemovedOutboundTags) == 0 &&
  35. len(d.AddedOutbounds) == 0 &&
  36. d.RoutingConfig == nil
  37. }
  38. // ComputeHotDiff compares two generated configs and returns the API operations
  39. // that transform a running instance from oldCfg to newCfg. ok is false when
  40. // the change touches anything that has no runtime reload API (log, dns,
  41. // policy, ...) and therefore requires a full process restart.
  42. func ComputeHotDiff(oldCfg, newCfg *Config) (*HotDiff, bool) {
  43. if oldCfg == nil || newCfg == nil {
  44. return nil, false
  45. }
  46. // Sections without a reload API must be semantically identical.
  47. // Comparison is whitespace-insensitive: a template save that merely
  48. // reformats the JSON (frontend textarea, API clients) must not be
  49. // mistaken for a real change that forces a restart.
  50. static := []struct {
  51. name string
  52. old, new json_util.RawMessage
  53. }{
  54. {"log", oldCfg.LogConfig, newCfg.LogConfig},
  55. {"dns", oldCfg.DNSConfig, newCfg.DNSConfig},
  56. {"transport", oldCfg.Transport, newCfg.Transport},
  57. {"policy", oldCfg.Policy, newCfg.Policy},
  58. {"api", oldCfg.API, newCfg.API},
  59. {"stats", oldCfg.Stats, newCfg.Stats},
  60. {"reverse", oldCfg.Reverse, newCfg.Reverse},
  61. {"fakedns", oldCfg.FakeDNS, newCfg.FakeDNS},
  62. {"observatory", oldCfg.Observatory, newCfg.Observatory},
  63. {"burstObservatory", oldCfg.BurstObservatory, newCfg.BurstObservatory},
  64. {"metrics", oldCfg.Metrics, newCfg.Metrics},
  65. {"geodata", oldCfg.Geodata, newCfg.Geodata},
  66. {"env", oldCfg.Env, newCfg.Env},
  67. }
  68. for _, section := range static {
  69. if !rawEqualNormalized(section.old, section.new) {
  70. logger.Debug("hot diff: section [", section.name, "] changed and has no reload API")
  71. return nil, false
  72. }
  73. }
  74. diff := &HotDiff{}
  75. if ok := diffInbounds(oldCfg, newCfg, diff); !ok {
  76. logger.Debug("hot diff: inbound change is not API-applicable")
  77. return nil, false
  78. }
  79. if ok := diffOutbounds(oldCfg, newCfg, diff); !ok {
  80. logger.Debug("hot diff: outbound change is not API-applicable (default outbound or tags)")
  81. return nil, false
  82. }
  83. if ok := diffRouting(oldCfg, newCfg, diff); !ok {
  84. logger.Debug("hot diff: routing change is not API-applicable (domainStrategy or section shape)")
  85. return nil, false
  86. }
  87. return diff, true
  88. }
  89. // diffInbounds fills diff with inbound removals/additions (a changed inbound
  90. // becomes remove+add). The api inbound carries the gRPC server the panel is
  91. // talking through, so any change touching it forces a restart.
  92. func diffInbounds(oldCfg, newCfg *Config, diff *HotDiff) bool {
  93. oldByTag, ok := inboundsByTag(oldCfg.InboundConfigs)
  94. if !ok {
  95. return false
  96. }
  97. newByTag, ok := inboundsByTag(newCfg.InboundConfigs)
  98. if !ok {
  99. return false
  100. }
  101. apiTag := apiTagFromConfig(newCfg.API)
  102. for i := range oldCfg.InboundConfigs {
  103. oldIb := &oldCfg.InboundConfigs[i]
  104. newIb, exists := newByTag[oldIb.Tag]
  105. if exists && inboundEqualNormalized(oldIb, newIb) {
  106. continue
  107. }
  108. if oldIb.Tag == apiTag || oldIb.Tag == "api" {
  109. return false
  110. }
  111. if exists && (inboundHasReverseClient(oldIb) || inboundHasReverseClient(newIb)) {
  112. logger.Debug("hot diff: inbound [", oldIb.Tag, "] carries a reverse-tagged client, forcing a full restart instead of a hot swap")
  113. return false
  114. }
  115. if exists && diffInboundUsers(oldIb, newIb, diff) {
  116. continue
  117. }
  118. if exists && (inboundUsesReality(oldIb) || inboundUsesReality(newIb)) {
  119. logger.Debug("hot diff: inbound [", oldIb.Tag, "] REALITY configuration changed; a gRPC remove+add does not reliably rebuild the REALITY authenticator, forcing a full restart")
  120. return false
  121. }
  122. if exists && (inboundUsesTproxy(oldIb) || inboundUsesTproxy(newIb)) {
  123. logger.Debug("hot diff: inbound [", oldIb.Tag, "] is a TPROXY target; a gRPC add reports success but does not reliably bind a working listener, forcing a full restart instead of a hot swap")
  124. return false
  125. }
  126. if exists && (inboundUsesSocksAccounts(oldIb) || inboundUsesSocksAccounts(newIb)) {
  127. logger.Debug("hot diff: inbound [", oldIb.Tag, "] is a password-auth SOCKS5 inbound (e.g. internal/amneziawgnet's per-peer relay); a gRPC remove+add reports success but was observed in production to silently drop an account, forcing a full restart instead of a hot swap")
  128. return false
  129. }
  130. diff.RemovedInboundTags = append(diff.RemovedInboundTags, oldIb.Tag)
  131. if exists {
  132. raw, err := json.Marshal(newIb)
  133. if err != nil {
  134. return false
  135. }
  136. diff.AddedInbounds = append(diff.AddedInbounds, raw)
  137. }
  138. }
  139. for i := range newCfg.InboundConfigs {
  140. newIb := &newCfg.InboundConfigs[i]
  141. if _, exists := oldByTag[newIb.Tag]; exists {
  142. continue
  143. }
  144. if newIb.Tag == apiTag || newIb.Tag == "api" {
  145. return false
  146. }
  147. if inboundUsesTproxy(newIb) {
  148. logger.Debug("hot diff: new inbound [", newIb.Tag, "] is a TPROXY target (e.g. internal/amneziawg's Xray egress bridge); a gRPC add reports success but does not reliably bind a working listener, forcing a full restart instead of a hot add")
  149. return false
  150. }
  151. if inboundUsesSocksAccounts(newIb) {
  152. logger.Debug("hot diff: new inbound [", newIb.Tag, "] is a password-auth SOCKS5 inbound (e.g. internal/amneziawgnet's per-peer relay); forcing a full restart instead of a hot add, same reasoning as the existing-inbound case above")
  153. return false
  154. }
  155. raw, err := json.Marshal(newIb)
  156. if err != nil {
  157. return false
  158. }
  159. diff.AddedInbounds = append(diff.AddedInbounds, raw)
  160. }
  161. return true
  162. }
  163. var userDiffableProtocols = map[string]struct{}{"vless": {}, "vmess": {}, "trojan": {}}
  164. // diffInboundUsers emits per-user AlterInbound ops when two same-tag inbounds
  165. // differ only in settings.clients, so the handler (and its listener) survives.
  166. func diffInboundUsers(oldIb, newIb *InboundConfig, diff *HotDiff) bool {
  167. if oldIb.Port != newIb.Port || oldIb.Protocol != newIb.Protocol || oldIb.Tag != newIb.Tag {
  168. return false
  169. }
  170. if _, ok := userDiffableProtocols[oldIb.Protocol]; !ok {
  171. return false
  172. }
  173. if !rawEqualNormalized(oldIb.Listen, newIb.Listen) ||
  174. !rawEqualNormalized(oldIb.StreamSettings, newIb.StreamSettings) ||
  175. !rawEqualNormalized(oldIb.Sniffing, newIb.Sniffing) {
  176. return false
  177. }
  178. oldClients, oldRest, ok := splitSettingsClients(oldIb.Settings)
  179. if !ok {
  180. return false
  181. }
  182. newClients, newRest, ok := splitSettingsClients(newIb.Settings)
  183. if !ok {
  184. return false
  185. }
  186. if !bytes.Equal(oldRest, newRest) {
  187. return false
  188. }
  189. for email, oldC := range oldClients {
  190. newC, exists := newClients[email]
  191. if exists && bytes.Equal(oldC.norm, newC.norm) {
  192. continue
  193. }
  194. diff.RemovedUsers = append(diff.RemovedUsers, UserOp{Tag: oldIb.Tag, Protocol: oldIb.Protocol, Email: email})
  195. if exists {
  196. diff.AddedUsers = append(diff.AddedUsers, UserOp{Tag: oldIb.Tag, Protocol: oldIb.Protocol, Email: email, User: newC.user})
  197. }
  198. }
  199. for email, newC := range newClients {
  200. if _, exists := oldClients[email]; !exists {
  201. diff.AddedUsers = append(diff.AddedUsers, UserOp{Tag: oldIb.Tag, Protocol: oldIb.Protocol, Email: email, User: newC.user})
  202. }
  203. }
  204. return true
  205. }
  206. type clientEntry struct {
  207. user map[string]any
  208. norm []byte
  209. }
  210. // splitSettingsClients indexes settings.clients by email and returns the rest of
  211. // the settings in canonical form; ok is false when a client has no unique email.
  212. func splitSettingsClients(raw json_util.RawMessage) (map[string]clientEntry, []byte, bool) {
  213. if len(raw) == 0 {
  214. return nil, nil, false
  215. }
  216. settings := map[string]any{}
  217. decoder := json.NewDecoder(bytes.NewReader(raw))
  218. decoder.UseNumber()
  219. if err := decoder.Decode(&settings); err != nil {
  220. return nil, nil, false
  221. }
  222. clientsRaw, hasClients := settings["clients"].([]any)
  223. if !hasClients {
  224. return nil, nil, false
  225. }
  226. clients := make(map[string]clientEntry, len(clientsRaw))
  227. for _, c := range clientsRaw {
  228. obj, ok := c.(map[string]any)
  229. if !ok {
  230. return nil, nil, false
  231. }
  232. email, _ := obj["email"].(string)
  233. if email == "" {
  234. return nil, nil, false
  235. }
  236. if _, dup := clients[email]; dup {
  237. return nil, nil, false
  238. }
  239. norm, err := json.Marshal(obj)
  240. if err != nil {
  241. return nil, nil, false
  242. }
  243. clients[email] = clientEntry{user: obj, norm: norm}
  244. }
  245. delete(settings, "clients")
  246. rest, err := json.Marshal(settings)
  247. if err != nil {
  248. return nil, nil, false
  249. }
  250. return clients, rest, true
  251. }
  252. func inboundUsesReality(ib *InboundConfig) bool {
  253. if ib == nil || len(ib.StreamSettings) == 0 {
  254. return false
  255. }
  256. var stream struct {
  257. Security string `json:"security"`
  258. }
  259. if err := json.Unmarshal(ib.StreamSettings, &stream); err != nil {
  260. return false
  261. }
  262. return stream.Security == "reality"
  263. }
  264. // inboundUsesTproxy: a sockopt.tproxy inbound (the tunnel protocol's TProxy
  265. // mode) hot-adds over gRPC "successfully" but binds no listener — restart.
  266. func inboundUsesTproxy(ib *InboundConfig) bool {
  267. if ib == nil || len(ib.StreamSettings) == 0 {
  268. return false
  269. }
  270. var stream struct {
  271. Sockopt struct {
  272. Tproxy string `json:"tproxy"`
  273. } `json:"sockopt"`
  274. }
  275. if err := json.Unmarshal(ib.StreamSettings, &stream); err != nil {
  276. return false
  277. }
  278. return stream.Sockopt.Tproxy != "" && stream.Sockopt.Tproxy != "off"
  279. }
  280. // inboundUsesSocksAccounts reports whether an inbound is a password-auth
  281. // SOCKS5 inbound with one or more named accounts -- the shape
  282. // internal/amneziawgnet's per-inbound relay (internal/web/service/xray.go's
  283. // injectAmneziawgnetSocks) is the only generator of in this fork; every
  284. // other SOCKS5 bridge this fork builds (panel egress, per-node egress,
  285. // mtproto egress) uses "noauth" with no per-account identity at all, so this
  286. // check can't accidentally rope in one of those lower-churn bridges.
  287. //
  288. // Real production incident, not a theoretical concern: a single client
  289. // edit under an AmneziaWG inbound left this inbound's settings unchanged in
  290. // every way relevant to accounts.user was already correct in the freshly
  291. // regenerated config, yet the account for a peer whose email contained
  292. // non-ASCII characters silently vanished from the running Xray process
  293. // after a gRPC remove+add hot swap -- while a full process restart (reading
  294. // the same JSON straight from disk) always produced the correct account
  295. // list. socks isn't in userDiffableProtocols (that only covers vless/vmess/
  296. // trojan, which use a wholly different clients+email shape, not
  297. // accounts+user), so without this check any settings drift on this inbound
  298. // -- even one unrelated to the account list itself -- falls through to the
  299. // generic remove+add path and can reproduce the same silent drop. Forcing a
  300. // full restart here is the same defensive choice already made above for
  301. // REALITY and TPROXY.
  302. func inboundUsesSocksAccounts(ib *InboundConfig) bool {
  303. if ib == nil || ib.Protocol != "socks" || len(ib.Settings) == 0 {
  304. return false
  305. }
  306. var settings struct {
  307. Auth string `json:"auth"`
  308. }
  309. if err := json.Unmarshal(ib.Settings, &settings); err != nil {
  310. return false
  311. }
  312. return settings.Auth == "password"
  313. }
  314. func inboundHasReverseClient(ib *InboundConfig) bool {
  315. if ib == nil {
  316. return false
  317. }
  318. var settings struct {
  319. Clients []struct {
  320. Reverse json.RawMessage `json:"reverse"`
  321. } `json:"clients"`
  322. }
  323. if err := json.Unmarshal(ib.Settings, &settings); err != nil {
  324. return false
  325. }
  326. for _, c := range settings.Clients {
  327. if len(c.Reverse) == 0 {
  328. continue
  329. }
  330. var tag any
  331. if err := json.Unmarshal(c.Reverse, &tag); err != nil || tag == nil {
  332. continue
  333. }
  334. return true
  335. }
  336. return false
  337. }
  338. // diffOutbounds fills diff with outbound removals/additions keyed by tag.
  339. // The first outbound is xray's default handler and the API can only append,
  340. // so any change to its identity or content forces a restart. Reordering of
  341. // the remaining outbounds is ignored — routing addresses them by tag.
  342. func diffOutbounds(oldCfg, newCfg *Config, diff *HotDiff) bool {
  343. oldOut, ok := parseOutbounds(oldCfg.OutboundConfigs)
  344. if !ok {
  345. return false
  346. }
  347. newOut, ok := parseOutbounds(newCfg.OutboundConfigs)
  348. if !ok {
  349. return false
  350. }
  351. if (len(oldOut) == 0) != (len(newOut) == 0) {
  352. return false
  353. }
  354. if len(oldOut) > 0 {
  355. if oldOut[0].tag != newOut[0].tag || !bytes.Equal(oldOut[0].norm, newOut[0].norm) {
  356. return false
  357. }
  358. }
  359. oldByTag := make(map[string]outboundEntry, len(oldOut))
  360. for _, e := range oldOut {
  361. oldByTag[e.tag] = e
  362. }
  363. newByTag := make(map[string]outboundEntry, len(newOut))
  364. for _, e := range newOut {
  365. newByTag[e.tag] = e
  366. }
  367. for _, oldE := range oldOut {
  368. newE, exists := newByTag[oldE.tag]
  369. if exists && bytes.Equal(oldE.norm, newE.norm) {
  370. continue
  371. }
  372. diff.RemovedOutboundTags = append(diff.RemovedOutboundTags, oldE.tag)
  373. if exists {
  374. diff.AddedOutbounds = append(diff.AddedOutbounds, newE.raw)
  375. }
  376. }
  377. for _, newE := range newOut {
  378. if _, exists := oldByTag[newE.tag]; !exists {
  379. diff.AddedOutbounds = append(diff.AddedOutbounds, newE.raw)
  380. }
  381. }
  382. return true
  383. }
  384. // diffRouting decides whether the routing change is limited to rules and
  385. // balancers — the only parts RoutingService.AddRule can replace at runtime.
  386. // domainStrategy/domainMatcher and any other key in the section are fixed at
  387. // process start.
  388. func diffRouting(oldCfg, newCfg *Config, diff *HotDiff) bool {
  389. if bytes.Equal(oldCfg.RouterConfig, newCfg.RouterConfig) {
  390. return true
  391. }
  392. // No routing section at start likely means no router feature (and no
  393. // RoutingService) in the running instance — only a restart can add it.
  394. if len(oldCfg.RouterConfig) == 0 || len(newCfg.RouterConfig) == 0 {
  395. return false
  396. }
  397. oldRest, ok := routingWithoutReloadable(oldCfg.RouterConfig)
  398. if !ok {
  399. return false
  400. }
  401. newRest, ok := routingWithoutReloadable(newCfg.RouterConfig)
  402. if !ok {
  403. return false
  404. }
  405. if !bytes.Equal(oldRest, newRest) {
  406. return false
  407. }
  408. diff.RoutingConfig = newCfg.RouterConfig
  409. return true
  410. }
  411. // routingWithoutReloadable returns the routing section normalized with the
  412. // runtime-reloadable keys removed, for comparing the restart-only remainder.
  413. func routingWithoutReloadable(raw []byte) ([]byte, bool) {
  414. parsed := map[string]any{}
  415. if len(raw) > 0 {
  416. decoder := json.NewDecoder(bytes.NewReader(raw))
  417. decoder.UseNumber()
  418. if err := decoder.Decode(&parsed); err != nil {
  419. return nil, false
  420. }
  421. }
  422. delete(parsed, "rules")
  423. delete(parsed, "balancers")
  424. out, err := json.Marshal(parsed)
  425. if err != nil {
  426. return nil, false
  427. }
  428. return out, true
  429. }
  430. // inboundEqualNormalized compares two inbounds ignoring JSON formatting in
  431. // their raw sections, so a reformatted template does not read as a changed
  432. // inbound.
  433. func inboundEqualNormalized(a, b *InboundConfig) bool {
  434. return a.Port == b.Port &&
  435. a.Protocol == b.Protocol &&
  436. a.Tag == b.Tag &&
  437. rawEqualNormalized(a.Listen, b.Listen) &&
  438. rawEqualNormalized(a.Settings, b.Settings) &&
  439. rawEqualNormalized(a.StreamSettings, b.StreamSettings) &&
  440. rawEqualNormalized(a.Sniffing, b.Sniffing)
  441. }
  442. // rawEqualNormalized reports whether two raw JSON values are semantically
  443. // equal: whitespace, object key order and an explicit `null` versus an
  444. // absent section are all ignored. UI editors rebuild objects on save (new
  445. // key order) and emit `null` for switched-off sections — none of that is a
  446. // reason to restart the core. Number precision is preserved via json.Number,
  447. // so genuinely different values never compare equal. Unparsable values only
  448. // compare equal byte-for-byte.
  449. func rawEqualNormalized(a, b json_util.RawMessage) bool {
  450. if bytes.Equal(a, b) {
  451. return true
  452. }
  453. na, ok := canonicalJSON(a)
  454. if !ok {
  455. return false
  456. }
  457. nb, ok := canonicalJSON(b)
  458. if !ok {
  459. return false
  460. }
  461. return bytes.Equal(na, nb)
  462. }
  463. // canonicalJSON renders a JSON value in canonical form: sorted object keys,
  464. // no insignificant whitespace, exact number digits (json.Number). Empty
  465. // input and JSON null both canonicalize to nil.
  466. func canonicalJSON(raw json_util.RawMessage) ([]byte, bool) {
  467. if len(raw) == 0 {
  468. return nil, true
  469. }
  470. decoder := json.NewDecoder(bytes.NewReader(raw))
  471. decoder.UseNumber()
  472. var value any
  473. if err := decoder.Decode(&value); err != nil {
  474. return nil, false
  475. }
  476. if value == nil {
  477. return nil, true
  478. }
  479. out, err := json.Marshal(value)
  480. if err != nil {
  481. return nil, false
  482. }
  483. return out, true
  484. }
  485. // inboundsByTag indexes inbounds by tag; ok is false when a tag is empty or
  486. // duplicated, since such handlers can't be addressed through the API.
  487. func inboundsByTag(inbounds []InboundConfig) (map[string]*InboundConfig, bool) {
  488. byTag := make(map[string]*InboundConfig, len(inbounds))
  489. for i := range inbounds {
  490. tag := inbounds[i].Tag
  491. if tag == "" {
  492. return nil, false
  493. }
  494. if _, dup := byTag[tag]; dup {
  495. return nil, false
  496. }
  497. byTag[tag] = &inbounds[i]
  498. }
  499. return byTag, true
  500. }
  501. type outboundEntry struct {
  502. tag string
  503. raw []byte // original JSON, used for AddOutbound
  504. norm []byte // canonical JSON, used for change detection
  505. }
  506. // parseOutbounds splits the outbounds array into per-entry raw/normalized
  507. // JSON. ok is false when the array is unparsable or an entry has an empty or
  508. // duplicate tag — those can't be addressed through the API.
  509. func parseOutbounds(raw json_util.RawMessage) ([]outboundEntry, bool) {
  510. if len(raw) == 0 {
  511. return nil, true
  512. }
  513. var elems []json.RawMessage
  514. if err := json.Unmarshal(raw, &elems); err != nil {
  515. return nil, false
  516. }
  517. entries := make([]outboundEntry, 0, len(elems))
  518. seen := make(map[string]struct{}, len(elems))
  519. for _, elem := range elems {
  520. var meta struct {
  521. Tag string `json:"tag"`
  522. }
  523. if err := json.Unmarshal(elem, &meta); err != nil {
  524. return nil, false
  525. }
  526. if meta.Tag == "" {
  527. return nil, false
  528. }
  529. if _, dup := seen[meta.Tag]; dup {
  530. return nil, false
  531. }
  532. seen[meta.Tag] = struct{}{}
  533. norm, ok := canonicalJSON(json_util.RawMessage(elem))
  534. if !ok {
  535. return nil, false
  536. }
  537. entries = append(entries, outboundEntry{tag: meta.Tag, raw: elem, norm: norm})
  538. }
  539. return entries, true
  540. }
  541. // apiTagFromConfig extracts api.tag from the api section, defaulting to "api".
  542. func apiTagFromConfig(api json_util.RawMessage) string {
  543. var parsed struct {
  544. Tag string `json:"tag"`
  545. }
  546. if len(api) > 0 && json.Unmarshal(api, &parsed) == nil && parsed.Tag != "" {
  547. return parsed.Tag
  548. }
  549. return "api"
  550. }