server.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. settingService service.SettingService
  16. lastStatus *service.Status
  17. lastGetStatusTime time.Time
  18. lastVersions []string
  19. lastGetVersionsTime time.Time
  20. }
  21. func NewServerController(g *gin.RouterGroup) *ServerController {
  22. a := &ServerController{
  23. lastGetStatusTime: time.Now(),
  24. }
  25. a.initRouter(g)
  26. a.startTask()
  27. return a
  28. }
  29. func (a *ServerController) initRouter(g *gin.RouterGroup) {
  30. g = g.Group("/server")
  31. g.Use(a.checkLogin)
  32. g.POST("/status", a.status)
  33. g.POST("/getXrayVersion", a.getXrayVersion)
  34. g.POST("/stopXrayService", a.stopXrayService)
  35. g.POST("/restartXrayService", a.restartXrayService)
  36. g.POST("/installXray/:version", a.installXray)
  37. g.POST("/updateGeofile", a.updateGeofile)
  38. g.POST("/updateGeofile/:fileName", a.updateGeofile)
  39. g.POST("/logs/:count", a.getLogs)
  40. g.POST("/xraylogs/:count", a.getXrayLogs)
  41. g.POST("/getConfigJson", a.getConfigJson)
  42. g.GET("/getDb", a.getDb)
  43. g.POST("/importDB", a.importDB)
  44. g.POST("/getNewX25519Cert", a.getNewX25519Cert)
  45. g.POST("/getNewmldsa65", a.getNewmldsa65)
  46. g.POST("/getNewEchCert", a.getNewEchCert)
  47. g.POST("/getNewVlessEnc", a.getNewVlessEnc)
  48. }
  49. func (a *ServerController) refreshStatus() {
  50. a.lastStatus = a.serverService.GetStatus(a.lastStatus)
  51. }
  52. func (a *ServerController) startTask() {
  53. webServer := global.GetWebServer()
  54. c := webServer.GetCron()
  55. c.AddFunc("@every 2s", func() {
  56. now := time.Now()
  57. if now.Sub(a.lastGetStatusTime) > time.Minute*3 {
  58. return
  59. }
  60. a.refreshStatus()
  61. })
  62. }
  63. func (a *ServerController) status(c *gin.Context) {
  64. a.lastGetStatusTime = time.Now()
  65. jsonObj(c, a.lastStatus, nil)
  66. }
  67. func (a *ServerController) getXrayVersion(c *gin.Context) {
  68. now := time.Now()
  69. if now.Sub(a.lastGetVersionsTime) <= time.Minute {
  70. jsonObj(c, a.lastVersions, nil)
  71. return
  72. }
  73. versions, err := a.serverService.GetXrayVersions()
  74. if err != nil {
  75. jsonMsg(c, I18nWeb(c, "getVersion"), err)
  76. return
  77. }
  78. a.lastVersions = versions
  79. a.lastGetVersionsTime = time.Now()
  80. jsonObj(c, versions, nil)
  81. }
  82. func (a *ServerController) installXray(c *gin.Context) {
  83. version := c.Param("version")
  84. err := a.serverService.UpdateXray(version)
  85. jsonMsg(c, I18nWeb(c, "pages.index.xraySwitchVersionPopover"), err)
  86. }
  87. func (a *ServerController) updateGeofile(c *gin.Context) {
  88. fileName := c.Param("fileName")
  89. err := a.serverService.UpdateGeofile(fileName)
  90. jsonMsg(c, I18nWeb(c, "pages.index.geofileUpdatePopover"), err)
  91. }
  92. func (a *ServerController) stopXrayService(c *gin.Context) {
  93. a.lastGetStatusTime = time.Now()
  94. err := a.serverService.StopXrayService()
  95. if err != nil {
  96. jsonMsg(c, I18nWeb(c, "pages.xray.stopError"), err)
  97. return
  98. }
  99. jsonMsg(c, I18nWeb(c, "pages.xray.stopSuccess"), err)
  100. }
  101. func (a *ServerController) restartXrayService(c *gin.Context) {
  102. err := a.serverService.RestartXrayService()
  103. if err != nil {
  104. jsonMsg(c, I18nWeb(c, "pages.xray.restartError"), err)
  105. return
  106. }
  107. jsonMsg(c, I18nWeb(c, "pages.xray.restartSuccess"), err)
  108. }
  109. func (a *ServerController) getLogs(c *gin.Context) {
  110. count := c.Param("count")
  111. level := c.PostForm("level")
  112. syslog := c.PostForm("syslog")
  113. logs := a.serverService.GetLogs(count, level, syslog)
  114. jsonObj(c, logs, nil)
  115. }
  116. func (a *ServerController) getXrayLogs(c *gin.Context) {
  117. count := c.Param("count")
  118. filter := c.PostForm("filter")
  119. showDirect := c.PostForm("showDirect")
  120. showBlocked := c.PostForm("showBlocked")
  121. showProxy := c.PostForm("showProxy")
  122. var freedoms []string
  123. var blackholes []string
  124. //getting tags for freedom and blackhole outbounds
  125. config, err := a.settingService.GetDefaultXrayConfig()
  126. if err == nil && config != nil {
  127. if cfgMap, ok := config.(map[string]interface{}); ok {
  128. if outbounds, ok := cfgMap["outbounds"].([]interface{}); ok {
  129. for _, outbound := range outbounds {
  130. if obMap, ok := outbound.(map[string]interface{}); ok {
  131. switch obMap["protocol"] {
  132. case "freedom":
  133. if tag, ok := obMap["tag"].(string); ok {
  134. freedoms = append(freedoms, tag)
  135. }
  136. case "blackhole":
  137. if tag, ok := obMap["tag"].(string); ok {
  138. blackholes = append(blackholes, tag)
  139. }
  140. }
  141. }
  142. }
  143. }
  144. }
  145. }
  146. if len(freedoms) == 0 {
  147. freedoms = []string{"direct"}
  148. }
  149. if len(blackholes) == 0 {
  150. blackholes = []string{"blocked"}
  151. }
  152. logs := a.serverService.GetXrayLogs(count, filter, showDirect, showBlocked, showProxy, freedoms, blackholes)
  153. jsonObj(c, logs, nil)
  154. }
  155. func (a *ServerController) getConfigJson(c *gin.Context) {
  156. configJson, err := a.serverService.GetConfigJson()
  157. if err != nil {
  158. jsonMsg(c, I18nWeb(c, "pages.index.getConfigError"), err)
  159. return
  160. }
  161. jsonObj(c, configJson, nil)
  162. }
  163. func (a *ServerController) getDb(c *gin.Context) {
  164. db, err := a.serverService.GetDb()
  165. if err != nil {
  166. jsonMsg(c, I18nWeb(c, "pages.index.getDatabaseError"), err)
  167. return
  168. }
  169. filename := "x-ui.db"
  170. if !isValidFilename(filename) {
  171. c.AbortWithError(http.StatusBadRequest, fmt.Errorf("invalid filename"))
  172. return
  173. }
  174. // Set the headers for the response
  175. c.Header("Content-Type", "application/octet-stream")
  176. c.Header("Content-Disposition", "attachment; filename="+filename)
  177. // Write the file contents to the response
  178. c.Writer.Write(db)
  179. }
  180. func isValidFilename(filename string) bool {
  181. // Validate that the filename only contains allowed characters
  182. return filenameRegex.MatchString(filename)
  183. }
  184. func (a *ServerController) importDB(c *gin.Context) {
  185. // Get the file from the request body
  186. file, _, err := c.Request.FormFile("db")
  187. if err != nil {
  188. jsonMsg(c, I18nWeb(c, "pages.index.readDatabaseError"), err)
  189. return
  190. }
  191. defer file.Close()
  192. // Always restart Xray before return
  193. defer a.serverService.RestartXrayService()
  194. defer func() {
  195. a.lastGetStatusTime = time.Now()
  196. }()
  197. // Import it
  198. err = a.serverService.ImportDB(file)
  199. if err != nil {
  200. jsonMsg(c, I18nWeb(c, "pages.index.importDatabaseError"), err)
  201. return
  202. }
  203. jsonObj(c, I18nWeb(c, "pages.index.importDatabaseSuccess"), nil)
  204. }
  205. func (a *ServerController) getNewX25519Cert(c *gin.Context) {
  206. cert, err := a.serverService.GetNewX25519Cert()
  207. if err != nil {
  208. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.getNewX25519CertError"), err)
  209. return
  210. }
  211. jsonObj(c, cert, nil)
  212. }
  213. func (a *ServerController) getNewmldsa65(c *gin.Context) {
  214. cert, err := a.serverService.GetNewmldsa65()
  215. if err != nil {
  216. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.getNewmldsa65Error"), err)
  217. return
  218. }
  219. jsonObj(c, cert, nil)
  220. }
  221. func (a *ServerController) getNewEchCert(c *gin.Context) {
  222. sni := c.PostForm("sni")
  223. cert, err := a.serverService.GetNewEchCert(sni)
  224. if err != nil {
  225. jsonMsg(c, "get ech certificate", err)
  226. return
  227. }
  228. jsonObj(c, cert, nil)
  229. }
  230. func (a *ServerController) getNewVlessEnc(c *gin.Context) {
  231. out, err := a.serverService.GetNewVlessEnc()
  232. if err != nil {
  233. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.getNewVlessEncError"), err)
  234. return
  235. }
  236. jsonObj(c, out, nil)
  237. }