server.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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. // Validate the filename for security (prevent path traversal attacks)
  120. if fileName != "" && !a.serverService.IsValidGeofileName(fileName) {
  121. jsonMsg(c, I18nWeb(c, "pages.index.geofileUpdatePopover"),
  122. fmt.Errorf("invalid filename: contains unsafe characters or path traversal patterns"))
  123. return
  124. }
  125. err := a.serverService.UpdateGeofile(fileName)
  126. jsonMsg(c, I18nWeb(c, "pages.index.geofileUpdatePopover"), err)
  127. }
  128. // stopXrayService stops the Xray service.
  129. func (a *ServerController) stopXrayService(c *gin.Context) {
  130. err := a.serverService.StopXrayService()
  131. if err != nil {
  132. jsonMsg(c, I18nWeb(c, "pages.xray.stopError"), err)
  133. return
  134. }
  135. jsonMsg(c, I18nWeb(c, "pages.xray.stopSuccess"), err)
  136. }
  137. // restartXrayService restarts the Xray service.
  138. func (a *ServerController) restartXrayService(c *gin.Context) {
  139. err := a.serverService.RestartXrayService()
  140. if err != nil {
  141. jsonMsg(c, I18nWeb(c, "pages.xray.restartError"), err)
  142. return
  143. }
  144. jsonMsg(c, I18nWeb(c, "pages.xray.restartSuccess"), err)
  145. }
  146. // getLogs retrieves the application logs based on count, level, and syslog filters.
  147. func (a *ServerController) getLogs(c *gin.Context) {
  148. count := c.Param("count")
  149. level := c.PostForm("level")
  150. syslog := c.PostForm("syslog")
  151. logs := a.serverService.GetLogs(count, level, syslog)
  152. jsonObj(c, logs, nil)
  153. }
  154. // getXrayLogs retrieves Xray logs with filtering options for direct, blocked, and proxy traffic.
  155. func (a *ServerController) getXrayLogs(c *gin.Context) {
  156. count := c.Param("count")
  157. filter := c.PostForm("filter")
  158. showDirect := c.PostForm("showDirect")
  159. showBlocked := c.PostForm("showBlocked")
  160. showProxy := c.PostForm("showProxy")
  161. var freedoms []string
  162. var blackholes []string
  163. //getting tags for freedom and blackhole outbounds
  164. config, err := a.settingService.GetDefaultXrayConfig()
  165. if err == nil && config != nil {
  166. if cfgMap, ok := config.(map[string]interface{}); ok {
  167. if outbounds, ok := cfgMap["outbounds"].([]interface{}); ok {
  168. for _, outbound := range outbounds {
  169. if obMap, ok := outbound.(map[string]interface{}); ok {
  170. switch obMap["protocol"] {
  171. case "freedom":
  172. if tag, ok := obMap["tag"].(string); ok {
  173. freedoms = append(freedoms, tag)
  174. }
  175. case "blackhole":
  176. if tag, ok := obMap["tag"].(string); ok {
  177. blackholes = append(blackholes, tag)
  178. }
  179. }
  180. }
  181. }
  182. }
  183. }
  184. }
  185. if len(freedoms) == 0 {
  186. freedoms = []string{"direct"}
  187. }
  188. if len(blackholes) == 0 {
  189. blackholes = []string{"blocked"}
  190. }
  191. logs := a.serverService.GetXrayLogs(count, filter, showDirect, showBlocked, showProxy, freedoms, blackholes)
  192. jsonObj(c, logs, nil)
  193. }
  194. // getConfigJson retrieves the Xray configuration as JSON.
  195. func (a *ServerController) getConfigJson(c *gin.Context) {
  196. configJson, err := a.serverService.GetConfigJson()
  197. if err != nil {
  198. jsonMsg(c, I18nWeb(c, "pages.index.getConfigError"), err)
  199. return
  200. }
  201. jsonObj(c, configJson, nil)
  202. }
  203. // getDb downloads the database file.
  204. func (a *ServerController) getDb(c *gin.Context) {
  205. db, err := a.serverService.GetDb()
  206. if err != nil {
  207. jsonMsg(c, I18nWeb(c, "pages.index.getDatabaseError"), err)
  208. return
  209. }
  210. filename := "x-ui.db"
  211. if !isValidFilename(filename) {
  212. c.AbortWithError(http.StatusBadRequest, fmt.Errorf("invalid filename"))
  213. return
  214. }
  215. // Set the headers for the response
  216. c.Header("Content-Type", "application/octet-stream")
  217. c.Header("Content-Disposition", "attachment; filename="+filename)
  218. // Write the file contents to the response
  219. c.Writer.Write(db)
  220. }
  221. func isValidFilename(filename string) bool {
  222. // Validate that the filename only contains allowed characters
  223. return filenameRegex.MatchString(filename)
  224. }
  225. // importDB imports a database file and restarts the Xray service.
  226. func (a *ServerController) importDB(c *gin.Context) {
  227. // Get the file from the request body
  228. file, _, err := c.Request.FormFile("db")
  229. if err != nil {
  230. jsonMsg(c, I18nWeb(c, "pages.index.readDatabaseError"), err)
  231. return
  232. }
  233. defer file.Close()
  234. // Always restart Xray before return
  235. defer a.serverService.RestartXrayService()
  236. // lastGetStatusTime removed; no longer needed
  237. // Import it
  238. err = a.serverService.ImportDB(file)
  239. if err != nil {
  240. jsonMsg(c, I18nWeb(c, "pages.index.importDatabaseError"), err)
  241. return
  242. }
  243. jsonObj(c, I18nWeb(c, "pages.index.importDatabaseSuccess"), nil)
  244. }
  245. // getNewX25519Cert generates a new X25519 certificate.
  246. func (a *ServerController) getNewX25519Cert(c *gin.Context) {
  247. cert, err := a.serverService.GetNewX25519Cert()
  248. if err != nil {
  249. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.getNewX25519CertError"), err)
  250. return
  251. }
  252. jsonObj(c, cert, nil)
  253. }
  254. // getNewmldsa65 generates a new ML-DSA-65 key.
  255. func (a *ServerController) getNewmldsa65(c *gin.Context) {
  256. cert, err := a.serverService.GetNewmldsa65()
  257. if err != nil {
  258. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.getNewmldsa65Error"), err)
  259. return
  260. }
  261. jsonObj(c, cert, nil)
  262. }
  263. // getNewEchCert generates a new ECH certificate for the given SNI.
  264. func (a *ServerController) getNewEchCert(c *gin.Context) {
  265. sni := c.PostForm("sni")
  266. cert, err := a.serverService.GetNewEchCert(sni)
  267. if err != nil {
  268. jsonMsg(c, "get ech certificate", err)
  269. return
  270. }
  271. jsonObj(c, cert, nil)
  272. }
  273. // getNewVlessEnc generates a new VLESS encryption key.
  274. func (a *ServerController) getNewVlessEnc(c *gin.Context) {
  275. out, err := a.serverService.GetNewVlessEnc()
  276. if err != nil {
  277. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.getNewVlessEncError"), err)
  278. return
  279. }
  280. jsonObj(c, out, nil)
  281. }
  282. // getNewUUID generates a new UUID.
  283. func (a *ServerController) getNewUUID(c *gin.Context) {
  284. uuidResp, err := a.serverService.GetNewUUID()
  285. if err != nil {
  286. jsonMsg(c, "Failed to generate UUID", err)
  287. return
  288. }
  289. jsonObj(c, uuidResp, nil)
  290. }
  291. // getNewmlkem768 generates a new ML-KEM-768 key.
  292. func (a *ServerController) getNewmlkem768(c *gin.Context) {
  293. out, err := a.serverService.GetNewmlkem768()
  294. if err != nil {
  295. jsonMsg(c, "Failed to generate mlkem768 keys", err)
  296. return
  297. }
  298. jsonObj(c, out, nil)
  299. }