server.go 12 KB

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