json_service.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  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. // Set per-request state on the shared SubService so any
  53. // resolveInboundAddress call inside picks node-aware host values.
  54. s.SubService.PrepareForRequest(host)
  55. inbounds, err := s.SubService.getInboundsBySubId(subId)
  56. if err != nil || len(inbounds) == 0 {
  57. return "", "", err
  58. }
  59. var header string
  60. var configArray []json_util.RawMessage
  61. seenEmails := make(map[string]struct{})
  62. // Prepare Inbounds
  63. for _, inbound := range inbounds {
  64. clients := s.SubService.matchingClients(inbound, subId)
  65. if len(clients) == 0 {
  66. continue
  67. }
  68. s.SubService.projectThroughFallbackMaster(inbound)
  69. for _, client := range clients {
  70. seenEmails[client.Email] = struct{}{}
  71. configArray = append(configArray, s.getConfig(inbound, client, host)...)
  72. }
  73. }
  74. if len(configArray) == 0 {
  75. return "", "", nil
  76. }
  77. emails := make([]string, 0, len(seenEmails))
  78. for e := range seenEmails {
  79. emails = append(emails, e)
  80. }
  81. traffic, _ := s.SubService.AggregateTrafficByEmails(emails)
  82. // Combile outbounds
  83. var finalJson []byte
  84. if len(configArray) == 1 {
  85. finalJson, _ = json.MarshalIndent(configArray[0], "", " ")
  86. } else {
  87. finalJson, _ = json.MarshalIndent(configArray, "", " ")
  88. }
  89. header = fmt.Sprintf("upload=%d; download=%d; total=%d; expire=%d", traffic.Up, traffic.Down, traffic.Total, traffic.ExpiryTime/1000)
  90. return string(finalJson), header, nil
  91. }
  92. func (s *SubJsonService) getConfig(inbound *model.Inbound, client model.Client, host string) []json_util.RawMessage {
  93. var newJsonArray []json_util.RawMessage
  94. stream := s.streamData(inbound.StreamSettings)
  95. // When externalProxy is empty the JSON config falls back to a
  96. // synthetic one whose `dest` is the host the client connects to.
  97. // For node-managed inbounds we want the node's address — request
  98. // host won't reach the right xray. resolveInboundAddress already
  99. // implements the node→subscriber-host fallback chain.
  100. defaultDest := s.SubService.resolveInboundAddress(inbound)
  101. if defaultDest == "" {
  102. defaultDest = host
  103. }
  104. externalProxies, ok := stream["externalProxy"].([]any)
  105. hasExternalProxy := ok && len(externalProxies) > 0
  106. if !hasExternalProxy {
  107. externalProxies = []any{
  108. map[string]any{
  109. "forceTls": "same",
  110. "dest": defaultDest,
  111. "port": float64(inbound.Port),
  112. "remark": "",
  113. },
  114. }
  115. }
  116. delete(stream, "externalProxy")
  117. for _, ep := range externalProxies {
  118. extPrxy := ep.(map[string]any)
  119. inbound.Listen = extPrxy["dest"].(string)
  120. inbound.Port = int(extPrxy["port"].(float64))
  121. newStream := cloneStreamForExternalProxy(stream)
  122. switch extPrxy["forceTls"].(string) {
  123. case "tls":
  124. if newStream["security"] != "tls" {
  125. newStream["security"] = "tls"
  126. newStream["tlsSettings"] = map[string]any{}
  127. }
  128. case "none":
  129. if newStream["security"] != "none" {
  130. newStream["security"] = "none"
  131. delete(newStream, "tlsSettings")
  132. }
  133. }
  134. security, _ := newStream["security"].(string)
  135. if hasExternalProxy {
  136. applyExternalProxyTLSToStream(extPrxy, newStream, security)
  137. }
  138. streamSettings, _ := json.MarshalIndent(newStream, "", " ")
  139. var newOutbounds []json_util.RawMessage
  140. switch inbound.Protocol {
  141. case "vmess":
  142. newOutbounds = append(newOutbounds, s.genVnext(inbound, streamSettings, client))
  143. case "vless":
  144. newOutbounds = append(newOutbounds, s.genVless(inbound, streamSettings, client))
  145. case "trojan", "shadowsocks":
  146. newOutbounds = append(newOutbounds, s.genServer(inbound, streamSettings, client))
  147. case "hysteria":
  148. newOutbounds = append(newOutbounds, s.genHy(inbound, newStream, client))
  149. }
  150. newOutbounds = append(newOutbounds, s.defaultOutbounds...)
  151. newConfigJson := make(map[string]any)
  152. maps.Copy(newConfigJson, s.configJson)
  153. newConfigJson["outbounds"] = newOutbounds
  154. newConfigJson["remarks"] = s.SubService.genRemark(inbound, client.Email, extPrxy["remark"].(string))
  155. newConfig, _ := json.MarshalIndent(newConfigJson, "", " ")
  156. newJsonArray = append(newJsonArray, newConfig)
  157. }
  158. return newJsonArray
  159. }
  160. func (s *SubJsonService) streamData(stream string) map[string]any {
  161. var streamSettings map[string]any
  162. json.Unmarshal([]byte(stream), &streamSettings)
  163. security, _ := streamSettings["security"].(string)
  164. switch security {
  165. case "tls":
  166. streamSettings["tlsSettings"] = s.tlsData(streamSettings["tlsSettings"].(map[string]any))
  167. case "reality":
  168. streamSettings["realitySettings"] = s.realityData(streamSettings["realitySettings"].(map[string]any))
  169. }
  170. delete(streamSettings, "sockopt")
  171. if s.finalMask != "" {
  172. s.applyGlobalFinalMask(streamSettings)
  173. }
  174. // remove proxy protocol
  175. network, _ := streamSettings["network"].(string)
  176. switch network {
  177. case "tcp":
  178. streamSettings["tcpSettings"] = s.removeAcceptProxy(streamSettings["tcpSettings"])
  179. case "ws":
  180. streamSettings["wsSettings"] = s.removeAcceptProxy(streamSettings["wsSettings"])
  181. case "httpupgrade":
  182. streamSettings["httpupgradeSettings"] = s.removeAcceptProxy(streamSettings["httpupgradeSettings"])
  183. case "xhttp":
  184. streamSettings["xhttpSettings"] = s.removeAcceptProxy(streamSettings["xhttpSettings"])
  185. if xhttp, ok := streamSettings["xhttpSettings"].(map[string]any); ok {
  186. delete(xhttp, "noSSEHeader")
  187. delete(xhttp, "scMaxBufferedPosts")
  188. delete(xhttp, "scStreamUpServerSecs")
  189. delete(xhttp, "serverMaxHeaderBytes")
  190. }
  191. }
  192. return streamSettings
  193. }
  194. func (s *SubJsonService) applyGlobalFinalMask(streamSettings map[string]any) {
  195. var fm map[string]any
  196. if err := json.Unmarshal([]byte(s.finalMask), &fm); err != nil || len(fm) == 0 {
  197. return
  198. }
  199. merged := mergeFinalMask(streamSettings["finalmask"], fm)
  200. if len(merged) > 0 {
  201. streamSettings["finalmask"] = merged
  202. }
  203. }
  204. func (s *SubJsonService) removeAcceptProxy(setting any) map[string]any {
  205. netSettings, ok := setting.(map[string]any)
  206. if ok {
  207. delete(netSettings, "acceptProxyProtocol")
  208. }
  209. return netSettings
  210. }
  211. func (s *SubJsonService) tlsData(tData map[string]any) map[string]any {
  212. tlsData := make(map[string]any, 1)
  213. tlsClientSettings, _ := tData["settings"].(map[string]any)
  214. tlsData["serverName"] = tData["serverName"]
  215. tlsData["alpn"] = tData["alpn"]
  216. if fingerprint, ok := tlsClientSettings["fingerprint"].(string); ok {
  217. tlsData["fingerprint"] = fingerprint
  218. }
  219. if ech, ok := tlsClientSettings["echConfigList"].(string); ok && ech != "" {
  220. tlsData["echConfigList"] = ech
  221. }
  222. if pins, ok := tlsClientSettings["pinnedPeerCertSha256"].([]any); ok && len(pins) > 0 {
  223. tlsData["pinnedPeerCertSha256"] = pins
  224. }
  225. return tlsData
  226. }
  227. func (s *SubJsonService) realityData(rData map[string]any) map[string]any {
  228. rltyData := make(map[string]any, 1)
  229. rltyClientSettings, _ := rData["settings"].(map[string]any)
  230. rltyData["show"] = false
  231. rltyData["publicKey"] = rltyClientSettings["publicKey"]
  232. rltyData["fingerprint"] = rltyClientSettings["fingerprint"]
  233. rltyData["mldsa65Verify"] = rltyClientSettings["mldsa65Verify"]
  234. // Set random data
  235. rltyData["spiderX"] = "/" + random.Seq(15)
  236. shortIds, ok := rData["shortIds"].([]any)
  237. if ok && len(shortIds) > 0 {
  238. rltyData["shortId"] = shortIds[random.Num(len(shortIds))].(string)
  239. } else {
  240. rltyData["shortId"] = ""
  241. }
  242. serverNames, ok := rData["serverNames"].([]any)
  243. if ok && len(serverNames) > 0 {
  244. rltyData["serverName"] = serverNames[random.Num(len(serverNames))].(string)
  245. } else {
  246. rltyData["serverName"] = ""
  247. }
  248. return rltyData
  249. }
  250. func (s *SubJsonService) genVnext(inbound *model.Inbound, streamSettings json_util.RawMessage, client model.Client) json_util.RawMessage {
  251. outbound := Outbound{}
  252. outbound.Protocol = string(inbound.Protocol)
  253. outbound.Tag = "proxy"
  254. if s.mux != "" {
  255. outbound.Mux = json_util.RawMessage(s.mux)
  256. }
  257. outbound.StreamSettings = streamSettings
  258. security := client.Security
  259. if security == "" {
  260. security = "auto"
  261. }
  262. outbound.Settings = map[string]any{
  263. "address": inbound.Listen,
  264. "port": inbound.Port,
  265. "id": client.ID,
  266. "security": security,
  267. "level": 8,
  268. }
  269. result, _ := json.MarshalIndent(outbound, "", " ")
  270. return result
  271. }
  272. func (s *SubJsonService) genVless(inbound *model.Inbound, streamSettings json_util.RawMessage, client model.Client) json_util.RawMessage {
  273. outbound := Outbound{}
  274. outbound.Protocol = string(inbound.Protocol)
  275. outbound.Tag = "proxy"
  276. if s.mux != "" {
  277. outbound.Mux = json_util.RawMessage(s.mux)
  278. }
  279. outbound.StreamSettings = streamSettings
  280. // Add encryption for VLESS outbound from inbound settings
  281. var inboundSettings map[string]any
  282. json.Unmarshal([]byte(inbound.Settings), &inboundSettings)
  283. encryption, _ := inboundSettings["encryption"].(string)
  284. settings := map[string]any{
  285. "address": inbound.Listen,
  286. "port": inbound.Port,
  287. "id": client.ID,
  288. "encryption": encryption,
  289. "level": 8,
  290. }
  291. if client.Flow != "" {
  292. settings["flow"] = client.Flow
  293. }
  294. outbound.Settings = settings
  295. result, _ := json.MarshalIndent(outbound, "", " ")
  296. return result
  297. }
  298. func (s *SubJsonService) genServer(inbound *model.Inbound, streamSettings json_util.RawMessage, client model.Client) json_util.RawMessage {
  299. outbound := Outbound{}
  300. serverData := make([]ServerSetting, 1)
  301. serverData[0] = ServerSetting{
  302. Address: inbound.Listen,
  303. Port: inbound.Port,
  304. Level: 8,
  305. Password: client.Password,
  306. }
  307. if inbound.Protocol == model.Shadowsocks {
  308. var inboundSettings map[string]any
  309. json.Unmarshal([]byte(inbound.Settings), &inboundSettings)
  310. method, _ := inboundSettings["method"].(string)
  311. serverData[0].Method = method
  312. // server password in multi-user 2022 protocols
  313. if strings.HasPrefix(method, "2022") {
  314. if serverPassword, ok := inboundSettings["password"].(string); ok {
  315. serverData[0].Password = fmt.Sprintf("%s:%s", serverPassword, client.Password)
  316. }
  317. }
  318. }
  319. outbound.Protocol = string(inbound.Protocol)
  320. outbound.Tag = "proxy"
  321. if s.mux != "" {
  322. outbound.Mux = json_util.RawMessage(s.mux)
  323. }
  324. outbound.StreamSettings = streamSettings
  325. settings := map[string]any{
  326. "address": serverData[0].Address,
  327. "port": serverData[0].Port,
  328. "password": serverData[0].Password,
  329. "level": 8,
  330. }
  331. if inbound.Protocol == model.Shadowsocks {
  332. settings["method"] = serverData[0].Method
  333. }
  334. outbound.Settings = settings
  335. result, _ := json.MarshalIndent(outbound, "", " ")
  336. return result
  337. }
  338. func (s *SubJsonService) genHy(inbound *model.Inbound, newStream map[string]any, client model.Client) json_util.RawMessage {
  339. outbound := Outbound{}
  340. outbound.Protocol = string(inbound.Protocol)
  341. outbound.Tag = "proxy"
  342. if s.mux != "" {
  343. outbound.Mux = json_util.RawMessage(s.mux)
  344. }
  345. var settings, stream map[string]any
  346. json.Unmarshal([]byte(inbound.Settings), &settings)
  347. version, _ := settings["version"].(float64)
  348. outbound.Settings = map[string]any{
  349. "version": int(version),
  350. "address": inbound.Listen,
  351. "port": inbound.Port,
  352. }
  353. json.Unmarshal([]byte(inbound.StreamSettings), &stream)
  354. hyStream := stream["hysteriaSettings"].(map[string]any)
  355. outHyStream := map[string]any{
  356. "version": int(version),
  357. "auth": client.Auth,
  358. }
  359. if udpIdleTimeout, ok := hyStream["udpIdleTimeout"].(float64); ok {
  360. outHyStream["udpIdleTimeout"] = int(udpIdleTimeout)
  361. }
  362. if masquerade, ok := hyStream["masquerade"].(map[string]any); ok {
  363. outHyStream["masquerade"] = masquerade
  364. }
  365. newStream["hysteriaSettings"] = outHyStream
  366. if finalmask, ok := hyStream["finalmask"].(map[string]any); ok {
  367. newStream["finalmask"] = mergeFinalMask(newStream["finalmask"], finalmask)
  368. }
  369. newStream["network"] = "hysteria"
  370. newStream["security"] = "tls"
  371. outbound.StreamSettings, _ = json.MarshalIndent(newStream, "", " ")
  372. result, _ := json.MarshalIndent(outbound, "", " ")
  373. return result
  374. }
  375. func mergeFinalMask(base any, extra map[string]any) map[string]any {
  376. merged := map[string]any{}
  377. if baseMap, ok := base.(map[string]any); ok {
  378. for key, value := range baseMap {
  379. switch key {
  380. case "tcp", "udp":
  381. if masks, ok := value.([]any); ok {
  382. merged[key] = append([]any(nil), masks...)
  383. }
  384. default:
  385. merged[key] = value
  386. }
  387. }
  388. }
  389. for key, value := range extra {
  390. switch key {
  391. case "tcp", "udp":
  392. baseMasks, _ := merged[key].([]any)
  393. extraMasks, _ := value.([]any)
  394. if len(extraMasks) > 0 {
  395. merged[key] = append(baseMasks, extraMasks...)
  396. }
  397. case "quicParams":
  398. if _, exists := merged[key]; !exists {
  399. merged[key] = value
  400. }
  401. default:
  402. merged[key] = value
  403. }
  404. }
  405. return merged
  406. }
  407. type Outbound struct {
  408. Protocol string `json:"protocol"`
  409. Tag string `json:"tag"`
  410. StreamSettings json_util.RawMessage `json:"streamSettings"`
  411. Mux json_util.RawMessage `json:"mux,omitempty"`
  412. Settings map[string]any `json:"settings,omitempty"`
  413. }
  414. type ServerSetting struct {
  415. Password string `json:"password"`
  416. Level int `json:"level"`
  417. Address string `json:"address"`
  418. Port int `json:"port"`
  419. Flow string `json:"flow,omitempty"`
  420. Method string `json:"method,omitempty"`
  421. }