subJsonService.go 12 KB

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