1
0

json_service.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  1. package sub
  2. import (
  3. _ "embed"
  4. "encoding/json"
  5. "fmt"
  6. "maps"
  7. "strings"
  8. "github.com/mhsanaei/3x-ui/v3/internal/database/model"
  9. "github.com/mhsanaei/3x-ui/v3/internal/util/json_util"
  10. "github.com/mhsanaei/3x-ui/v3/internal/util/random"
  11. wgutil "github.com/mhsanaei/3x-ui/v3/internal/util/wireguard"
  12. )
  13. //go:embed default.json
  14. var defaultJson string
  15. // SubJsonService handles JSON subscription configuration generation and management.
  16. type SubJsonService struct {
  17. configJson map[string]any
  18. defaultOutbounds []json_util.RawMessage
  19. finalMask string
  20. mux string
  21. SubService *SubService
  22. }
  23. // NewSubJsonService creates a new JSON subscription service with the given configuration.
  24. func NewSubJsonService(mux string, rules string, finalMask string, subService *SubService) *SubJsonService {
  25. var configJson map[string]any
  26. var defaultOutbounds []json_util.RawMessage
  27. _ = json.Unmarshal([]byte(defaultJson), &configJson)
  28. if outboundSlices, ok := configJson["outbounds"].([]any); ok {
  29. for _, defaultOutbound := range outboundSlices {
  30. jsonBytes, _ := json.Marshal(defaultOutbound)
  31. defaultOutbounds = append(defaultOutbounds, jsonBytes)
  32. }
  33. }
  34. if rules != "" {
  35. var newRules []any
  36. routing, _ := configJson["routing"].(map[string]any)
  37. defaultRules, _ := routing["rules"].([]any)
  38. _ = json.Unmarshal([]byte(rules), &newRules)
  39. defaultRules = append(newRules, defaultRules...)
  40. routing["rules"] = defaultRules
  41. configJson["routing"] = routing
  42. }
  43. return &SubJsonService{
  44. configJson: configJson,
  45. defaultOutbounds: defaultOutbounds,
  46. finalMask: finalMask,
  47. mux: mux,
  48. SubService: subService,
  49. }
  50. }
  51. // GetJson generates a JSON subscription configuration for the given subscription ID and host.
  52. func (s *SubJsonService) GetJson(subId string, host string) (string, string, error) {
  53. subReq := s.SubService.ForRequest(host)
  54. subReq.subscriptionBody = true
  55. inbounds, err := subReq.getInboundsBySubId(subId)
  56. if err != nil {
  57. return "", "", err
  58. }
  59. externalLinks, err := subReq.getClientExternalLinksBySubId(subId)
  60. if err != nil {
  61. return "", "", err
  62. }
  63. if len(inbounds) == 0 && len(externalLinks) == 0 {
  64. return "", "", nil
  65. }
  66. var header string
  67. var configArray []json_util.RawMessage
  68. seenEmails := make(map[string]struct{})
  69. // Prepare Inbounds
  70. for _, inbound := range inbounds {
  71. clients := subReq.matchingClients(inbound, subId)
  72. if len(clients) == 0 {
  73. continue
  74. }
  75. subReq.projectThroughFallbackMaster(inbound)
  76. if hostEps := subReq.hostEndpoints(inbound, "json"); len(hostEps) > 0 {
  77. injectExternalProxy(inbound, hostEps)
  78. }
  79. for _, client := range clients {
  80. seenEmails[client.Email] = struct{}{}
  81. configArray = append(configArray, s.getConfig(subReq, inbound, client, host)...)
  82. }
  83. }
  84. for _, ext := range externalLinks {
  85. for _, el := range expandEntry(ext) {
  86. outbound := parsedExternalOutbound(el.Link)
  87. if outbound == nil {
  88. continue
  89. }
  90. seenEmails[ext.Email] = struct{}{}
  91. remark := el.Name
  92. if remark == "" {
  93. remark = ext.Email
  94. }
  95. newOutbounds := []json_util.RawMessage{outbound}
  96. newOutbounds = append(newOutbounds, s.defaultOutbounds...)
  97. newConfigJson := make(map[string]any)
  98. maps.Copy(newConfigJson, s.configJson)
  99. newConfigJson["outbounds"] = newOutbounds
  100. newConfigJson["remarks"] = remark
  101. newConfig, _ := json.MarshalIndent(newConfigJson, "", " ")
  102. configArray = append(configArray, newConfig)
  103. }
  104. }
  105. if len(configArray) == 0 {
  106. return "", "", nil
  107. }
  108. emails := make([]string, 0, len(seenEmails))
  109. for e := range seenEmails {
  110. emails = append(emails, e)
  111. }
  112. traffic, _ := subReq.AggregateTrafficByEmails(emails)
  113. // Combile outbounds
  114. var finalJson []byte
  115. if len(configArray) == 1 {
  116. finalJson, _ = json.MarshalIndent(configArray[0], "", " ")
  117. } else {
  118. finalJson, _ = json.MarshalIndent(configArray, "", " ")
  119. }
  120. header = fmt.Sprintf("upload=%d; download=%d; total=%d; expire=%d", traffic.Up, traffic.Down, traffic.Total, traffic.ExpiryTime/1000)
  121. return string(finalJson), header, nil
  122. }
  123. func (s *SubJsonService) getConfig(subReq *SubService, inbound *model.Inbound, client model.Client, host string) []json_util.RawMessage {
  124. var newJsonArray []json_util.RawMessage
  125. stream := s.streamData(inbound.StreamSettings, subKey(client))
  126. // When externalProxy is empty the JSON config falls back to a
  127. // synthetic one whose `dest` is the host the client connects to.
  128. // For node-managed inbounds we want the node's address — request
  129. // host won't reach the right xray. resolveInboundAddress already
  130. // implements the node→subscriber-host fallback chain.
  131. defaultDest := subReq.resolveInboundAddress(inbound)
  132. if defaultDest == "" {
  133. defaultDest = host
  134. }
  135. // Per-inbound xmux takes precedence over the global subJsonMux.
  136. // When xmux is present inside xhttpSettings, XHTTP multiplexing
  137. // is handled by xmux — don't also set the legacy outbound.Mux.
  138. mux := s.mux
  139. if xhttp, ok := stream["xhttpSettings"].(map[string]any); ok {
  140. if _, hasXmux := xhttp["xmux"]; hasXmux {
  141. mux = ""
  142. }
  143. }
  144. externalProxies, ok := stream["externalProxy"].([]any)
  145. hasExternalProxy := ok && len(externalProxies) > 0
  146. if !hasExternalProxy {
  147. externalProxies = []any{
  148. map[string]any{
  149. "forceTls": "same",
  150. "dest": defaultDest,
  151. "port": float64(inbound.Port),
  152. "remark": "",
  153. },
  154. }
  155. }
  156. delete(stream, "externalProxy")
  157. network, _ := stream["network"].(string)
  158. for _, ep := range externalProxies {
  159. extPrxy := ep.(map[string]any)
  160. // Expand the host's {{VAR}} remark template for this client (no-op for
  161. // the synthetic/legacy entry) before it's used as the config remark.
  162. subReq.renderHostRemark(inbound, client, extPrxy, network)
  163. inbound.Listen = extPrxy["dest"].(string)
  164. inbound.Port = int(extPrxy["port"].(float64))
  165. newStream := cloneStreamForExternalProxy(stream)
  166. switch extPrxy["forceTls"].(string) {
  167. case "tls":
  168. if newStream["security"] != "tls" {
  169. newStream["security"] = "tls"
  170. newStream["tlsSettings"] = map[string]any{}
  171. }
  172. case "none":
  173. if newStream["security"] != "none" {
  174. newStream["security"] = "none"
  175. delete(newStream, "tlsSettings")
  176. }
  177. }
  178. security, _ := newStream["security"].(string)
  179. if hasExternalProxy {
  180. applyExternalProxyTLSToStream(extPrxy, newStream, security)
  181. }
  182. applyHostStreamOverrides(extPrxy, newStream)
  183. streamSettings, _ := json.MarshalIndent(newStream, "", " ")
  184. hostMux := hostMuxOverride(extPrxy)
  185. var newOutbounds []json_util.RawMessage
  186. switch inbound.Protocol {
  187. case "vmess":
  188. newOutbounds = append(newOutbounds, s.genVnext(inbound, streamSettings, client, jsonMux(mux, hostMux)))
  189. case "vless":
  190. vc := client
  191. vc.ID = applyVlessRoute(client.ID, hostVlessRoute(extPrxy))
  192. newOutbounds = append(newOutbounds, s.genVless(subReq, inbound, streamSettings, vc, jsonMux(mux, hostMux)))
  193. case "trojan", "shadowsocks":
  194. newOutbounds = append(newOutbounds, s.genServer(subReq, inbound, streamSettings, client, jsonMux(mux, hostMux)))
  195. case "hysteria":
  196. newOutbounds = append(newOutbounds, s.genHy(inbound, newStream, client, jsonMux(mux, hostMux)))
  197. case "wireguard":
  198. wgOutbound := s.genWireguard(inbound, client)
  199. if wgOutbound == nil {
  200. continue
  201. }
  202. newOutbounds = append(newOutbounds, wgOutbound)
  203. }
  204. newOutbounds = append(newOutbounds, s.defaultOutbounds...)
  205. newConfigJson := make(map[string]any)
  206. maps.Copy(newConfigJson, s.configJson)
  207. transport, _ := newStream["network"].(string)
  208. newConfigJson["outbounds"] = newOutbounds
  209. newConfigJson["remarks"] = subReq.endpointRemark(inbound, client.Email, extPrxy, transport)
  210. newConfig, _ := json.MarshalIndent(newConfigJson, "", " ")
  211. newJsonArray = append(newJsonArray, newConfig)
  212. }
  213. return newJsonArray
  214. }
  215. func (s *SubJsonService) streamData(stream string, clientKey string) map[string]any {
  216. var streamSettings map[string]any
  217. if err := json.Unmarshal([]byte(stream), &streamSettings); err != nil || streamSettings == nil {
  218. streamSettings = map[string]any{}
  219. }
  220. security, _ := streamSettings["security"].(string)
  221. switch security {
  222. case "tls":
  223. if tlsSettings, ok := streamSettings["tlsSettings"].(map[string]any); ok {
  224. streamSettings["tlsSettings"] = s.tlsData(tlsSettings)
  225. } else {
  226. delete(streamSettings, "tlsSettings")
  227. }
  228. case "reality":
  229. if realitySettings, ok := streamSettings["realitySettings"].(map[string]any); ok {
  230. streamSettings["realitySettings"] = s.realityData(realitySettings, clientKey)
  231. } else {
  232. delete(streamSettings, "realitySettings")
  233. }
  234. }
  235. delete(streamSettings, "sockopt")
  236. if s.finalMask != "" {
  237. s.applyGlobalFinalMask(streamSettings)
  238. }
  239. // remove proxy protocol
  240. network, _ := streamSettings["network"].(string)
  241. switch network {
  242. case "tcp":
  243. streamSettings["tcpSettings"] = s.removeAcceptProxy(streamSettings["tcpSettings"])
  244. case "ws":
  245. streamSettings["wsSettings"] = s.removeAcceptProxy(streamSettings["wsSettings"])
  246. case "httpupgrade":
  247. streamSettings["httpupgradeSettings"] = s.removeAcceptProxy(streamSettings["httpupgradeSettings"])
  248. case "xhttp":
  249. streamSettings["xhttpSettings"] = s.removeAcceptProxy(streamSettings["xhttpSettings"])
  250. if xhttp, ok := streamSettings["xhttpSettings"].(map[string]any); ok {
  251. delete(xhttp, "noSSEHeader")
  252. delete(xhttp, "scMaxBufferedPosts")
  253. delete(xhttp, "scStreamUpServerSecs")
  254. delete(xhttp, "serverMaxHeaderBytes")
  255. // Values matching xray-core's own defaults stay off the wire:
  256. // old panels seeded them into every stored config and the
  257. // literal scMinPostsIntervalMs=30 is a DPI fingerprint (#5141).
  258. if v, _ := xhttp["scMaxEachPostBytes"].(string); v == "" || v == "1000000" {
  259. delete(xhttp, "scMaxEachPostBytes")
  260. }
  261. if v, _ := xhttp["scMinPostsIntervalMs"].(string); v == "" || v == "30" {
  262. delete(xhttp, "scMinPostsIntervalMs")
  263. }
  264. }
  265. }
  266. return streamSettings
  267. }
  268. func (s *SubJsonService) applyGlobalFinalMask(streamSettings map[string]any) {
  269. var fm map[string]any
  270. if err := json.Unmarshal([]byte(s.finalMask), &fm); err != nil || len(fm) == 0 {
  271. return
  272. }
  273. merged := mergeFinalMask(streamSettings["finalmask"], fm)
  274. if len(merged) > 0 {
  275. streamSettings["finalmask"] = merged
  276. }
  277. }
  278. func (s *SubJsonService) removeAcceptProxy(setting any) map[string]any {
  279. netSettings, ok := setting.(map[string]any)
  280. if ok {
  281. delete(netSettings, "acceptProxyProtocol")
  282. }
  283. return netSettings
  284. }
  285. func (s *SubJsonService) tlsData(tData map[string]any) map[string]any {
  286. tlsData := make(map[string]any, 1)
  287. tlsClientSettings, _ := tData["settings"].(map[string]any)
  288. tlsData["serverName"] = tData["serverName"]
  289. tlsData["alpn"] = tData["alpn"]
  290. if fingerprint, ok := tlsClientSettings["fingerprint"].(string); ok {
  291. tlsData["fingerprint"] = fingerprint
  292. }
  293. if ech, ok := tlsClientSettings["echConfigList"].(string); ok && ech != "" {
  294. tlsData["echConfigList"] = ech
  295. }
  296. if vcn, ok := verifyPeerCertByNameValue(tlsClientSettings); ok {
  297. tlsData["verifyPeerCertByName"] = vcn
  298. }
  299. // xray-core now parses pinnedPeerCertSha256 as a comma-separated string, not
  300. // an array; emit the joined form so v2ray clients can import the config (#5401).
  301. if pins, ok := pinnedSha256List(tlsClientSettings); ok {
  302. tlsData["pinnedPeerCertSha256"] = strings.Join(pins, ",")
  303. }
  304. return tlsData
  305. }
  306. func (s *SubJsonService) realityData(rData map[string]any, clientKey string) map[string]any {
  307. rltyData := make(map[string]any, 1)
  308. rltyClientSettings, _ := rData["settings"].(map[string]any)
  309. rltyData["show"] = false
  310. rltyData["publicKey"] = rltyClientSettings["publicKey"]
  311. rltyData["fingerprint"] = rltyClientSettings["fingerprint"]
  312. rltyData["mldsa65Verify"] = rltyClientSettings["mldsa65Verify"]
  313. seed, _ := rltyClientSettings["spiderX"].(string)
  314. rltyData["spiderX"] = deriveSpiderX(seed, clientKey)
  315. shortIds, ok := rData["shortIds"].([]any)
  316. if ok && len(shortIds) > 0 {
  317. rltyData["shortId"] = shortIds[random.Num(len(shortIds))].(string)
  318. } else {
  319. rltyData["shortId"] = ""
  320. }
  321. serverNames, ok := rData["serverNames"].([]any)
  322. if ok && len(serverNames) > 0 {
  323. rltyData["serverName"] = serverNames[random.Num(len(serverNames))].(string)
  324. } else {
  325. rltyData["serverName"] = ""
  326. }
  327. return rltyData
  328. }
  329. // jsonMux picks the per-host mux override when present, else the global mux.
  330. func jsonMux(global, override string) string {
  331. if override != "" {
  332. return override
  333. }
  334. return global
  335. }
  336. func (s *SubJsonService) genVnext(inbound *model.Inbound, streamSettings json_util.RawMessage, client model.Client, mux string) json_util.RawMessage {
  337. outbound := Outbound{}
  338. outbound.Protocol = string(inbound.Protocol)
  339. outbound.Tag = "proxy"
  340. if mux != "" {
  341. outbound.Mux = json_util.RawMessage(mux)
  342. }
  343. outbound.StreamSettings = streamSettings
  344. security := client.Security
  345. if security == "" {
  346. security = "auto"
  347. }
  348. outbound.Settings = map[string]any{
  349. "address": inbound.Listen,
  350. "port": inbound.Port,
  351. "id": client.ID,
  352. "security": security,
  353. "level": 8,
  354. }
  355. result, _ := json.MarshalIndent(outbound, "", " ")
  356. return result
  357. }
  358. func (s *SubJsonService) genVless(subReq *SubService, inbound *model.Inbound, streamSettings json_util.RawMessage, client model.Client, mux string) json_util.RawMessage {
  359. outbound := Outbound{}
  360. outbound.Protocol = string(inbound.Protocol)
  361. outbound.Tag = "proxy"
  362. if mux != "" {
  363. outbound.Mux = json_util.RawMessage(mux)
  364. }
  365. outbound.StreamSettings = streamSettings
  366. // Add encryption for VLESS outbound from inbound settings
  367. inboundSettings := subReq.linkSettings(inbound)
  368. encryption, _ := inboundSettings["encryption"].(string)
  369. settings := map[string]any{
  370. "address": inbound.Listen,
  371. "port": inbound.Port,
  372. "id": client.ID,
  373. "encryption": encryption,
  374. "level": 8,
  375. }
  376. if client.Flow != "" {
  377. settings["flow"] = client.Flow
  378. }
  379. outbound.Settings = settings
  380. result, _ := json.MarshalIndent(outbound, "", " ")
  381. return result
  382. }
  383. func (s *SubJsonService) genServer(subReq *SubService, inbound *model.Inbound, streamSettings json_util.RawMessage, client model.Client, mux string) json_util.RawMessage {
  384. outbound := Outbound{}
  385. serverData := make([]ServerSetting, 1)
  386. serverData[0] = ServerSetting{
  387. Address: inbound.Listen,
  388. Port: inbound.Port,
  389. Level: 8,
  390. Password: client.Password,
  391. }
  392. if inbound.Protocol == model.Shadowsocks {
  393. inboundSettings := subReq.linkSettings(inbound)
  394. method, _ := inboundSettings["method"].(string)
  395. serverData[0].Method = method
  396. // server password in multi-user 2022 protocols
  397. if strings.HasPrefix(method, "2022") {
  398. if serverPassword, ok := inboundSettings["password"].(string); ok {
  399. serverData[0].Password = fmt.Sprintf("%s:%s", serverPassword, client.Password)
  400. }
  401. }
  402. }
  403. outbound.Protocol = string(inbound.Protocol)
  404. outbound.Tag = "proxy"
  405. if mux != "" {
  406. outbound.Mux = json_util.RawMessage(mux)
  407. }
  408. outbound.StreamSettings = streamSettings
  409. // Wrap the endpoint in a "servers" array (the standard Xray schema for
  410. // Shadowsocks/Trojan outbounds). The flat top-level form only parses on very
  411. // recent xray-core; older bundled cores (e.g. in v2rayN) reject it, so SS
  412. // links fail to connect. See genVnext/genVless for the VMess/VLESS shape.
  413. server := map[string]any{
  414. "address": serverData[0].Address,
  415. "port": serverData[0].Port,
  416. "password": serverData[0].Password,
  417. "level": 8,
  418. }
  419. if inbound.Protocol == model.Shadowsocks {
  420. server["method"] = serverData[0].Method
  421. }
  422. outbound.Settings = map[string]any{
  423. "servers": []any{server},
  424. }
  425. result, _ := json.MarshalIndent(outbound, "", " ")
  426. return result
  427. }
  428. func (s *SubJsonService) genHy(inbound *model.Inbound, newStream map[string]any, client model.Client, mux string) json_util.RawMessage {
  429. outbound := Outbound{}
  430. outbound.Protocol = string(inbound.Protocol)
  431. outbound.Tag = "proxy"
  432. if mux != "" {
  433. outbound.Mux = json_util.RawMessage(mux)
  434. }
  435. var settings, stream map[string]any
  436. _ = json.Unmarshal([]byte(inbound.Settings), &settings)
  437. version, _ := settings["version"].(float64)
  438. outbound.Settings = map[string]any{
  439. "version": int(version),
  440. "address": inbound.Listen,
  441. "port": inbound.Port,
  442. }
  443. _ = json.Unmarshal([]byte(inbound.StreamSettings), &stream)
  444. hyStream := stream["hysteriaSettings"].(map[string]any)
  445. outHyStream := map[string]any{
  446. "version": int(version),
  447. "auth": client.Auth,
  448. }
  449. if udpIdleTimeout, ok := hyStream["udpIdleTimeout"].(float64); ok {
  450. outHyStream["udpIdleTimeout"] = int(udpIdleTimeout)
  451. }
  452. if masquerade, ok := hyStream["masquerade"].(map[string]any); ok {
  453. outHyStream["masquerade"] = masquerade
  454. }
  455. newStream["hysteriaSettings"] = outHyStream
  456. if finalmask, ok := hyStream["finalmask"].(map[string]any); ok {
  457. newStream["finalmask"] = mergeFinalMask(newStream["finalmask"], finalmask)
  458. }
  459. newStream["network"] = "hysteria"
  460. newStream["security"] = "tls"
  461. outbound.StreamSettings, _ = json.MarshalIndent(newStream, "", " ")
  462. result, _ := json.MarshalIndent(outbound, "", " ")
  463. return result
  464. }
  465. // genWireguard builds an Xray wireguard outbound for a native WireGuard inbound,
  466. // mirroring genWireguardLink: the peer public key is derived from the inbound
  467. // secretKey, the client owns the private key / tunnel address / pre-shared key,
  468. // and the peer routes the full tunnel. Returns nil when the client has no key.
  469. func (s *SubJsonService) genWireguard(inbound *model.Inbound, client model.Client) json_util.RawMessage {
  470. if client.PrivateKey == "" {
  471. return nil
  472. }
  473. var inboundSettings map[string]any
  474. _ = json.Unmarshal([]byte(inbound.Settings), &inboundSettings)
  475. secretKey, _ := inboundSettings["secretKey"].(string)
  476. peer := map[string]any{
  477. "endpoint": joinHostPort(inbound.Listen, inbound.Port),
  478. "allowedIPs": []string{"0.0.0.0/0", "::/0"},
  479. }
  480. if secretKey != "" {
  481. if pub, err := wgutil.PublicKeyFromPrivate(secretKey); err == nil {
  482. peer["publicKey"] = pub
  483. }
  484. }
  485. if client.PreSharedKey != "" {
  486. peer["preSharedKey"] = client.PreSharedKey
  487. }
  488. if client.KeepAlive > 0 {
  489. peer["keepAlive"] = client.KeepAlive
  490. }
  491. settings := map[string]any{
  492. "secretKey": client.PrivateKey,
  493. "peers": []any{peer},
  494. }
  495. if len(client.AllowedIPs) > 0 {
  496. settings["address"] = client.AllowedIPs
  497. }
  498. if mtu, ok := inboundSettings["mtu"].(float64); ok && mtu > 0 {
  499. settings["mtu"] = int(mtu)
  500. }
  501. outbound := map[string]any{
  502. "protocol": string(inbound.Protocol),
  503. "tag": "proxy",
  504. "settings": settings,
  505. }
  506. result, _ := json.MarshalIndent(outbound, "", " ")
  507. return result
  508. }
  509. func mergeFinalMask(base any, extra map[string]any) map[string]any {
  510. merged := map[string]any{}
  511. if baseMap, ok := base.(map[string]any); ok {
  512. for key, value := range baseMap {
  513. switch key {
  514. case "tcp", "udp":
  515. if masks, ok := value.([]any); ok {
  516. merged[key] = append([]any(nil), masks...)
  517. }
  518. default:
  519. merged[key] = value
  520. }
  521. }
  522. }
  523. for key, value := range extra {
  524. switch key {
  525. case "tcp", "udp":
  526. baseMasks, _ := merged[key].([]any)
  527. extraMasks, _ := value.([]any)
  528. if len(extraMasks) > 0 {
  529. merged[key] = append(baseMasks, extraMasks...)
  530. }
  531. case "quicParams":
  532. if _, exists := merged[key]; !exists {
  533. merged[key] = value
  534. }
  535. default:
  536. merged[key] = value
  537. }
  538. }
  539. return merged
  540. }
  541. type Outbound struct {
  542. Protocol string `json:"protocol"`
  543. Tag string `json:"tag"`
  544. StreamSettings json_util.RawMessage `json:"streamSettings"`
  545. Mux json_util.RawMessage `json:"mux,omitempty"`
  546. Settings map[string]any `json:"settings,omitempty"`
  547. }
  548. type ServerSetting struct {
  549. Password string `json:"password"`
  550. Level int `json:"level"`
  551. Address string `json:"address"`
  552. Port int `json:"port"`
  553. Flow string `json:"flow,omitempty"`
  554. Method string `json:"method,omitempty"`
  555. }