server.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. package controller
  2. import (
  3. "fmt"
  4. "net/http"
  5. "regexp"
  6. "slices"
  7. "strconv"
  8. "time"
  9. "github.com/mhsanaei/3x-ui/v3/logger"
  10. "github.com/mhsanaei/3x-ui/v3/web/entity"
  11. "github.com/mhsanaei/3x-ui/v3/web/global"
  12. "github.com/mhsanaei/3x-ui/v3/web/service"
  13. "github.com/mhsanaei/3x-ui/v3/web/websocket"
  14. "github.com/gin-gonic/gin"
  15. )
  16. var filenameRegex = regexp.MustCompile(`^[a-zA-Z0-9_\-.]+$`)
  17. // ServerController handles server management and status-related operations.
  18. type ServerController struct {
  19. BaseController
  20. serverService service.ServerService
  21. settingService service.SettingService
  22. panelService service.PanelService
  23. xrayMetricsService service.XrayMetricsService
  24. lastStatus *service.Status
  25. lastVersions []string
  26. lastGetVersionsTime int64 // unix seconds
  27. }
  28. // NewServerController creates a new ServerController, initializes routes, and starts background tasks.
  29. func NewServerController(g *gin.RouterGroup) *ServerController {
  30. a := &ServerController{}
  31. a.initRouter(g)
  32. a.startTask()
  33. return a
  34. }
  35. // initRouter sets up the routes for server status, Xray management, and utility endpoints.
  36. func (a *ServerController) initRouter(g *gin.RouterGroup) {
  37. g.GET("/status", a.status)
  38. g.GET("/cpuHistory/:bucket", a.getCpuHistoryBucket)
  39. g.GET("/history/:metric/:bucket", a.getMetricHistoryBucket)
  40. g.GET("/xrayMetricsState", a.getXrayMetricsState)
  41. g.GET("/xrayMetricsHistory/:metric/:bucket", a.getXrayMetricsHistoryBucket)
  42. g.GET("/xrayObservatory", a.getXrayObservatory)
  43. g.GET("/xrayObservatoryHistory/:tag/:bucket", a.getXrayObservatoryHistoryBucket)
  44. g.GET("/getXrayVersion", a.getXrayVersion)
  45. g.GET("/getPanelUpdateInfo", a.getPanelUpdateInfo)
  46. g.GET("/getConfigJson", a.getConfigJson)
  47. g.GET("/getDb", a.getDb)
  48. g.GET("/getNewUUID", a.getNewUUID)
  49. g.GET("/getNewX25519Cert", a.getNewX25519Cert)
  50. g.GET("/getNewmldsa65", a.getNewmldsa65)
  51. g.GET("/getNewmlkem768", a.getNewmlkem768)
  52. g.GET("/getNewVlessEnc", a.getNewVlessEnc)
  53. g.POST("/stopXrayService", a.stopXrayService)
  54. g.POST("/restartXrayService", a.restartXrayService)
  55. g.POST("/installXray/:version", a.installXray)
  56. g.POST("/updatePanel", a.updatePanel)
  57. g.POST("/updateGeofile", a.updateGeofile)
  58. g.POST("/updateGeofile/:fileName", a.updateGeofile)
  59. g.POST("/logs/:count", a.getLogs)
  60. g.POST("/xraylogs/:count", a.getXrayLogs)
  61. g.POST("/importDB", a.importDB)
  62. g.POST("/getNewEchCert", a.getNewEchCert)
  63. }
  64. // refreshStatus updates the cached server status and collects time-series
  65. // metrics. CPU/Mem/Net/Online/Load are all written in one call so the
  66. // SystemHistoryModal's tabs share an identical x-axis.
  67. func (a *ServerController) refreshStatus() {
  68. a.lastStatus = a.serverService.GetStatus(a.lastStatus)
  69. if a.lastStatus != nil {
  70. now := time.Now()
  71. a.serverService.AppendStatusSample(now, a.lastStatus)
  72. a.xrayMetricsService.Sample(now)
  73. // Broadcast status update via WebSocket
  74. websocket.BroadcastStatus(a.lastStatus)
  75. }
  76. }
  77. // startTask initiates background tasks for continuous status monitoring.
  78. func (a *ServerController) startTask() {
  79. webServer := global.GetWebServer()
  80. c := webServer.GetCron()
  81. c.AddFunc("@every 2s", func() {
  82. // Always refresh to keep CPU history collected continuously.
  83. // Sampling is lightweight and capped to ~6 hours in memory.
  84. a.refreshStatus()
  85. })
  86. }
  87. // status returns the current server status information.
  88. func (a *ServerController) status(c *gin.Context) { jsonObj(c, a.lastStatus, nil) }
  89. // allowedHistoryBuckets is the bucket-second whitelist shared by both
  90. // /cpuHistory/:bucket and /history/:metric/:bucket. Restricting it
  91. // prevents callers from triggering arbitrary aggregation work and keeps
  92. // the front-end's bucket selector self-documenting.
  93. var allowedHistoryBuckets = map[int]bool{
  94. 2: true, // Real-time view
  95. 30: true, // 30s intervals
  96. 60: true, // 1m intervals
  97. 120: true, // 2m intervals
  98. 180: true, // 3m intervals
  99. 300: true, // 5m intervals
  100. }
  101. // getCpuHistoryBucket retrieves aggregated CPU usage history based on the specified time bucket.
  102. // Kept for back-compat; new callers should use /history/cpu/:bucket which
  103. // returns {"t","v"} (uniform across all metrics) instead of {"t","cpu"}.
  104. func (a *ServerController) getCpuHistoryBucket(c *gin.Context) {
  105. bucketStr := c.Param("bucket")
  106. bucket, err := strconv.Atoi(bucketStr)
  107. if err != nil || bucket <= 0 {
  108. jsonMsg(c, "invalid bucket", fmt.Errorf("bad bucket"))
  109. return
  110. }
  111. if !allowedHistoryBuckets[bucket] {
  112. jsonMsg(c, "invalid bucket", fmt.Errorf("unsupported bucket"))
  113. return
  114. }
  115. points := a.serverService.AggregateCpuHistory(bucket, 60)
  116. jsonObj(c, points, nil)
  117. }
  118. // getMetricHistoryBucket returns up to 60 buckets of history for a single
  119. // system metric (cpu, mem, netUp, netDown, online, load1/5/15). The
  120. // SystemHistoryModal calls one endpoint per active tab.
  121. func (a *ServerController) getMetricHistoryBucket(c *gin.Context) {
  122. metric := c.Param("metric")
  123. if !slices.Contains(service.SystemMetricKeys, metric) {
  124. jsonMsg(c, "invalid metric", fmt.Errorf("unknown metric"))
  125. return
  126. }
  127. bucket, err := strconv.Atoi(c.Param("bucket"))
  128. if err != nil || bucket <= 0 || !allowedHistoryBuckets[bucket] {
  129. jsonMsg(c, "invalid bucket", fmt.Errorf("unsupported bucket"))
  130. return
  131. }
  132. jsonObj(c, a.serverService.AggregateSystemMetric(metric, bucket, 60), nil)
  133. }
  134. func (a *ServerController) getXrayMetricsState(c *gin.Context) {
  135. jsonObj(c, a.xrayMetricsService.State(), nil)
  136. }
  137. func (a *ServerController) getXrayMetricsHistoryBucket(c *gin.Context) {
  138. metric := c.Param("metric")
  139. if !slices.Contains(service.XrayMetricKeys, metric) {
  140. jsonMsg(c, "invalid metric", fmt.Errorf("unknown metric"))
  141. return
  142. }
  143. bucket, err := strconv.Atoi(c.Param("bucket"))
  144. if err != nil || bucket <= 0 || !allowedHistoryBuckets[bucket] {
  145. jsonMsg(c, "invalid bucket", fmt.Errorf("unsupported bucket"))
  146. return
  147. }
  148. jsonObj(c, a.xrayMetricsService.AggregateMetric(metric, bucket, 60), nil)
  149. }
  150. func (a *ServerController) getXrayObservatory(c *gin.Context) {
  151. jsonObj(c, a.xrayMetricsService.ObservatorySnapshot(), nil)
  152. }
  153. func (a *ServerController) getXrayObservatoryHistoryBucket(c *gin.Context) {
  154. tag := c.Param("tag")
  155. if !a.xrayMetricsService.HasObservatoryTag(tag) {
  156. jsonMsg(c, "invalid tag", fmt.Errorf("unknown observatory tag"))
  157. return
  158. }
  159. bucket, err := strconv.Atoi(c.Param("bucket"))
  160. if err != nil || bucket <= 0 || !allowedHistoryBuckets[bucket] {
  161. jsonMsg(c, "invalid bucket", fmt.Errorf("unsupported bucket"))
  162. return
  163. }
  164. jsonObj(c, a.xrayMetricsService.AggregateObservatory(tag, bucket, 60), nil)
  165. }
  166. func (a *ServerController) getXrayVersion(c *gin.Context) {
  167. const cacheTTLSeconds = 15 * 60
  168. now := time.Now().Unix()
  169. if a.lastVersions != nil && now-a.lastGetVersionsTime <= cacheTTLSeconds {
  170. jsonObj(c, a.lastVersions, nil)
  171. return
  172. }
  173. versions, err := a.serverService.GetXrayVersions()
  174. if err != nil {
  175. if a.lastVersions != nil {
  176. logger.Warning("getXrayVersion failed; serving cached list:", err)
  177. jsonObj(c, a.lastVersions, nil)
  178. return
  179. }
  180. jsonMsg(c, I18nWeb(c, "getVersion"), err)
  181. return
  182. }
  183. a.lastVersions = versions
  184. a.lastGetVersionsTime = now
  185. jsonObj(c, versions, nil)
  186. }
  187. // getPanelUpdateInfo retrieves the current and latest panel version.
  188. func (a *ServerController) getPanelUpdateInfo(c *gin.Context) {
  189. info, err := a.panelService.GetUpdateInfo()
  190. if err != nil {
  191. logger.Debug("panel update check failed:", err)
  192. c.JSON(http.StatusOK, entity.Msg{Success: false})
  193. return
  194. }
  195. jsonObj(c, info, nil)
  196. }
  197. // installXray installs or updates Xray to the specified version.
  198. func (a *ServerController) installXray(c *gin.Context) {
  199. version := c.Param("version")
  200. err := a.serverService.UpdateXray(version)
  201. jsonMsg(c, I18nWeb(c, "pages.index.xraySwitchVersionPopover"), err)
  202. }
  203. // updatePanel starts a panel self-update to the latest release.
  204. func (a *ServerController) updatePanel(c *gin.Context) {
  205. err := a.panelService.StartUpdate()
  206. jsonMsg(c, I18nWeb(c, "pages.index.panelUpdateStartedPopover"), err)
  207. }
  208. // updateGeofile updates the specified geo file for Xray.
  209. func (a *ServerController) updateGeofile(c *gin.Context) {
  210. fileName := c.Param("fileName")
  211. // Validate the filename for security (prevent path traversal attacks)
  212. if fileName != "" && !a.serverService.IsValidGeofileName(fileName) {
  213. jsonMsg(c, I18nWeb(c, "pages.index.geofileUpdatePopover"),
  214. fmt.Errorf("invalid filename: contains unsafe characters or path traversal patterns"))
  215. return
  216. }
  217. err := a.serverService.UpdateGeofile(fileName)
  218. jsonMsg(c, I18nWeb(c, "pages.index.geofileUpdatePopover"), err)
  219. }
  220. // stopXrayService stops the Xray service.
  221. func (a *ServerController) stopXrayService(c *gin.Context) {
  222. err := a.serverService.StopXrayService()
  223. if err != nil {
  224. jsonMsg(c, I18nWeb(c, "pages.xray.stopError"), err)
  225. websocket.BroadcastXrayState("error", err.Error())
  226. return
  227. }
  228. jsonMsg(c, I18nWeb(c, "pages.xray.stopSuccess"), err)
  229. websocket.BroadcastXrayState("stop", "")
  230. websocket.BroadcastNotification(
  231. I18nWeb(c, "pages.xray.stopSuccess"),
  232. "Xray service has been stopped",
  233. "warning",
  234. )
  235. }
  236. // restartXrayService restarts the Xray service.
  237. func (a *ServerController) restartXrayService(c *gin.Context) {
  238. err := a.serverService.RestartXrayService()
  239. if err != nil {
  240. jsonMsg(c, I18nWeb(c, "pages.xray.restartError"), err)
  241. websocket.BroadcastXrayState("error", err.Error())
  242. return
  243. }
  244. jsonMsg(c, I18nWeb(c, "pages.xray.restartSuccess"), err)
  245. websocket.BroadcastXrayState("running", "")
  246. websocket.BroadcastNotification(
  247. I18nWeb(c, "pages.xray.restartSuccess"),
  248. "Xray service has been restarted successfully",
  249. "success",
  250. )
  251. }
  252. // getLogs retrieves the application logs based on count, level, and syslog filters.
  253. func (a *ServerController) getLogs(c *gin.Context) {
  254. count := c.Param("count")
  255. level := c.PostForm("level")
  256. syslog := c.PostForm("syslog")
  257. logs := a.serverService.GetLogs(count, level, syslog)
  258. jsonObj(c, logs, nil)
  259. }
  260. // getXrayLogs retrieves Xray logs with filtering options for direct, blocked, and proxy traffic.
  261. func (a *ServerController) getXrayLogs(c *gin.Context) {
  262. count := c.Param("count")
  263. filter := c.PostForm("filter")
  264. showDirect := c.PostForm("showDirect")
  265. showBlocked := c.PostForm("showBlocked")
  266. showProxy := c.PostForm("showProxy")
  267. var freedoms []string
  268. var blackholes []string
  269. //getting tags for freedom and blackhole outbounds
  270. config, err := a.settingService.GetDefaultXrayConfig()
  271. if err == nil && config != nil {
  272. if cfgMap, ok := config.(map[string]any); ok {
  273. if outbounds, ok := cfgMap["outbounds"].([]any); ok {
  274. for _, outbound := range outbounds {
  275. if obMap, ok := outbound.(map[string]any); ok {
  276. switch obMap["protocol"] {
  277. case "freedom":
  278. if tag, ok := obMap["tag"].(string); ok {
  279. freedoms = append(freedoms, tag)
  280. }
  281. case "blackhole":
  282. if tag, ok := obMap["tag"].(string); ok {
  283. blackholes = append(blackholes, tag)
  284. }
  285. }
  286. }
  287. }
  288. }
  289. }
  290. }
  291. if len(freedoms) == 0 {
  292. freedoms = []string{"direct"}
  293. }
  294. if len(blackholes) == 0 {
  295. blackholes = []string{"blocked"}
  296. }
  297. logs := a.serverService.GetXrayLogs(count, filter, showDirect, showBlocked, showProxy, freedoms, blackholes)
  298. jsonObj(c, logs, nil)
  299. }
  300. // getConfigJson retrieves the Xray configuration as JSON.
  301. func (a *ServerController) getConfigJson(c *gin.Context) {
  302. configJson, err := a.serverService.GetConfigJson()
  303. if err != nil {
  304. jsonMsg(c, I18nWeb(c, "pages.index.getConfigError"), err)
  305. return
  306. }
  307. jsonObj(c, configJson, nil)
  308. }
  309. // getDb downloads the database file.
  310. func (a *ServerController) getDb(c *gin.Context) {
  311. db, err := a.serverService.GetDb()
  312. if err != nil {
  313. jsonMsg(c, I18nWeb(c, "pages.index.getDatabaseError"), err)
  314. return
  315. }
  316. filename := "x-ui.db"
  317. if !isValidFilename(filename) {
  318. c.AbortWithError(http.StatusBadRequest, fmt.Errorf("invalid filename"))
  319. return
  320. }
  321. // Set the headers for the response
  322. c.Header("Content-Type", "application/octet-stream")
  323. c.Header("Content-Disposition", "attachment; filename="+filename)
  324. // Write the file contents to the response
  325. c.Writer.Write(db)
  326. }
  327. func isValidFilename(filename string) bool {
  328. // Validate that the filename only contains allowed characters
  329. return filenameRegex.MatchString(filename)
  330. }
  331. // importDB imports a database file and restarts the Xray service.
  332. func (a *ServerController) importDB(c *gin.Context) {
  333. // Get the file from the request body
  334. file, _, err := c.Request.FormFile("db")
  335. if err != nil {
  336. jsonMsg(c, I18nWeb(c, "pages.index.readDatabaseError"), err)
  337. return
  338. }
  339. defer file.Close()
  340. err = a.serverService.ImportDB(file)
  341. if err != nil {
  342. jsonMsg(c, I18nWeb(c, "pages.index.importDatabaseError"), err)
  343. return
  344. }
  345. jsonObj(c, I18nWeb(c, "pages.index.importDatabaseSuccess"), nil)
  346. }
  347. // getNewX25519Cert generates a new X25519 certificate.
  348. func (a *ServerController) getNewX25519Cert(c *gin.Context) {
  349. cert, err := a.serverService.GetNewX25519Cert()
  350. if err != nil {
  351. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.getNewX25519CertError"), err)
  352. return
  353. }
  354. jsonObj(c, cert, nil)
  355. }
  356. // getNewmldsa65 generates a new ML-DSA-65 key.
  357. func (a *ServerController) getNewmldsa65(c *gin.Context) {
  358. cert, err := a.serverService.GetNewmldsa65()
  359. if err != nil {
  360. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.getNewmldsa65Error"), err)
  361. return
  362. }
  363. jsonObj(c, cert, nil)
  364. }
  365. // getNewEchCert generates a new ECH certificate for the given SNI.
  366. func (a *ServerController) getNewEchCert(c *gin.Context) {
  367. sni := c.PostForm("sni")
  368. cert, err := a.serverService.GetNewEchCert(sni)
  369. if err != nil {
  370. jsonMsg(c, "get ech certificate", err)
  371. return
  372. }
  373. jsonObj(c, cert, nil)
  374. }
  375. // getNewVlessEnc generates a new VLESS encryption key.
  376. func (a *ServerController) getNewVlessEnc(c *gin.Context) {
  377. out, err := a.serverService.GetNewVlessEnc()
  378. if err != nil {
  379. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.getNewVlessEncError"), err)
  380. return
  381. }
  382. jsonObj(c, out, nil)
  383. }
  384. // getNewUUID generates a new UUID.
  385. func (a *ServerController) getNewUUID(c *gin.Context) {
  386. uuidResp, err := a.serverService.GetNewUUID()
  387. if err != nil {
  388. jsonMsg(c, "Failed to generate UUID", err)
  389. return
  390. }
  391. jsonObj(c, uuidResp, nil)
  392. }
  393. // getNewmlkem768 generates a new ML-KEM-768 key.
  394. func (a *ServerController) getNewmlkem768(c *gin.Context) {
  395. out, err := a.serverService.GetNewmlkem768()
  396. if err != nil {
  397. jsonMsg(c, "Failed to generate mlkem768 keys", err)
  398. return
  399. }
  400. jsonObj(c, out, nil)
  401. }