server.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. package controller
  2. import (
  3. "errors"
  4. "fmt"
  5. "net/http"
  6. "regexp"
  7. "slices"
  8. "strconv"
  9. "time"
  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/util/netsafe"
  13. "github.com/mhsanaei/3x-ui/v3/internal/web/entity"
  14. "github.com/mhsanaei/3x-ui/v3/internal/web/global"
  15. "github.com/mhsanaei/3x-ui/v3/internal/web/service"
  16. "github.com/mhsanaei/3x-ui/v3/internal/web/service/panel"
  17. "github.com/mhsanaei/3x-ui/v3/internal/web/websocket"
  18. "github.com/gin-gonic/gin"
  19. )
  20. var filenameRegex = regexp.MustCompile(`^[a-zA-Z0-9_\-.]+$`)
  21. // ServerController handles server management and status-related operations.
  22. type ServerController struct {
  23. BaseController
  24. serverService service.ServerService
  25. settingService service.SettingService
  26. panelService panel.PanelService
  27. xrayMetricsService service.XrayMetricsService
  28. }
  29. // NewServerController creates a new ServerController, initializes routes, and starts background tasks.
  30. func NewServerController(g *gin.RouterGroup) *ServerController {
  31. a := &ServerController{}
  32. service.RestoreSystemMetrics()
  33. a.initRouter(g)
  34. a.startTask()
  35. return a
  36. }
  37. // initRouter sets up the routes for server status, Xray management, and utility endpoints.
  38. func (a *ServerController) initRouter(g *gin.RouterGroup) {
  39. g.GET("/status", a.status)
  40. g.GET("/cpuHistory/:bucket", a.getCpuHistoryBucket)
  41. g.GET("/history/:metric/:bucket", a.getMetricHistoryBucket)
  42. g.GET("/xrayMetricsState", a.getXrayMetricsState)
  43. g.GET("/xrayMetricsHistory/:metric/:bucket", a.getXrayMetricsHistoryBucket)
  44. g.GET("/xrayObservatory", a.getXrayObservatory)
  45. g.GET("/xrayObservatoryHistory/:tag/:bucket", a.getXrayObservatoryHistoryBucket)
  46. g.GET("/getXrayVersion", a.getXrayVersion)
  47. g.GET("/getPanelUpdateInfo", a.getPanelUpdateInfo)
  48. g.GET("/getUpdateStatus", a.getUpdateStatus)
  49. g.GET("/getConfigJson", a.getConfigJson)
  50. g.GET("/getDb", a.getDb)
  51. g.GET("/getMigration", a.getMigration)
  52. g.GET("/getNewUUID", a.getNewUUID)
  53. g.GET("/getWebCertFiles", a.getWebCertFiles)
  54. g.GET("/descendants", a.descendants)
  55. g.GET("/getNewX25519Cert", a.getNewX25519Cert)
  56. g.GET("/getNewmldsa65", a.getNewmldsa65)
  57. g.GET("/getNewmlkem768", a.getNewmlkem768)
  58. g.GET("/getNewVlessEnc", a.getNewVlessEnc)
  59. g.GET("/clientIps", a.getClientIps)
  60. g.GET("/fail2banStatus", a.getFail2banStatus)
  61. g.POST("/stopXrayService", a.stopXrayService)
  62. g.POST("/restartXrayService", a.restartXrayService)
  63. g.POST("/installXray/:version", a.installXray)
  64. g.POST("/updatePanel", a.updatePanel)
  65. g.POST("/setUpdateChannel", a.setUpdateChannel)
  66. g.POST("/updateGeofile", a.updateGeofile)
  67. g.POST("/updateGeofile/:fileName", a.updateGeofile)
  68. g.POST("/logs/:count", a.getLogs)
  69. g.POST("/xraylogs/:count", a.getXrayLogs)
  70. g.POST("/amneziawglogs/:count", a.getAmneziaWGLogs)
  71. g.POST("/importDB", a.importDB)
  72. g.POST("/getNewEchCert", a.getNewEchCert)
  73. g.POST("/getCertHash", a.getCertHash)
  74. g.POST("/getRemoteCertHash", a.getRemoteCertHash)
  75. g.POST("/scanRealityTarget", a.scanRealityTarget)
  76. g.POST("/scanRealityTargets", a.scanRealityTargets)
  77. g.POST("/clientIps", a.setClientIps)
  78. }
  79. // startTask registers the @2s ticker that refreshes server status, samples
  80. // xray metrics, and pushes the new snapshot to all websocket subscribers.
  81. // State + sampling live in ServerService; the controller only orchestrates
  82. // the cross-service side effects (xrayMetrics sample + websocket broadcast).
  83. func (a *ServerController) startTask() {
  84. c := global.GetWebServer().GetCron()
  85. _, _ = c.AddFunc("@every 2s", func() {
  86. status := a.serverService.RefreshStatus()
  87. if status == nil {
  88. return
  89. }
  90. a.xrayMetricsService.Sample(time.Now())
  91. websocket.BroadcastStatus(status)
  92. })
  93. _, _ = c.AddFunc("@every 1m", func() {
  94. if err := service.PersistSystemMetrics(); err != nil {
  95. logger.Warning("persist system metrics failed:", err)
  96. }
  97. })
  98. }
  99. // status returns the current server status information.
  100. func (a *ServerController) status(c *gin.Context) { jsonObj(c, a.serverService.CurrentStatus(), nil) }
  101. func (a *ServerController) getFail2banStatus(c *gin.Context) {
  102. jsonObj(c, a.serverService.GetFail2banStatus(), nil)
  103. }
  104. func parseHistoryBucket(c *gin.Context) (int, bool) {
  105. bucket, err := strconv.Atoi(c.Param("bucket"))
  106. if err != nil || bucket <= 0 || !service.IsAllowedHistoryBucket(bucket) {
  107. jsonMsg(c, "invalid bucket", fmt.Errorf("unsupported bucket"))
  108. return 0, false
  109. }
  110. return bucket, true
  111. }
  112. // getCpuHistoryBucket retrieves aggregated CPU usage history based on the specified time bucket.
  113. // Kept for back-compat; new callers should use /history/cpu/:bucket which
  114. // returns {"t","v"} (uniform across all metrics) instead of {"t","cpu"}.
  115. func (a *ServerController) getCpuHistoryBucket(c *gin.Context) {
  116. bucket, ok := parseHistoryBucket(c)
  117. if !ok {
  118. return
  119. }
  120. jsonObj(c, a.serverService.AggregateCpuHistory(bucket, 60), nil)
  121. }
  122. // getMetricHistoryBucket returns up to 60 buckets of history for a single
  123. // system metric (cpu, mem, netUp, netDown, online, load1/5/15). The
  124. // SystemHistoryModal calls one endpoint per active tab.
  125. func (a *ServerController) getMetricHistoryBucket(c *gin.Context) {
  126. metric := c.Param("metric")
  127. if !slices.Contains(service.SystemMetricKeys, metric) {
  128. jsonMsg(c, "invalid metric", fmt.Errorf("unknown metric"))
  129. return
  130. }
  131. bucket, ok := parseHistoryBucket(c)
  132. if !ok {
  133. return
  134. }
  135. jsonObj(c, a.serverService.AggregateSystemMetric(metric, bucket, 60), nil)
  136. }
  137. func (a *ServerController) getXrayMetricsState(c *gin.Context) {
  138. jsonObj(c, a.xrayMetricsService.State(), nil)
  139. }
  140. func (a *ServerController) getXrayMetricsHistoryBucket(c *gin.Context) {
  141. metric := c.Param("metric")
  142. if !slices.Contains(service.XrayMetricKeys, metric) {
  143. jsonMsg(c, "invalid metric", fmt.Errorf("unknown metric"))
  144. return
  145. }
  146. bucket, ok := parseHistoryBucket(c)
  147. if !ok {
  148. return
  149. }
  150. jsonObj(c, a.xrayMetricsService.AggregateMetric(metric, bucket, 60), nil)
  151. }
  152. func (a *ServerController) getXrayObservatory(c *gin.Context) {
  153. jsonObj(c, a.xrayMetricsService.ObservatorySnapshot(), nil)
  154. }
  155. func (a *ServerController) getXrayObservatoryHistoryBucket(c *gin.Context) {
  156. tag := c.Param("tag")
  157. if !a.xrayMetricsService.HasObservatoryTag(tag) {
  158. jsonMsg(c, "invalid tag", fmt.Errorf("unknown observatory tag"))
  159. return
  160. }
  161. bucket, ok := parseHistoryBucket(c)
  162. if !ok {
  163. return
  164. }
  165. jsonObj(c, a.xrayMetricsService.AggregateObservatory(tag, bucket, 60), nil)
  166. }
  167. func (a *ServerController) getXrayVersion(c *gin.Context) {
  168. versions, err := a.serverService.GetXrayVersionsCached()
  169. if err != nil {
  170. jsonMsg(c, I18nWeb(c, "getVersion"), err)
  171. return
  172. }
  173. jsonObj(c, versions, nil)
  174. }
  175. // getPanelUpdateInfo retrieves the current and latest panel version.
  176. func (a *ServerController) getPanelUpdateInfo(c *gin.Context) {
  177. info, err := a.panelService.GetUpdateInfo()
  178. if err != nil {
  179. logger.Debug("panel update check failed:", err)
  180. c.JSON(http.StatusOK, entity.Msg{Success: false})
  181. return
  182. }
  183. jsonObj(c, info, nil)
  184. }
  185. // installXray installs or updates Xray to the specified version.
  186. func (a *ServerController) installXray(c *gin.Context) {
  187. version := c.Param("version")
  188. err := a.serverService.UpdateXray(version)
  189. jsonMsg(c, I18nWeb(c, "pages.index.xraySwitchVersionPopover"), err)
  190. }
  191. // updatePanel starts a panel self-update. With no "dev" form value it follows
  192. // this panel's own channel setting; an explicit "dev" (sent by the master node
  193. // updater) overrides it for this run. The response's runId identifies this
  194. // update for a later getUpdateStatus poll.
  195. func (a *ServerController) updatePanel(c *gin.Context) {
  196. devParam := c.PostForm("dev")
  197. var runID int64
  198. var err error
  199. if devParam == "" {
  200. runID, err = a.panelService.StartUpdate()
  201. } else {
  202. dev, perr := strconv.ParseBool(devParam)
  203. if perr != nil {
  204. jsonMsg(c, "invalid data", perr)
  205. return
  206. }
  207. runID, err = a.panelService.StartUpdateChannel(dev)
  208. }
  209. var obj any
  210. if err == nil {
  211. obj = gin.H{"runId": strconv.FormatInt(runID, 10)}
  212. }
  213. jsonMsgObj(c, I18nWeb(c, "pages.index.panelUpdateStartedPopover"), obj, err)
  214. }
  215. // getUpdateStatus reports the outcome of the most recently launched panel
  216. // self-update (see updatePanel). Compare the returned runId against the one
  217. // updatePanel returned to tell this run's result apart from a stale one.
  218. func (a *ServerController) getUpdateStatus(c *gin.Context) {
  219. jsonObj(c, a.panelService.GetUpdateStatus(), nil)
  220. }
  221. // setUpdateChannel toggles whether self-update tracks the rolling dev release.
  222. func (a *ServerController) setUpdateChannel(c *gin.Context) {
  223. dev, err := strconv.ParseBool(c.PostForm("dev"))
  224. if err != nil {
  225. jsonMsg(c, "invalid data", err)
  226. return
  227. }
  228. err = a.settingService.SetDevChannelEnable(dev)
  229. jsonMsg(c, I18nWeb(c, "pages.index.updateChannelChanged"), err)
  230. }
  231. // updateGeofile updates the specified geo file for Xray.
  232. func (a *ServerController) updateGeofile(c *gin.Context) {
  233. fileName := c.Param("fileName")
  234. if fileName != "" && !a.serverService.IsValidGeofileName(fileName) {
  235. jsonMsg(c, I18nWeb(c, "pages.index.geofileUpdatePopover"),
  236. fmt.Errorf("invalid filename: contains unsafe characters or path traversal patterns"))
  237. return
  238. }
  239. err := a.serverService.UpdateGeofile(fileName)
  240. jsonMsg(c, I18nWeb(c, "pages.index.geofileUpdatePopover"), err)
  241. }
  242. // stopXrayService stops the Xray service.
  243. func (a *ServerController) stopXrayService(c *gin.Context) {
  244. err := a.serverService.StopXrayService()
  245. if err != nil {
  246. jsonMsg(c, I18nWeb(c, "pages.xray.stopError"), err)
  247. websocket.BroadcastXrayState("error", err.Error())
  248. return
  249. }
  250. jsonMsg(c, I18nWeb(c, "pages.xray.stopSuccess"), err)
  251. websocket.BroadcastXrayState("stop", "")
  252. websocket.BroadcastNotification(
  253. I18nWeb(c, "pages.xray.stopSuccess"),
  254. "Xray service has been stopped",
  255. "warning",
  256. )
  257. }
  258. // restartXrayService restarts the Xray service.
  259. func (a *ServerController) restartXrayService(c *gin.Context) {
  260. err := a.serverService.RestartXrayService()
  261. if err != nil {
  262. jsonMsg(c, I18nWeb(c, "pages.xray.restartError"), err)
  263. websocket.BroadcastXrayState("error", err.Error())
  264. return
  265. }
  266. jsonMsg(c, I18nWeb(c, "pages.xray.restartSuccess"), err)
  267. websocket.BroadcastXrayState("running", "")
  268. websocket.BroadcastNotification(
  269. I18nWeb(c, "pages.xray.restartSuccess"),
  270. "Xray service has been restarted successfully",
  271. "success",
  272. )
  273. }
  274. // getLogs retrieves the application logs based on count, level, and syslog filters.
  275. func (a *ServerController) getLogs(c *gin.Context) {
  276. logs := a.serverService.GetLogs(c.Param("count"), c.PostForm("level"), c.PostForm("syslog"))
  277. jsonObj(c, logs, nil)
  278. }
  279. // getXrayLogs retrieves Xray logs with filtering options for direct, blocked, and proxy traffic.
  280. func (a *ServerController) getXrayLogs(c *gin.Context) {
  281. freedoms, blackholes := a.serverService.GetDefaultLogOutboundTags()
  282. logs := a.serverService.GetXrayLogs(
  283. c.Param("count"),
  284. c.PostForm("filter"),
  285. c.PostForm("showDirect"),
  286. c.PostForm("showBlocked"),
  287. c.PostForm("showProxy"),
  288. freedoms,
  289. blackholes,
  290. )
  291. jsonObj(c, logs, nil)
  292. }
  293. // getAmneziaWGLogs retrieves the live AmneziaWG peer activity and the panel's
  294. // own AmneziaWG event lines, optionally narrowed by a free-text filter.
  295. func (a *ServerController) getAmneziaWGLogs(c *gin.Context) {
  296. logs := a.serverService.GetAmneziaWGLogs(c.Param("count"), c.PostForm("filter"))
  297. jsonObj(c, logs, nil)
  298. }
  299. // getConfigJson retrieves the Xray configuration as JSON.
  300. func (a *ServerController) getConfigJson(c *gin.Context) {
  301. configJson, err := a.serverService.GetConfigJson()
  302. if err != nil {
  303. jsonMsg(c, I18nWeb(c, "pages.index.getConfigError"), err)
  304. return
  305. }
  306. jsonObj(c, configJson, nil)
  307. }
  308. // getDb downloads the database file.
  309. func (a *ServerController) getDb(c *gin.Context) {
  310. db, err := a.serverService.GetDb()
  311. if err != nil {
  312. jsonMsg(c, I18nWeb(c, "pages.index.getDatabaseError"), err)
  313. return
  314. }
  315. filename := a.serverService.BackupFilename(c.Request.Host)
  316. if !filenameRegex.MatchString(filename) {
  317. _ = c.AbortWithError(http.StatusBadRequest, fmt.Errorf("invalid filename"))
  318. return
  319. }
  320. c.Header("Content-Type", "application/octet-stream")
  321. c.Header("Content-Disposition", "attachment; filename="+filename)
  322. _, _ = c.Writer.Write(db)
  323. }
  324. // getMigration downloads a cross-engine migration file: a .dump on SQLite or a
  325. // .db SQLite database on PostgreSQL, so the data can seed the other backend.
  326. func (a *ServerController) getMigration(c *gin.Context) {
  327. data, filename, err := a.serverService.GetMigration()
  328. if err != nil {
  329. jsonMsg(c, I18nWeb(c, "pages.index.getDatabaseError"), err)
  330. return
  331. }
  332. if !filenameRegex.MatchString(filename) {
  333. _ = c.AbortWithError(http.StatusBadRequest, fmt.Errorf("invalid filename"))
  334. return
  335. }
  336. c.Header("Content-Type", "application/octet-stream")
  337. c.Header("Content-Disposition", "attachment; filename="+filename)
  338. _, _ = c.Writer.Write(data)
  339. }
  340. // importDB imports a database file and restarts the Xray service.
  341. func (a *ServerController) importDB(c *gin.Context) {
  342. file, _, err := c.Request.FormFile("db")
  343. if err != nil {
  344. jsonMsg(c, I18nWeb(c, "pages.index.readDatabaseError"), err)
  345. return
  346. }
  347. defer file.Close()
  348. // Absent field keeps this machine's own listen addresses, certificates and
  349. // node identity: the safe default for the common case of moving a config to
  350. // a new host. Send keepHostSettings=false to clone a machine wholesale.
  351. keepHostSettings := c.Request.FormValue("keepHostSettings") != "false"
  352. if err := a.serverService.ImportDB(file, keepHostSettings); err != nil {
  353. jsonMsg(c, I18nWeb(c, "pages.index.importDatabaseError"), err)
  354. return
  355. }
  356. // Startup-registered routes (subPath) must match the restored DB, and the
  357. // browser's restartPanel follow-up can 401 once the imported users land (#6446).
  358. _ = a.panelService.RestartPanel(3 * time.Second)
  359. jsonObj(c, I18nWeb(c, "pages.index.importDatabaseSuccess"), nil)
  360. }
  361. // descendants publishes read-only summaries of the nodes this panel manages so
  362. // a parent panel can surface them as transitive sub-nodes in a chained
  363. // topology. Called by the parent via the node's API token (#4983).
  364. func (a *ServerController) descendants(c *gin.Context) {
  365. data, err := (&service.NodeService{}).LocalDescendants()
  366. jsonObj(c, data, err)
  367. }
  368. // getWebCertFiles returns this panel's own web TLS certificate and key file
  369. // paths. The central panel calls it on a node (via the node's API token) so
  370. // "Set Cert from Panel" can fill a node-assigned inbound with paths that exist
  371. // on the node's filesystem instead of the central panel's — see issue #4854.
  372. func (a *ServerController) getWebCertFiles(c *gin.Context) {
  373. certFile, err := a.settingService.GetCertFile()
  374. if err != nil {
  375. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  376. return
  377. }
  378. keyFile, err := a.settingService.GetKeyFile()
  379. if err != nil {
  380. jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
  381. return
  382. }
  383. jsonObj(c, gin.H{"webCertFile": certFile, "webKeyFile": keyFile}, nil)
  384. }
  385. // getNewX25519Cert generates a new X25519 certificate.
  386. func (a *ServerController) getNewX25519Cert(c *gin.Context) {
  387. cert, err := a.serverService.GetNewX25519Cert()
  388. if err != nil {
  389. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.getNewX25519CertError"), err)
  390. return
  391. }
  392. jsonObj(c, cert, nil)
  393. }
  394. // getNewmldsa65 generates a new ML-DSA-65 key.
  395. func (a *ServerController) getNewmldsa65(c *gin.Context) {
  396. cert, err := a.serverService.GetNewmldsa65()
  397. if err != nil {
  398. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.getNewmldsa65Error"), err)
  399. return
  400. }
  401. jsonObj(c, cert, nil)
  402. }
  403. // getNewEchCert generates a new ECH certificate for the given SNI.
  404. func (a *ServerController) getNewEchCert(c *gin.Context) {
  405. cert, err := a.serverService.GetNewEchCert(c.PostForm("sni"))
  406. if err != nil {
  407. jsonMsg(c, "get ech certificate", err)
  408. return
  409. }
  410. jsonObj(c, cert, nil)
  411. }
  412. // getCertHash returns the hex SHA-256 of the given certificate (file path or
  413. // inline content) so the panel can fill the pinned-cert field.
  414. func (a *ServerController) getCertHash(c *gin.Context) {
  415. hashes, err := a.serverService.GetCertHash(c.PostForm("certFile"), c.PostForm("certContent"))
  416. if err != nil {
  417. jsonMsg(c, "get cert hash", err)
  418. return
  419. }
  420. jsonObj(c, hashes, nil)
  421. }
  422. // getRemoteCertHash runs `xray tls ping` against the given server and returns
  423. // its live certificate SHA-256 hash(es) for pinning.
  424. func (a *ServerController) getRemoteCertHash(c *gin.Context) {
  425. allowPrivate := c.PostForm("allowPrivate") == "true"
  426. hashes, err := a.serverService.GetRemoteCertHash(c.PostForm("server"), allowPrivate)
  427. if errors.Is(err, netsafe.ErrPrivateAddressBlocked) {
  428. jsonMsgObj(c, "get remote cert hash", gin.H{"privateTarget": true}, err)
  429. return
  430. }
  431. if err != nil {
  432. jsonMsg(c, "get remote cert hash", err)
  433. return
  434. }
  435. jsonObj(c, hashes, nil)
  436. }
  437. // scanRealityTarget probes the candidate REALITY target with the given sni and
  438. // returns a feasibility verdict; allowPrivate is the panel's confirmed opt-in.
  439. func (a *ServerController) scanRealityTarget(c *gin.Context) {
  440. xver, _ := strconv.Atoi(c.PostForm("xver"))
  441. allowPrivate := c.PostForm("allowPrivate") == "true"
  442. res, err := a.serverService.ScanRealityTarget(c.PostForm("target"), c.PostForm("sni"), xver, allowPrivate)
  443. if err != nil {
  444. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.scanRealityTargetError"), err)
  445. return
  446. }
  447. jsonObj(c, res, nil)
  448. }
  449. // scanRealityTargets probes the supplied comma-separated targets, or the
  450. // realityScanCandidates setting when empty, ranked by feasibility then latency.
  451. func (a *ServerController) scanRealityTargets(c *gin.Context) {
  452. res, err := a.serverService.ScanRealityTargets(c.PostForm("targets"))
  453. if err != nil {
  454. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.scanRealityTargetError"), err)
  455. return
  456. }
  457. jsonObj(c, res, nil)
  458. }
  459. // getNewVlessEnc generates a new VLESS encryption key.
  460. func (a *ServerController) getNewVlessEnc(c *gin.Context) {
  461. out, err := a.serverService.GetNewVlessEnc()
  462. if err != nil {
  463. jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.getNewVlessEncError"), err)
  464. return
  465. }
  466. jsonObj(c, out, nil)
  467. }
  468. // getNewUUID generates a new UUID.
  469. func (a *ServerController) getNewUUID(c *gin.Context) {
  470. uuidResp, err := a.serverService.GetNewUUID()
  471. if err != nil {
  472. jsonMsg(c, "Failed to generate UUID", err)
  473. return
  474. }
  475. jsonObj(c, uuidResp, nil)
  476. }
  477. // getNewmlkem768 generates a new ML-KEM-768 key.
  478. func (a *ServerController) getNewmlkem768(c *gin.Context) {
  479. out, err := a.serverService.GetNewmlkem768()
  480. if err != nil {
  481. jsonMsg(c, "Failed to generate mlkem768 keys", err)
  482. return
  483. }
  484. jsonObj(c, out, nil)
  485. }
  486. func (a *ServerController) getClientIps(c *gin.Context) {
  487. ips, err := (&service.InboundService{}).GetAllInboundClientIps()
  488. jsonObj(c, ips, err)
  489. }
  490. func (a *ServerController) setClientIps(c *gin.Context) {
  491. var ips []model.InboundClientIps
  492. if err := c.ShouldBindJSON(&ips); err != nil {
  493. jsonMsg(c, "invalid data", err)
  494. return
  495. }
  496. err := (&service.InboundService{}).MergeInboundClientIps(ips)
  497. jsonMsg(c, "Client IPs merged", err)
  498. }