subJsonService.go 11 KB

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