server.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745
  1. package service
  2. import (
  3. "archive/zip"
  4. "bytes"
  5. "encoding/json"
  6. "fmt"
  7. "io"
  8. "io/fs"
  9. "mime/multipart"
  10. "net/http"
  11. "os"
  12. "os/exec"
  13. "runtime"
  14. "strconv"
  15. "strings"
  16. "time"
  17. "x-ui/config"
  18. "x-ui/database"
  19. "x-ui/logger"
  20. "x-ui/util/common"
  21. "x-ui/util/sys"
  22. "x-ui/xray"
  23. "github.com/shirou/gopsutil/v4/cpu"
  24. "github.com/shirou/gopsutil/v4/disk"
  25. "github.com/shirou/gopsutil/v4/host"
  26. "github.com/shirou/gopsutil/v4/load"
  27. "github.com/shirou/gopsutil/v4/mem"
  28. "github.com/shirou/gopsutil/v4/net"
  29. )
  30. type ProcessState string
  31. const (
  32. Running ProcessState = "running"
  33. Stop ProcessState = "stop"
  34. Error ProcessState = "error"
  35. )
  36. type Status struct {
  37. T time.Time `json:"-"`
  38. Cpu float64 `json:"cpu"`
  39. CpuCores int `json:"cpuCores"`
  40. LogicalPro int `json:"logicalPro"`
  41. CpuSpeedMhz float64 `json:"cpuSpeedMhz"`
  42. Mem struct {
  43. Current uint64 `json:"current"`
  44. Total uint64 `json:"total"`
  45. } `json:"mem"`
  46. Swap struct {
  47. Current uint64 `json:"current"`
  48. Total uint64 `json:"total"`
  49. } `json:"swap"`
  50. Disk struct {
  51. Current uint64 `json:"current"`
  52. Total uint64 `json:"total"`
  53. } `json:"disk"`
  54. Xray struct {
  55. State ProcessState `json:"state"`
  56. ErrorMsg string `json:"errorMsg"`
  57. Version string `json:"version"`
  58. } `json:"xray"`
  59. Uptime uint64 `json:"uptime"`
  60. Loads []float64 `json:"loads"`
  61. TcpCount int `json:"tcpCount"`
  62. UdpCount int `json:"udpCount"`
  63. NetIO struct {
  64. Up uint64 `json:"up"`
  65. Down uint64 `json:"down"`
  66. } `json:"netIO"`
  67. NetTraffic struct {
  68. Sent uint64 `json:"sent"`
  69. Recv uint64 `json:"recv"`
  70. } `json:"netTraffic"`
  71. PublicIP struct {
  72. IPv4 string `json:"ipv4"`
  73. IPv6 string `json:"ipv6"`
  74. } `json:"publicIP"`
  75. AppStats struct {
  76. Threads uint32 `json:"threads"`
  77. Mem uint64 `json:"mem"`
  78. Uptime uint64 `json:"uptime"`
  79. } `json:"appStats"`
  80. }
  81. type Release struct {
  82. TagName string `json:"tag_name"`
  83. }
  84. type ServerService struct {
  85. xrayService XrayService
  86. inboundService InboundService
  87. cachedIPv4 string
  88. cachedIPv6 string
  89. noIPv6 bool
  90. }
  91. func getPublicIP(url string) string {
  92. client := &http.Client{
  93. Timeout: 3 * time.Second,
  94. }
  95. resp, err := client.Get(url)
  96. if err != nil {
  97. return "N/A"
  98. }
  99. defer resp.Body.Close()
  100. // Don't retry if access is blocked or region-restricted
  101. if resp.StatusCode == http.StatusForbidden || resp.StatusCode == http.StatusUnavailableForLegalReasons {
  102. return "N/A"
  103. }
  104. if resp.StatusCode != http.StatusOK {
  105. return "N/A"
  106. }
  107. ip, err := io.ReadAll(resp.Body)
  108. if err != nil {
  109. return "N/A"
  110. }
  111. ipString := strings.TrimSpace(string(ip))
  112. if ipString == "" {
  113. return "N/A"
  114. }
  115. return ipString
  116. }
  117. func (s *ServerService) GetStatus(lastStatus *Status) *Status {
  118. now := time.Now()
  119. status := &Status{
  120. T: now,
  121. }
  122. // CPU stats
  123. percents, err := cpu.Percent(0, false)
  124. if err != nil {
  125. logger.Warning("get cpu percent failed:", err)
  126. } else {
  127. status.Cpu = percents[0]
  128. }
  129. status.CpuCores, err = cpu.Counts(false)
  130. if err != nil {
  131. logger.Warning("get cpu cores count failed:", err)
  132. }
  133. status.LogicalPro = runtime.NumCPU()
  134. cpuInfos, err := cpu.Info()
  135. if err != nil {
  136. logger.Warning("get cpu info failed:", err)
  137. } else if len(cpuInfos) > 0 {
  138. status.CpuSpeedMhz = cpuInfos[0].Mhz
  139. } else {
  140. logger.Warning("could not find cpu info")
  141. }
  142. // Uptime
  143. upTime, err := host.Uptime()
  144. if err != nil {
  145. logger.Warning("get uptime failed:", err)
  146. } else {
  147. status.Uptime = upTime
  148. }
  149. // Memory stats
  150. memInfo, err := mem.VirtualMemory()
  151. if err != nil {
  152. logger.Warning("get virtual memory failed:", err)
  153. } else {
  154. status.Mem.Current = memInfo.Used
  155. status.Mem.Total = memInfo.Total
  156. }
  157. swapInfo, err := mem.SwapMemory()
  158. if err != nil {
  159. logger.Warning("get swap memory failed:", err)
  160. } else {
  161. status.Swap.Current = swapInfo.Used
  162. status.Swap.Total = swapInfo.Total
  163. }
  164. // Disk stats
  165. diskInfo, err := disk.Usage("/")
  166. if err != nil {
  167. logger.Warning("get disk usage failed:", err)
  168. } else {
  169. status.Disk.Current = diskInfo.Used
  170. status.Disk.Total = diskInfo.Total
  171. }
  172. // Load averages
  173. avgState, err := load.Avg()
  174. if err != nil {
  175. logger.Warning("get load avg failed:", err)
  176. } else {
  177. status.Loads = []float64{avgState.Load1, avgState.Load5, avgState.Load15}
  178. }
  179. // Network stats
  180. ioStats, err := net.IOCounters(false)
  181. if err != nil {
  182. logger.Warning("get io counters failed:", err)
  183. } else if len(ioStats) > 0 {
  184. ioStat := ioStats[0]
  185. status.NetTraffic.Sent = ioStat.BytesSent
  186. status.NetTraffic.Recv = ioStat.BytesRecv
  187. if lastStatus != nil {
  188. duration := now.Sub(lastStatus.T)
  189. seconds := float64(duration) / float64(time.Second)
  190. up := uint64(float64(status.NetTraffic.Sent-lastStatus.NetTraffic.Sent) / seconds)
  191. down := uint64(float64(status.NetTraffic.Recv-lastStatus.NetTraffic.Recv) / seconds)
  192. status.NetIO.Up = up
  193. status.NetIO.Down = down
  194. }
  195. } else {
  196. logger.Warning("can not find io counters")
  197. }
  198. // TCP/UDP connections
  199. status.TcpCount, err = sys.GetTCPCount()
  200. if err != nil {
  201. logger.Warning("get tcp connections failed:", err)
  202. }
  203. status.UdpCount, err = sys.GetUDPCount()
  204. if err != nil {
  205. logger.Warning("get udp connections failed:", err)
  206. }
  207. // IP fetching with caching
  208. showIp4ServiceLists := []string{"https://api.ipify.org", "https://4.ident.me"}
  209. showIp6ServiceLists := []string{"https://api6.ipify.org", "https://6.ident.me"}
  210. if s.cachedIPv4 == "" {
  211. for _, ip4Service := range showIp4ServiceLists {
  212. s.cachedIPv4 = getPublicIP(ip4Service)
  213. if s.cachedIPv4 != "N/A" {
  214. break
  215. }
  216. }
  217. }
  218. if s.cachedIPv6 == "" && !s.noIPv6 {
  219. for _, ip6Service := range showIp6ServiceLists {
  220. s.cachedIPv6 = getPublicIP(ip6Service)
  221. if s.cachedIPv6 != "N/A" {
  222. break
  223. }
  224. }
  225. }
  226. if s.cachedIPv6 == "N/A" {
  227. s.noIPv6 = true
  228. }
  229. status.PublicIP.IPv4 = s.cachedIPv4
  230. status.PublicIP.IPv6 = s.cachedIPv6
  231. // Xray status
  232. if s.xrayService.IsXrayRunning() {
  233. status.Xray.State = Running
  234. status.Xray.ErrorMsg = ""
  235. } else {
  236. err := s.xrayService.GetXrayErr()
  237. if err != nil {
  238. status.Xray.State = Error
  239. } else {
  240. status.Xray.State = Stop
  241. }
  242. status.Xray.ErrorMsg = s.xrayService.GetXrayResult()
  243. }
  244. status.Xray.Version = s.xrayService.GetXrayVersion()
  245. // Application stats
  246. var rtm runtime.MemStats
  247. runtime.ReadMemStats(&rtm)
  248. status.AppStats.Mem = rtm.Sys
  249. status.AppStats.Threads = uint32(runtime.NumGoroutine())
  250. if p != nil && p.IsRunning() {
  251. status.AppStats.Uptime = p.GetUptime()
  252. } else {
  253. status.AppStats.Uptime = 0
  254. }
  255. return status
  256. }
  257. func (s *ServerService) GetXrayVersions() ([]string, error) {
  258. const (
  259. XrayURL = "https://api.github.com/repos/XTLS/Xray-core/releases"
  260. bufferSize = 8192
  261. )
  262. resp, err := http.Get(XrayURL)
  263. if err != nil {
  264. return nil, err
  265. }
  266. defer resp.Body.Close()
  267. buffer := bytes.NewBuffer(make([]byte, bufferSize))
  268. buffer.Reset()
  269. if _, err := buffer.ReadFrom(resp.Body); err != nil {
  270. return nil, err
  271. }
  272. var releases []Release
  273. if err := json.Unmarshal(buffer.Bytes(), &releases); err != nil {
  274. return nil, err
  275. }
  276. var versions []string
  277. for _, release := range releases {
  278. tagVersion := strings.TrimPrefix(release.TagName, "v")
  279. tagParts := strings.Split(tagVersion, ".")
  280. if len(tagParts) != 3 {
  281. continue
  282. }
  283. major, err1 := strconv.Atoi(tagParts[0])
  284. minor, err2 := strconv.Atoi(tagParts[1])
  285. patch, err3 := strconv.Atoi(tagParts[2])
  286. if err1 != nil || err2 != nil || err3 != nil {
  287. continue
  288. }
  289. if major > 25 || (major == 25 && minor > 6) || (major == 25 && minor == 6 && patch >= 8) {
  290. versions = append(versions, release.TagName)
  291. }
  292. }
  293. return versions, nil
  294. }
  295. func (s *ServerService) StopXrayService() error {
  296. err := s.xrayService.StopXray()
  297. if err != nil {
  298. logger.Error("stop xray failed:", err)
  299. return err
  300. }
  301. return nil
  302. }
  303. func (s *ServerService) RestartXrayService() error {
  304. s.xrayService.StopXray()
  305. err := s.xrayService.RestartXray(true)
  306. if err != nil {
  307. logger.Error("start xray failed:", err)
  308. return err
  309. }
  310. return nil
  311. }
  312. func (s *ServerService) downloadXRay(version string) (string, error) {
  313. osName := runtime.GOOS
  314. arch := runtime.GOARCH
  315. switch osName {
  316. case "darwin":
  317. osName = "macos"
  318. }
  319. switch arch {
  320. case "amd64":
  321. arch = "64"
  322. case "arm64":
  323. arch = "arm64-v8a"
  324. case "armv7":
  325. arch = "arm32-v7a"
  326. case "armv6":
  327. arch = "arm32-v6"
  328. case "armv5":
  329. arch = "arm32-v5"
  330. case "386":
  331. arch = "32"
  332. case "s390x":
  333. arch = "s390x"
  334. }
  335. fileName := fmt.Sprintf("Xray-%s-%s.zip", osName, arch)
  336. url := fmt.Sprintf("https://github.com/XTLS/Xray-core/releases/download/%s/%s", version, fileName)
  337. resp, err := http.Get(url)
  338. if err != nil {
  339. return "", err
  340. }
  341. defer resp.Body.Close()
  342. os.Remove(fileName)
  343. file, err := os.Create(fileName)
  344. if err != nil {
  345. return "", err
  346. }
  347. defer file.Close()
  348. _, err = io.Copy(file, resp.Body)
  349. if err != nil {
  350. return "", err
  351. }
  352. return fileName, nil
  353. }
  354. func (s *ServerService) UpdateXray(version string) error {
  355. zipFileName, err := s.downloadXRay(version)
  356. if err != nil {
  357. return err
  358. }
  359. zipFile, err := os.Open(zipFileName)
  360. if err != nil {
  361. return err
  362. }
  363. defer func() {
  364. zipFile.Close()
  365. os.Remove(zipFileName)
  366. }()
  367. stat, err := zipFile.Stat()
  368. if err != nil {
  369. return err
  370. }
  371. reader, err := zip.NewReader(zipFile, stat.Size())
  372. if err != nil {
  373. return err
  374. }
  375. s.xrayService.StopXray()
  376. defer func() {
  377. err := s.xrayService.RestartXray(true)
  378. if err != nil {
  379. logger.Error("start xray failed:", err)
  380. }
  381. }()
  382. copyZipFile := func(zipName string, fileName string) error {
  383. zipFile, err := reader.Open(zipName)
  384. if err != nil {
  385. return err
  386. }
  387. os.Remove(fileName)
  388. file, err := os.OpenFile(fileName, os.O_CREATE|os.O_RDWR|os.O_TRUNC, fs.ModePerm)
  389. if err != nil {
  390. return err
  391. }
  392. defer file.Close()
  393. _, err = io.Copy(file, zipFile)
  394. return err
  395. }
  396. err = copyZipFile("xray", xray.GetBinaryPath())
  397. if err != nil {
  398. return err
  399. }
  400. return nil
  401. }
  402. func (s *ServerService) GetLogs(count string, level string, syslog string) []string {
  403. c, _ := strconv.Atoi(count)
  404. var lines []string
  405. if syslog == "true" {
  406. cmdArgs := []string{"journalctl", "-u", "x-ui", "--no-pager", "-n", count, "-p", level}
  407. // Run the command
  408. cmd := exec.Command(cmdArgs[0], cmdArgs[1:]...)
  409. var out bytes.Buffer
  410. cmd.Stdout = &out
  411. err := cmd.Run()
  412. if err != nil {
  413. return []string{"Failed to run journalctl command!"}
  414. }
  415. lines = strings.Split(out.String(), "\n")
  416. } else {
  417. lines = logger.GetLogs(c, level)
  418. }
  419. return lines
  420. }
  421. func (s *ServerService) GetConfigJson() (any, error) {
  422. config, err := s.xrayService.GetXrayConfig()
  423. if err != nil {
  424. return nil, err
  425. }
  426. contents, err := json.MarshalIndent(config, "", " ")
  427. if err != nil {
  428. return nil, err
  429. }
  430. var jsonData any
  431. err = json.Unmarshal(contents, &jsonData)
  432. if err != nil {
  433. return nil, err
  434. }
  435. return jsonData, nil
  436. }
  437. func (s *ServerService) GetDb() ([]byte, error) {
  438. // Update by manually trigger a checkpoint operation
  439. err := database.Checkpoint()
  440. if err != nil {
  441. return nil, err
  442. }
  443. // Open the file for reading
  444. file, err := os.Open(config.GetDBPath())
  445. if err != nil {
  446. return nil, err
  447. }
  448. defer file.Close()
  449. // Read the file contents
  450. fileContents, err := io.ReadAll(file)
  451. if err != nil {
  452. return nil, err
  453. }
  454. return fileContents, nil
  455. }
  456. func (s *ServerService) ImportDB(file multipart.File) error {
  457. // Check if the file is a SQLite database
  458. isValidDb, err := database.IsSQLiteDB(file)
  459. if err != nil {
  460. return common.NewErrorf("Error checking db file format: %v", err)
  461. }
  462. if !isValidDb {
  463. return common.NewError("Invalid db file format")
  464. }
  465. // Reset the file reader to the beginning
  466. _, err = file.Seek(0, 0)
  467. if err != nil {
  468. return common.NewErrorf("Error resetting file reader: %v", err)
  469. }
  470. // Save the file as a temporary file
  471. tempPath := fmt.Sprintf("%s.temp", config.GetDBPath())
  472. // Remove the existing temporary file (if any)
  473. if _, err := os.Stat(tempPath); err == nil {
  474. if errRemove := os.Remove(tempPath); errRemove != nil {
  475. return common.NewErrorf("Error removing existing temporary db file: %v", errRemove)
  476. }
  477. }
  478. // Create the temporary file
  479. tempFile, err := os.Create(tempPath)
  480. if err != nil {
  481. return common.NewErrorf("Error creating temporary db file: %v", err)
  482. }
  483. // Robust deferred cleanup for the temporary file
  484. defer func() {
  485. if tempFile != nil {
  486. if cerr := tempFile.Close(); cerr != nil {
  487. logger.Warningf("Warning: failed to close temp file: %v", cerr)
  488. }
  489. }
  490. if _, err := os.Stat(tempPath); err == nil {
  491. if rerr := os.Remove(tempPath); rerr != nil {
  492. logger.Warningf("Warning: failed to remove temp file: %v", rerr)
  493. }
  494. }
  495. }()
  496. // Save uploaded file to temporary file
  497. if _, err = io.Copy(tempFile, file); err != nil {
  498. return common.NewErrorf("Error saving db: %v", err)
  499. }
  500. // Check if we can init the db or not
  501. if err = database.InitDB(tempPath); err != nil {
  502. return common.NewErrorf("Error checking db: %v", err)
  503. }
  504. // Stop Xray
  505. s.StopXrayService()
  506. // Backup the current database for fallback
  507. fallbackPath := fmt.Sprintf("%s.backup", config.GetDBPath())
  508. // Remove the existing fallback file (if any)
  509. if _, err := os.Stat(fallbackPath); err == nil {
  510. if errRemove := os.Remove(fallbackPath); errRemove != nil {
  511. return common.NewErrorf("Error removing existing fallback db file: %v", errRemove)
  512. }
  513. }
  514. // Move the current database to the fallback location
  515. if err = os.Rename(config.GetDBPath(), fallbackPath); err != nil {
  516. return common.NewErrorf("Error backing up current db file: %v", err)
  517. }
  518. // Defer fallback cleanup ONLY if everything goes well
  519. defer func() {
  520. if _, err := os.Stat(fallbackPath); err == nil {
  521. if rerr := os.Remove(fallbackPath); rerr != nil {
  522. logger.Warningf("Warning: failed to remove fallback file: %v", rerr)
  523. }
  524. }
  525. }()
  526. // Move temp to DB path
  527. if err = os.Rename(tempPath, config.GetDBPath()); err != nil {
  528. // Restore from fallback
  529. if errRename := os.Rename(fallbackPath, config.GetDBPath()); errRename != nil {
  530. return common.NewErrorf("Error moving db file and restoring fallback: %v", errRename)
  531. }
  532. return common.NewErrorf("Error moving db file: %v", err)
  533. }
  534. // Migrate DB
  535. if err = database.InitDB(config.GetDBPath()); err != nil {
  536. if errRename := os.Rename(fallbackPath, config.GetDBPath()); errRename != nil {
  537. return common.NewErrorf("Error migrating db and restoring fallback: %v", errRename)
  538. }
  539. return common.NewErrorf("Error migrating db: %v", err)
  540. }
  541. s.inboundService.MigrateDB()
  542. // Start Xray
  543. if err = s.RestartXrayService(); err != nil {
  544. return common.NewErrorf("Imported DB but failed to start Xray: %v", err)
  545. }
  546. return nil
  547. }
  548. func (s *ServerService) UpdateGeofile(fileName string) error {
  549. files := []struct {
  550. URL string
  551. FileName string
  552. }{
  553. {"https://github.com/Loyalsoldier/v2ray-rules-dat/releases/latest/download/geoip.dat", "geoip.dat"},
  554. {"https://github.com/Loyalsoldier/v2ray-rules-dat/releases/latest/download/geosite.dat", "geosite.dat"},
  555. {"https://github.com/chocolate4u/Iran-v2ray-rules/releases/latest/download/geoip.dat", "geoip_IR.dat"},
  556. {"https://github.com/chocolate4u/Iran-v2ray-rules/releases/latest/download/geosite.dat", "geosite_IR.dat"},
  557. {"https://github.com/runetfreedom/russia-v2ray-rules-dat/releases/latest/download/geoip.dat", "geoip_RU.dat"},
  558. {"https://github.com/runetfreedom/russia-v2ray-rules-dat/releases/latest/download/geosite.dat", "geosite_RU.dat"},
  559. }
  560. downloadFile := func(url, destPath string) error {
  561. resp, err := http.Get(url)
  562. if err != nil {
  563. return common.NewErrorf("Failed to download Geofile from %s: %v", url, err)
  564. }
  565. defer resp.Body.Close()
  566. file, err := os.Create(destPath)
  567. if err != nil {
  568. return common.NewErrorf("Failed to create Geofile %s: %v", destPath, err)
  569. }
  570. defer file.Close()
  571. _, err = io.Copy(file, resp.Body)
  572. if err != nil {
  573. return common.NewErrorf("Failed to save Geofile %s: %v", destPath, err)
  574. }
  575. return nil
  576. }
  577. var fileURL string
  578. for _, file := range files {
  579. if file.FileName == fileName {
  580. fileURL = file.URL
  581. break
  582. }
  583. }
  584. if fileURL == "" {
  585. return common.NewErrorf("File '%s' not found in the list of Geofiles", fileName)
  586. }
  587. destPath := fmt.Sprintf("%s/%s", config.GetBinFolderPath(), fileName)
  588. if err := downloadFile(fileURL, destPath); err != nil {
  589. return common.NewErrorf("Error downloading Geofile '%s': %v", fileName, err)
  590. }
  591. err := s.RestartXrayService()
  592. if err != nil {
  593. return common.NewErrorf("Updated Geofile '%s' but Failed to start Xray: %v", fileName, err)
  594. }
  595. return nil
  596. }
  597. func (s *ServerService) GetNewX25519Cert() (any, error) {
  598. // Run the command
  599. cmd := exec.Command(xray.GetBinaryPath(), "x25519")
  600. var out bytes.Buffer
  601. cmd.Stdout = &out
  602. err := cmd.Run()
  603. if err != nil {
  604. return nil, err
  605. }
  606. lines := strings.Split(out.String(), "\n")
  607. privateKeyLine := strings.Split(lines[0], ":")
  608. publicKeyLine := strings.Split(lines[1], ":")
  609. privateKey := strings.TrimSpace(privateKeyLine[1])
  610. publicKey := strings.TrimSpace(publicKeyLine[1])
  611. keyPair := map[string]any{
  612. "privateKey": privateKey,
  613. "publicKey": publicKey,
  614. }
  615. return keyPair, nil
  616. }
  617. func (s *ServerService) GetNewmldsa65() (any, error) {
  618. // Run the command
  619. cmd := exec.Command(xray.GetBinaryPath(), "mldsa65")
  620. var out bytes.Buffer
  621. cmd.Stdout = &out
  622. err := cmd.Run()
  623. if err != nil {
  624. return nil, err
  625. }
  626. lines := strings.Split(out.String(), "\n")
  627. SeedLine := strings.Split(lines[0], ":")
  628. VerifyLine := strings.Split(lines[1], ":")
  629. seed := strings.TrimSpace(SeedLine[1])
  630. verify := strings.TrimSpace(VerifyLine[1])
  631. keyPair := map[string]any{
  632. "seed": seed,
  633. "verify": verify,
  634. }
  635. return keyPair, nil
  636. }