1
0

subJsonService.go 14 KB

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