json_service.go 20 KB

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