1
0

server.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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/database"
  10. "github.com/mhsanaei/3x-ui/v3/logger"
  11. "github.com/mhsanaei/3x-ui/v3/web/entity"
  12. "github.com/mhsanaei/3x-ui/v3/web/global"
  13. "github.com/mhsanaei/3x-ui/v3/web/service"
  14. "github.com/mhsanaei/3x-ui/v3/web/websocket"
  15. "github.com/gin-gonic/gin"
  16. )
  17. var filenameRegex = regexp.MustCompile(`^[a-zA-Z0-9_\-.]+$`)
  18. // ServerController handles server management and status-related operations.
  19. type ServerController struct {
  20. BaseController
  21. serverService service.ServerService
  22. settingService service.SettingService
  23. panelService service.PanelService
  24. xrayMetricsService service.XrayMetricsService
  25. }
  26. // NewServerController creates a new ServerController, initializes routes, and starts background tasks.
  27. func NewServerController(g *gin.RouterGroup) *ServerController {
  28. a := &ServerController{}
  29. service.RestoreSystemMetrics()
  30. a.initRouter(g)
  31. a.startTask()
  32. return a
  33. }
  34. // initRouter sets up the routes for server status, Xray management, and utility endpoints.
  35. func (a *ServerController) initRouter(g *gin.RouterGroup) {
  36. g.GET("/status", a.status)
  37. g.GET("/cpuHistory/:bucket", a.getCpuHistoryBucket)
  38. g.GET("/history/:metric/:bucket", a.getMetricHistoryBucket)
  39. g.GET("/xrayMetricsState", a.getXrayMetricsState)
  40. g.GET("/xrayMetricsHistory/:metric/:bucket", a.getXrayMetricsHistoryBucket)
  41. g.GET("/xrayObservatory", a.getXrayObservatory)
  42. g.GET("/xrayObservatoryHistory/:tag/:bucket", a.getXrayObservatoryHistoryBucket)
  43. g.GET("/getXrayVersion", a.getXrayVersion)
  44. g.GET("/getPanelUpdateInfo", a.getPanelUpdateInfo)
  45. g.GET("/getConfigJson", a.getConfigJson)
  46. g.GET("/getDb", a.getDb)
  47. g.GET("/getNewUUID", a.getNewUUID)
  48. g.GET("/getNewX25519Cert", a.getNewX25519Cert)
  49. g.GET("/getNewmldsa65", a.getNewmldsa65)
  50. g.GET("/getNewmlkem768", a.getNewmlkem768)
  51. g.GET("/getNewVlessEnc", a.getNewVlessEnc)
  52. g.POST("/stopXrayService", a.stopXrayService)
  53. g.POST("/restartXrayService", a.restartXrayService)
  54. g.POST("/installXray/:version", a.installXray)
  55. g.POST("/updatePanel", a.updatePanel)
  56. g.POST("/updateGeofile", a.updateGeofile)
  57. g.POST("/updateGeofile/:fileName", a.updateGeofile)
  58. g.POST("/logs/:count", a.getLogs)
  59. g.POST("/xraylogs/:count", a.getXrayLogs)
  60. g.POST("/importDB", a.importDB)
  61. g.POST("/getNewEchCert", a.getNewEchCert)
  62. }
  63. // startTask registers the @2s ticker that refreshes server status, samples
  64. // xray metrics, and pushes the new snapshot to all websocket subscribers.
  65. // State + sampling live in ServerService; the controller only orchestrates
  66. // the cross-service side effects (xrayMetrics sample + websocket broadcast).
  67. func (a *ServerController) startTask() {
  68. c := global.GetWebServer().GetCron()
  69. c.AddFunc("@every 2s", func() {
  70. status := a.serverService.RefreshStatus()
  71. if status == nil {
  72. return
  73. }
  74. a.xrayMetricsService.Sample(time.Now())
  75. websocket.BroadcastStatus(status)
  76. })
  77. c.AddFunc("@every 1m", func() {
  78. if err := service.PersistSystemMetrics(); err != nil {
  79. logger.Warning("persist system metrics failed:", err)
  80. }
  81. })
  82. }
  83. // status returns the current server status information.
  84. func (a *ServerController) status(c *gin.Context) { jsonObj(c, a.serverService.LastStatus(), nil) }
  85. func parseHistoryBucket(c *gin.Context) (int, bool) {
  86. bucket, err := strconv.Atoi(c.Param("bucket"))
  87. if err != nil || bucket <= 0 || !service.IsAllowedHistoryBucket(bucket) {
  88. jsonMsg(c, "invalid bucket", fmt.Errorf("unsupported bucket"))
  89. return 0, false
  90. }
  91. return bucket, true
  92. }
  93. // getCpuHistoryBucket retrieves aggregated CPU usage history based on the specified time bucket.
  94. // Kept for back-compat; new callers should use /history/cpu/:bucket which
  95. // returns {"t","v"} (uniform across all metrics) instead of {"t","cpu"}.
  96. func (a *ServerController) getCpuHistoryBucket(c *gin.Context) {
  97. bucket, ok := parseHistoryBucket(c)
  98. if !ok {
  99. return
  100. }
  101. jsonObj(c, a.serverService.AggregateCpuHistory(bucket, 60), nil)
  102. }
  103. // getMetricHistoryBucket returns up to 60 buckets of history for a single
  104. // system metric (cpu, mem, netUp, netDown, online, load1/5/15). The
  105. // SystemHistoryModal calls one endpoint per active tab.
  106. func (a *ServerController) getMetricHistoryBucket(c *gin.Context) {
  107. metric := c.Param("metric")
  108. if !slices.Contains(service.SystemMetricKeys, metric) {
  109. jsonMsg(c, "invalid metric", fmt.Errorf("unknown metric"))
  110. return
  111. }
  112. bucket, ok := parseHistoryBucket(c)
  113. if !ok {
  114. return
  115. }
  116. jsonObj(c, a.serverService.AggregateSystemMetric(metric, bucket, 60), nil)
  117. }
  118. func (a *ServerController) getXrayMetricsState(c *gin.Context) {
  119. jsonObj(c, a.xrayMetricsService.State(), nil)
  120. }
  121. func (a *ServerController) getXrayMetricsHistoryBucket(c *gin.Context) {
  122. metric := c.Param("metric")
  123. if !slices.Contains(service.XrayMetricKeys, metric) {
  124. jsonMsg(c, "invalid metric", fmt.Errorf("unknown metric"))
  125. return
  126. }
  127. bucket, ok := parseHistoryBucket(c)
  128. if !ok {
  129. return
  130. }
  131. jsonObj(c, a.xrayMetricsService.AggregateMetric(metric, bucket, 60), nil)
  132. }
  133. func (a *ServerController) getXrayObservatory(c *gin.Context) {
  134. jsonObj(c, a.xrayMetricsService.ObservatorySnapshot(), nil)
  135. }
  136. func (a *ServerController) getXrayObservatoryHistoryBucket(c *gin.Context) {
  137. tag := c.Param("tag")
  138. if !a.xrayMetricsService.HasObservatoryTag(tag) {
  139. jsonMsg(c, "invalid tag", fmt.Errorf("unknown observatory tag"))
  140. return
  141. }
  142. bucket, ok := parseHistoryBucket(c)
  143. if !ok {
  144. return
  145. }
  146. jsonObj(c, a.xrayMetricsService.AggregateObservatory(tag, bucket, 60), nil)
  147. }
  148. func (a *ServerController) getXrayVersion(c *gin.Context) {
  149. versions, err := a.serverService.GetXrayVersionsCached()
  150. if err != nil {
  151. jsonMsg(c, I18nWeb(c, "getVersion"), err)
  152. return
  153. }
  154. jsonObj(c, versions, nil)
  155. }
  156. // getPanelUpdateInfo retrieves the current and latest panel version.
  157. func (a *ServerController) getPanelUpdateInfo(c *gin.Context) {
  158. info, err := a.panelService.GetUpdateInfo()
  159. if err != nil {
  160. logger.Debug("panel update check failed:", err)
  161. c.JSON(http.StatusOK, entity.Msg{Success: false})
  162. return
  163. }
  164. jsonObj(c, info, nil)
  165. }
  166. // installXray installs or updates Xray to the specified version.
  167. func (a *ServerController) installXray(c *gin.Context) {
  168. version := c.Param("version")
  169. err := a.serverService.UpdateXray(version)
  170. jsonMsg(c, I18nWeb(c, "pages.index.xraySwitchVersionPopover"), err)
  171. }
  172. // updatePanel starts a panel self-update to the latest release.
  173. func (a *ServerController) updatePanel(c *gin.Context) {
  174. err := a.panelService.StartUpdate()
  175. jsonMsg(c, I18nWeb(c, "pages.index.panelUpdateStartedPopover"), err)
  176. }
  177. // updateGeofile updates the specified geo file for Xray.
  178. func (a *ServerController) updateGeofile(c *gin.Context) {
  179. fileName := c.Param("fileName")
  180. if fileName != "" && !a.serverService.IsValidGeofileName(fileName) {
  181. jsonMsg(c, I18nWeb(c, "pages.index.geofileUpdatePopover"),
  182. fmt.Errorf("invalid filename: contains unsafe characters or path traversal patterns"))
  183. return
  184. }
  185. err := a.serverService.UpdateGeofile(fileName)
  186. jsonMsg(c, I18nWeb(c, "pages.index.geofileUpdatePopover"), err)
  187. }
  188. // stopXrayService stops the Xray service.
  189. func (a *ServerController) stopXrayService(c *gin.Context) {
  190. err := a.serverService.StopXrayService()
  191. if err != nil {
  192. jsonMsg(c, I18nWeb(c, "pages.xray.stopError"), err)
  193. websocket.BroadcastXrayState("error", err.Error())
  194. return
  195. }
  196. jsonMsg(c, I18nWeb(c, "pages.xray.stopSuccess"), err)
  197. websocket.BroadcastXrayState("stop", "")
  198. websocket.BroadcastNotification(
  199. I18nWeb(c, "pages.xray.stopSuccess"),
  200. "Xray service has been stopped",
  201. "warning",
  202. )
  203. }
  204. // restartXrayService restarts the Xray service.
  205. func (a *ServerController) restartXrayService(c *gin.Context) {
  206. err := a.serverService.RestartXrayService()
  207. if err != nil {
  208. jsonMsg(c, I18nWeb(c, "pages.xray.restartError"), err)
  209. websocket.BroadcastXrayState("error", err.Error())
  210. return
  211. }
  212. jsonMsg(c, I18nWeb(c, "pages.xray.restartSuccess"), err)
  213. websocket.BroadcastXrayState("running", "")
  214. websocket.BroadcastNotification(
  215. I18nWeb(c, "pages.xray.restartSuccess"),
  216. "Xray service has been restarted successfully",
  217. "success",
  218. )
  219. }
  220. // getLogs retrieves the application logs based on count, level, and syslog filters.
  221. func (a *ServerController) getLogs(c *gin.Context) {
  222. logs := a.serverService.GetLogs(c.Param("count"), c.PostForm("level"), c.PostForm("syslog"))
  223. jsonObj(c, logs, nil)
  224. }
  225. // getXrayLogs retrieves Xray logs with filtering options for direct, blocked, and proxy traffic.
  226. func (a *ServerController) getXrayLogs(c *gin.Context) {
  227. freedoms, blackholes := a.serverService.GetDefaultLogOutboundTags()
  228. logs := a.serverService.GetXrayLogs(
  229. c.Param("count"),
  230. c.PostForm("filter"),
  231. c.PostForm("showDirect"),
  232. c.PostForm("showBlocked"),
  233. c.PostForm("showProxy"),
  234. freedoms,
  235. blackholes,
  236. )
  237. jsonObj(c, logs, nil)
  238. }
  239. // getConfigJson retrieves the Xray configuration as JSON.
  240. func (a *ServerController) getConfigJson(c *gin.Context) {
  241. configJson, err := a.serverService.GetConfigJson()
  242. if err != nil {
  243. jsonMsg(c, I18nWeb(c, "pages.index.getConfigError"), err)
  244. return
  245. }
  246. jsonObj(c, configJson, nil)
  247. }
  248. // getDb downloads the database file.
  249. func (a *ServerController) getDb(c *gin.Context) {
  250. db, err := a.serverService.GetDb()
  251. if err != nil {
  252. jsonMsg(c, I18nWeb(c, "pages.index.getDatabaseError"), err)
  253. return
  254. }
  255. filename := "x-ui.db"
  256. if database.IsPostgres() {
  257. filename = "x-ui.dump"
  258. }
  259. if !filenameRegex.MatchString(filename) {
  260. c.AbortWithError(http.StatusBadRequest, fmt.Errorf("invalid filename"))
  261. return
  262. }
  263. c.Header("Content-Type", "application/octet-stream")
  264. c.Header("Content-Disposition", "attachment; filename="+filename)
  265. c.Writer.Write(db)
  266. }
  267. // importDB imports a database file and restarts the Xray service.
  268. func (a *ServerController) importDB(c *gin.Context) {
  269. file, _, err := c.Request.FormFile("db")
  270. if err != nil {
  271. jsonMsg(c, I18nWeb(c, "pages.index.readDatabaseError"), err)
  272. return
  273. }
  274. defer file.Close()
  275. if err := a.serverService.ImportDB(file); err != nil {
  276. jsonMsg(c, I18nWeb(c, "pages.index.importDatabaseError"), err)
  277. return
  278. }
  279. jsonObj(c, I18nWeb(c, "pages.index.importDatabaseSuccess"), nil)
  280. }
  281. // getNewX25519Cert generates a new X25519 certificate.
  282. func (a *ServerController) getNewX25519Cert(c *gin.Context) {
  283. cert, err := a.serverService.GetNewX25519Cert()
  284. if err != nil {
  285. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.getNewX25519CertError"), err)
  286. return
  287. }
  288. jsonObj(c, cert, nil)
  289. }
  290. // getNewmldsa65 generates a new ML-DSA-65 key.
  291. func (a *ServerController) getNewmldsa65(c *gin.Context) {
  292. cert, err := a.serverService.GetNewmldsa65()
  293. if err != nil {
  294. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.getNewmldsa65Error"), err)
  295. return
  296. }
  297. jsonObj(c, cert, nil)
  298. }
  299. // getNewEchCert generates a new ECH certificate for the given SNI.
  300. func (a *ServerController) getNewEchCert(c *gin.Context) {
  301. cert, err := a.serverService.GetNewEchCert(c.PostForm("sni"))
  302. if err != nil {
  303. jsonMsg(c, "get ech certificate", err)
  304. return
  305. }
  306. jsonObj(c, cert, nil)
  307. }
  308. // getNewVlessEnc generates a new VLESS encryption key.
  309. func (a *ServerController) getNewVlessEnc(c *gin.Context) {
  310. out, err := a.serverService.GetNewVlessEnc()
  311. if err != nil {
  312. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.getNewVlessEncError"), err)
  313. return
  314. }
  315. jsonObj(c, out, nil)
  316. }
  317. // getNewUUID generates a new UUID.
  318. func (a *ServerController) getNewUUID(c *gin.Context) {
  319. uuidResp, err := a.serverService.GetNewUUID()
  320. if err != nil {
  321. jsonMsg(c, "Failed to generate UUID", err)
  322. return
  323. }
  324. jsonObj(c, uuidResp, nil)
  325. }
  326. // getNewmlkem768 generates a new ML-KEM-768 key.
  327. func (a *ServerController) getNewmlkem768(c *gin.Context) {
  328. out, err := a.serverService.GetNewmlkem768()
  329. if err != nil {
  330. jsonMsg(c, "Failed to generate mlkem768 keys", err)
  331. return
  332. }
  333. jsonObj(c, out, nil)
  334. }