1
0

server.go 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942
  1. package service
  2. import (
  3. "archive/zip"
  4. "bufio"
  5. "bytes"
  6. "context"
  7. "crypto/sha256"
  8. "crypto/x509"
  9. "encoding/hex"
  10. "encoding/json"
  11. "encoding/pem"
  12. "fmt"
  13. "io"
  14. "mime/multipart"
  15. "net/http"
  16. "net/url"
  17. "os"
  18. "os/exec"
  19. "path/filepath"
  20. "regexp"
  21. "runtime"
  22. "slices"
  23. "strconv"
  24. "strings"
  25. "sync"
  26. "time"
  27. "github.com/mhsanaei/3x-ui/v3/internal/config"
  28. "github.com/mhsanaei/3x-ui/v3/internal/database"
  29. "github.com/mhsanaei/3x-ui/v3/internal/logger"
  30. "github.com/mhsanaei/3x-ui/v3/internal/util/common"
  31. "github.com/mhsanaei/3x-ui/v3/internal/util/sys"
  32. "github.com/mhsanaei/3x-ui/v3/internal/xray"
  33. "github.com/google/uuid"
  34. "github.com/shirou/gopsutil/v4/cpu"
  35. "github.com/shirou/gopsutil/v4/disk"
  36. "github.com/shirou/gopsutil/v4/host"
  37. "github.com/shirou/gopsutil/v4/load"
  38. "github.com/shirou/gopsutil/v4/mem"
  39. "github.com/shirou/gopsutil/v4/net"
  40. )
  41. // ProcessState represents the current state of a system process.
  42. type ProcessState string
  43. // Process state constants
  44. const (
  45. Running ProcessState = "running" // Process is running normally
  46. Stop ProcessState = "stop" // Process is stopped
  47. Error ProcessState = "error" // Process is in error state
  48. )
  49. // Status represents comprehensive system and application status information.
  50. // It includes CPU, memory, disk, network statistics, and Xray process status.
  51. type Status struct {
  52. T time.Time `json:"-"`
  53. Cpu float64 `json:"cpu"`
  54. CpuCores int `json:"cpuCores"`
  55. LogicalPro int `json:"logicalPro"`
  56. CpuSpeedMhz float64 `json:"cpuSpeedMhz"`
  57. Mem struct {
  58. Current uint64 `json:"current"`
  59. Total uint64 `json:"total"`
  60. } `json:"mem"`
  61. Swap struct {
  62. Current uint64 `json:"current"`
  63. Total uint64 `json:"total"`
  64. } `json:"swap"`
  65. Disk struct {
  66. Current uint64 `json:"current"`
  67. Total uint64 `json:"total"`
  68. } `json:"disk"`
  69. DiskIO struct {
  70. Read uint64 `json:"read"`
  71. Write uint64 `json:"write"`
  72. } `json:"diskIO"`
  73. DiskTraffic struct {
  74. Read uint64 `json:"read"`
  75. Write uint64 `json:"write"`
  76. } `json:"diskTraffic"`
  77. Xray struct {
  78. State ProcessState `json:"state"`
  79. ErrorMsg string `json:"errorMsg"`
  80. Version string `json:"version"`
  81. } `json:"xray"`
  82. PanelVersion string `json:"panelVersion"`
  83. PanelGuid string `json:"panelGuid"`
  84. Uptime uint64 `json:"uptime"`
  85. Loads []float64 `json:"loads"`
  86. TcpCount int `json:"tcpCount"`
  87. UdpCount int `json:"udpCount"`
  88. NetIO struct {
  89. Up uint64 `json:"up"`
  90. Down uint64 `json:"down"`
  91. PktUp uint64 `json:"pktUp"`
  92. PktDown uint64 `json:"pktDown"`
  93. } `json:"netIO"`
  94. NetTraffic struct {
  95. Sent uint64 `json:"sent"`
  96. Recv uint64 `json:"recv"`
  97. PktSent uint64 `json:"pktSent"`
  98. PktRecv uint64 `json:"pktRecv"`
  99. } `json:"netTraffic"`
  100. PublicIP struct {
  101. IPv4 string `json:"ipv4"`
  102. IPv6 string `json:"ipv6"`
  103. } `json:"publicIP"`
  104. AppStats struct {
  105. Threads uint32 `json:"threads"`
  106. Mem uint64 `json:"mem"`
  107. Uptime uint64 `json:"uptime"`
  108. } `json:"appStats"`
  109. }
  110. // Release represents information about a software release from GitHub.
  111. type Release struct {
  112. TagName string `json:"tag_name"` // The tag name of the release
  113. }
  114. // ServerService provides business logic for server monitoring and management.
  115. // It handles system status collection, IP detection, and application statistics.
  116. type ServerService struct {
  117. xrayService XrayService
  118. inboundService InboundService
  119. settingService SettingService
  120. cachedIPv4 string
  121. cachedIPv6 string
  122. noIPv6 bool
  123. mu sync.Mutex
  124. lastCPUTimes cpu.TimesStat
  125. hasLastCPUSample bool
  126. hasNativeCPUSample bool
  127. emaCPU float64
  128. cachedCpuSpeedMhz float64
  129. lastCpuInfoAttempt time.Time
  130. lastStatusMu sync.RWMutex
  131. lastStatus *Status
  132. versionsCacheMu sync.Mutex
  133. versionsCache *cachedXrayVersions
  134. }
  135. type cachedXrayVersions struct {
  136. versions []string
  137. fetchedAt time.Time
  138. }
  139. // xrayVersionsCacheTTL bounds how often /getXrayVersion hits GitHub. The list
  140. // is purely informational (rendered in the "switch Xray version" picker) so a
  141. // quarter-hour staleness window is fine and saves the API budget.
  142. const xrayVersionsCacheTTL = 15 * time.Minute
  143. // allowedHistoryBuckets is the bucket-second whitelist for time-series
  144. // aggregation endpoints (server + node metrics). Restricting it prevents
  145. // callers from triggering arbitrary aggregation work and keeps the
  146. // frontend's bucket selector self-documenting.
  147. var allowedHistoryBuckets = map[int]bool{
  148. 2: true, // Real-time view
  149. 30: true, // 30s intervals
  150. 60: true, // 1m intervals
  151. 120: true, // 2m intervals
  152. 180: true, // 3m intervals
  153. 300: true, // 5m intervals
  154. }
  155. // IsAllowedHistoryBucket reports whether a bucket-seconds value is in the
  156. // whitelist used by /server/history, /server/cpuHistory, /server/xrayMetricsHistory,
  157. // /server/xrayObservatoryHistory, and /nodes/history.
  158. func IsAllowedHistoryBucket(bucketSeconds int) bool {
  159. return allowedHistoryBuckets[bucketSeconds]
  160. }
  161. // LastStatus returns the most recent Status snapshot collected by
  162. // RefreshStatus. Safe for concurrent readers.
  163. func (s *ServerService) LastStatus() *Status {
  164. s.lastStatusMu.RLock()
  165. defer s.lastStatusMu.RUnlock()
  166. return s.lastStatus
  167. }
  168. // RefreshStatus collects a new system snapshot, stores it as LastStatus, and
  169. // appends it to the system-metrics time series. Returns the new snapshot (may
  170. // be nil if collection failed). Called by the background ticker; the caller is
  171. // responsible for any side effects (websocket broadcast, xray metrics sample).
  172. func (s *ServerService) RefreshStatus() *Status {
  173. next := s.GetStatus(s.LastStatus())
  174. if next == nil {
  175. return nil
  176. }
  177. s.lastStatusMu.Lock()
  178. s.lastStatus = next
  179. s.lastStatusMu.Unlock()
  180. s.AppendStatusSample(time.Now(), next)
  181. return next
  182. }
  183. // GetXrayVersionsCached wraps GetXrayVersions with a TTL cache. On fetch
  184. // failure we serve the last successful list (if any) so the UI doesn't go
  185. // blank during a GitHub API hiccup; if there's no cache at all the underlying
  186. // error is surfaced.
  187. func (s *ServerService) GetXrayVersionsCached() ([]string, error) {
  188. s.versionsCacheMu.Lock()
  189. cache := s.versionsCache
  190. s.versionsCacheMu.Unlock()
  191. if cache != nil && time.Since(cache.fetchedAt) <= xrayVersionsCacheTTL {
  192. return cache.versions, nil
  193. }
  194. versions, err := s.GetXrayVersions()
  195. if err != nil {
  196. if cache != nil {
  197. logger.Warning("GetXrayVersionsCached: serving stale list:", err)
  198. return cache.versions, nil
  199. }
  200. return nil, err
  201. }
  202. s.versionsCacheMu.Lock()
  203. s.versionsCache = &cachedXrayVersions{versions: versions, fetchedAt: time.Now()}
  204. s.versionsCacheMu.Unlock()
  205. return versions, nil
  206. }
  207. // GetDefaultLogOutboundTags scans the default Xray config for freedom and
  208. // blackhole outbound tags so /getXrayLogs can colour-code log lines without
  209. // the controller re-doing the JSON walk. Falls back to the historical
  210. // "direct"/"blocked" defaults when the config can't be read.
  211. func (s *ServerService) GetDefaultLogOutboundTags() (freedoms, blackholes []string) {
  212. config, err := s.settingService.GetDefaultXrayConfig()
  213. if err == nil && config != nil {
  214. if cfgMap, ok := config.(map[string]any); ok {
  215. if outbounds, ok := cfgMap["outbounds"].([]any); ok {
  216. for _, outbound := range outbounds {
  217. obMap, ok := outbound.(map[string]any)
  218. if !ok {
  219. continue
  220. }
  221. tag, _ := obMap["tag"].(string)
  222. if tag == "" {
  223. continue
  224. }
  225. switch obMap["protocol"] {
  226. case "freedom":
  227. freedoms = append(freedoms, tag)
  228. case "blackhole":
  229. blackholes = append(blackholes, tag)
  230. }
  231. }
  232. }
  233. }
  234. }
  235. if len(freedoms) == 0 {
  236. freedoms = []string{"direct"}
  237. }
  238. if len(blackholes) == 0 {
  239. blackholes = []string{"blocked"}
  240. }
  241. return freedoms, blackholes
  242. }
  243. // AggregateCpuHistory returns up to maxPoints averaged buckets of size bucketSeconds.
  244. // Kept for back-compat with the original /panel/api/server/cpuHistory/:bucket route;
  245. // the response key is "cpu" (not "v") so legacy consumers parse unchanged.
  246. func (s *ServerService) AggregateCpuHistory(bucketSeconds int, maxPoints int) []map[string]any {
  247. out := systemMetrics.aggregate("cpu", bucketSeconds, maxPoints)
  248. for _, p := range out {
  249. p["cpu"] = p["v"]
  250. delete(p, "v")
  251. }
  252. return out
  253. }
  254. // AggregateSystemMetric returns up to maxPoints averaged buckets for any
  255. // known system metric (see SystemMetricKeys). Output points have keys
  256. // {"t": unixSec, "v": value}; the caller decides how to format the value.
  257. func (s *ServerService) AggregateSystemMetric(metric string, bucketSeconds int, maxPoints int) []map[string]any {
  258. return systemMetrics.aggregate(metric, bucketSeconds, maxPoints)
  259. }
  260. type LogEntry struct {
  261. DateTime time.Time
  262. FromAddress string
  263. ToAddress string
  264. Inbound string
  265. Outbound string
  266. Email string
  267. Event int
  268. }
  269. func getPublicIP(url string) string {
  270. client := &http.Client{
  271. Timeout: 3 * time.Second,
  272. }
  273. resp, err := client.Get(url)
  274. if err != nil {
  275. return "N/A"
  276. }
  277. defer resp.Body.Close()
  278. // Don't retry if access is blocked or region-restricted
  279. if resp.StatusCode == http.StatusForbidden || resp.StatusCode == http.StatusUnavailableForLegalReasons {
  280. return "N/A"
  281. }
  282. if resp.StatusCode != http.StatusOK {
  283. return "N/A"
  284. }
  285. ip, err := io.ReadAll(resp.Body)
  286. if err != nil {
  287. return "N/A"
  288. }
  289. ipString := strings.TrimSpace(string(ip))
  290. if ipString == "" {
  291. return "N/A"
  292. }
  293. return ipString
  294. }
  295. func (s *ServerService) GetStatus(lastStatus *Status) *Status {
  296. now := time.Now()
  297. status := &Status{
  298. T: now,
  299. }
  300. // CPU stats
  301. util, err := s.sampleCPUUtilization()
  302. if err != nil {
  303. logger.Warning("get cpu percent failed:", err)
  304. } else {
  305. status.Cpu = util
  306. }
  307. status.CpuCores, err = cpu.Counts(false)
  308. if err != nil {
  309. logger.Warning("get cpu cores count failed:", err)
  310. }
  311. status.LogicalPro = runtime.NumCPU()
  312. if status.CpuSpeedMhz = s.cachedCpuSpeedMhz; s.cachedCpuSpeedMhz == 0 && time.Since(s.lastCpuInfoAttempt) > 5*time.Minute {
  313. s.lastCpuInfoAttempt = time.Now()
  314. done := make(chan struct{})
  315. go func() {
  316. defer close(done)
  317. cpuInfos, err := cpu.Info()
  318. if err != nil {
  319. logger.Warning("get cpu info failed:", err)
  320. return
  321. }
  322. if len(cpuInfos) > 0 {
  323. s.cachedCpuSpeedMhz = cpuInfos[0].Mhz
  324. status.CpuSpeedMhz = s.cachedCpuSpeedMhz
  325. } else {
  326. logger.Warning("could not find cpu info")
  327. }
  328. }()
  329. select {
  330. case <-done:
  331. case <-time.After(1500 * time.Millisecond):
  332. logger.Warning("cpu info query timed out; will retry later")
  333. }
  334. } else if s.cachedCpuSpeedMhz != 0 {
  335. status.CpuSpeedMhz = s.cachedCpuSpeedMhz
  336. }
  337. // Uptime
  338. upTime, err := host.Uptime()
  339. if err != nil {
  340. logger.Warning("get uptime failed:", err)
  341. } else {
  342. status.Uptime = upTime
  343. }
  344. // Memory stats
  345. memInfo, err := mem.VirtualMemory()
  346. if err != nil {
  347. logger.Warning("get virtual memory failed:", err)
  348. } else {
  349. status.Mem.Current = memInfo.Used
  350. status.Mem.Total = memInfo.Total
  351. }
  352. swapInfo, err := mem.SwapMemory()
  353. if err != nil {
  354. logger.Warning("get swap memory failed:", err)
  355. } else {
  356. status.Swap.Current = swapInfo.Used
  357. status.Swap.Total = swapInfo.Total
  358. }
  359. // Disk stats
  360. diskInfo, err := disk.Usage("/")
  361. if err != nil {
  362. logger.Warning("get disk usage failed:", err)
  363. } else {
  364. status.Disk.Current = diskInfo.Used
  365. status.Disk.Total = diskInfo.Total
  366. }
  367. diskIOStats, err := disk.IOCounters()
  368. if err != nil {
  369. logger.Warning("get disk io counters failed:", err)
  370. } else {
  371. var totalRead, totalWrite uint64
  372. for _, counter := range diskIOStats {
  373. totalRead += counter.ReadBytes
  374. totalWrite += counter.WriteBytes
  375. }
  376. status.DiskTraffic.Read = totalRead
  377. status.DiskTraffic.Write = totalWrite
  378. if lastStatus != nil {
  379. duration := now.Sub(lastStatus.T)
  380. seconds := float64(duration) / float64(time.Second)
  381. if seconds > 0 && status.DiskTraffic.Read >= lastStatus.DiskTraffic.Read {
  382. status.DiskIO.Read = uint64(float64(status.DiskTraffic.Read-lastStatus.DiskTraffic.Read) / seconds)
  383. }
  384. if seconds > 0 && status.DiskTraffic.Write >= lastStatus.DiskTraffic.Write {
  385. status.DiskIO.Write = uint64(float64(status.DiskTraffic.Write-lastStatus.DiskTraffic.Write) / seconds)
  386. }
  387. }
  388. }
  389. // Load averages
  390. avgState, err := load.Avg()
  391. if err != nil {
  392. logger.Warning("get load avg failed:", err)
  393. } else {
  394. status.Loads = []float64{avgState.Load1, avgState.Load5, avgState.Load15}
  395. }
  396. // Network stats
  397. ioStats, err := net.IOCounters(true)
  398. if err != nil {
  399. logger.Warning("get io counters failed:", err)
  400. } else {
  401. var totalSent, totalRecv, totalPktSent, totalPktRecv uint64
  402. for _, iface := range ioStats {
  403. name := strings.ToLower(iface.Name)
  404. if isVirtualInterface(name) {
  405. continue
  406. }
  407. totalSent += iface.BytesSent
  408. totalRecv += iface.BytesRecv
  409. totalPktSent += iface.PacketsSent
  410. totalPktRecv += iface.PacketsRecv
  411. }
  412. status.NetTraffic.Sent = totalSent
  413. status.NetTraffic.Recv = totalRecv
  414. status.NetTraffic.PktSent = totalPktSent
  415. status.NetTraffic.PktRecv = totalPktRecv
  416. if lastStatus != nil {
  417. duration := now.Sub(lastStatus.T)
  418. seconds := float64(duration) / float64(time.Second)
  419. up := uint64(float64(status.NetTraffic.Sent-lastStatus.NetTraffic.Sent) / seconds)
  420. down := uint64(float64(status.NetTraffic.Recv-lastStatus.NetTraffic.Recv) / seconds)
  421. status.NetIO.Up = up
  422. status.NetIO.Down = down
  423. if seconds > 0 && status.NetTraffic.PktSent >= lastStatus.NetTraffic.PktSent {
  424. status.NetIO.PktUp = uint64(float64(status.NetTraffic.PktSent-lastStatus.NetTraffic.PktSent) / seconds)
  425. }
  426. if seconds > 0 && status.NetTraffic.PktRecv >= lastStatus.NetTraffic.PktRecv {
  427. status.NetIO.PktDown = uint64(float64(status.NetTraffic.PktRecv-lastStatus.NetTraffic.PktRecv) / seconds)
  428. }
  429. }
  430. }
  431. // TCP/UDP connections
  432. status.TcpCount, err = sys.GetTCPCount()
  433. if err != nil {
  434. logger.Warning("get tcp connections failed:", err)
  435. }
  436. status.UdpCount, err = sys.GetUDPCount()
  437. if err != nil {
  438. logger.Warning("get udp connections failed:", err)
  439. }
  440. // IP fetching with caching
  441. showIp4ServiceLists := []string{
  442. "https://api4.ipify.org",
  443. "https://ipv4.icanhazip.com",
  444. "https://v4.api.ipinfo.io/ip",
  445. "https://ipv4.myexternalip.com/raw",
  446. "https://4.ident.me",
  447. "https://check-host.net/ip",
  448. }
  449. showIp6ServiceLists := []string{
  450. "https://api6.ipify.org",
  451. "https://ipv6.icanhazip.com",
  452. "https://v6.api.ipinfo.io/ip",
  453. "https://ipv6.myexternalip.com/raw",
  454. "https://6.ident.me",
  455. }
  456. if s.cachedIPv4 == "" {
  457. for _, ip4Service := range showIp4ServiceLists {
  458. s.cachedIPv4 = getPublicIP(ip4Service)
  459. if s.cachedIPv4 != "N/A" {
  460. break
  461. }
  462. }
  463. }
  464. if s.cachedIPv6 == "" && !s.noIPv6 {
  465. for _, ip6Service := range showIp6ServiceLists {
  466. s.cachedIPv6 = getPublicIP(ip6Service)
  467. if s.cachedIPv6 != "N/A" {
  468. break
  469. }
  470. }
  471. }
  472. if s.cachedIPv6 == "N/A" {
  473. s.noIPv6 = true
  474. }
  475. status.PublicIP.IPv4 = s.cachedIPv4
  476. status.PublicIP.IPv6 = s.cachedIPv6
  477. // Xray status
  478. if s.xrayService.IsXrayRunning() {
  479. status.Xray.State = Running
  480. status.Xray.ErrorMsg = ""
  481. } else {
  482. err := s.xrayService.GetXrayErr()
  483. if err != nil {
  484. status.Xray.State = Error
  485. } else {
  486. status.Xray.State = Stop
  487. }
  488. status.Xray.ErrorMsg = s.xrayService.GetXrayResult()
  489. }
  490. status.Xray.Version = s.xrayService.GetXrayVersion()
  491. status.PanelVersion = config.GetVersion()
  492. if guid, err := s.settingService.GetPanelGuid(); err == nil {
  493. status.PanelGuid = guid
  494. }
  495. // Application stats
  496. var rtm runtime.MemStats
  497. runtime.ReadMemStats(&rtm)
  498. status.AppStats.Mem = rtm.Sys
  499. status.AppStats.Threads = uint32(runtime.NumGoroutine())
  500. if p != nil && p.IsRunning() {
  501. status.AppStats.Uptime = p.GetUptime()
  502. } else {
  503. status.AppStats.Uptime = 0
  504. }
  505. return status
  506. }
  507. // AppendCpuSample is preserved for callers that only have the CPU number.
  508. // New callers should prefer AppendStatusSample which writes the full set.
  509. func (s *ServerService) AppendCpuSample(t time.Time, v float64) {
  510. systemMetrics.append("cpu", t, v)
  511. }
  512. // AppendStatusSample writes one tick of every metric we keep — CPU, memory
  513. // percent, network throughput (bytes/s), online client count, and the three
  514. // load averages. Called by RefreshStatus on the same @2s cadence as
  515. // AppendCpuSample, so all series stay aligned.
  516. func (s *ServerService) AppendStatusSample(t time.Time, status *Status) {
  517. if status == nil {
  518. return
  519. }
  520. systemMetrics.append("cpu", t, status.Cpu)
  521. if status.Mem.Total > 0 {
  522. systemMetrics.append("mem", t, float64(status.Mem.Current)*100.0/float64(status.Mem.Total))
  523. }
  524. if status.Swap.Total > 0 {
  525. systemMetrics.append("swap", t, float64(status.Swap.Current)*100.0/float64(status.Swap.Total))
  526. } else {
  527. systemMetrics.append("swap", t, 0)
  528. }
  529. systemMetrics.append("netUp", t, float64(status.NetIO.Up))
  530. systemMetrics.append("netDown", t, float64(status.NetIO.Down))
  531. systemMetrics.append("diskRead", t, float64(status.DiskIO.Read))
  532. systemMetrics.append("diskWrite", t, float64(status.DiskIO.Write))
  533. if status.Disk.Total > 0 {
  534. systemMetrics.append("diskUsage", t, float64(status.Disk.Current)*100.0/float64(status.Disk.Total))
  535. }
  536. systemMetrics.append("pktUp", t, float64(status.NetIO.PktUp))
  537. systemMetrics.append("pktDown", t, float64(status.NetIO.PktDown))
  538. systemMetrics.append("tcpCount", t, float64(status.TcpCount))
  539. systemMetrics.append("udpCount", t, float64(status.UdpCount))
  540. online := 0
  541. if p != nil && p.IsRunning() {
  542. online = len(p.GetOnlineClients())
  543. }
  544. systemMetrics.append("online", t, float64(online))
  545. if len(status.Loads) >= 3 {
  546. systemMetrics.append("load1", t, status.Loads[0])
  547. systemMetrics.append("load5", t, status.Loads[1])
  548. systemMetrics.append("load15", t, status.Loads[2])
  549. }
  550. }
  551. func (s *ServerService) sampleCPUUtilization() (float64, error) {
  552. // Try native platform-specific CPU implementation first (Windows, Linux, macOS)
  553. if pct, err := sys.CPUPercentRaw(); err == nil {
  554. s.mu.Lock()
  555. // First call to native method returns 0 (initializes baseline)
  556. if !s.hasNativeCPUSample {
  557. s.hasNativeCPUSample = true
  558. s.mu.Unlock()
  559. return 0, nil
  560. }
  561. // Smooth with EMA
  562. const alpha = 0.3
  563. if s.emaCPU == 0 {
  564. s.emaCPU = pct
  565. } else {
  566. s.emaCPU = alpha*pct + (1-alpha)*s.emaCPU
  567. }
  568. val := s.emaCPU
  569. s.mu.Unlock()
  570. return val, nil
  571. }
  572. // If native call fails, fall back to gopsutil times
  573. // Read aggregate CPU times (all CPUs combined)
  574. times, err := cpu.Times(false)
  575. if err != nil {
  576. return 0, err
  577. }
  578. if len(times) == 0 {
  579. return 0, fmt.Errorf("no cpu times available")
  580. }
  581. cur := times[0]
  582. s.mu.Lock()
  583. defer s.mu.Unlock()
  584. // If this is the first sample, initialize and return current EMA (0 by default)
  585. if !s.hasLastCPUSample {
  586. s.lastCPUTimes = cur
  587. s.hasLastCPUSample = true
  588. return s.emaCPU, nil
  589. }
  590. // Compute busy and total deltas
  591. // Note: Guest and GuestNice times are already included in User and Nice respectively,
  592. // so we exclude them to avoid double-counting (Linux kernel accounting)
  593. idleDelta := cur.Idle - s.lastCPUTimes.Idle
  594. busyDelta := (cur.User - s.lastCPUTimes.User) +
  595. (cur.System - s.lastCPUTimes.System) +
  596. (cur.Nice - s.lastCPUTimes.Nice) +
  597. (cur.Iowait - s.lastCPUTimes.Iowait) +
  598. (cur.Irq - s.lastCPUTimes.Irq) +
  599. (cur.Softirq - s.lastCPUTimes.Softirq) +
  600. (cur.Steal - s.lastCPUTimes.Steal)
  601. totalDelta := busyDelta + idleDelta
  602. // Update last sample for next time
  603. s.lastCPUTimes = cur
  604. // Guard against division by zero or negative deltas (e.g., counter resets)
  605. if totalDelta <= 0 {
  606. return s.emaCPU, nil
  607. }
  608. raw := 100.0 * (busyDelta / totalDelta)
  609. if raw < 0 {
  610. raw = 0
  611. }
  612. if raw > 100 {
  613. raw = 100
  614. }
  615. // Exponential moving average to smooth spikes
  616. const alpha = 0.3 // smoothing factor (0<alpha<=1). Higher = more responsive, lower = smoother
  617. if s.emaCPU == 0 {
  618. // Initialize EMA with the first real reading to avoid long warm-up from zero
  619. s.emaCPU = raw
  620. } else {
  621. s.emaCPU = alpha*raw + (1-alpha)*s.emaCPU
  622. }
  623. return s.emaCPU, nil
  624. }
  625. const (
  626. maxXrayArchiveBytes = 200 << 20
  627. maxXrayBinaryBytes = 200 << 20
  628. // maxXrayDigestBytes caps the .dgst checksum sidecar read; it is a few
  629. // hundred bytes in practice.
  630. maxXrayDigestBytes = 64 << 10
  631. )
  632. func (s *ServerService) GetXrayVersions() ([]string, error) {
  633. const (
  634. XrayURL = "https://api.github.com/repos/XTLS/Xray-core/releases"
  635. bufferSize = 8192
  636. )
  637. resp, err := s.settingService.NewProxiedHTTPClient(10 * time.Second).Get(XrayURL)
  638. if err != nil {
  639. return nil, err
  640. }
  641. defer resp.Body.Close()
  642. // Check HTTP status code - GitHub API returns object instead of array on error
  643. if resp.StatusCode != http.StatusOK {
  644. bodyBytes, _ := io.ReadAll(resp.Body)
  645. var errorResponse struct {
  646. Message string `json:"message"`
  647. }
  648. if json.Unmarshal(bodyBytes, &errorResponse) == nil && errorResponse.Message != "" {
  649. return nil, fmt.Errorf("GitHub API error: %s", errorResponse.Message)
  650. }
  651. return nil, fmt.Errorf("GitHub API returned status %d: %s", resp.StatusCode, resp.Status)
  652. }
  653. buffer := bytes.NewBuffer(make([]byte, bufferSize))
  654. buffer.Reset()
  655. if _, err := buffer.ReadFrom(resp.Body); err != nil {
  656. return nil, err
  657. }
  658. var releases []Release
  659. if err := json.Unmarshal(buffer.Bytes(), &releases); err != nil {
  660. return nil, err
  661. }
  662. var versions []string
  663. for _, release := range releases {
  664. tagVersion := strings.TrimPrefix(release.TagName, "v")
  665. tagParts := strings.Split(tagVersion, ".")
  666. if len(tagParts) != 3 {
  667. continue
  668. }
  669. major, err1 := strconv.Atoi(tagParts[0])
  670. minor, err2 := strconv.Atoi(tagParts[1])
  671. patch, err3 := strconv.Atoi(tagParts[2])
  672. if err1 != nil || err2 != nil || err3 != nil {
  673. continue
  674. }
  675. if major > 26 || (major == 26 && minor > 4) || (major == 26 && minor == 4 && patch >= 25) {
  676. versions = append(versions, release.TagName)
  677. }
  678. }
  679. return versions, nil
  680. }
  681. func (s *ServerService) StopXrayService() error {
  682. err := s.xrayService.StopXray()
  683. if err != nil {
  684. logger.Error("stop xray failed:", err)
  685. return err
  686. }
  687. return nil
  688. }
  689. func (s *ServerService) RestartXrayService() error {
  690. err := s.xrayService.RestartXray(true)
  691. if err != nil {
  692. logger.Error("start xray failed:", err)
  693. return err
  694. }
  695. return nil
  696. }
  697. func (s *ServerService) downloadXRay(version string) (string, error) {
  698. osName := runtime.GOOS
  699. arch := runtime.GOARCH
  700. switch osName {
  701. case "darwin":
  702. osName = "macos"
  703. case "windows":
  704. osName = "windows"
  705. }
  706. switch arch {
  707. case "amd64":
  708. arch = "64"
  709. case "arm64":
  710. arch = "arm64-v8a"
  711. case "armv7":
  712. arch = "arm32-v7a"
  713. case "armv6":
  714. arch = "arm32-v6"
  715. case "armv5":
  716. arch = "arm32-v5"
  717. case "386":
  718. arch = "32"
  719. case "s390x":
  720. arch = "s390x"
  721. }
  722. fileName := fmt.Sprintf("Xray-%s-%s.zip", osName, arch)
  723. url := fmt.Sprintf("https://github.com/XTLS/Xray-core/releases/download/%s/%s", version, fileName)
  724. client := s.settingService.NewProxiedHTTPClient(60 * time.Second)
  725. resp, err := client.Get(url)
  726. if err != nil {
  727. return "", err
  728. }
  729. defer resp.Body.Close()
  730. if resp.StatusCode != http.StatusOK {
  731. return "", fmt.Errorf("download xray: unexpected HTTP %d", resp.StatusCode)
  732. }
  733. if resp.ContentLength > maxXrayArchiveBytes {
  734. return "", fmt.Errorf("download xray: archive exceeds %d bytes", maxXrayArchiveBytes)
  735. }
  736. file, err := os.CreateTemp("", "xray-*.zip")
  737. if err != nil {
  738. return "", err
  739. }
  740. path := file.Name()
  741. ok := false
  742. defer func() {
  743. _ = file.Close()
  744. if !ok {
  745. _ = os.Remove(path)
  746. }
  747. }()
  748. n, err := io.Copy(file, io.LimitReader(resp.Body, maxXrayArchiveBytes+1))
  749. if err != nil {
  750. return "", err
  751. }
  752. if n > maxXrayArchiveBytes {
  753. return "", fmt.Errorf("download xray: archive exceeds %d bytes", maxXrayArchiveBytes)
  754. }
  755. // Verify the archive against the SHA2-256 published in the release's .dgst
  756. // sidecar before installing it. TLS protects the transport, not the artifact;
  757. // a corrupted or tampered asset must not be installed and run as xray.
  758. want, err := s.fetchXrayDigestSHA256(client, url+".dgst")
  759. if err != nil {
  760. return "", err
  761. }
  762. if _, err := file.Seek(0, io.SeekStart); err != nil {
  763. return "", err
  764. }
  765. hasher := sha256.New()
  766. if _, err := io.Copy(hasher, file); err != nil {
  767. return "", err
  768. }
  769. if got := hex.EncodeToString(hasher.Sum(nil)); !strings.EqualFold(got, want) {
  770. // User-facing warning: the archive's SHA-256 does not match the official
  771. // release checksum, so the download is corrupted or has been tampered
  772. // with. Abort the install so a bad binary is never run, and tell the user
  773. // to retry/re-download rather than proceed with a mismatched image.
  774. return "", fmt.Errorf("Xray update aborted: the downloaded archive does not match the official SHA-256 checksum, so the image is corrupted or differs from the official release. Please exit and re-download the official image, then try again (expected %s, got %s)", want, got)
  775. }
  776. ok = true
  777. return path, nil
  778. }
  779. // fetchXrayDigestSHA256 downloads the .dgst sidecar XTLS publishes next to each
  780. // release asset and returns the SHA2-256 hex digest it lists.
  781. func (s *ServerService) fetchXrayDigestSHA256(client *http.Client, dgstURL string) (string, error) {
  782. resp, err := client.Get(dgstURL)
  783. if err != nil {
  784. return "", fmt.Errorf("download xray checksum: %w", err)
  785. }
  786. defer resp.Body.Close()
  787. if resp.StatusCode != http.StatusOK {
  788. return "", fmt.Errorf("download xray checksum: unexpected HTTP %d", resp.StatusCode)
  789. }
  790. raw, err := io.ReadAll(io.LimitReader(resp.Body, maxXrayDigestBytes))
  791. if err != nil {
  792. return "", fmt.Errorf("download xray checksum: %w", err)
  793. }
  794. return parseXrayDigestSHA256(raw)
  795. }
  796. // parseXrayDigestSHA256 extracts the lowercase SHA2-256 hex from an XTLS .dgst
  797. // file, whose lines are "ALGO= <hex>" (the relevant one being "SHA2-256= ...").
  798. func parseXrayDigestSHA256(dgst []byte) (string, error) {
  799. for _, line := range strings.Split(string(dgst), "\n") {
  800. rest, ok := strings.CutPrefix(strings.TrimSpace(line), "SHA2-256=")
  801. if !ok {
  802. continue
  803. }
  804. h := strings.ToLower(strings.TrimSpace(rest))
  805. if len(h) != 64 {
  806. return "", fmt.Errorf("xray checksum: malformed SHA2-256 entry in digest")
  807. }
  808. return h, nil
  809. }
  810. return "", fmt.Errorf("xray checksum: no SHA2-256 entry in digest")
  811. }
  812. func (s *ServerService) UpdateXray(version string) error {
  813. versions, err := s.GetXrayVersions()
  814. if err != nil {
  815. return err
  816. }
  817. if !slices.Contains(versions, version) {
  818. return fmt.Errorf("xray version %q is not in the fetched release list", version)
  819. }
  820. // 1. Stop xray before doing anything
  821. if err := s.StopXrayService(); err != nil {
  822. logger.Warning("failed to stop xray before update:", err)
  823. }
  824. // 2. Download the zip
  825. zipFileName, err := s.downloadXRay(version)
  826. if err != nil {
  827. return err
  828. }
  829. defer os.Remove(zipFileName)
  830. zipFile, err := os.Open(zipFileName)
  831. if err != nil {
  832. return err
  833. }
  834. defer zipFile.Close()
  835. stat, err := zipFile.Stat()
  836. if err != nil {
  837. return err
  838. }
  839. reader, err := zip.NewReader(zipFile, stat.Size())
  840. if err != nil {
  841. return err
  842. }
  843. // 3. Helper to extract files
  844. copyZipFile := func(zipName string, fileName string) error {
  845. zipFile, err := reader.Open(zipName)
  846. if err != nil {
  847. return err
  848. }
  849. defer zipFile.Close()
  850. if err := os.MkdirAll(filepath.Dir(fileName), 0755); err != nil {
  851. return err
  852. }
  853. tmpFile, err := os.CreateTemp(filepath.Dir(fileName), ".xray-*")
  854. if err != nil {
  855. return err
  856. }
  857. tmpPath := tmpFile.Name()
  858. ok := false
  859. defer func() {
  860. _ = tmpFile.Close()
  861. if !ok {
  862. _ = os.Remove(tmpPath)
  863. }
  864. }()
  865. n, err := io.Copy(tmpFile, io.LimitReader(zipFile, maxXrayBinaryBytes+1))
  866. if err != nil {
  867. return err
  868. }
  869. if n > maxXrayBinaryBytes {
  870. return fmt.Errorf("xray binary exceeds %d bytes", maxXrayBinaryBytes)
  871. }
  872. if err := tmpFile.Chmod(0755); err != nil {
  873. return err
  874. }
  875. if err := tmpFile.Close(); err != nil {
  876. return err
  877. }
  878. if runtime.GOOS == "windows" {
  879. _ = os.Remove(fileName)
  880. }
  881. if err := os.Rename(tmpPath, fileName); err != nil {
  882. return err
  883. }
  884. ok = true
  885. return nil
  886. }
  887. // 4. Extract correct binary
  888. if runtime.GOOS == "windows" {
  889. targetBinary := filepath.Join(config.GetBinFolderPath(), "xray-windows-amd64.exe")
  890. err = copyZipFile("xray.exe", targetBinary)
  891. } else {
  892. err = copyZipFile("xray", xray.GetBinaryPath())
  893. }
  894. if err != nil {
  895. return err
  896. }
  897. // 5. Restart xray
  898. if err := s.xrayService.RestartXray(true); err != nil {
  899. logger.Error("start xray failed:", err)
  900. return err
  901. }
  902. return nil
  903. }
  904. func (s *ServerService) GetLogs(count string, level string, syslog string) []string {
  905. c, _ := strconv.Atoi(count)
  906. var lines []string
  907. if syslog == "true" {
  908. // Check if running on Windows - journalctl is not available
  909. if runtime.GOOS == "windows" {
  910. return []string{"Syslog is not supported on Windows. Please use application logs instead by unchecking the 'Syslog' option."}
  911. }
  912. // Validate and sanitize count parameter
  913. countInt, err := strconv.Atoi(count)
  914. if err != nil || countInt < 1 || countInt > 10000 {
  915. return []string{"Invalid count parameter - must be a number between 1 and 10000"}
  916. }
  917. // Validate level parameter - only allow valid syslog levels
  918. validLevels := map[string]bool{
  919. "0": true, "emerg": true,
  920. "1": true, "alert": true,
  921. "2": true, "crit": true,
  922. "3": true, "err": true,
  923. "4": true, "warning": true,
  924. "5": true, "notice": true,
  925. "6": true, "info": true,
  926. "7": true, "debug": true,
  927. }
  928. if !validLevels[level] {
  929. return []string{"Invalid level parameter - must be a valid syslog level"}
  930. }
  931. // Use hardcoded command with validated parameters
  932. cmd := exec.Command("journalctl", "-u", "x-ui", "--no-pager", "-n", strconv.Itoa(countInt), "-p", level)
  933. var out bytes.Buffer
  934. cmd.Stdout = &out
  935. err = cmd.Run()
  936. if err != nil {
  937. return []string{"Failed to run journalctl command! Make sure systemd is available and x-ui service is registered."}
  938. }
  939. lines = strings.Split(out.String(), "\n")
  940. } else {
  941. lines = logger.GetLogs(c, level)
  942. }
  943. return lines
  944. }
  945. func (s *ServerService) GetXrayLogs(
  946. count string,
  947. filter string,
  948. showDirect string,
  949. showBlocked string,
  950. showProxy string,
  951. freedoms []string,
  952. blackholes []string) []LogEntry {
  953. const (
  954. Direct = iota
  955. Blocked
  956. Proxied
  957. )
  958. countInt, _ := strconv.Atoi(count)
  959. var entries []LogEntry
  960. pathToAccessLog, err := xray.GetAccessLogPath()
  961. if err != nil {
  962. return nil
  963. }
  964. file, err := os.Open(pathToAccessLog)
  965. if err != nil {
  966. return nil
  967. }
  968. defer file.Close()
  969. scanner := bufio.NewScanner(file)
  970. for scanner.Scan() {
  971. line := strings.TrimSpace(scanner.Text())
  972. if line == "" || strings.Contains(line, "api -> api") {
  973. //skipping empty lines and api calls
  974. continue
  975. }
  976. if filter != "" && !strings.Contains(line, filter) {
  977. //applying filter if it's not empty
  978. continue
  979. }
  980. var entry LogEntry
  981. parts := strings.Fields(line)
  982. for i, part := range parts {
  983. if i == 0 {
  984. dateTime, err := time.ParseInLocation("2006/01/02 15:04:05.999999", parts[0]+" "+parts[1], time.Local)
  985. if err != nil {
  986. continue
  987. }
  988. entry.DateTime = dateTime.UTC()
  989. }
  990. if part == "from" {
  991. entry.FromAddress = strings.TrimLeft(parts[i+1], "/")
  992. } else if part == "accepted" {
  993. entry.ToAddress = strings.TrimLeft(parts[i+1], "/")
  994. } else if strings.HasPrefix(part, "[") {
  995. entry.Inbound = part[1:]
  996. } else if strings.HasSuffix(part, "]") {
  997. entry.Outbound = part[:len(part)-1]
  998. } else if part == "email:" {
  999. entry.Email = parts[i+1]
  1000. }
  1001. }
  1002. if logEntryContains(line, freedoms) {
  1003. if showDirect == "false" {
  1004. continue
  1005. }
  1006. entry.Event = Direct
  1007. } else if logEntryContains(line, blackholes) {
  1008. if showBlocked == "false" {
  1009. continue
  1010. }
  1011. entry.Event = Blocked
  1012. } else {
  1013. if showProxy == "false" {
  1014. continue
  1015. }
  1016. entry.Event = Proxied
  1017. }
  1018. entries = append(entries, entry)
  1019. }
  1020. if err := scanner.Err(); err != nil {
  1021. return nil
  1022. }
  1023. if len(entries) > countInt {
  1024. entries = entries[len(entries)-countInt:]
  1025. }
  1026. return entries
  1027. }
  1028. // isVirtualInterface returns true for loopback and virtual/tunnel interfaces
  1029. // that should be excluded from network traffic statistics.
  1030. func isVirtualInterface(name string) bool {
  1031. // Exact matches
  1032. if name == "lo" || name == "lo0" {
  1033. return true
  1034. }
  1035. // Prefix matches for virtual/tunnel interfaces
  1036. virtualPrefixes := []string{
  1037. "loopback",
  1038. "docker",
  1039. "br-",
  1040. "veth",
  1041. "virbr",
  1042. "tun",
  1043. "tap",
  1044. "wg",
  1045. "tailscale",
  1046. "zt",
  1047. }
  1048. for _, prefix := range virtualPrefixes {
  1049. if strings.HasPrefix(name, prefix) {
  1050. return true
  1051. }
  1052. }
  1053. return false
  1054. }
  1055. func logEntryContains(line string, suffixes []string) bool {
  1056. for _, sfx := range suffixes {
  1057. if strings.Contains(line, sfx+"]") {
  1058. return true
  1059. }
  1060. }
  1061. return false
  1062. }
  1063. func (s *ServerService) GetConfigJson() (any, error) {
  1064. config, err := s.xrayService.GetXrayConfig()
  1065. if err != nil {
  1066. return nil, err
  1067. }
  1068. contents, err := json.MarshalIndent(config, "", " ")
  1069. if err != nil {
  1070. return nil, err
  1071. }
  1072. var jsonData any
  1073. err = json.Unmarshal(contents, &jsonData)
  1074. if err != nil {
  1075. return nil, err
  1076. }
  1077. return jsonData, nil
  1078. }
  1079. func (s *ServerService) GetDb() ([]byte, error) {
  1080. if database.IsPostgres() {
  1081. return s.exportPostgresDB()
  1082. }
  1083. // Update by manually trigger a checkpoint operation
  1084. err := database.Checkpoint()
  1085. if err != nil {
  1086. return nil, err
  1087. }
  1088. // Open the file for reading
  1089. file, err := os.Open(config.GetDBPath())
  1090. if err != nil {
  1091. return nil, err
  1092. }
  1093. defer file.Close()
  1094. // Read the file contents
  1095. fileContents, err := io.ReadAll(file)
  1096. if err != nil {
  1097. return nil, err
  1098. }
  1099. return fileContents, nil
  1100. }
  1101. // GetMigration produces a cross-engine migration file plus its filename: on a
  1102. // SQLite panel it returns a portable .dump (SQL text), and on a PostgreSQL panel
  1103. // it returns a .db SQLite database built from the live data. Either output can
  1104. // then seed a panel running on the other backend.
  1105. func (s *ServerService) GetMigration() ([]byte, string, error) {
  1106. if database.IsPostgres() {
  1107. tmp, err := os.CreateTemp("", "x-ui-migration-*.db")
  1108. if err != nil {
  1109. return nil, "", err
  1110. }
  1111. tmpPath := tmp.Name()
  1112. tmp.Close()
  1113. defer os.Remove(tmpPath)
  1114. if err := database.ExportPostgresToSQLite(config.GetDBDSN(), tmpPath); err != nil {
  1115. return nil, "", err
  1116. }
  1117. data, err := os.ReadFile(tmpPath)
  1118. if err != nil {
  1119. return nil, "", err
  1120. }
  1121. return data, "x-ui.db", nil
  1122. }
  1123. // SQLite panel: checkpoint so the .db reflects the latest writes, then dump.
  1124. if err := database.Checkpoint(); err != nil {
  1125. return nil, "", err
  1126. }
  1127. data, err := database.DumpSQLiteToBytes(config.GetDBPath())
  1128. if err != nil {
  1129. return nil, "", err
  1130. }
  1131. return data, "x-ui.dump", nil
  1132. }
  1133. func (s *ServerService) ImportDB(file multipart.File) error {
  1134. if database.IsPostgres() {
  1135. return s.importPostgresDB(file)
  1136. }
  1137. // Check if the file is a SQLite database
  1138. isValidDb, err := database.IsSQLiteDB(file)
  1139. if err != nil {
  1140. return common.NewErrorf("Error checking db file format: %v", err)
  1141. }
  1142. if !isValidDb {
  1143. return common.NewError("Invalid db file format")
  1144. }
  1145. // Reset the file reader to the beginning
  1146. _, err = file.Seek(0, 0)
  1147. if err != nil {
  1148. return common.NewErrorf("Error resetting file reader: %v", err)
  1149. }
  1150. // Save the file as a temporary file
  1151. tempPath := fmt.Sprintf("%s.temp", config.GetDBPath())
  1152. // Remove the existing temporary file (if any)
  1153. if _, err := os.Stat(tempPath); err == nil {
  1154. if errRemove := os.Remove(tempPath); errRemove != nil {
  1155. return common.NewErrorf("Error removing existing temporary db file: %v", errRemove)
  1156. }
  1157. }
  1158. // Create the temporary file
  1159. tempFile, err := os.Create(tempPath)
  1160. if err != nil {
  1161. return common.NewErrorf("Error creating temporary db file: %v", err)
  1162. }
  1163. // Robust deferred cleanup for the temporary file
  1164. defer func() {
  1165. if tempFile != nil {
  1166. if cerr := tempFile.Close(); cerr != nil {
  1167. logger.Warningf("Warning: failed to close temp file: %v", cerr)
  1168. }
  1169. }
  1170. if _, err := os.Stat(tempPath); err == nil {
  1171. if rerr := os.Remove(tempPath); rerr != nil {
  1172. logger.Warningf("Warning: failed to remove temp file: %v", rerr)
  1173. }
  1174. }
  1175. }()
  1176. // Save uploaded file to temporary file
  1177. if _, err = io.Copy(tempFile, file); err != nil {
  1178. return common.NewErrorf("Error saving db: %v", err)
  1179. }
  1180. // Close temp file before opening via sqlite
  1181. if err = tempFile.Close(); err != nil {
  1182. return common.NewErrorf("Error closing temporary db file: %v", err)
  1183. }
  1184. tempFile = nil
  1185. // Validate integrity (no migrations / side effects)
  1186. if err = database.ValidateSQLiteDB(tempPath); err != nil {
  1187. return common.NewErrorf("Invalid or corrupt db file: %v", err)
  1188. }
  1189. xrayStopped := true
  1190. defer func() {
  1191. if xrayStopped {
  1192. if errR := s.RestartXrayService(); errR != nil {
  1193. logger.Warningf("Failed to restart Xray after DB import error: %v", errR)
  1194. }
  1195. }
  1196. }()
  1197. if errStop := s.StopXrayService(); errStop != nil {
  1198. logger.Warningf("Failed to stop Xray before DB import: %v", errStop)
  1199. }
  1200. if errClose := database.CloseDB(); errClose != nil {
  1201. logger.Warningf("Failed to close existing DB before replacement: %v", errClose)
  1202. }
  1203. // Backup the current database for fallback
  1204. fallbackPath := fmt.Sprintf("%s.backup", config.GetDBPath())
  1205. // Remove the existing fallback file (if any)
  1206. if _, err := os.Stat(fallbackPath); err == nil {
  1207. if errRemove := os.Remove(fallbackPath); errRemove != nil {
  1208. return common.NewErrorf("Error removing existing fallback db file: %v", errRemove)
  1209. }
  1210. }
  1211. // Move the current database to the fallback location
  1212. if err = os.Rename(config.GetDBPath(), fallbackPath); err != nil {
  1213. return common.NewErrorf("Error backing up current db file: %v", err)
  1214. }
  1215. // Defer fallback cleanup ONLY if everything goes well
  1216. defer func() {
  1217. if _, err := os.Stat(fallbackPath); err == nil {
  1218. if rerr := os.Remove(fallbackPath); rerr != nil {
  1219. logger.Warningf("Warning: failed to remove fallback file: %v", rerr)
  1220. }
  1221. }
  1222. }()
  1223. // Move temp to DB path
  1224. if err = os.Rename(tempPath, config.GetDBPath()); err != nil {
  1225. // Restore from fallback
  1226. if errRename := os.Rename(fallbackPath, config.GetDBPath()); errRename != nil {
  1227. return common.NewErrorf("Error moving db file and restoring fallback: %v", errRename)
  1228. }
  1229. return common.NewErrorf("Error moving db file: %v", err)
  1230. }
  1231. // Open & migrate new DB
  1232. if err = database.InitDB(config.GetDBPath()); err != nil {
  1233. if errRename := os.Rename(fallbackPath, config.GetDBPath()); errRename != nil {
  1234. return common.NewErrorf("Error migrating db and restoring fallback: %v", errRename)
  1235. }
  1236. return common.NewErrorf("Error migrating db: %v", err)
  1237. }
  1238. s.inboundService.MigrateDB()
  1239. xrayStopped = false
  1240. if err = s.RestartXrayService(); err != nil {
  1241. return common.NewErrorf("Imported DB but failed to start Xray: %v", err)
  1242. }
  1243. return nil
  1244. }
  1245. // pgConnEnv turns the configured PostgreSQL DSN into the PG* environment used by
  1246. // pg_dump/pg_restore, keeping the password out of the process argument list.
  1247. func pgConnEnv(dsn string) (env []string, dbname string, err error) {
  1248. u, err := url.Parse(strings.TrimSpace(dsn))
  1249. if err != nil {
  1250. return nil, "", err
  1251. }
  1252. if u.Scheme != "postgres" && u.Scheme != "postgresql" {
  1253. return nil, "", common.NewErrorf("unsupported DSN scheme %q", u.Scheme)
  1254. }
  1255. dbname = strings.TrimPrefix(u.Path, "/")
  1256. if dbname == "" {
  1257. return nil, "", common.NewError("PostgreSQL DSN is missing a database name")
  1258. }
  1259. host := u.Hostname()
  1260. if host == "" {
  1261. host = "127.0.0.1"
  1262. }
  1263. port := u.Port()
  1264. if port == "" {
  1265. port = "5432"
  1266. }
  1267. env = append(os.Environ(), "PGHOST="+host, "PGPORT="+port, "PGDATABASE="+dbname)
  1268. if user := u.User.Username(); user != "" {
  1269. env = append(env, "PGUSER="+user)
  1270. }
  1271. if pass, ok := u.User.Password(); ok {
  1272. env = append(env, "PGPASSWORD="+pass)
  1273. }
  1274. if sslmode := u.Query().Get("sslmode"); sslmode != "" {
  1275. env = append(env, "PGSSLMODE="+sslmode)
  1276. }
  1277. return env, dbname, nil
  1278. }
  1279. func (s *ServerService) exportPostgresDB() ([]byte, error) {
  1280. bin, err := exec.LookPath("pg_dump")
  1281. if err != nil {
  1282. return nil, common.NewError("pg_dump not found on the server; install the postgresql-client package to back up a PostgreSQL database")
  1283. }
  1284. env, dbname, err := pgConnEnv(config.GetDBDSN())
  1285. if err != nil {
  1286. return nil, common.NewErrorf("invalid PostgreSQL DSN: %v", err)
  1287. }
  1288. cmd := exec.Command(bin, "--format=custom", "--no-owner", "--no-privileges", "--dbname", dbname)
  1289. cmd.Env = env
  1290. var out, stderr bytes.Buffer
  1291. cmd.Stdout = &out
  1292. cmd.Stderr = &stderr
  1293. if err := cmd.Run(); err != nil {
  1294. return nil, common.NewErrorf("pg_dump failed: %v: %s", err, strings.TrimSpace(stderr.String()))
  1295. }
  1296. return out.Bytes(), nil
  1297. }
  1298. func (s *ServerService) importPostgresDB(file multipart.File) error {
  1299. header := make([]byte, 5)
  1300. if _, err := file.ReadAt(header, 0); err != nil {
  1301. return common.NewErrorf("Error reading dump file: %v", err)
  1302. }
  1303. if string(header) != "PGDMP" {
  1304. return common.NewError("Invalid file: expected a PostgreSQL custom-format dump (.dump) created by this panel's Back Up")
  1305. }
  1306. if _, err := file.Seek(0, 0); err != nil {
  1307. return common.NewErrorf("Error resetting file reader: %v", err)
  1308. }
  1309. bin, err := exec.LookPath("pg_restore")
  1310. if err != nil {
  1311. return common.NewError("pg_restore not found on the server; install the postgresql-client package to restore a PostgreSQL database")
  1312. }
  1313. env, dbname, err := pgConnEnv(config.GetDBDSN())
  1314. if err != nil {
  1315. return common.NewErrorf("invalid PostgreSQL DSN: %v", err)
  1316. }
  1317. tempFile, err := os.CreateTemp("", "x-ui-pg-restore-*.dump")
  1318. if err != nil {
  1319. return common.NewErrorf("Error creating temporary dump file: %v", err)
  1320. }
  1321. tempPath := tempFile.Name()
  1322. defer os.Remove(tempPath)
  1323. if _, err := io.Copy(tempFile, file); err != nil {
  1324. tempFile.Close()
  1325. return common.NewErrorf("Error saving dump: %v", err)
  1326. }
  1327. if err := tempFile.Close(); err != nil {
  1328. return common.NewErrorf("Error closing temporary dump file: %v", err)
  1329. }
  1330. xrayStopped := true
  1331. defer func() {
  1332. if xrayStopped {
  1333. if errR := s.RestartXrayService(); errR != nil {
  1334. logger.Warningf("Failed to restart Xray after DB restore error: %v", errR)
  1335. }
  1336. }
  1337. }()
  1338. if errStop := s.StopXrayService(); errStop != nil {
  1339. logger.Warningf("Failed to stop Xray before DB restore: %v", errStop)
  1340. }
  1341. if errClose := database.CloseDB(); errClose != nil {
  1342. logger.Warningf("Failed to close existing DB before restore: %v", errClose)
  1343. }
  1344. cmd := exec.Command(bin,
  1345. "--clean", "--if-exists", "--no-owner", "--no-privileges",
  1346. "--single-transaction", "--dbname", dbname, tempPath,
  1347. )
  1348. cmd.Env = env
  1349. var stderr bytes.Buffer
  1350. cmd.Stderr = &stderr
  1351. runErr := cmd.Run()
  1352. if errInit := database.InitDB(config.GetDBPath()); errInit != nil {
  1353. return common.NewErrorf("Restore finished but reopening the database failed: %v", errInit)
  1354. }
  1355. s.inboundService.MigrateDB()
  1356. if runErr != nil {
  1357. return common.NewErrorf("pg_restore failed (database left unchanged): %v: %s", runErr, strings.TrimSpace(stderr.String()))
  1358. }
  1359. xrayStopped = false
  1360. if err := s.RestartXrayService(); err != nil {
  1361. return common.NewErrorf("Restored DB but failed to start Xray: %v", err)
  1362. }
  1363. return nil
  1364. }
  1365. // IsValidGeofileName validates that the filename is safe for geofile operations.
  1366. // It checks for path traversal attempts and ensures the filename contains only safe characters.
  1367. func (s *ServerService) IsValidGeofileName(filename string) bool {
  1368. if filename == "" {
  1369. return false
  1370. }
  1371. // Check for path traversal attempts
  1372. if strings.Contains(filename, "..") {
  1373. return false
  1374. }
  1375. // Check for path separators (both forward and backward slash)
  1376. if strings.ContainsAny(filename, `/\`) {
  1377. return false
  1378. }
  1379. // Check for absolute path indicators
  1380. if filepath.IsAbs(filename) {
  1381. return false
  1382. }
  1383. // Additional security: only allow alphanumeric, dots, underscores, and hyphens
  1384. // This is stricter than the general filename regex
  1385. validGeofilePattern := `^[a-zA-Z0-9._-]+\.dat$`
  1386. matched, _ := regexp.MatchString(validGeofilePattern, filename)
  1387. return matched
  1388. }
  1389. func (s *ServerService) UpdateGeofile(fileName string) error {
  1390. type geofileEntry struct {
  1391. URL string
  1392. FileName string
  1393. }
  1394. geofileAllowlist := map[string]geofileEntry{
  1395. "geoip.dat": {"https://github.com/Loyalsoldier/v2ray-rules-dat/releases/latest/download/geoip.dat", "geoip.dat"},
  1396. "geosite.dat": {"https://github.com/Loyalsoldier/v2ray-rules-dat/releases/latest/download/geosite.dat", "geosite.dat"},
  1397. "geoip_IR.dat": {"https://github.com/chocolate4u/Iran-v2ray-rules/releases/latest/download/geoip.dat", "geoip_IR.dat"},
  1398. "geosite_IR.dat": {"https://github.com/chocolate4u/Iran-v2ray-rules/releases/latest/download/geosite.dat", "geosite_IR.dat"},
  1399. "geoip_RU.dat": {"https://github.com/runetfreedom/russia-v2ray-rules-dat/releases/latest/download/geoip.dat", "geoip_RU.dat"},
  1400. "geosite_RU.dat": {"https://github.com/runetfreedom/russia-v2ray-rules-dat/releases/latest/download/geosite.dat", "geosite_RU.dat"},
  1401. }
  1402. // Strict allowlist check to avoid writing uncontrolled files
  1403. if fileName != "" {
  1404. if _, ok := geofileAllowlist[fileName]; !ok {
  1405. return common.NewErrorf("Invalid geofile name: %q not in allowlist", fileName)
  1406. }
  1407. }
  1408. client := s.settingService.NewProxiedHTTPClient(0)
  1409. downloadFile := func(url, destPath string) error {
  1410. var req *http.Request
  1411. req, err := http.NewRequest("GET", url, nil)
  1412. if err != nil {
  1413. return common.NewErrorf("Failed to create HTTP request for %s: %v", url, err)
  1414. }
  1415. var localFileModTime time.Time
  1416. if fileInfo, err := os.Stat(destPath); err == nil {
  1417. localFileModTime = fileInfo.ModTime()
  1418. if !localFileModTime.IsZero() {
  1419. req.Header.Set("If-Modified-Since", localFileModTime.UTC().Format(http.TimeFormat))
  1420. }
  1421. }
  1422. resp, err := client.Do(req)
  1423. if err != nil {
  1424. return common.NewErrorf("Failed to download Geofile from %s: %v", url, err)
  1425. }
  1426. defer resp.Body.Close()
  1427. // Parse Last-Modified header from server
  1428. var serverModTime time.Time
  1429. serverModTimeStr := resp.Header.Get("Last-Modified")
  1430. if serverModTimeStr != "" {
  1431. parsedTime, err := time.Parse(http.TimeFormat, serverModTimeStr)
  1432. if err != nil {
  1433. logger.Warningf("Failed to parse Last-Modified header for %s: %v", url, err)
  1434. } else {
  1435. serverModTime = parsedTime
  1436. }
  1437. }
  1438. // Function to update local file's modification time
  1439. updateFileModTime := func() {
  1440. if !serverModTime.IsZero() {
  1441. if err := os.Chtimes(destPath, serverModTime, serverModTime); err != nil {
  1442. logger.Warningf("Failed to update modification time for %s: %v", destPath, err)
  1443. }
  1444. }
  1445. }
  1446. // Handle 304 Not Modified
  1447. if resp.StatusCode == http.StatusNotModified {
  1448. updateFileModTime()
  1449. return nil
  1450. }
  1451. if resp.StatusCode != http.StatusOK {
  1452. return common.NewErrorf("Failed to download Geofile from %s: received status code %d", url, resp.StatusCode)
  1453. }
  1454. file, err := os.Create(destPath)
  1455. if err != nil {
  1456. return common.NewErrorf("Failed to create Geofile %s: %v", destPath, err)
  1457. }
  1458. defer file.Close()
  1459. _, err = io.Copy(file, resp.Body)
  1460. if err != nil {
  1461. return common.NewErrorf("Failed to save Geofile %s: %v", destPath, err)
  1462. }
  1463. updateFileModTime()
  1464. return nil
  1465. }
  1466. var errorMessages []string
  1467. if fileName == "" {
  1468. // Download all geofiles
  1469. for _, entry := range geofileAllowlist {
  1470. destPath := filepath.Join(config.GetBinFolderPath(), entry.FileName)
  1471. if err := downloadFile(entry.URL, destPath); err != nil {
  1472. errorMessages = append(errorMessages, fmt.Sprintf("Error downloading Geofile '%s': %v", entry.FileName, err))
  1473. }
  1474. }
  1475. } else {
  1476. entry := geofileAllowlist[fileName]
  1477. destPath := filepath.Join(config.GetBinFolderPath(), entry.FileName)
  1478. if err := downloadFile(entry.URL, destPath); err != nil {
  1479. errorMessages = append(errorMessages, fmt.Sprintf("Error downloading Geofile '%s': %v", entry.FileName, err))
  1480. }
  1481. }
  1482. err := s.RestartXrayService()
  1483. if err != nil {
  1484. errorMessages = append(errorMessages, fmt.Sprintf("Updated Geofile '%s' but Failed to start Xray: %v", fileName, err))
  1485. }
  1486. if len(errorMessages) > 0 {
  1487. return common.NewErrorf("%s", strings.Join(errorMessages, "\r\n"))
  1488. }
  1489. return nil
  1490. }
  1491. func (s *ServerService) GetNewX25519Cert() (any, error) {
  1492. // Run the command
  1493. cmd := exec.Command(xray.GetBinaryPath(), "x25519")
  1494. var out bytes.Buffer
  1495. cmd.Stdout = &out
  1496. err := cmd.Run()
  1497. if err != nil {
  1498. return nil, err
  1499. }
  1500. lines := strings.Split(out.String(), "\n")
  1501. privateKeyLine := strings.Split(lines[0], ":")
  1502. publicKeyLine := strings.Split(lines[1], ":")
  1503. privateKey := strings.TrimSpace(privateKeyLine[1])
  1504. publicKey := strings.TrimSpace(publicKeyLine[1])
  1505. keyPair := map[string]any{
  1506. "privateKey": privateKey,
  1507. "publicKey": publicKey,
  1508. }
  1509. return keyPair, nil
  1510. }
  1511. func (s *ServerService) GetNewmldsa65() (any, error) {
  1512. // Run the command
  1513. cmd := exec.Command(xray.GetBinaryPath(), "mldsa65")
  1514. var out bytes.Buffer
  1515. cmd.Stdout = &out
  1516. err := cmd.Run()
  1517. if err != nil {
  1518. return nil, err
  1519. }
  1520. lines := strings.Split(out.String(), "\n")
  1521. SeedLine := strings.Split(lines[0], ":")
  1522. VerifyLine := strings.Split(lines[1], ":")
  1523. seed := strings.TrimSpace(SeedLine[1])
  1524. verify := strings.TrimSpace(VerifyLine[1])
  1525. keyPair := map[string]any{
  1526. "seed": seed,
  1527. "verify": verify,
  1528. }
  1529. return keyPair, nil
  1530. }
  1531. // GetCertHash parses a certificate (from a file path or inline PEM/DER content)
  1532. // and returns the hex-encoded SHA-256 over each certificate's raw DER — the
  1533. // value xray-core's pinnedPeerCertSha256 (pcs) expects. Lets the panel fill the
  1534. // pinned-cert field from the inbound's own certificate without the user
  1535. // computing the hash by hand.
  1536. func (s *ServerService) GetCertHash(certFile string, certContent string) ([]string, error) {
  1537. var certBytes []byte
  1538. if path := strings.TrimSpace(certFile); path != "" {
  1539. b, err := os.ReadFile(path)
  1540. if err != nil {
  1541. return nil, err
  1542. }
  1543. certBytes = b
  1544. } else if strings.TrimSpace(certContent) != "" {
  1545. certBytes = []byte(certContent)
  1546. } else {
  1547. return nil, common.NewError("no certificate provided")
  1548. }
  1549. var certs []*x509.Certificate
  1550. if bytes.Contains(certBytes, []byte("BEGIN")) {
  1551. rest := certBytes
  1552. for {
  1553. block, remain := pem.Decode(rest)
  1554. if block == nil {
  1555. break
  1556. }
  1557. cert, err := x509.ParseCertificate(block.Bytes)
  1558. if err != nil {
  1559. return nil, common.NewError("unable to decode certificate: ", err)
  1560. }
  1561. certs = append(certs, cert)
  1562. rest = remain
  1563. }
  1564. } else {
  1565. parsed, err := x509.ParseCertificates(certBytes)
  1566. if err != nil {
  1567. return nil, common.NewError("unable to parse certificates: ", err)
  1568. }
  1569. certs = parsed
  1570. }
  1571. if len(certs) == 0 {
  1572. return nil, common.NewError("no certificates found")
  1573. }
  1574. hashes := make([]string, 0, len(certs))
  1575. for _, cert := range certs {
  1576. sum := sha256.Sum256(cert.Raw)
  1577. hashes = append(hashes, hex.EncodeToString(sum[:]))
  1578. }
  1579. return hashes, nil
  1580. }
  1581. // GetRemoteCertHash runs `xray tls ping <server>` to fetch the live certificate
  1582. // SHA-256 of a remote endpoint — the value to put in pinnedPeerCertSha256 (pcs)
  1583. // when pinning a server whose certificate file you don't hold (a CDN front, a
  1584. // REALITY dest, an external proxy). Returns the unique leaf-certificate hashes.
  1585. func (s *ServerService) GetRemoteCertHash(server string) ([]string, error) {
  1586. server = strings.TrimSpace(server)
  1587. if server == "" {
  1588. return nil, common.NewError("no server provided")
  1589. }
  1590. ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
  1591. defer cancel()
  1592. cmd := exec.CommandContext(ctx, xray.GetBinaryPath(), "tls", "ping", server)
  1593. var out bytes.Buffer
  1594. cmd.Stdout = &out
  1595. cmd.Stderr = &out
  1596. if err := cmd.Run(); err != nil && out.Len() == 0 {
  1597. return nil, err
  1598. }
  1599. hexRe := regexp.MustCompile(`[0-9a-fA-F]{64}`)
  1600. seen := make(map[string]struct{})
  1601. var leaves []string
  1602. for _, line := range strings.Split(out.String(), "\n") {
  1603. if !strings.Contains(line, "leaf SHA256") {
  1604. continue
  1605. }
  1606. hash := strings.ToLower(hexRe.FindString(line))
  1607. if hash == "" {
  1608. continue
  1609. }
  1610. if _, ok := seen[hash]; !ok {
  1611. seen[hash] = struct{}{}
  1612. leaves = append(leaves, hash)
  1613. }
  1614. }
  1615. if len(leaves) == 0 {
  1616. return nil, common.NewError("no certificate hash found for ", server)
  1617. }
  1618. return leaves, nil
  1619. }
  1620. func (s *ServerService) GetNewEchCert(sni string) (any, error) {
  1621. // Run the command
  1622. cmd := exec.Command(xray.GetBinaryPath(), "tls", "ech", "--serverName", sni)
  1623. var out bytes.Buffer
  1624. cmd.Stdout = &out
  1625. err := cmd.Run()
  1626. if err != nil {
  1627. return nil, err
  1628. }
  1629. lines := strings.Split(out.String(), "\n")
  1630. if len(lines) < 4 {
  1631. return nil, common.NewError("invalid ech cert")
  1632. }
  1633. configList := lines[1]
  1634. serverKeys := lines[3]
  1635. return map[string]any{
  1636. "echServerKeys": serverKeys,
  1637. "echConfigList": configList,
  1638. }, nil
  1639. }
  1640. func (s *ServerService) GetNewVlessEnc() (any, error) {
  1641. cmd := exec.Command(xray.GetBinaryPath(), "vlessenc")
  1642. var out bytes.Buffer
  1643. cmd.Stdout = &out
  1644. if err := cmd.Run(); err != nil {
  1645. return nil, err
  1646. }
  1647. return map[string]any{
  1648. "auths": parseVlessEncAuths(out.String()),
  1649. }, nil
  1650. }
  1651. func parseVlessEncAuths(output string) []map[string]string {
  1652. lines := strings.Split(output, "\n")
  1653. var auths []map[string]string
  1654. var current map[string]string
  1655. for _, line := range lines {
  1656. line = strings.TrimSpace(line)
  1657. if strings.HasPrefix(line, "Authentication:") {
  1658. if current != nil {
  1659. auths = append(auths, current)
  1660. }
  1661. label := strings.TrimSpace(strings.TrimPrefix(line, "Authentication:"))
  1662. current = map[string]string{
  1663. "id": vlessEncAuthID(label),
  1664. "label": label,
  1665. }
  1666. } else if strings.HasPrefix(line, `"decryption"`) || strings.HasPrefix(line, `"encryption"`) {
  1667. parts := strings.SplitN(line, ":", 2)
  1668. if len(parts) == 2 && current != nil {
  1669. key := strings.Trim(parts[0], `" `)
  1670. val := strings.TrimSpace(parts[1])
  1671. val = strings.TrimSuffix(val, ",")
  1672. val = strings.Trim(val, `" `)
  1673. current[key] = val
  1674. }
  1675. }
  1676. }
  1677. if current != nil {
  1678. auths = append(auths, current)
  1679. }
  1680. return auths
  1681. }
  1682. func vlessEncAuthID(label string) string {
  1683. normalized := strings.NewReplacer("-", "", "_", "", " ", "").Replace(strings.ToLower(label))
  1684. switch {
  1685. case strings.Contains(normalized, "mlkem768"):
  1686. return "mlkem768"
  1687. case strings.Contains(normalized, "x25519"):
  1688. return "x25519"
  1689. default:
  1690. return normalized
  1691. }
  1692. }
  1693. func (s *ServerService) GetNewUUID() (map[string]string, error) {
  1694. newUUID, err := uuid.NewRandom()
  1695. if err != nil {
  1696. return nil, fmt.Errorf("failed to generate UUID: %w", err)
  1697. }
  1698. return map[string]string{
  1699. "uuid": newUUID.String(),
  1700. }, nil
  1701. }
  1702. func (s *ServerService) GetNewmlkem768() (any, error) {
  1703. // Run the command
  1704. cmd := exec.Command(xray.GetBinaryPath(), "mlkem768")
  1705. var out bytes.Buffer
  1706. cmd.Stdout = &out
  1707. err := cmd.Run()
  1708. if err != nil {
  1709. return nil, err
  1710. }
  1711. lines := strings.Split(out.String(), "\n")
  1712. SeedLine := strings.Split(lines[0], ":")
  1713. ClientLine := strings.Split(lines[1], ":")
  1714. seed := strings.TrimSpace(SeedLine[1])
  1715. client := strings.TrimSpace(ClientLine[1])
  1716. keyPair := map[string]any{
  1717. "seed": seed,
  1718. "client": client,
  1719. }
  1720. return keyPair, nil
  1721. }