1
0

server.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. package controller
  2. import (
  3. "fmt"
  4. "net/http"
  5. "regexp"
  6. "time"
  7. "x-ui/web/global"
  8. "x-ui/web/service"
  9. "github.com/gin-gonic/gin"
  10. )
  11. var filenameRegex = regexp.MustCompile(`^[a-zA-Z0-9_\-.]+$`)
  12. type ServerController struct {
  13. BaseController
  14. serverService service.ServerService
  15. lastStatus *service.Status
  16. lastGetStatusTime time.Time
  17. lastVersions []string
  18. lastGetVersionsTime time.Time
  19. }
  20. func NewServerController(g *gin.RouterGroup) *ServerController {
  21. a := &ServerController{
  22. lastGetStatusTime: time.Now(),
  23. }
  24. a.initRouter(g)
  25. a.startTask()
  26. return a
  27. }
  28. func (a *ServerController) initRouter(g *gin.RouterGroup) {
  29. g = g.Group("/server")
  30. g.Use(a.checkLogin)
  31. g.POST("/status", a.status)
  32. g.POST("/getXrayVersion", a.getXrayVersion)
  33. g.POST("/stopXrayService", a.stopXrayService)
  34. g.POST("/restartXrayService", a.restartXrayService)
  35. g.POST("/installXray/:version", a.installXray)
  36. g.POST("/logs/:count", a.getLogs)
  37. g.POST("/getConfigJson", a.getConfigJson)
  38. g.GET("/getDb", a.getDb)
  39. g.POST("/importDB", a.importDB)
  40. g.POST("/getNewX25519Cert", a.getNewX25519Cert)
  41. }
  42. func (a *ServerController) refreshStatus() {
  43. a.lastStatus = a.serverService.GetStatus(a.lastStatus)
  44. }
  45. func (a *ServerController) startTask() {
  46. webServer := global.GetWebServer()
  47. c := webServer.GetCron()
  48. c.AddFunc("@every 2s", func() {
  49. now := time.Now()
  50. if now.Sub(a.lastGetStatusTime) > time.Minute*3 {
  51. return
  52. }
  53. a.refreshStatus()
  54. })
  55. }
  56. func (a *ServerController) status(c *gin.Context) {
  57. a.lastGetStatusTime = time.Now()
  58. jsonObj(c, a.lastStatus, nil)
  59. }
  60. func (a *ServerController) getXrayVersion(c *gin.Context) {
  61. now := time.Now()
  62. if now.Sub(a.lastGetVersionsTime) <= time.Minute {
  63. jsonObj(c, a.lastVersions, nil)
  64. return
  65. }
  66. versions, err := a.serverService.GetXrayVersions()
  67. if err != nil {
  68. jsonMsg(c, I18nWeb(c, "getVersion"), err)
  69. return
  70. }
  71. a.lastVersions = versions
  72. a.lastGetVersionsTime = time.Now()
  73. jsonObj(c, versions, nil)
  74. }
  75. func (a *ServerController) installXray(c *gin.Context) {
  76. version := c.Param("version")
  77. err := a.serverService.UpdateXray(version)
  78. jsonMsg(c, I18nWeb(c, "install")+" xray", err)
  79. }
  80. func (a *ServerController) stopXrayService(c *gin.Context) {
  81. a.lastGetStatusTime = time.Now()
  82. err := a.serverService.StopXrayService()
  83. if err != nil {
  84. jsonMsg(c, "", err)
  85. return
  86. }
  87. jsonMsg(c, "Xray stopped", err)
  88. }
  89. func (a *ServerController) restartXrayService(c *gin.Context) {
  90. err := a.serverService.RestartXrayService()
  91. if err != nil {
  92. jsonMsg(c, "", err)
  93. return
  94. }
  95. jsonMsg(c, "Xray restarted", err)
  96. }
  97. func (a *ServerController) getLogs(c *gin.Context) {
  98. count := c.Param("count")
  99. level := c.PostForm("level")
  100. syslog := c.PostForm("syslog")
  101. logs := a.serverService.GetLogs(count, level, syslog)
  102. jsonObj(c, logs, nil)
  103. }
  104. func (a *ServerController) getConfigJson(c *gin.Context) {
  105. configJson, err := a.serverService.GetConfigJson()
  106. if err != nil {
  107. jsonMsg(c, "get config.json", err)
  108. return
  109. }
  110. jsonObj(c, configJson, nil)
  111. }
  112. func (a *ServerController) getDb(c *gin.Context) {
  113. db, err := a.serverService.GetDb()
  114. if err != nil {
  115. jsonMsg(c, "get Database", err)
  116. return
  117. }
  118. filename := "x-ui.db"
  119. if !isValidFilename(filename) {
  120. c.AbortWithError(http.StatusBadRequest, fmt.Errorf("invalid filename"))
  121. return
  122. }
  123. // Set the headers for the response
  124. c.Header("Content-Type", "application/octet-stream")
  125. c.Header("Content-Disposition", "attachment; filename="+filename)
  126. // Write the file contents to the response
  127. c.Writer.Write(db)
  128. }
  129. func isValidFilename(filename string) bool {
  130. // Validate that the filename only contains allowed characters
  131. return filenameRegex.MatchString(filename)
  132. }
  133. func (a *ServerController) importDB(c *gin.Context) {
  134. // Get the file from the request body
  135. file, _, err := c.Request.FormFile("db")
  136. if err != nil {
  137. jsonMsg(c, "Error reading db file", err)
  138. return
  139. }
  140. defer file.Close()
  141. // Always restart Xray before return
  142. defer a.serverService.RestartXrayService()
  143. defer func() {
  144. a.lastGetStatusTime = time.Now()
  145. }()
  146. // Import it
  147. err = a.serverService.ImportDB(file)
  148. if err != nil {
  149. jsonMsg(c, "", err)
  150. return
  151. }
  152. jsonObj(c, "Import DB", nil)
  153. }
  154. func (a *ServerController) getNewX25519Cert(c *gin.Context) {
  155. cert, err := a.serverService.GetNewX25519Cert()
  156. if err != nil {
  157. jsonMsg(c, "get x25519 certificate", err)
  158. return
  159. }
  160. jsonObj(c, cert, nil)
  161. }