server.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. package controller
  2. import (
  3. "fmt"
  4. "net/http"
  5. "regexp"
  6. "strconv"
  7. "time"
  8. "x-ui/web/global"
  9. "x-ui/web/service"
  10. "github.com/gin-gonic/gin"
  11. )
  12. var filenameRegex = regexp.MustCompile(`^[a-zA-Z0-9_\-.]+$`)
  13. type ServerController struct {
  14. BaseController
  15. serverService service.ServerService
  16. settingService service.SettingService
  17. lastStatus *service.Status
  18. lastGetStatusTime time.Time
  19. lastVersions []string
  20. lastGetVersionsTime time.Time
  21. }
  22. func NewServerController(g *gin.RouterGroup) *ServerController {
  23. a := &ServerController{
  24. lastGetStatusTime: time.Now(),
  25. }
  26. a.initRouter(g)
  27. a.startTask()
  28. return a
  29. }
  30. func (a *ServerController) initRouter(g *gin.RouterGroup) {
  31. g.GET("/status", a.status)
  32. g.GET("/cpuHistory", a.getCpuHistory)
  33. g.GET("/getXrayVersion", a.getXrayVersion)
  34. g.GET("/getConfigJson", a.getConfigJson)
  35. g.GET("/getDb", a.getDb)
  36. g.GET("/getNewUUID", a.getNewUUID)
  37. g.GET("/getNewX25519Cert", a.getNewX25519Cert)
  38. g.GET("/getNewmldsa65", a.getNewmldsa65)
  39. g.GET("/getNewmlkem768", a.getNewmlkem768)
  40. g.GET("/getNewVlessEnc", a.getNewVlessEnc)
  41. g.POST("/stopXrayService", a.stopXrayService)
  42. g.POST("/restartXrayService", a.restartXrayService)
  43. g.POST("/installXray/:version", a.installXray)
  44. g.POST("/updateGeofile", a.updateGeofile)
  45. g.POST("/updateGeofile/:fileName", a.updateGeofile)
  46. g.POST("/logs/:count", a.getLogs)
  47. g.POST("/xraylogs/:count", a.getXrayLogs)
  48. g.POST("/importDB", a.importDB)
  49. g.POST("/getNewEchCert", a.getNewEchCert)
  50. }
  51. func (a *ServerController) refreshStatus() {
  52. a.lastStatus = a.serverService.GetStatus(a.lastStatus)
  53. // collect cpu history when status is fresh
  54. if a.lastStatus != nil {
  55. a.serverService.AppendCpuSample(time.Now(), a.lastStatus.Cpu)
  56. }
  57. }
  58. func (a *ServerController) startTask() {
  59. webServer := global.GetWebServer()
  60. c := webServer.GetCron()
  61. c.AddFunc("@every 2s", func() {
  62. // Always refresh to keep CPU history collected continuously.
  63. // Sampling is lightweight and capped to ~6 hours in memory.
  64. a.refreshStatus()
  65. })
  66. }
  67. func (a *ServerController) status(c *gin.Context) {
  68. a.lastGetStatusTime = time.Now()
  69. jsonObj(c, a.lastStatus, nil)
  70. }
  71. // getCpuHistory returns recent CPU utilization points.
  72. // Query param q=minutes (int). Bounds: 1..360 (6 hours). Defaults to 60.
  73. func (a *ServerController) getCpuHistory(c *gin.Context) {
  74. minsStr := c.Query("q")
  75. mins := 60
  76. if minsStr != "" {
  77. if v, err := strconv.Atoi(minsStr); err == nil {
  78. mins = v
  79. }
  80. }
  81. if mins < 1 {
  82. mins = 1
  83. }
  84. if mins > 360 {
  85. mins = 360
  86. }
  87. res := a.serverService.GetCpuHistory(mins)
  88. jsonObj(c, res, nil)
  89. }
  90. func (a *ServerController) getXrayVersion(c *gin.Context) {
  91. now := time.Now()
  92. if now.Sub(a.lastGetVersionsTime) <= time.Minute {
  93. jsonObj(c, a.lastVersions, nil)
  94. return
  95. }
  96. versions, err := a.serverService.GetXrayVersions()
  97. if err != nil {
  98. jsonMsg(c, I18nWeb(c, "getVersion"), err)
  99. return
  100. }
  101. a.lastVersions = versions
  102. a.lastGetVersionsTime = time.Now()
  103. jsonObj(c, versions, nil)
  104. }
  105. func (a *ServerController) installXray(c *gin.Context) {
  106. version := c.Param("version")
  107. err := a.serverService.UpdateXray(version)
  108. jsonMsg(c, I18nWeb(c, "pages.index.xraySwitchVersionPopover"), err)
  109. }
  110. func (a *ServerController) updateGeofile(c *gin.Context) {
  111. fileName := c.Param("fileName")
  112. err := a.serverService.UpdateGeofile(fileName)
  113. jsonMsg(c, I18nWeb(c, "pages.index.geofileUpdatePopover"), err)
  114. }
  115. func (a *ServerController) stopXrayService(c *gin.Context) {
  116. a.lastGetStatusTime = time.Now()
  117. err := a.serverService.StopXrayService()
  118. if err != nil {
  119. jsonMsg(c, I18nWeb(c, "pages.xray.stopError"), err)
  120. return
  121. }
  122. jsonMsg(c, I18nWeb(c, "pages.xray.stopSuccess"), err)
  123. }
  124. func (a *ServerController) restartXrayService(c *gin.Context) {
  125. err := a.serverService.RestartXrayService()
  126. if err != nil {
  127. jsonMsg(c, I18nWeb(c, "pages.xray.restartError"), err)
  128. return
  129. }
  130. jsonMsg(c, I18nWeb(c, "pages.xray.restartSuccess"), err)
  131. }
  132. func (a *ServerController) getLogs(c *gin.Context) {
  133. count := c.Param("count")
  134. level := c.PostForm("level")
  135. syslog := c.PostForm("syslog")
  136. logs := a.serverService.GetLogs(count, level, syslog)
  137. jsonObj(c, logs, nil)
  138. }
  139. func (a *ServerController) getXrayLogs(c *gin.Context) {
  140. count := c.Param("count")
  141. filter := c.PostForm("filter")
  142. showDirect := c.PostForm("showDirect")
  143. showBlocked := c.PostForm("showBlocked")
  144. showProxy := c.PostForm("showProxy")
  145. var freedoms []string
  146. var blackholes []string
  147. //getting tags for freedom and blackhole outbounds
  148. config, err := a.settingService.GetDefaultXrayConfig()
  149. if err == nil && config != nil {
  150. if cfgMap, ok := config.(map[string]interface{}); ok {
  151. if outbounds, ok := cfgMap["outbounds"].([]interface{}); ok {
  152. for _, outbound := range outbounds {
  153. if obMap, ok := outbound.(map[string]interface{}); ok {
  154. switch obMap["protocol"] {
  155. case "freedom":
  156. if tag, ok := obMap["tag"].(string); ok {
  157. freedoms = append(freedoms, tag)
  158. }
  159. case "blackhole":
  160. if tag, ok := obMap["tag"].(string); ok {
  161. blackholes = append(blackholes, tag)
  162. }
  163. }
  164. }
  165. }
  166. }
  167. }
  168. }
  169. if len(freedoms) == 0 {
  170. freedoms = []string{"direct"}
  171. }
  172. if len(blackholes) == 0 {
  173. blackholes = []string{"blocked"}
  174. }
  175. logs := a.serverService.GetXrayLogs(count, filter, showDirect, showBlocked, showProxy, freedoms, blackholes)
  176. jsonObj(c, logs, nil)
  177. }
  178. func (a *ServerController) getConfigJson(c *gin.Context) {
  179. configJson, err := a.serverService.GetConfigJson()
  180. if err != nil {
  181. jsonMsg(c, I18nWeb(c, "pages.index.getConfigError"), err)
  182. return
  183. }
  184. jsonObj(c, configJson, nil)
  185. }
  186. func (a *ServerController) getDb(c *gin.Context) {
  187. db, err := a.serverService.GetDb()
  188. if err != nil {
  189. jsonMsg(c, I18nWeb(c, "pages.index.getDatabaseError"), err)
  190. return
  191. }
  192. filename := "x-ui.db"
  193. if !isValidFilename(filename) {
  194. c.AbortWithError(http.StatusBadRequest, fmt.Errorf("invalid filename"))
  195. return
  196. }
  197. // Set the headers for the response
  198. c.Header("Content-Type", "application/octet-stream")
  199. c.Header("Content-Disposition", "attachment; filename="+filename)
  200. // Write the file contents to the response
  201. c.Writer.Write(db)
  202. }
  203. func isValidFilename(filename string) bool {
  204. // Validate that the filename only contains allowed characters
  205. return filenameRegex.MatchString(filename)
  206. }
  207. func (a *ServerController) importDB(c *gin.Context) {
  208. // Get the file from the request body
  209. file, _, err := c.Request.FormFile("db")
  210. if err != nil {
  211. jsonMsg(c, I18nWeb(c, "pages.index.readDatabaseError"), err)
  212. return
  213. }
  214. defer file.Close()
  215. // Always restart Xray before return
  216. defer a.serverService.RestartXrayService()
  217. defer func() {
  218. a.lastGetStatusTime = time.Now()
  219. }()
  220. // Import it
  221. err = a.serverService.ImportDB(file)
  222. if err != nil {
  223. jsonMsg(c, I18nWeb(c, "pages.index.importDatabaseError"), err)
  224. return
  225. }
  226. jsonObj(c, I18nWeb(c, "pages.index.importDatabaseSuccess"), nil)
  227. }
  228. func (a *ServerController) getNewX25519Cert(c *gin.Context) {
  229. cert, err := a.serverService.GetNewX25519Cert()
  230. if err != nil {
  231. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.getNewX25519CertError"), err)
  232. return
  233. }
  234. jsonObj(c, cert, nil)
  235. }
  236. func (a *ServerController) getNewmldsa65(c *gin.Context) {
  237. cert, err := a.serverService.GetNewmldsa65()
  238. if err != nil {
  239. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.getNewmldsa65Error"), err)
  240. return
  241. }
  242. jsonObj(c, cert, nil)
  243. }
  244. func (a *ServerController) getNewEchCert(c *gin.Context) {
  245. sni := c.PostForm("sni")
  246. cert, err := a.serverService.GetNewEchCert(sni)
  247. if err != nil {
  248. jsonMsg(c, "get ech certificate", err)
  249. return
  250. }
  251. jsonObj(c, cert, nil)
  252. }
  253. func (a *ServerController) getNewVlessEnc(c *gin.Context) {
  254. out, err := a.serverService.GetNewVlessEnc()
  255. if err != nil {
  256. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.getNewVlessEncError"), err)
  257. return
  258. }
  259. jsonObj(c, out, nil)
  260. }
  261. func (a *ServerController) getNewUUID(c *gin.Context) {
  262. uuidResp, err := a.serverService.GetNewUUID()
  263. if err != nil {
  264. jsonMsg(c, "Failed to generate UUID", err)
  265. return
  266. }
  267. jsonObj(c, uuidResp, nil)
  268. }
  269. func (a *ServerController) getNewmlkem768(c *gin.Context) {
  270. out, err := a.serverService.GetNewmlkem768()
  271. if err != nil {
  272. jsonMsg(c, "Failed to generate mlkem768 keys", err)
  273. return
  274. }
  275. jsonObj(c, out, nil)
  276. }