subJsonService.go 13 KB

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