server.go 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. package controller
  2. import (
  3. "fmt"
  4. "net/http"
  5. "regexp"
  6. "strconv"
  7. "time"
  8. "github.com/mhsanaei/3x-ui/v2/web/global"
  9. "github.com/mhsanaei/3x-ui/v2/web/service"
  10. "github.com/gin-gonic/gin"
  11. )
  12. var filenameRegex = regexp.MustCompile(`^[a-zA-Z0-9_\-.]+$`)
  13. // ServerController handles server management and status-related operations.
  14. type ServerController struct {
  15. BaseController
  16. serverService service.ServerService
  17. settingService service.SettingService
  18. lastStatus *service.Status
  19. lastVersions []string
  20. lastGetVersionsTime int64 // unix seconds
  21. }
  22. // NewServerController creates a new ServerController, initializes routes, and starts background tasks.
  23. func NewServerController(g *gin.RouterGroup) *ServerController {
  24. a := &ServerController{}
  25. a.initRouter(g)
  26. a.startTask()
  27. return a
  28. }
  29. // initRouter sets up the routes for server status, Xray management, and utility endpoints.
  30. func (a *ServerController) initRouter(g *gin.RouterGroup) {
  31. g.GET("/status", a.status)
  32. g.GET("/cpuHistory/:bucket", a.getCpuHistoryBucket)
  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. // refreshStatus updates the cached server status and collects CPU history.
  52. func (a *ServerController) refreshStatus() {
  53. a.lastStatus = a.serverService.GetStatus(a.lastStatus)
  54. // collect cpu history when status is fresh
  55. if a.lastStatus != nil {
  56. a.serverService.AppendCpuSample(time.Now(), a.lastStatus.Cpu)
  57. }
  58. }
  59. // startTask initiates background tasks for continuous status monitoring.
  60. func (a *ServerController) startTask() {
  61. webServer := global.GetWebServer()
  62. c := webServer.GetCron()
  63. c.AddFunc("@every 2s", func() {
  64. // Always refresh to keep CPU history collected continuously.
  65. // Sampling is lightweight and capped to ~6 hours in memory.
  66. a.refreshStatus()
  67. })
  68. }
  69. // status returns the current server status information.
  70. func (a *ServerController) status(c *gin.Context) { jsonObj(c, a.lastStatus, nil) }
  71. // getCpuHistoryBucket retrieves aggregated CPU usage history based on the specified time bucket.
  72. func (a *ServerController) getCpuHistoryBucket(c *gin.Context) {
  73. bucketStr := c.Param("bucket")
  74. bucket, err := strconv.Atoi(bucketStr)
  75. if err != nil || bucket <= 0 {
  76. jsonMsg(c, "invalid bucket", fmt.Errorf("bad bucket"))
  77. return
  78. }
  79. allowed := map[int]bool{
  80. 2: true, // Real-time view
  81. 30: true, // 30s intervals
  82. 60: true, // 1m intervals
  83. 120: true, // 2m intervals
  84. 180: true, // 3m intervals
  85. 300: true, // 5m intervals
  86. }
  87. if !allowed[bucket] {
  88. jsonMsg(c, "invalid bucket", fmt.Errorf("unsupported bucket"))
  89. return
  90. }
  91. points := a.serverService.AggregateCpuHistory(bucket, 60)
  92. jsonObj(c, points, nil)
  93. }
  94. // getXrayVersion retrieves available Xray versions, with caching for 1 minute.
  95. func (a *ServerController) getXrayVersion(c *gin.Context) {
  96. now := time.Now().Unix()
  97. if now-a.lastGetVersionsTime <= 60 { // 1 minute cache
  98. jsonObj(c, a.lastVersions, nil)
  99. return
  100. }
  101. versions, err := a.serverService.GetXrayVersions()
  102. if err != nil {
  103. jsonMsg(c, I18nWeb(c, "getVersion"), err)
  104. return
  105. }
  106. a.lastVersions = versions
  107. a.lastGetVersionsTime = now
  108. jsonObj(c, versions, nil)
  109. }
  110. // installXray installs or updates Xray to the specified version.
  111. func (a *ServerController) installXray(c *gin.Context) {
  112. version := c.Param("version")
  113. err := a.serverService.UpdateXray(version)
  114. jsonMsg(c, I18nWeb(c, "pages.index.xraySwitchVersionPopover"), err)
  115. }
  116. // updateGeofile updates the specified geo file for Xray.
  117. func (a *ServerController) updateGeofile(c *gin.Context) {
  118. fileName := c.Param("fileName")
  119. err := a.serverService.UpdateGeofile(fileName)
  120. jsonMsg(c, I18nWeb(c, "pages.index.geofileUpdatePopover"), err)
  121. }
  122. // stopXrayService stops the Xray service.
  123. func (a *ServerController) stopXrayService(c *gin.Context) {
  124. err := a.serverService.StopXrayService()
  125. if err != nil {
  126. jsonMsg(c, I18nWeb(c, "pages.xray.stopError"), err)
  127. return
  128. }
  129. jsonMsg(c, I18nWeb(c, "pages.xray.stopSuccess"), err)
  130. }
  131. // restartXrayService restarts the Xray service.
  132. func (a *ServerController) restartXrayService(c *gin.Context) {
  133. err := a.serverService.RestartXrayService()
  134. if err != nil {
  135. jsonMsg(c, I18nWeb(c, "pages.xray.restartError"), err)
  136. return
  137. }
  138. jsonMsg(c, I18nWeb(c, "pages.xray.restartSuccess"), err)
  139. }
  140. // getLogs retrieves the application logs based on count, level, and syslog filters.
  141. func (a *ServerController) getLogs(c *gin.Context) {
  142. count := c.Param("count")
  143. level := c.PostForm("level")
  144. syslog := c.PostForm("syslog")
  145. logs := a.serverService.GetLogs(count, level, syslog)
  146. jsonObj(c, logs, nil)
  147. }
  148. // getXrayLogs retrieves Xray logs with filtering options for direct, blocked, and proxy traffic.
  149. func (a *ServerController) getXrayLogs(c *gin.Context) {
  150. count := c.Param("count")
  151. filter := c.PostForm("filter")
  152. showDirect := c.PostForm("showDirect")
  153. showBlocked := c.PostForm("showBlocked")
  154. showProxy := c.PostForm("showProxy")
  155. var freedoms []string
  156. var blackholes []string
  157. //getting tags for freedom and blackhole outbounds
  158. config, err := a.settingService.GetDefaultXrayConfig()
  159. if err == nil && config != nil {
  160. if cfgMap, ok := config.(map[string]interface{}); ok {
  161. if outbounds, ok := cfgMap["outbounds"].([]interface{}); ok {
  162. for _, outbound := range outbounds {
  163. if obMap, ok := outbound.(map[string]interface{}); ok {
  164. switch obMap["protocol"] {
  165. case "freedom":
  166. if tag, ok := obMap["tag"].(string); ok {
  167. freedoms = append(freedoms, tag)
  168. }
  169. case "blackhole":
  170. if tag, ok := obMap["tag"].(string); ok {
  171. blackholes = append(blackholes, tag)
  172. }
  173. }
  174. }
  175. }
  176. }
  177. }
  178. }
  179. if len(freedoms) == 0 {
  180. freedoms = []string{"direct"}
  181. }
  182. if len(blackholes) == 0 {
  183. blackholes = []string{"blocked"}
  184. }
  185. logs := a.serverService.GetXrayLogs(count, filter, showDirect, showBlocked, showProxy, freedoms, blackholes)
  186. jsonObj(c, logs, nil)
  187. }
  188. // getConfigJson retrieves the Xray configuration as JSON.
  189. func (a *ServerController) getConfigJson(c *gin.Context) {
  190. configJson, err := a.serverService.GetConfigJson()
  191. if err != nil {
  192. jsonMsg(c, I18nWeb(c, "pages.index.getConfigError"), err)
  193. return
  194. }
  195. jsonObj(c, configJson, nil)
  196. }
  197. // getDb downloads the database file.
  198. func (a *ServerController) getDb(c *gin.Context) {
  199. db, err := a.serverService.GetDb()
  200. if err != nil {
  201. jsonMsg(c, I18nWeb(c, "pages.index.getDatabaseError"), err)
  202. return
  203. }
  204. filename := "x-ui.db"
  205. if !isValidFilename(filename) {
  206. c.AbortWithError(http.StatusBadRequest, fmt.Errorf("invalid filename"))
  207. return
  208. }
  209. // Set the headers for the response
  210. c.Header("Content-Type", "application/octet-stream")
  211. c.Header("Content-Disposition", "attachment; filename="+filename)
  212. // Write the file contents to the response
  213. c.Writer.Write(db)
  214. }
  215. func isValidFilename(filename string) bool {
  216. // Validate that the filename only contains allowed characters
  217. return filenameRegex.MatchString(filename)
  218. }
  219. // importDB imports a database file and restarts the Xray service.
  220. func (a *ServerController) importDB(c *gin.Context) {
  221. // Get the file from the request body
  222. file, _, err := c.Request.FormFile("db")
  223. if err != nil {
  224. jsonMsg(c, I18nWeb(c, "pages.index.readDatabaseError"), err)
  225. return
  226. }
  227. defer file.Close()
  228. // Always restart Xray before return
  229. defer a.serverService.RestartXrayService()
  230. // lastGetStatusTime removed; no longer needed
  231. // Import it
  232. err = a.serverService.ImportDB(file)
  233. if err != nil {
  234. jsonMsg(c, I18nWeb(c, "pages.index.importDatabaseError"), err)
  235. return
  236. }
  237. jsonObj(c, I18nWeb(c, "pages.index.importDatabaseSuccess"), nil)
  238. }
  239. // getNewX25519Cert generates a new X25519 certificate.
  240. func (a *ServerController) getNewX25519Cert(c *gin.Context) {
  241. cert, err := a.serverService.GetNewX25519Cert()
  242. if err != nil {
  243. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.getNewX25519CertError"), err)
  244. return
  245. }
  246. jsonObj(c, cert, nil)
  247. }
  248. // getNewmldsa65 generates a new ML-DSA-65 key.
  249. func (a *ServerController) getNewmldsa65(c *gin.Context) {
  250. cert, err := a.serverService.GetNewmldsa65()
  251. if err != nil {
  252. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.getNewmldsa65Error"), err)
  253. return
  254. }
  255. jsonObj(c, cert, nil)
  256. }
  257. // getNewEchCert generates a new ECH certificate for the given SNI.
  258. func (a *ServerController) getNewEchCert(c *gin.Context) {
  259. sni := c.PostForm("sni")
  260. cert, err := a.serverService.GetNewEchCert(sni)
  261. if err != nil {
  262. jsonMsg(c, "get ech certificate", err)
  263. return
  264. }
  265. jsonObj(c, cert, nil)
  266. }
  267. // getNewVlessEnc generates a new VLESS encryption key.
  268. func (a *ServerController) getNewVlessEnc(c *gin.Context) {
  269. out, err := a.serverService.GetNewVlessEnc()
  270. if err != nil {
  271. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.getNewVlessEncError"), err)
  272. return
  273. }
  274. jsonObj(c, out, nil)
  275. }
  276. // getNewUUID generates a new UUID.
  277. func (a *ServerController) getNewUUID(c *gin.Context) {
  278. uuidResp, err := a.serverService.GetNewUUID()
  279. if err != nil {
  280. jsonMsg(c, "Failed to generate UUID", err)
  281. return
  282. }
  283. jsonObj(c, uuidResp, nil)
  284. }
  285. // getNewmlkem768 generates a new ML-KEM-768 key.
  286. func (a *ServerController) getNewmlkem768(c *gin.Context) {
  287. out, err := a.serverService.GetNewmlkem768()
  288. if err != nil {
  289. jsonMsg(c, "Failed to generate mlkem768 keys", err)
  290. return
  291. }
  292. jsonObj(c, out, nil)
  293. }