1
0

json_service.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  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. )
  12. //go:embed default.json
  13. var defaultJson string
  14. // SubJsonService handles JSON subscription configuration generation and management.
  15. type SubJsonService struct {
  16. configJson map[string]any
  17. defaultOutbounds []json_util.RawMessage
  18. finalMask string
  19. mux string
  20. SubService *SubService
  21. }
  22. // NewSubJsonService creates a new JSON subscription service with the given configuration.
  23. func NewSubJsonService(mux string, rules string, finalMask string, subService *SubService) *SubJsonService {
  24. var configJson map[string]any
  25. var defaultOutbounds []json_util.RawMessage
  26. _ = json.Unmarshal([]byte(defaultJson), &configJson)
  27. if outboundSlices, ok := configJson["outbounds"].([]any); ok {
  28. for _, defaultOutbound := range outboundSlices {
  29. jsonBytes, _ := json.Marshal(defaultOutbound)
  30. defaultOutbounds = append(defaultOutbounds, jsonBytes)
  31. }
  32. }
  33. if rules != "" {
  34. var newRules []any
  35. routing, _ := configJson["routing"].(map[string]any)
  36. defaultRules, _ := routing["rules"].([]any)
  37. _ = json.Unmarshal([]byte(rules), &newRules)
  38. defaultRules = append(newRules, defaultRules...)
  39. routing["rules"] = defaultRules
  40. configJson["routing"] = routing
  41. }
  42. return &SubJsonService{
  43. configJson: configJson,
  44. defaultOutbounds: defaultOutbounds,
  45. finalMask: finalMask,
  46. mux: mux,
  47. SubService: subService,
  48. }
  49. }
  50. // GetJson generates a JSON subscription configuration for the given subscription ID and host.
  51. func (s *SubJsonService) GetJson(subId string, host string) (string, string, error) {
  52. subReq := s.SubService.ForRequest(host)
  53. subReq.subscriptionBody = true
  54. inbounds, err := subReq.getInboundsBySubId(subId)
  55. if err != nil {
  56. return "", "", err
  57. }
  58. externalLinks, err := subReq.getClientExternalLinksBySubId(subId)
  59. if err != nil {
  60. return "", "", err
  61. }
  62. if len(inbounds) == 0 && len(externalLinks) == 0 {
  63. return "", "", nil
  64. }
  65. var header string
  66. var configArray []json_util.RawMessage
  67. seenEmails := make(map[string]struct{})
  68. // Prepare Inbounds
  69. for _, inbound := range inbounds {
  70. clients := subReq.matchingClients(inbound, subId)
  71. if len(clients) == 0 {
  72. continue
  73. }
  74. subReq.projectThroughFallbackMaster(inbound)
  75. if hostEps := subReq.hostEndpoints(inbound, "json"); len(hostEps) > 0 {
  76. injectExternalProxy(inbound, hostEps)
  77. }
  78. for _, client := range clients {
  79. seenEmails[client.Email] = struct{}{}
  80. configArray = append(configArray, s.getConfig(subReq, inbound, client, host)...)
  81. }
  82. }
  83. for _, ext := range externalLinks {
  84. for _, el := range expandEntry(ext) {
  85. outbound := parsedExternalOutbound(el.Link)
  86. if outbound == nil {
  87. continue
  88. }
  89. seenEmails[ext.Email] = struct{}{}
  90. remark := el.Name
  91. if remark == "" {
  92. remark = ext.Email
  93. }
  94. newOutbounds := []json_util.RawMessage{outbound}
  95. newOutbounds = append(newOutbounds, s.defaultOutbounds...)
  96. newConfigJson := make(map[string]any)
  97. maps.Copy(newConfigJson, s.configJson)
  98. newConfigJson["outbounds"] = newOutbounds
  99. newConfigJson["remarks"] = remark
  100. newConfig, _ := json.MarshalIndent(newConfigJson, "", " ")
  101. configArray = append(configArray, newConfig)
  102. }
  103. }
  104. if len(configArray) == 0 {
  105. return "", "", nil
  106. }
  107. emails := make([]string, 0, len(seenEmails))
  108. for e := range seenEmails {
  109. emails = append(emails, e)
  110. }
  111. traffic, _ := subReq.AggregateTrafficByEmails(emails)
  112. // Combile outbounds
  113. var finalJson []byte
  114. if len(configArray) == 1 {
  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)
  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 := ep.(map[string]any)
  159. // Expand the host's {{VAR}} remark template for this client (no-op for
  160. // the synthetic/legacy entry) before it's used as the config remark.
  161. subReq.renderHostRemark(inbound, client, extPrxy, network)
  162. inbound.Listen = extPrxy["dest"].(string)
  163. inbound.Port = int(extPrxy["port"].(float64))
  164. newStream := cloneStreamForExternalProxy(stream)
  165. switch extPrxy["forceTls"].(string) {
  166. case "tls":
  167. if newStream["security"] != "tls" {
  168. newStream["security"] = "tls"
  169. newStream["tlsSettings"] = map[string]any{}
  170. }
  171. case "none":
  172. if newStream["security"] != "none" {
  173. newStream["security"] = "none"
  174. delete(newStream, "tlsSettings")
  175. }
  176. }
  177. security, _ := newStream["security"].(string)
  178. if hasExternalProxy {
  179. applyExternalProxyTLSToStream(extPrxy, newStream, security)
  180. }
  181. applyHostStreamOverrides(extPrxy, newStream)
  182. streamSettings, _ := json.MarshalIndent(newStream, "", " ")
  183. hostMux := hostMuxOverride(extPrxy)
  184. var newOutbounds []json_util.RawMessage
  185. switch inbound.Protocol {
  186. case "vmess":
  187. newOutbounds = append(newOutbounds, s.genVnext(inbound, streamSettings, client, jsonMux(mux, hostMux)))
  188. case "vless":
  189. vc := client
  190. vc.ID = applyVlessRoute(client.ID, hostVlessRoute(extPrxy))
  191. newOutbounds = append(newOutbounds, s.genVless(inbound, streamSettings, vc, jsonMux(mux, hostMux)))
  192. case "trojan", "shadowsocks":
  193. newOutbounds = append(newOutbounds, s.genServer(inbound, streamSettings, client, jsonMux(mux, hostMux)))
  194. case "hysteria":
  195. newOutbounds = append(newOutbounds, s.genHy(inbound, newStream, client, jsonMux(mux, hostMux)))
  196. }
  197. newOutbounds = append(newOutbounds, s.defaultOutbounds...)
  198. newConfigJson := make(map[string]any)
  199. maps.Copy(newConfigJson, s.configJson)
  200. transport, _ := newStream["network"].(string)
  201. newConfigJson["outbounds"] = newOutbounds
  202. newConfigJson["remarks"] = subReq.endpointRemark(inbound, client.Email, extPrxy, transport)
  203. newConfig, _ := json.MarshalIndent(newConfigJson, "", " ")
  204. newJsonArray = append(newJsonArray, newConfig)
  205. }
  206. return newJsonArray
  207. }
  208. func (s *SubJsonService) streamData(stream string) map[string]any {
  209. var streamSettings map[string]any
  210. _ = json.Unmarshal([]byte(stream), &streamSettings)
  211. security, _ := streamSettings["security"].(string)
  212. switch security {
  213. case "tls":
  214. streamSettings["tlsSettings"] = s.tlsData(streamSettings["tlsSettings"].(map[string]any))
  215. case "reality":
  216. streamSettings["realitySettings"] = s.realityData(streamSettings["realitySettings"].(map[string]any))
  217. }
  218. delete(streamSettings, "sockopt")
  219. if s.finalMask != "" {
  220. s.applyGlobalFinalMask(streamSettings)
  221. }
  222. // remove proxy protocol
  223. network, _ := streamSettings["network"].(string)
  224. switch network {
  225. case "tcp":
  226. streamSettings["tcpSettings"] = s.removeAcceptProxy(streamSettings["tcpSettings"])
  227. case "ws":
  228. streamSettings["wsSettings"] = s.removeAcceptProxy(streamSettings["wsSettings"])
  229. case "httpupgrade":
  230. streamSettings["httpupgradeSettings"] = s.removeAcceptProxy(streamSettings["httpupgradeSettings"])
  231. case "xhttp":
  232. streamSettings["xhttpSettings"] = s.removeAcceptProxy(streamSettings["xhttpSettings"])
  233. if xhttp, ok := streamSettings["xhttpSettings"].(map[string]any); ok {
  234. delete(xhttp, "noSSEHeader")
  235. delete(xhttp, "scMaxBufferedPosts")
  236. delete(xhttp, "scStreamUpServerSecs")
  237. delete(xhttp, "serverMaxHeaderBytes")
  238. // Values matching xray-core's own defaults stay off the wire:
  239. // old panels seeded them into every stored config and the
  240. // literal scMinPostsIntervalMs=30 is a DPI fingerprint (#5141).
  241. if v, _ := xhttp["scMaxEachPostBytes"].(string); v == "" || v == "1000000" {
  242. delete(xhttp, "scMaxEachPostBytes")
  243. }
  244. if v, _ := xhttp["scMinPostsIntervalMs"].(string); v == "" || v == "30" {
  245. delete(xhttp, "scMinPostsIntervalMs")
  246. }
  247. }
  248. }
  249. return streamSettings
  250. }
  251. func (s *SubJsonService) applyGlobalFinalMask(streamSettings map[string]any) {
  252. var fm map[string]any
  253. if err := json.Unmarshal([]byte(s.finalMask), &fm); err != nil || len(fm) == 0 {
  254. return
  255. }
  256. merged := mergeFinalMask(streamSettings["finalmask"], fm)
  257. if len(merged) > 0 {
  258. streamSettings["finalmask"] = merged
  259. }
  260. }
  261. func (s *SubJsonService) removeAcceptProxy(setting any) map[string]any {
  262. netSettings, ok := setting.(map[string]any)
  263. if ok {
  264. delete(netSettings, "acceptProxyProtocol")
  265. }
  266. return netSettings
  267. }
  268. func (s *SubJsonService) tlsData(tData map[string]any) map[string]any {
  269. tlsData := make(map[string]any, 1)
  270. tlsClientSettings, _ := tData["settings"].(map[string]any)
  271. tlsData["serverName"] = tData["serverName"]
  272. tlsData["alpn"] = tData["alpn"]
  273. if fingerprint, ok := tlsClientSettings["fingerprint"].(string); ok {
  274. tlsData["fingerprint"] = fingerprint
  275. }
  276. if ech, ok := tlsClientSettings["echConfigList"].(string); ok && ech != "" {
  277. tlsData["echConfigList"] = ech
  278. }
  279. if vcn, ok := verifyPeerCertByNameValue(tlsClientSettings); ok {
  280. tlsData["verifyPeerCertByName"] = vcn
  281. }
  282. // xray-core now parses pinnedPeerCertSha256 as a comma-separated string, not
  283. // an array; emit the joined form so v2ray clients can import the config (#5401).
  284. if pins, ok := pinnedSha256List(tlsClientSettings); ok {
  285. tlsData["pinnedPeerCertSha256"] = strings.Join(pins, ",")
  286. }
  287. return tlsData
  288. }
  289. func (s *SubJsonService) realityData(rData map[string]any) map[string]any {
  290. rltyData := make(map[string]any, 1)
  291. rltyClientSettings, _ := rData["settings"].(map[string]any)
  292. rltyData["show"] = false
  293. rltyData["publicKey"] = rltyClientSettings["publicKey"]
  294. rltyData["fingerprint"] = rltyClientSettings["fingerprint"]
  295. rltyData["mldsa65Verify"] = rltyClientSettings["mldsa65Verify"]
  296. // Set random data
  297. rltyData["spiderX"] = "/" + random.Seq(15)
  298. shortIds, ok := rData["shortIds"].([]any)
  299. if ok && len(shortIds) > 0 {
  300. rltyData["shortId"] = shortIds[random.Num(len(shortIds))].(string)
  301. } else {
  302. rltyData["shortId"] = ""
  303. }
  304. serverNames, ok := rData["serverNames"].([]any)
  305. if ok && len(serverNames) > 0 {
  306. rltyData["serverName"] = serverNames[random.Num(len(serverNames))].(string)
  307. } else {
  308. rltyData["serverName"] = ""
  309. }
  310. return rltyData
  311. }
  312. // jsonMux picks the per-host mux override when present, else the global mux.
  313. func jsonMux(global, override string) string {
  314. if override != "" {
  315. return override
  316. }
  317. return global
  318. }
  319. func (s *SubJsonService) genVnext(inbound *model.Inbound, streamSettings json_util.RawMessage, client model.Client, mux string) json_util.RawMessage {
  320. outbound := Outbound{}
  321. outbound.Protocol = string(inbound.Protocol)
  322. outbound.Tag = "proxy"
  323. if mux != "" {
  324. outbound.Mux = json_util.RawMessage(mux)
  325. }
  326. outbound.StreamSettings = streamSettings
  327. security := client.Security
  328. if security == "" {
  329. security = "auto"
  330. }
  331. outbound.Settings = map[string]any{
  332. "address": inbound.Listen,
  333. "port": inbound.Port,
  334. "id": client.ID,
  335. "security": security,
  336. "level": 8,
  337. }
  338. result, _ := json.MarshalIndent(outbound, "", " ")
  339. return result
  340. }
  341. func (s *SubJsonService) genVless(inbound *model.Inbound, streamSettings json_util.RawMessage, client model.Client, mux string) json_util.RawMessage {
  342. outbound := Outbound{}
  343. outbound.Protocol = string(inbound.Protocol)
  344. outbound.Tag = "proxy"
  345. if mux != "" {
  346. outbound.Mux = json_util.RawMessage(mux)
  347. }
  348. outbound.StreamSettings = streamSettings
  349. // Add encryption for VLESS outbound from inbound settings
  350. var inboundSettings map[string]any
  351. _ = json.Unmarshal([]byte(inbound.Settings), &inboundSettings)
  352. encryption, _ := inboundSettings["encryption"].(string)
  353. settings := map[string]any{
  354. "address": inbound.Listen,
  355. "port": inbound.Port,
  356. "id": client.ID,
  357. "encryption": encryption,
  358. "level": 8,
  359. }
  360. if client.Flow != "" {
  361. settings["flow"] = client.Flow
  362. }
  363. outbound.Settings = settings
  364. result, _ := json.MarshalIndent(outbound, "", " ")
  365. return result
  366. }
  367. func (s *SubJsonService) genServer(inbound *model.Inbound, streamSettings json_util.RawMessage, client model.Client, mux string) json_util.RawMessage {
  368. outbound := Outbound{}
  369. serverData := make([]ServerSetting, 1)
  370. serverData[0] = ServerSetting{
  371. Address: inbound.Listen,
  372. Port: inbound.Port,
  373. Level: 8,
  374. Password: client.Password,
  375. }
  376. if inbound.Protocol == model.Shadowsocks {
  377. var inboundSettings map[string]any
  378. _ = json.Unmarshal([]byte(inbound.Settings), &inboundSettings)
  379. method, _ := inboundSettings["method"].(string)
  380. serverData[0].Method = method
  381. // server password in multi-user 2022 protocols
  382. if strings.HasPrefix(method, "2022") {
  383. if serverPassword, ok := inboundSettings["password"].(string); ok {
  384. serverData[0].Password = fmt.Sprintf("%s:%s", serverPassword, client.Password)
  385. }
  386. }
  387. }
  388. outbound.Protocol = string(inbound.Protocol)
  389. outbound.Tag = "proxy"
  390. if mux != "" {
  391. outbound.Mux = json_util.RawMessage(mux)
  392. }
  393. outbound.StreamSettings = streamSettings
  394. // Wrap the endpoint in a "servers" array (the standard Xray schema for
  395. // Shadowsocks/Trojan outbounds). The flat top-level form only parses on very
  396. // recent xray-core; older bundled cores (e.g. in v2rayN) reject it, so SS
  397. // links fail to connect. See genVnext/genVless for the VMess/VLESS shape.
  398. server := map[string]any{
  399. "address": serverData[0].Address,
  400. "port": serverData[0].Port,
  401. "password": serverData[0].Password,
  402. "level": 8,
  403. }
  404. if inbound.Protocol == model.Shadowsocks {
  405. server["method"] = serverData[0].Method
  406. }
  407. outbound.Settings = map[string]any{
  408. "servers": []any{server},
  409. }
  410. result, _ := json.MarshalIndent(outbound, "", " ")
  411. return result
  412. }
  413. func (s *SubJsonService) genHy(inbound *model.Inbound, newStream map[string]any, client model.Client, mux string) json_util.RawMessage {
  414. outbound := Outbound{}
  415. outbound.Protocol = string(inbound.Protocol)
  416. outbound.Tag = "proxy"
  417. if mux != "" {
  418. outbound.Mux = json_util.RawMessage(mux)
  419. }
  420. var settings, stream map[string]any
  421. _ = json.Unmarshal([]byte(inbound.Settings), &settings)
  422. version, _ := settings["version"].(float64)
  423. outbound.Settings = map[string]any{
  424. "version": int(version),
  425. "address": inbound.Listen,
  426. "port": inbound.Port,
  427. }
  428. _ = json.Unmarshal([]byte(inbound.StreamSettings), &stream)
  429. hyStream := stream["hysteriaSettings"].(map[string]any)
  430. outHyStream := map[string]any{
  431. "version": int(version),
  432. "auth": client.Auth,
  433. }
  434. if udpIdleTimeout, ok := hyStream["udpIdleTimeout"].(float64); ok {
  435. outHyStream["udpIdleTimeout"] = int(udpIdleTimeout)
  436. }
  437. if masquerade, ok := hyStream["masquerade"].(map[string]any); ok {
  438. outHyStream["masquerade"] = masquerade
  439. }
  440. newStream["hysteriaSettings"] = outHyStream
  441. if finalmask, ok := hyStream["finalmask"].(map[string]any); ok {
  442. newStream["finalmask"] = mergeFinalMask(newStream["finalmask"], finalmask)
  443. }
  444. newStream["network"] = "hysteria"
  445. newStream["security"] = "tls"
  446. outbound.StreamSettings, _ = json.MarshalIndent(newStream, "", " ")
  447. result, _ := json.MarshalIndent(outbound, "", " ")
  448. return result
  449. }
  450. func mergeFinalMask(base any, extra map[string]any) map[string]any {
  451. merged := map[string]any{}
  452. if baseMap, ok := base.(map[string]any); ok {
  453. for key, value := range baseMap {
  454. switch key {
  455. case "tcp", "udp":
  456. if masks, ok := value.([]any); ok {
  457. merged[key] = append([]any(nil), masks...)
  458. }
  459. default:
  460. merged[key] = value
  461. }
  462. }
  463. }
  464. for key, value := range extra {
  465. switch key {
  466. case "tcp", "udp":
  467. baseMasks, _ := merged[key].([]any)
  468. extraMasks, _ := value.([]any)
  469. if len(extraMasks) > 0 {
  470. merged[key] = append(baseMasks, extraMasks...)
  471. }
  472. case "quicParams":
  473. if _, exists := merged[key]; !exists {
  474. merged[key] = value
  475. }
  476. default:
  477. merged[key] = value
  478. }
  479. }
  480. return merged
  481. }
  482. type Outbound struct {
  483. Protocol string `json:"protocol"`
  484. Tag string `json:"tag"`
  485. StreamSettings json_util.RawMessage `json:"streamSettings"`
  486. Mux json_util.RawMessage `json:"mux,omitempty"`
  487. Settings map[string]any `json:"settings,omitempty"`
  488. }
  489. type ServerSetting struct {
  490. Password string `json:"password"`
  491. Level int `json:"level"`
  492. Address string `json:"address"`
  493. Port int `json:"port"`
  494. Flow string `json:"flow,omitempty"`
  495. Method string `json:"method,omitempty"`
  496. }