subJsonService.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. package sub
  2. import (
  3. _ "embed"
  4. "encoding/json"
  5. "fmt"
  6. "maps"
  7. "strings"
  8. "time"
  9. "github.com/mhsanaei/3x-ui/v3/database/model"
  10. "github.com/mhsanaei/3x-ui/v3/logger"
  11. "github.com/mhsanaei/3x-ui/v3/util/json_util"
  12. "github.com/mhsanaei/3x-ui/v3/util/random"
  13. "github.com/mhsanaei/3x-ui/v3/web/service"
  14. "github.com/mhsanaei/3x-ui/v3/xray"
  15. )
  16. //go:embed default.json
  17. var defaultJson string
  18. // SubJsonService handles JSON subscription configuration generation and management.
  19. type SubJsonService struct {
  20. configJson map[string]any
  21. defaultOutbounds []json_util.RawMessage
  22. fragmentOrNoises bool
  23. mux string
  24. inboundService service.InboundService
  25. SubService *SubService
  26. }
  27. // NewSubJsonService creates a new JSON subscription service with the given configuration.
  28. func NewSubJsonService(fragment string, noises string, mux string, rules string, subService *SubService) *SubJsonService {
  29. var configJson map[string]any
  30. var defaultOutbounds []json_util.RawMessage
  31. json.Unmarshal([]byte(defaultJson), &configJson)
  32. if outboundSlices, ok := configJson["outbounds"].([]any); ok {
  33. for _, defaultOutbound := range outboundSlices {
  34. jsonBytes, _ := json.Marshal(defaultOutbound)
  35. defaultOutbounds = append(defaultOutbounds, jsonBytes)
  36. }
  37. }
  38. fragmentOrNoises := false
  39. if fragment != "" || noises != "" {
  40. fragmentOrNoises = true
  41. defaultOutboundsSettings := map[string]any{
  42. "domainStrategy": "UseIP",
  43. "redirect": "",
  44. }
  45. if fragment != "" {
  46. defaultOutboundsSettings["fragment"] = json_util.RawMessage(fragment)
  47. }
  48. if noises != "" {
  49. defaultOutboundsSettings["noises"] = json_util.RawMessage(noises)
  50. }
  51. defaultDirectOutbound := map[string]any{
  52. "protocol": "freedom",
  53. "settings": defaultOutboundsSettings,
  54. "tag": "direct_out",
  55. }
  56. jsonBytes, _ := json.MarshalIndent(defaultDirectOutbound, "", " ")
  57. defaultOutbounds = append(defaultOutbounds, jsonBytes)
  58. }
  59. if rules != "" {
  60. var newRules []any
  61. routing, _ := configJson["routing"].(map[string]any)
  62. defaultRules, _ := routing["rules"].([]any)
  63. json.Unmarshal([]byte(rules), &newRules)
  64. defaultRules = append(newRules, defaultRules...)
  65. routing["rules"] = defaultRules
  66. configJson["routing"] = routing
  67. }
  68. return &SubJsonService{
  69. configJson: configJson,
  70. defaultOutbounds: defaultOutbounds,
  71. fragmentOrNoises: fragmentOrNoises,
  72. mux: mux,
  73. SubService: subService,
  74. }
  75. }
  76. // GetJson generates a JSON subscription configuration for the given subscription ID and host.
  77. func (s *SubJsonService) GetJson(subId string, host string) (string, string, error) {
  78. // Set per-request state on the shared SubService so any
  79. // resolveInboundAddress call inside picks node-aware host values.
  80. s.SubService.PrepareForRequest(host)
  81. inbounds, err := s.SubService.getInboundsBySubId(subId)
  82. if err != nil || len(inbounds) == 0 {
  83. return "", "", err
  84. }
  85. var header string
  86. var traffic xray.ClientTraffic
  87. var clientTraffics []xray.ClientTraffic
  88. var configArray []json_util.RawMessage
  89. seenEmails := make(map[string]struct{})
  90. // Prepare Inbounds
  91. for _, inbound := range inbounds {
  92. clients, err := s.inboundService.GetClients(inbound)
  93. if err != nil {
  94. logger.Error("SubJsonService - GetClients: Unable to get clients from inbound")
  95. }
  96. if clients == nil {
  97. continue
  98. }
  99. s.SubService.projectThroughFallbackMaster(inbound)
  100. for _, client := range clients {
  101. if client.SubID == subId {
  102. _, clientTraffics = s.SubService.appendUniqueTraffic(seenEmails, clientTraffics, inbound.ClientStats, client.Email)
  103. configArray = append(configArray, s.getConfig(inbound, client, host)...)
  104. }
  105. }
  106. }
  107. if len(configArray) == 0 {
  108. return "", "", nil
  109. }
  110. // Prepare statistics
  111. now := time.Now().UnixMilli()
  112. for index, clientTraffic := range clientTraffics {
  113. if index == 0 {
  114. traffic.Up = clientTraffic.Up
  115. traffic.Down = clientTraffic.Down
  116. traffic.Total = clientTraffic.Total
  117. traffic.ExpiryTime = subscriptionExpiryFromClient(now, clientTraffic.ExpiryTime)
  118. } else {
  119. traffic.Up += clientTraffic.Up
  120. traffic.Down += clientTraffic.Down
  121. if traffic.Total == 0 || clientTraffic.Total == 0 {
  122. traffic.Total = 0
  123. } else {
  124. traffic.Total += clientTraffic.Total
  125. }
  126. normalized := subscriptionExpiryFromClient(now, clientTraffic.ExpiryTime)
  127. if normalized != traffic.ExpiryTime {
  128. traffic.ExpiryTime = 0
  129. }
  130. }
  131. }
  132. // Combile outbounds
  133. var finalJson []byte
  134. if len(configArray) == 1 {
  135. finalJson, _ = json.MarshalIndent(configArray[0], "", " ")
  136. } else {
  137. finalJson, _ = json.MarshalIndent(configArray, "", " ")
  138. }
  139. header = fmt.Sprintf("upload=%d; download=%d; total=%d; expire=%d", traffic.Up, traffic.Down, traffic.Total, traffic.ExpiryTime/1000)
  140. return string(finalJson), header, nil
  141. }
  142. func (s *SubJsonService) getConfig(inbound *model.Inbound, client model.Client, host string) []json_util.RawMessage {
  143. var newJsonArray []json_util.RawMessage
  144. stream := s.streamData(inbound.StreamSettings)
  145. // When externalProxy is empty the JSON config falls back to a
  146. // synthetic one whose `dest` is the host the client connects to.
  147. // For node-managed inbounds we want the node's address — request
  148. // host won't reach the right xray. resolveInboundAddress already
  149. // implements the node→listen→request-host fallback chain.
  150. defaultDest := s.SubService.resolveInboundAddress(inbound)
  151. if defaultDest == "" {
  152. defaultDest = host
  153. }
  154. externalProxies, ok := stream["externalProxy"].([]any)
  155. hasExternalProxy := ok && len(externalProxies) > 0
  156. if !hasExternalProxy {
  157. externalProxies = []any{
  158. map[string]any{
  159. "forceTls": "same",
  160. "dest": defaultDest,
  161. "port": float64(inbound.Port),
  162. "remark": "",
  163. },
  164. }
  165. }
  166. delete(stream, "externalProxy")
  167. for _, ep := range externalProxies {
  168. extPrxy := ep.(map[string]any)
  169. inbound.Listen = extPrxy["dest"].(string)
  170. inbound.Port = int(extPrxy["port"].(float64))
  171. newStream := cloneStreamForExternalProxy(stream)
  172. switch extPrxy["forceTls"].(string) {
  173. case "tls":
  174. if newStream["security"] != "tls" {
  175. newStream["security"] = "tls"
  176. newStream["tlsSettings"] = map[string]any{}
  177. }
  178. case "none":
  179. if newStream["security"] != "none" {
  180. newStream["security"] = "none"
  181. delete(newStream, "tlsSettings")
  182. }
  183. }
  184. security, _ := newStream["security"].(string)
  185. if hasExternalProxy {
  186. applyExternalProxyTLSToStream(extPrxy, newStream, security)
  187. }
  188. streamSettings, _ := json.MarshalIndent(newStream, "", " ")
  189. var newOutbounds []json_util.RawMessage
  190. switch inbound.Protocol {
  191. case "vmess":
  192. newOutbounds = append(newOutbounds, s.genVnext(inbound, streamSettings, client))
  193. case "vless":
  194. newOutbounds = append(newOutbounds, s.genVless(inbound, streamSettings, client))
  195. case "trojan", "shadowsocks":
  196. newOutbounds = append(newOutbounds, s.genServer(inbound, streamSettings, client))
  197. case "hysteria", "hysteria2":
  198. newOutbounds = append(newOutbounds, s.genHy(inbound, newStream, client))
  199. }
  200. newOutbounds = append(newOutbounds, s.defaultOutbounds...)
  201. newConfigJson := make(map[string]any)
  202. maps.Copy(newConfigJson, s.configJson)
  203. newConfigJson["outbounds"] = newOutbounds
  204. newConfigJson["remarks"] = s.SubService.genRemark(inbound, client.Email, extPrxy["remark"].(string))
  205. newConfig, _ := json.MarshalIndent(newConfigJson, "", " ")
  206. newJsonArray = append(newJsonArray, newConfig)
  207. }
  208. return newJsonArray
  209. }
  210. func (s *SubJsonService) streamData(stream string) map[string]any {
  211. var streamSettings map[string]any
  212. json.Unmarshal([]byte(stream), &streamSettings)
  213. security, _ := streamSettings["security"].(string)
  214. switch security {
  215. case "tls":
  216. streamSettings["tlsSettings"] = s.tlsData(streamSettings["tlsSettings"].(map[string]any))
  217. case "reality":
  218. streamSettings["realitySettings"] = s.realityData(streamSettings["realitySettings"].(map[string]any))
  219. }
  220. delete(streamSettings, "sockopt")
  221. if s.fragmentOrNoises {
  222. streamSettings["sockopt"] = json_util.RawMessage(`{"dialerProxy": "direct_out", "tcpKeepAliveIdle": 100}`)
  223. }
  224. // remove proxy protocol
  225. network, _ := streamSettings["network"].(string)
  226. switch network {
  227. case "tcp":
  228. streamSettings["tcpSettings"] = s.removeAcceptProxy(streamSettings["tcpSettings"])
  229. case "ws":
  230. streamSettings["wsSettings"] = s.removeAcceptProxy(streamSettings["wsSettings"])
  231. case "httpupgrade":
  232. streamSettings["httpupgradeSettings"] = s.removeAcceptProxy(streamSettings["httpupgradeSettings"])
  233. case "xhttp":
  234. streamSettings["xhttpSettings"] = s.removeAcceptProxy(streamSettings["xhttpSettings"])
  235. if xhttp, ok := streamSettings["xhttpSettings"].(map[string]any); ok {
  236. delete(xhttp, "noSSEHeader")
  237. delete(xhttp, "scMaxBufferedPosts")
  238. delete(xhttp, "scStreamUpServerSecs")
  239. delete(xhttp, "serverMaxHeaderBytes")
  240. }
  241. }
  242. return streamSettings
  243. }
  244. func (s *SubJsonService) removeAcceptProxy(setting any) map[string]any {
  245. netSettings, ok := setting.(map[string]any)
  246. if ok {
  247. delete(netSettings, "acceptProxyProtocol")
  248. }
  249. return netSettings
  250. }
  251. func (s *SubJsonService) tlsData(tData map[string]any) map[string]any {
  252. tlsData := make(map[string]any, 1)
  253. tlsClientSettings, _ := tData["settings"].(map[string]any)
  254. tlsData["serverName"] = tData["serverName"]
  255. tlsData["alpn"] = tData["alpn"]
  256. if fingerprint, ok := tlsClientSettings["fingerprint"].(string); ok {
  257. tlsData["fingerprint"] = fingerprint
  258. }
  259. return tlsData
  260. }
  261. func (s *SubJsonService) realityData(rData map[string]any) map[string]any {
  262. rltyData := make(map[string]any, 1)
  263. rltyClientSettings, _ := rData["settings"].(map[string]any)
  264. rltyData["show"] = false
  265. rltyData["publicKey"] = rltyClientSettings["publicKey"]
  266. rltyData["fingerprint"] = rltyClientSettings["fingerprint"]
  267. rltyData["mldsa65Verify"] = rltyClientSettings["mldsa65Verify"]
  268. // Set random data
  269. rltyData["spiderX"] = "/" + random.Seq(15)
  270. shortIds, ok := rData["shortIds"].([]any)
  271. if ok && len(shortIds) > 0 {
  272. rltyData["shortId"] = shortIds[random.Num(len(shortIds))].(string)
  273. } else {
  274. rltyData["shortId"] = ""
  275. }
  276. serverNames, ok := rData["serverNames"].([]any)
  277. if ok && len(serverNames) > 0 {
  278. rltyData["serverName"] = serverNames[random.Num(len(serverNames))].(string)
  279. } else {
  280. rltyData["serverName"] = ""
  281. }
  282. return rltyData
  283. }
  284. func (s *SubJsonService) genVnext(inbound *model.Inbound, streamSettings json_util.RawMessage, client model.Client) json_util.RawMessage {
  285. outbound := Outbound{}
  286. usersData := make([]UserVnext, 1)
  287. usersData[0].ID = client.ID
  288. usersData[0].Email = client.Email
  289. usersData[0].Security = client.Security
  290. vnextData := make([]VnextSetting, 1)
  291. vnextData[0] = VnextSetting{
  292. Address: inbound.Listen,
  293. Port: inbound.Port,
  294. Users: usersData,
  295. }
  296. outbound.Protocol = string(inbound.Protocol)
  297. outbound.Tag = "proxy"
  298. if s.mux != "" {
  299. outbound.Mux = json_util.RawMessage(s.mux)
  300. }
  301. outbound.StreamSettings = streamSettings
  302. outbound.Settings = map[string]any{
  303. "vnext": vnextData,
  304. }
  305. result, _ := json.MarshalIndent(outbound, "", " ")
  306. return result
  307. }
  308. func (s *SubJsonService) genVless(inbound *model.Inbound, streamSettings json_util.RawMessage, client model.Client) json_util.RawMessage {
  309. outbound := Outbound{}
  310. outbound.Protocol = string(inbound.Protocol)
  311. outbound.Tag = "proxy"
  312. if s.mux != "" {
  313. outbound.Mux = json_util.RawMessage(s.mux)
  314. }
  315. outbound.StreamSettings = streamSettings
  316. // Add encryption for VLESS outbound from inbound settings
  317. var inboundSettings map[string]any
  318. json.Unmarshal([]byte(inbound.Settings), &inboundSettings)
  319. encryption, _ := inboundSettings["encryption"].(string)
  320. user := map[string]any{
  321. "id": client.ID,
  322. "level": 8,
  323. "encryption": encryption,
  324. }
  325. if client.Flow != "" {
  326. user["flow"] = client.Flow
  327. }
  328. vnext := map[string]any{
  329. "address": inbound.Listen,
  330. "port": inbound.Port,
  331. "users": []any{user},
  332. }
  333. outbound.Settings = map[string]any{
  334. "vnext": []any{vnext},
  335. }
  336. result, _ := json.MarshalIndent(outbound, "", " ")
  337. return result
  338. }
  339. func (s *SubJsonService) genServer(inbound *model.Inbound, streamSettings json_util.RawMessage, client model.Client) json_util.RawMessage {
  340. outbound := Outbound{}
  341. serverData := make([]ServerSetting, 1)
  342. serverData[0] = ServerSetting{
  343. Address: inbound.Listen,
  344. Port: inbound.Port,
  345. Level: 8,
  346. Password: client.Password,
  347. }
  348. if inbound.Protocol == model.Shadowsocks {
  349. var inboundSettings map[string]any
  350. json.Unmarshal([]byte(inbound.Settings), &inboundSettings)
  351. method, _ := inboundSettings["method"].(string)
  352. serverData[0].Method = method
  353. // server password in multi-user 2022 protocols
  354. if strings.HasPrefix(method, "2022") {
  355. if serverPassword, ok := inboundSettings["password"].(string); ok {
  356. serverData[0].Password = fmt.Sprintf("%s:%s", serverPassword, client.Password)
  357. }
  358. }
  359. }
  360. outbound.Protocol = string(inbound.Protocol)
  361. outbound.Tag = "proxy"
  362. if s.mux != "" {
  363. outbound.Mux = json_util.RawMessage(s.mux)
  364. }
  365. outbound.StreamSettings = streamSettings
  366. outbound.Settings = map[string]any{
  367. "servers": serverData,
  368. }
  369. result, _ := json.MarshalIndent(outbound, "", " ")
  370. return result
  371. }
  372. func (s *SubJsonService) genHy(inbound *model.Inbound, newStream map[string]any, client model.Client) json_util.RawMessage {
  373. outbound := Outbound{}
  374. outbound.Protocol = string(inbound.Protocol)
  375. outbound.Tag = "proxy"
  376. if s.mux != "" {
  377. outbound.Mux = json_util.RawMessage(s.mux)
  378. }
  379. var settings, stream map[string]any
  380. json.Unmarshal([]byte(inbound.Settings), &settings)
  381. version, _ := settings["version"].(float64)
  382. outbound.Settings = map[string]any{
  383. "version": int(version),
  384. "address": inbound.Listen,
  385. "port": inbound.Port,
  386. }
  387. json.Unmarshal([]byte(inbound.StreamSettings), &stream)
  388. hyStream := stream["hysteriaSettings"].(map[string]any)
  389. outHyStream := map[string]any{
  390. "version": int(version),
  391. "auth": client.Auth,
  392. }
  393. if udpIdleTimeout, ok := hyStream["udpIdleTimeout"].(float64); ok {
  394. outHyStream["udpIdleTimeout"] = int(udpIdleTimeout)
  395. }
  396. if masquerade, ok := hyStream["masquerade"].(map[string]any); ok {
  397. outHyStream["masquerade"] = masquerade
  398. }
  399. newStream["hysteriaSettings"] = outHyStream
  400. if finalmask, ok := hyStream["finalmask"].(map[string]any); ok {
  401. newStream["finalmask"] = finalmask
  402. }
  403. newStream["network"] = "hysteria"
  404. newStream["security"] = "tls"
  405. outbound.StreamSettings, _ = json.MarshalIndent(newStream, "", " ")
  406. result, _ := json.MarshalIndent(outbound, "", " ")
  407. return result
  408. }
  409. type Outbound struct {
  410. Protocol string `json:"protocol"`
  411. Tag string `json:"tag"`
  412. StreamSettings json_util.RawMessage `json:"streamSettings"`
  413. Mux json_util.RawMessage `json:"mux,omitempty"`
  414. Settings map[string]any `json:"settings,omitempty"`
  415. }
  416. type VnextSetting struct {
  417. Address string `json:"address"`
  418. Port int `json:"port"`
  419. Users []UserVnext `json:"users"`
  420. }
  421. type UserVnext struct {
  422. ID string `json:"id"`
  423. Email string `json:"email,omitempty"`
  424. Security string `json:"security,omitempty"`
  425. }
  426. type ServerSetting struct {
  427. Password string `json:"password"`
  428. Level int `json:"level"`
  429. Address string `json:"address"`
  430. Port int `json:"port"`
  431. Flow string `json:"flow,omitempty"`
  432. Method string `json:"method,omitempty"`
  433. }