subJsonService.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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["tlsSettings"] = map[string]any{}
  156. }
  157. case "none":
  158. if newStream["security"] != "none" {
  159. newStream["security"] = "none"
  160. delete(newStream, "tlsSettings")
  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. newOutbounds = append(newOutbounds, s.genVless(inbound, streamSettings, client))
  170. case "trojan", "shadowsocks":
  171. newOutbounds = append(newOutbounds, s.genServer(inbound, streamSettings, client))
  172. }
  173. newOutbounds = append(newOutbounds, s.defaultOutbounds...)
  174. newConfigJson := make(map[string]any)
  175. for key, value := range s.configJson {
  176. newConfigJson[key] = value
  177. }
  178. newConfigJson["outbounds"] = newOutbounds
  179. newConfigJson["remarks"] = s.SubService.genRemark(inbound, client.Email, extPrxy["remark"].(string))
  180. newConfig, _ := json.MarshalIndent(newConfigJson, "", " ")
  181. newJsonArray = append(newJsonArray, newConfig)
  182. }
  183. return newJsonArray
  184. }
  185. func (s *SubJsonService) streamData(stream string) map[string]any {
  186. var streamSettings map[string]any
  187. json.Unmarshal([]byte(stream), &streamSettings)
  188. security, _ := streamSettings["security"].(string)
  189. switch security {
  190. case "tls":
  191. streamSettings["tlsSettings"] = s.tlsData(streamSettings["tlsSettings"].(map[string]any))
  192. case "reality":
  193. streamSettings["realitySettings"] = s.realityData(streamSettings["realitySettings"].(map[string]any))
  194. }
  195. delete(streamSettings, "sockopt")
  196. if s.fragment != "" {
  197. streamSettings["sockopt"] = json_util.RawMessage(`{"dialerProxy": "fragment", "tcpKeepAliveIdle": 100, "tcpMptcp": true, "penetrate": true}`)
  198. }
  199. // remove proxy protocol
  200. network, _ := streamSettings["network"].(string)
  201. switch network {
  202. case "tcp":
  203. streamSettings["tcpSettings"] = s.removeAcceptProxy(streamSettings["tcpSettings"])
  204. case "ws":
  205. streamSettings["wsSettings"] = s.removeAcceptProxy(streamSettings["wsSettings"])
  206. case "httpupgrade":
  207. streamSettings["httpupgradeSettings"] = s.removeAcceptProxy(streamSettings["httpupgradeSettings"])
  208. }
  209. return streamSettings
  210. }
  211. func (s *SubJsonService) removeAcceptProxy(setting any) map[string]any {
  212. netSettings, ok := setting.(map[string]any)
  213. if ok {
  214. delete(netSettings, "acceptProxyProtocol")
  215. }
  216. return netSettings
  217. }
  218. func (s *SubJsonService) tlsData(tData map[string]any) map[string]any {
  219. tlsData := make(map[string]any, 1)
  220. tlsClientSettings, _ := tData["settings"].(map[string]any)
  221. tlsData["serverName"] = tData["serverName"]
  222. tlsData["alpn"] = tData["alpn"]
  223. if allowInsecure, ok := tlsClientSettings["allowInsecure"].(bool); ok {
  224. tlsData["allowInsecure"] = allowInsecure
  225. }
  226. if fingerprint, ok := tlsClientSettings["fingerprint"].(string); ok {
  227. tlsData["fingerprint"] = fingerprint
  228. }
  229. return tlsData
  230. }
  231. func (s *SubJsonService) realityData(rData map[string]any) map[string]any {
  232. rltyData := make(map[string]any, 1)
  233. rltyClientSettings, _ := rData["settings"].(map[string]any)
  234. rltyData["show"] = false
  235. rltyData["publicKey"] = rltyClientSettings["publicKey"]
  236. rltyData["fingerprint"] = rltyClientSettings["fingerprint"]
  237. rltyData["mldsa65Verify"] = rltyClientSettings["mldsa65Verify"]
  238. // Set random data
  239. rltyData["spiderX"] = "/" + random.Seq(15)
  240. shortIds, ok := rData["shortIds"].([]any)
  241. if ok && len(shortIds) > 0 {
  242. rltyData["shortId"] = shortIds[random.Num(len(shortIds))].(string)
  243. } else {
  244. rltyData["shortId"] = ""
  245. }
  246. serverNames, ok := rData["serverNames"].([]any)
  247. if ok && len(serverNames) > 0 {
  248. rltyData["serverName"] = serverNames[random.Num(len(serverNames))].(string)
  249. } else {
  250. rltyData["serverName"] = ""
  251. }
  252. return rltyData
  253. }
  254. func (s *SubJsonService) genVnext(inbound *model.Inbound, streamSettings json_util.RawMessage, client model.Client) json_util.RawMessage {
  255. outbound := Outbound{}
  256. usersData := make([]UserVnext, 1)
  257. usersData[0].ID = client.ID
  258. usersData[0].Email = client.Email
  259. usersData[0].Security = client.Security
  260. vnextData := make([]VnextSetting, 1)
  261. vnextData[0] = VnextSetting{
  262. Address: inbound.Listen,
  263. Port: inbound.Port,
  264. Users: usersData,
  265. }
  266. outbound.Protocol = string(inbound.Protocol)
  267. outbound.Tag = "proxy"
  268. if s.mux != "" {
  269. outbound.Mux = json_util.RawMessage(s.mux)
  270. }
  271. outbound.StreamSettings = streamSettings
  272. outbound.Settings = map[string]any{
  273. "vnext": vnextData,
  274. }
  275. result, _ := json.MarshalIndent(outbound, "", " ")
  276. return result
  277. }
  278. func (s *SubJsonService) genVless(inbound *model.Inbound, streamSettings json_util.RawMessage, client model.Client) json_util.RawMessage {
  279. outbound := Outbound{}
  280. outbound.Protocol = string(inbound.Protocol)
  281. outbound.Tag = "proxy"
  282. if s.mux != "" {
  283. outbound.Mux = json_util.RawMessage(s.mux)
  284. }
  285. outbound.StreamSettings = streamSettings
  286. settings := make(map[string]any)
  287. settings["address"] = inbound.Listen
  288. settings["port"] = inbound.Port
  289. settings["id"] = client.ID
  290. if client.Flow != "" {
  291. settings["flow"] = client.Flow
  292. }
  293. // Add encryption for VLESS outbound from inbound settings
  294. var inboundSettings map[string]any
  295. json.Unmarshal([]byte(inbound.Settings), &inboundSettings)
  296. if encryption, ok := inboundSettings["encryption"].(string); ok {
  297. settings["encryption"] = encryption
  298. }
  299. outbound.Settings = settings
  300. result, _ := json.MarshalIndent(outbound, "", " ")
  301. return result
  302. }
  303. func (s *SubJsonService) genServer(inbound *model.Inbound, streamSettings json_util.RawMessage, client model.Client) json_util.RawMessage {
  304. outbound := Outbound{}
  305. serverData := make([]ServerSetting, 1)
  306. serverData[0] = ServerSetting{
  307. Address: inbound.Listen,
  308. Port: inbound.Port,
  309. Level: 8,
  310. Password: client.Password,
  311. }
  312. if inbound.Protocol == model.Shadowsocks {
  313. var inboundSettings map[string]any
  314. json.Unmarshal([]byte(inbound.Settings), &inboundSettings)
  315. method, _ := inboundSettings["method"].(string)
  316. serverData[0].Method = method
  317. // server password in multi-user 2022 protocols
  318. if strings.HasPrefix(method, "2022") {
  319. if serverPassword, ok := inboundSettings["password"].(string); ok {
  320. serverData[0].Password = fmt.Sprintf("%s:%s", serverPassword, client.Password)
  321. }
  322. }
  323. }
  324. outbound.Protocol = string(inbound.Protocol)
  325. outbound.Tag = "proxy"
  326. if s.mux != "" {
  327. outbound.Mux = json_util.RawMessage(s.mux)
  328. }
  329. outbound.StreamSettings = streamSettings
  330. outbound.Settings = map[string]any{
  331. "servers": serverData,
  332. }
  333. result, _ := json.MarshalIndent(outbound, "", " ")
  334. return result
  335. }
  336. type Outbound struct {
  337. Protocol string `json:"protocol"`
  338. Tag string `json:"tag"`
  339. StreamSettings json_util.RawMessage `json:"streamSettings"`
  340. Mux json_util.RawMessage `json:"mux,omitempty"`
  341. Settings map[string]any `json:"settings,omitempty"`
  342. }
  343. type VnextSetting struct {
  344. Address string `json:"address"`
  345. Port int `json:"port"`
  346. Users []UserVnext `json:"users"`
  347. }
  348. type UserVnext struct {
  349. ID string `json:"id"`
  350. Email string `json:"email,omitempty"`
  351. Security string `json:"security,omitempty"`
  352. }
  353. type ServerSetting struct {
  354. Password string `json:"password"`
  355. Level int `json:"level"`
  356. Address string `json:"address"`
  357. Port int `json:"port"`
  358. Flow string `json:"flow,omitempty"`
  359. Method string `json:"method,omitempty"`
  360. }