subJsonService.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. package sub
  2. import (
  3. _ "embed"
  4. "encoding/json"
  5. "fmt"
  6. "strings"
  7. "github.com/mhsanaei/3x-ui/v2/database/model"
  8. "github.com/mhsanaei/3x-ui/v2/logger"
  9. "github.com/mhsanaei/3x-ui/v2/util/json_util"
  10. "github.com/mhsanaei/3x-ui/v2/util/random"
  11. "github.com/mhsanaei/3x-ui/v2/web/service"
  12. "github.com/mhsanaei/3x-ui/v2/xray"
  13. )
  14. //go:embed default.json
  15. var defaultJson string
  16. // SubJsonService handles JSON subscription configuration generation and management.
  17. type SubJsonService struct {
  18. configJson map[string]any
  19. defaultOutbounds []json_util.RawMessage
  20. fragment string
  21. noises string
  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. if rules != "" {
  38. var newRules []any
  39. routing, _ := configJson["routing"].(map[string]any)
  40. defaultRules, _ := routing["rules"].([]any)
  41. json.Unmarshal([]byte(rules), &newRules)
  42. defaultRules = append(newRules, defaultRules...)
  43. routing["rules"] = defaultRules
  44. configJson["routing"] = routing
  45. }
  46. if fragment != "" {
  47. defaultOutbounds = append(defaultOutbounds, json_util.RawMessage(fragment))
  48. }
  49. if noises != "" {
  50. defaultOutbounds = append(defaultOutbounds, json_util.RawMessage(noises))
  51. }
  52. return &SubJsonService{
  53. configJson: configJson,
  54. defaultOutbounds: defaultOutbounds,
  55. fragment: fragment,
  56. noises: noises,
  57. mux: mux,
  58. SubService: subService,
  59. }
  60. }
  61. // GetJson generates a JSON subscription configuration for the given subscription ID and host.
  62. func (s *SubJsonService) GetJson(subId string, host string) (string, string, error) {
  63. inbounds, err := s.SubService.getInboundsBySubId(subId)
  64. if err != nil || len(inbounds) == 0 {
  65. return "", "", err
  66. }
  67. var header string
  68. var traffic xray.ClientTraffic
  69. var clientTraffics []xray.ClientTraffic
  70. var configArray []json_util.RawMessage
  71. // Prepare Inbounds
  72. for _, inbound := range inbounds {
  73. clients, err := s.inboundService.GetClients(inbound)
  74. if err != nil {
  75. logger.Error("SubJsonService - GetClients: Unable to get clients from inbound")
  76. }
  77. if clients == nil {
  78. continue
  79. }
  80. if len(inbound.Listen) > 0 && inbound.Listen[0] == '@' {
  81. listen, port, streamSettings, err := s.SubService.getFallbackMaster(inbound.Listen, inbound.StreamSettings)
  82. if err == nil {
  83. inbound.Listen = listen
  84. inbound.Port = port
  85. inbound.StreamSettings = streamSettings
  86. }
  87. }
  88. for _, client := range clients {
  89. if client.Enable && client.SubID == subId {
  90. clientTraffics = append(clientTraffics, s.SubService.getClientTraffics(inbound.ClientStats, client.Email))
  91. newConfigs := s.getConfig(inbound, client, host)
  92. configArray = append(configArray, newConfigs...)
  93. }
  94. }
  95. }
  96. if len(configArray) == 0 {
  97. return "", "", nil
  98. }
  99. // Prepare statistics
  100. for index, clientTraffic := range clientTraffics {
  101. if index == 0 {
  102. traffic.Up = clientTraffic.Up
  103. traffic.Down = clientTraffic.Down
  104. traffic.Total = clientTraffic.Total
  105. if clientTraffic.ExpiryTime > 0 {
  106. traffic.ExpiryTime = clientTraffic.ExpiryTime
  107. }
  108. } else {
  109. traffic.Up += clientTraffic.Up
  110. traffic.Down += clientTraffic.Down
  111. if traffic.Total == 0 || clientTraffic.Total == 0 {
  112. traffic.Total = 0
  113. } else {
  114. traffic.Total += clientTraffic.Total
  115. }
  116. if clientTraffic.ExpiryTime != traffic.ExpiryTime {
  117. traffic.ExpiryTime = 0
  118. }
  119. }
  120. }
  121. // Combile outbounds
  122. var finalJson []byte
  123. if len(configArray) == 1 {
  124. finalJson, _ = json.MarshalIndent(configArray[0], "", " ")
  125. } else {
  126. finalJson, _ = json.MarshalIndent(configArray, "", " ")
  127. }
  128. header = fmt.Sprintf("upload=%d; download=%d; total=%d; expire=%d", traffic.Up, traffic.Down, traffic.Total, traffic.ExpiryTime/1000)
  129. return string(finalJson), header, nil
  130. }
  131. func (s *SubJsonService) getConfig(inbound *model.Inbound, client model.Client, host string) []json_util.RawMessage {
  132. var newJsonArray []json_util.RawMessage
  133. stream := s.streamData(inbound.StreamSettings)
  134. externalProxies, ok := stream["externalProxy"].([]any)
  135. if !ok || len(externalProxies) == 0 {
  136. externalProxies = []any{
  137. map[string]any{
  138. "forceTls": "same",
  139. "dest": host,
  140. "port": float64(inbound.Port),
  141. "remark": "",
  142. },
  143. }
  144. }
  145. delete(stream, "externalProxy")
  146. for _, ep := range externalProxies {
  147. extPrxy := ep.(map[string]any)
  148. inbound.Listen = extPrxy["dest"].(string)
  149. inbound.Port = int(extPrxy["port"].(float64))
  150. newStream := stream
  151. switch extPrxy["forceTls"].(string) {
  152. case "tls":
  153. if newStream["security"] != "tls" {
  154. newStream["security"] = "tls"
  155. newStream["tslSettings"] = map[string]any{}
  156. }
  157. case "none":
  158. if newStream["security"] != "none" {
  159. newStream["security"] = "none"
  160. delete(newStream, "tslSettings")
  161. }
  162. }
  163. streamSettings, _ := json.MarshalIndent(newStream, "", " ")
  164. var newOutbounds []json_util.RawMessage
  165. switch inbound.Protocol {
  166. case "vmess":
  167. newOutbounds = append(newOutbounds, s.genVnext(inbound, streamSettings, client, ""))
  168. case "vless":
  169. var vlessSettings model.VLESSSettings
  170. _ = json.Unmarshal([]byte(inbound.Settings), &vlessSettings)
  171. newOutbounds = append(newOutbounds,
  172. s.genVnext(inbound, streamSettings, client, vlessSettings.Encryption))
  173. case "trojan", "shadowsocks":
  174. newOutbounds = append(newOutbounds, s.genServer(inbound, streamSettings, client))
  175. }
  176. newOutbounds = append(newOutbounds, s.defaultOutbounds...)
  177. newConfigJson := make(map[string]any)
  178. for key, value := range s.configJson {
  179. newConfigJson[key] = value
  180. }
  181. newConfigJson["outbounds"] = newOutbounds
  182. newConfigJson["remarks"] = s.SubService.genRemark(inbound, client.Email, extPrxy["remark"].(string))
  183. newConfig, _ := json.MarshalIndent(newConfigJson, "", " ")
  184. newJsonArray = append(newJsonArray, newConfig)
  185. }
  186. return newJsonArray
  187. }
  188. func (s *SubJsonService) streamData(stream string) map[string]any {
  189. var streamSettings map[string]any
  190. json.Unmarshal([]byte(stream), &streamSettings)
  191. security, _ := streamSettings["security"].(string)
  192. switch security {
  193. case "tls":
  194. streamSettings["tlsSettings"] = s.tlsData(streamSettings["tlsSettings"].(map[string]any))
  195. case "reality":
  196. streamSettings["realitySettings"] = s.realityData(streamSettings["realitySettings"].(map[string]any))
  197. }
  198. delete(streamSettings, "sockopt")
  199. if s.fragment != "" {
  200. streamSettings["sockopt"] = json_util.RawMessage(`{"dialerProxy": "fragment", "tcpKeepAliveIdle": 100, "tcpMptcp": true, "penetrate": true}`)
  201. }
  202. // remove proxy protocol
  203. network, _ := streamSettings["network"].(string)
  204. switch network {
  205. case "tcp":
  206. streamSettings["tcpSettings"] = s.removeAcceptProxy(streamSettings["tcpSettings"])
  207. case "ws":
  208. streamSettings["wsSettings"] = s.removeAcceptProxy(streamSettings["wsSettings"])
  209. case "httpupgrade":
  210. streamSettings["httpupgradeSettings"] = s.removeAcceptProxy(streamSettings["httpupgradeSettings"])
  211. }
  212. return streamSettings
  213. }
  214. func (s *SubJsonService) removeAcceptProxy(setting any) map[string]any {
  215. netSettings, ok := setting.(map[string]any)
  216. if ok {
  217. delete(netSettings, "acceptProxyProtocol")
  218. }
  219. return netSettings
  220. }
  221. func (s *SubJsonService) tlsData(tData map[string]any) map[string]any {
  222. tlsData := make(map[string]any, 1)
  223. tlsClientSettings, _ := tData["settings"].(map[string]any)
  224. tlsData["serverName"] = tData["serverName"]
  225. tlsData["alpn"] = tData["alpn"]
  226. if allowInsecure, ok := tlsClientSettings["allowInsecure"].(bool); ok {
  227. tlsData["allowInsecure"] = allowInsecure
  228. }
  229. if fingerprint, ok := tlsClientSettings["fingerprint"].(string); ok {
  230. tlsData["fingerprint"] = fingerprint
  231. }
  232. return tlsData
  233. }
  234. func (s *SubJsonService) realityData(rData map[string]any) map[string]any {
  235. rltyData := make(map[string]any, 1)
  236. rltyClientSettings, _ := rData["settings"].(map[string]any)
  237. rltyData["show"] = false
  238. rltyData["publicKey"] = rltyClientSettings["publicKey"]
  239. rltyData["fingerprint"] = rltyClientSettings["fingerprint"]
  240. rltyData["mldsa65Verify"] = rltyClientSettings["mldsa65Verify"]
  241. // Set random data
  242. rltyData["spiderX"] = "/" + random.Seq(15)
  243. shortIds, ok := rData["shortIds"].([]any)
  244. if ok && len(shortIds) > 0 {
  245. rltyData["shortId"] = shortIds[random.Num(len(shortIds))].(string)
  246. } else {
  247. rltyData["shortId"] = ""
  248. }
  249. serverNames, ok := rData["serverNames"].([]any)
  250. if ok && len(serverNames) > 0 {
  251. rltyData["serverName"] = serverNames[random.Num(len(serverNames))].(string)
  252. } else {
  253. rltyData["serverName"] = ""
  254. }
  255. return rltyData
  256. }
  257. func (s *SubJsonService) genVnext(inbound *model.Inbound, streamSettings json_util.RawMessage, client model.Client, encryption string) json_util.RawMessage {
  258. outbound := Outbound{}
  259. outbound.Protocol = string(inbound.Protocol)
  260. outbound.Tag = "proxy"
  261. if s.mux != "" {
  262. outbound.Mux = json_util.RawMessage(s.mux)
  263. }
  264. outbound.StreamSettings = streamSettings
  265. // Emit flattened settings inside Settings to match new Xray format
  266. settings := make(map[string]any)
  267. settings["address"] = inbound.Listen
  268. settings["port"] = inbound.Port
  269. settings["id"] = client.ID
  270. if inbound.Protocol == model.VLESS {
  271. settings["flow"] = client.Flow
  272. settings["encryption"] = encryption
  273. }
  274. if inbound.Protocol == model.VMESS {
  275. settings["security"] = client.Security
  276. }
  277. outbound.Settings = settings
  278. result, _ := json.MarshalIndent(outbound, "", " ")
  279. return result
  280. }
  281. func (s *SubJsonService) genServer(inbound *model.Inbound, streamSettings json_util.RawMessage, client model.Client) json_util.RawMessage {
  282. outbound := Outbound{}
  283. serverData := make([]ServerSetting, 1)
  284. serverData[0] = ServerSetting{
  285. Address: inbound.Listen,
  286. Port: inbound.Port,
  287. Level: 8,
  288. Password: client.Password,
  289. }
  290. if inbound.Protocol == model.Shadowsocks {
  291. var inboundSettings map[string]any
  292. json.Unmarshal([]byte(inbound.Settings), &inboundSettings)
  293. method, _ := inboundSettings["method"].(string)
  294. serverData[0].Method = method
  295. // server password in multi-user 2022 protocols
  296. if strings.HasPrefix(method, "2022") {
  297. if serverPassword, ok := inboundSettings["password"].(string); ok {
  298. serverData[0].Password = fmt.Sprintf("%s:%s", serverPassword, client.Password)
  299. }
  300. }
  301. }
  302. outbound.Protocol = string(inbound.Protocol)
  303. outbound.Tag = "proxy"
  304. if s.mux != "" {
  305. outbound.Mux = json_util.RawMessage(s.mux)
  306. }
  307. outbound.StreamSettings = streamSettings
  308. outbound.Settings = map[string]any{
  309. "servers": serverData,
  310. }
  311. result, _ := json.MarshalIndent(outbound, "", " ")
  312. return result
  313. }
  314. type Outbound struct {
  315. Protocol string `json:"protocol"`
  316. Tag string `json:"tag"`
  317. StreamSettings json_util.RawMessage `json:"streamSettings"`
  318. Mux json_util.RawMessage `json:"mux,omitempty"`
  319. Settings map[string]any `json:"settings,omitempty"`
  320. }
  321. // Legacy vnext-related structs removed for flattened schema
  322. type ServerSetting struct {
  323. Password string `json:"password"`
  324. Level int `json:"level"`
  325. Address string `json:"address"`
  326. Port int `json:"port"`
  327. Flow string `json:"flow,omitempty"`
  328. Method string `json:"method,omitempty"`
  329. }