server.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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/internal/database"
  10. "github.com/mhsanaei/3x-ui/v3/internal/database/model"
  11. "github.com/mhsanaei/3x-ui/v3/internal/logger"
  12. "github.com/mhsanaei/3x-ui/v3/internal/web/entity"
  13. "github.com/mhsanaei/3x-ui/v3/internal/web/global"
  14. "github.com/mhsanaei/3x-ui/v3/internal/web/service"
  15. "github.com/mhsanaei/3x-ui/v3/internal/web/service/panel"
  16. "github.com/mhsanaei/3x-ui/v3/internal/web/websocket"
  17. "github.com/gin-gonic/gin"
  18. )
  19. var filenameRegex = regexp.MustCompile(`^[a-zA-Z0-9_\-.]+$`)
  20. // ServerController handles server management and status-related operations.
  21. type ServerController struct {
  22. BaseController
  23. serverService service.ServerService
  24. settingService service.SettingService
  25. panelService panel.PanelService
  26. xrayMetricsService service.XrayMetricsService
  27. }
  28. // NewServerController creates a new ServerController, initializes routes, and starts background tasks.
  29. func NewServerController(g *gin.RouterGroup) *ServerController {
  30. a := &ServerController{}
  31. service.RestoreSystemMetrics()
  32. a.initRouter(g)
  33. a.startTask()
  34. return a
  35. }
  36. // initRouter sets up the routes for server status, Xray management, and utility endpoints.
  37. func (a *ServerController) initRouter(g *gin.RouterGroup) {
  38. g.GET("/status", a.status)
  39. g.GET("/cpuHistory/:bucket", a.getCpuHistoryBucket)
  40. g.GET("/history/:metric/:bucket", a.getMetricHistoryBucket)
  41. g.GET("/xrayMetricsState", a.getXrayMetricsState)
  42. g.GET("/xrayMetricsHistory/:metric/:bucket", a.getXrayMetricsHistoryBucket)
  43. g.GET("/xrayObservatory", a.getXrayObservatory)
  44. g.GET("/xrayObservatoryHistory/:tag/:bucket", a.getXrayObservatoryHistoryBucket)
  45. g.GET("/getXrayVersion", a.getXrayVersion)
  46. g.GET("/getPanelUpdateInfo", a.getPanelUpdateInfo)
  47. g.GET("/getConfigJson", a.getConfigJson)
  48. g.GET("/getDb", a.getDb)
  49. g.GET("/getMigration", a.getMigration)
  50. g.GET("/getNewUUID", a.getNewUUID)
  51. g.GET("/getWebCertFiles", a.getWebCertFiles)
  52. g.GET("/descendants", a.descendants)
  53. g.GET("/getNewX25519Cert", a.getNewX25519Cert)
  54. g.GET("/getNewmldsa65", a.getNewmldsa65)
  55. g.GET("/getNewmlkem768", a.getNewmlkem768)
  56. g.GET("/getNewVlessEnc", a.getNewVlessEnc)
  57. g.GET("/clientIps", a.getClientIps)
  58. g.POST("/stopXrayService", a.stopXrayService)
  59. g.POST("/restartXrayService", a.restartXrayService)
  60. g.POST("/installXray/:version", a.installXray)
  61. g.POST("/updatePanel", a.updatePanel)
  62. g.POST("/updateGeofile", a.updateGeofile)
  63. g.POST("/updateGeofile/:fileName", a.updateGeofile)
  64. g.POST("/logs/:count", a.getLogs)
  65. g.POST("/xraylogs/:count", a.getXrayLogs)
  66. g.POST("/importDB", a.importDB)
  67. g.POST("/getNewEchCert", a.getNewEchCert)
  68. g.POST("/getCertHash", a.getCertHash)
  69. g.POST("/getRemoteCertHash", a.getRemoteCertHash)
  70. g.POST("/clientIps", a.setClientIps)
  71. }
  72. // startTask registers the @2s ticker that refreshes server status, samples
  73. // xray metrics, and pushes the new snapshot to all websocket subscribers.
  74. // State + sampling live in ServerService; the controller only orchestrates
  75. // the cross-service side effects (xrayMetrics sample + websocket broadcast).
  76. func (a *ServerController) startTask() {
  77. c := global.GetWebServer().GetCron()
  78. c.AddFunc("@every 2s", func() {
  79. status := a.serverService.RefreshStatus()
  80. if status == nil {
  81. return
  82. }
  83. a.xrayMetricsService.Sample(time.Now())
  84. websocket.BroadcastStatus(status)
  85. })
  86. c.AddFunc("@every 1m", func() {
  87. if err := service.PersistSystemMetrics(); err != nil {
  88. logger.Warning("persist system metrics failed:", err)
  89. }
  90. })
  91. }
  92. // status returns the current server status information.
  93. func (a *ServerController) status(c *gin.Context) { jsonObj(c, a.serverService.LastStatus(), nil) }
  94. func parseHistoryBucket(c *gin.Context) (int, bool) {
  95. bucket, err := strconv.Atoi(c.Param("bucket"))
  96. if err != nil || bucket <= 0 || !service.IsAllowedHistoryBucket(bucket) {
  97. jsonMsg(c, "invalid bucket", fmt.Errorf("unsupported bucket"))
  98. return 0, false
  99. }
  100. return bucket, true
  101. }
  102. // getCpuHistoryBucket retrieves aggregated CPU usage history based on the specified time bucket.
  103. // Kept for back-compat; new callers should use /history/cpu/:bucket which
  104. // returns {"t","v"} (uniform across all metrics) instead of {"t","cpu"}.
  105. func (a *ServerController) getCpuHistoryBucket(c *gin.Context) {
  106. bucket, ok := parseHistoryBucket(c)
  107. if !ok {
  108. return
  109. }
  110. jsonObj(c, a.serverService.AggregateCpuHistory(bucket, 60), nil)
  111. }
  112. // getMetricHistoryBucket returns up to 60 buckets of history for a single
  113. // system metric (cpu, mem, netUp, netDown, online, load1/5/15). The
  114. // SystemHistoryModal calls one endpoint per active tab.
  115. func (a *ServerController) getMetricHistoryBucket(c *gin.Context) {
  116. metric := c.Param("metric")
  117. if !slices.Contains(service.SystemMetricKeys, metric) {
  118. jsonMsg(c, "invalid metric", fmt.Errorf("unknown metric"))
  119. return
  120. }
  121. bucket, ok := parseHistoryBucket(c)
  122. if !ok {
  123. return
  124. }
  125. jsonObj(c, a.serverService.AggregateSystemMetric(metric, bucket, 60), nil)
  126. }
  127. func (a *ServerController) getXrayMetricsState(c *gin.Context) {
  128. jsonObj(c, a.xrayMetricsService.State(), nil)
  129. }
  130. func (a *ServerController) getXrayMetricsHistoryBucket(c *gin.Context) {
  131. metric := c.Param("metric")
  132. if !slices.Contains(service.XrayMetricKeys, metric) {
  133. jsonMsg(c, "invalid metric", fmt.Errorf("unknown metric"))
  134. return
  135. }
  136. bucket, ok := parseHistoryBucket(c)
  137. if !ok {
  138. return
  139. }
  140. jsonObj(c, a.xrayMetricsService.AggregateMetric(metric, bucket, 60), nil)
  141. }
  142. func (a *ServerController) getXrayObservatory(c *gin.Context) {
  143. jsonObj(c, a.xrayMetricsService.ObservatorySnapshot(), nil)
  144. }
  145. func (a *ServerController) getXrayObservatoryHistoryBucket(c *gin.Context) {
  146. tag := c.Param("tag")
  147. if !a.xrayMetricsService.HasObservatoryTag(tag) {
  148. jsonMsg(c, "invalid tag", fmt.Errorf("unknown observatory tag"))
  149. return
  150. }
  151. bucket, ok := parseHistoryBucket(c)
  152. if !ok {
  153. return
  154. }
  155. jsonObj(c, a.xrayMetricsService.AggregateObservatory(tag, bucket, 60), nil)
  156. }
  157. func (a *ServerController) getXrayVersion(c *gin.Context) {
  158. versions, err := a.serverService.GetXrayVersionsCached()
  159. if err != nil {
  160. jsonMsg(c, I18nWeb(c, "getVersion"), err)
  161. return
  162. }
  163. jsonObj(c, versions, nil)
  164. }
  165. // getPanelUpdateInfo retrieves the current and latest panel version.
  166. func (a *ServerController) getPanelUpdateInfo(c *gin.Context) {
  167. info, err := a.panelService.GetUpdateInfo()
  168. if err != nil {
  169. logger.Debug("panel update check failed:", err)
  170. c.JSON(http.StatusOK, entity.Msg{Success: false})
  171. return
  172. }
  173. jsonObj(c, info, nil)
  174. }
  175. // installXray installs or updates Xray to the specified version.
  176. func (a *ServerController) installXray(c *gin.Context) {
  177. version := c.Param("version")
  178. err := a.serverService.UpdateXray(version)
  179. jsonMsg(c, I18nWeb(c, "pages.index.xraySwitchVersionPopover"), err)
  180. }
  181. // updatePanel starts a panel self-update to the latest release.
  182. func (a *ServerController) updatePanel(c *gin.Context) {
  183. err := a.panelService.StartUpdate()
  184. jsonMsg(c, I18nWeb(c, "pages.index.panelUpdateStartedPopover"), err)
  185. }
  186. // updateGeofile updates the specified geo file for Xray.
  187. func (a *ServerController) updateGeofile(c *gin.Context) {
  188. fileName := c.Param("fileName")
  189. if fileName != "" && !a.serverService.IsValidGeofileName(fileName) {
  190. jsonMsg(c, I18nWeb(c, "pages.index.geofileUpdatePopover"),
  191. fmt.Errorf("invalid filename: contains unsafe characters or path traversal patterns"))
  192. return
  193. }
  194. err := a.serverService.UpdateGeofile(fileName)
  195. jsonMsg(c, I18nWeb(c, "pages.index.geofileUpdatePopover"), err)
  196. }
  197. // stopXrayService stops the Xray service.
  198. func (a *ServerController) stopXrayService(c *gin.Context) {
  199. err := a.serverService.StopXrayService()
  200. if err != nil {
  201. jsonMsg(c, I18nWeb(c, "pages.xray.stopError"), err)
  202. websocket.BroadcastXrayState("error", err.Error())
  203. return
  204. }
  205. jsonMsg(c, I18nWeb(c, "pages.xray.stopSuccess"), err)
  206. websocket.BroadcastXrayState("stop", "")
  207. websocket.BroadcastNotification(
  208. I18nWeb(c, "pages.xray.stopSuccess"),
  209. "Xray service has been stopped",
  210. "warning",
  211. )
  212. }
  213. // restartXrayService restarts the Xray service.
  214. func (a *ServerController) restartXrayService(c *gin.Context) {
  215. err := a.serverService.RestartXrayService()
  216. if err != nil {
  217. jsonMsg(c, I18nWeb(c, "pages.xray.restartError"), err)
  218. websocket.BroadcastXrayState("error", err.Error())
  219. return
  220. }
  221. jsonMsg(c, I18nWeb(c, "pages.xray.restartSuccess"), err)
  222. websocket.BroadcastXrayState("running", "")
  223. websocket.BroadcastNotification(
  224. I18nWeb(c, "pages.xray.restartSuccess"),
  225. "Xray service has been restarted successfully",
  226. "success",
  227. )
  228. }
  229. // getLogs retrieves the application logs based on count, level, and syslog filters.
  230. func (a *ServerController) getLogs(c *gin.Context) {
  231. logs := a.serverService.GetLogs(c.Param("count"), c.PostForm("level"), c.PostForm("syslog"))
  232. jsonObj(c, logs, nil)
  233. }
  234. // getXrayLogs retrieves Xray logs with filtering options for direct, blocked, and proxy traffic.
  235. func (a *ServerController) getXrayLogs(c *gin.Context) {
  236. freedoms, blackholes := a.serverService.GetDefaultLogOutboundTags()
  237. logs := a.serverService.GetXrayLogs(
  238. c.Param("count"),
  239. c.PostForm("filter"),
  240. c.PostForm("showDirect"),
  241. c.PostForm("showBlocked"),
  242. c.PostForm("showProxy"),
  243. freedoms,
  244. blackholes,
  245. )
  246. jsonObj(c, logs, nil)
  247. }
  248. // getConfigJson retrieves the Xray configuration as JSON.
  249. func (a *ServerController) getConfigJson(c *gin.Context) {
  250. configJson, err := a.serverService.GetConfigJson()
  251. if err != nil {
  252. jsonMsg(c, I18nWeb(c, "pages.index.getConfigError"), err)
  253. return
  254. }
  255. jsonObj(c, configJson, nil)
  256. }
  257. // getDb downloads the database file.
  258. func (a *ServerController) getDb(c *gin.Context) {
  259. db, err := a.serverService.GetDb()
  260. if err != nil {
  261. jsonMsg(c, I18nWeb(c, "pages.index.getDatabaseError"), err)
  262. return
  263. }
  264. filename := "x-ui.db"
  265. if database.IsPostgres() {
  266. filename = "x-ui.dump"
  267. }
  268. if !filenameRegex.MatchString(filename) {
  269. c.AbortWithError(http.StatusBadRequest, fmt.Errorf("invalid filename"))
  270. return
  271. }
  272. c.Header("Content-Type", "application/octet-stream")
  273. c.Header("Content-Disposition", "attachment; filename="+filename)
  274. c.Writer.Write(db)
  275. }
  276. // getMigration downloads a cross-engine migration file: a .dump on SQLite or a
  277. // .db SQLite database on PostgreSQL, so the data can seed the other backend.
  278. func (a *ServerController) getMigration(c *gin.Context) {
  279. data, filename, err := a.serverService.GetMigration()
  280. if err != nil {
  281. jsonMsg(c, I18nWeb(c, "pages.index.getDatabaseError"), err)
  282. return
  283. }
  284. if !filenameRegex.MatchString(filename) {
  285. c.AbortWithError(http.StatusBadRequest, fmt.Errorf("invalid filename"))
  286. return
  287. }
  288. c.Header("Content-Type", "application/octet-stream")
  289. c.Header("Content-Disposition", "attachment; filename="+filename)
  290. c.Writer.Write(data)
  291. }
  292. // importDB imports a database file and restarts the Xray service.
  293. func (a *ServerController) importDB(c *gin.Context) {
  294. file, _, err := c.Request.FormFile("db")
  295. if err != nil {
  296. jsonMsg(c, I18nWeb(c, "pages.index.readDatabaseError"), err)
  297. return
  298. }
  299. defer file.Close()
  300. if err := a.serverService.ImportDB(file); err != nil {
  301. jsonMsg(c, I18nWeb(c, "pages.index.importDatabaseError"), err)
  302. return
  303. }
  304. jsonObj(c, I18nWeb(c, "pages.index.importDatabaseSuccess"), nil)
  305. }
  306. // descendants publishes read-only summaries of the nodes this panel manages so
  307. // a parent panel can surface them as transitive sub-nodes in a chained
  308. // topology. Called by the parent via the node's API token (#4983).
  309. func (a *ServerController) descendants(c *gin.Context) {
  310. data, err := (&service.NodeService{}).LocalDescendants()
  311. jsonObj(c, data, err)
  312. }
  313. // getWebCertFiles returns this panel's own web TLS certificate and key file
  314. // paths. The central panel calls it on a node (via the node's API token) so
  315. // "Set Cert from Panel" can fill a node-assigned inbound with paths that exist
  316. // on the node's filesystem instead of the central panel's — see issue #4854.
  317. func (a *ServerController) getWebCertFiles(c *gin.Context) {
  318. certFile, err := a.settingService.GetCertFile()
  319. if err != nil {
  320. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  321. return
  322. }
  323. keyFile, err := a.settingService.GetKeyFile()
  324. if err != nil {
  325. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  326. return
  327. }
  328. jsonObj(c, gin.H{"webCertFile": certFile, "webKeyFile": keyFile}, nil)
  329. }
  330. // getNewX25519Cert generates a new X25519 certificate.
  331. func (a *ServerController) getNewX25519Cert(c *gin.Context) {
  332. cert, err := a.serverService.GetNewX25519Cert()
  333. if err != nil {
  334. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.getNewX25519CertError"), err)
  335. return
  336. }
  337. jsonObj(c, cert, nil)
  338. }
  339. // getNewmldsa65 generates a new ML-DSA-65 key.
  340. func (a *ServerController) getNewmldsa65(c *gin.Context) {
  341. cert, err := a.serverService.GetNewmldsa65()
  342. if err != nil {
  343. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.getNewmldsa65Error"), err)
  344. return
  345. }
  346. jsonObj(c, cert, nil)
  347. }
  348. // getNewEchCert generates a new ECH certificate for the given SNI.
  349. func (a *ServerController) getNewEchCert(c *gin.Context) {
  350. cert, err := a.serverService.GetNewEchCert(c.PostForm("sni"))
  351. if err != nil {
  352. jsonMsg(c, "get ech certificate", err)
  353. return
  354. }
  355. jsonObj(c, cert, nil)
  356. }
  357. // getCertHash returns the hex SHA-256 of the given certificate (file path or
  358. // inline content) so the panel can fill the pinned-cert field.
  359. func (a *ServerController) getCertHash(c *gin.Context) {
  360. hashes, err := a.serverService.GetCertHash(c.PostForm("certFile"), c.PostForm("certContent"))
  361. if err != nil {
  362. jsonMsg(c, "get cert hash", err)
  363. return
  364. }
  365. jsonObj(c, hashes, nil)
  366. }
  367. // getRemoteCertHash runs `xray tls ping` against the given server and returns
  368. // its live certificate SHA-256 hash(es) for pinning.
  369. func (a *ServerController) getRemoteCertHash(c *gin.Context) {
  370. hashes, err := a.serverService.GetRemoteCertHash(c.PostForm("server"))
  371. if err != nil {
  372. jsonMsg(c, "get remote cert hash", err)
  373. return
  374. }
  375. jsonObj(c, hashes, nil)
  376. }
  377. // getNewVlessEnc generates a new VLESS encryption key.
  378. func (a *ServerController) getNewVlessEnc(c *gin.Context) {
  379. out, err := a.serverService.GetNewVlessEnc()
  380. if err != nil {
  381. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.getNewVlessEncError"), err)
  382. return
  383. }
  384. jsonObj(c, out, nil)
  385. }
  386. // getNewUUID generates a new UUID.
  387. func (a *ServerController) getNewUUID(c *gin.Context) {
  388. uuidResp, err := a.serverService.GetNewUUID()
  389. if err != nil {
  390. jsonMsg(c, "Failed to generate UUID", err)
  391. return
  392. }
  393. jsonObj(c, uuidResp, nil)
  394. }
  395. // getNewmlkem768 generates a new ML-KEM-768 key.
  396. func (a *ServerController) getNewmlkem768(c *gin.Context) {
  397. out, err := a.serverService.GetNewmlkem768()
  398. if err != nil {
  399. jsonMsg(c, "Failed to generate mlkem768 keys", err)
  400. return
  401. }
  402. jsonObj(c, out, nil)
  403. }
  404. func (a *ServerController) getClientIps(c *gin.Context) {
  405. ips, err := (&service.InboundService{}).GetAllInboundClientIps()
  406. jsonObj(c, ips, err)
  407. }
  408. func (a *ServerController) setClientIps(c *gin.Context) {
  409. var ips []model.InboundClientIps
  410. if err := c.ShouldBindJSON(&ips); err != nil {
  411. jsonMsg(c, "invalid data", err)
  412. return
  413. }
  414. err := (&service.InboundService{}).MergeInboundClientIps(ips)
  415. jsonMsg(c, "Client IPs merged", err)
  416. }