From e00c3f1823a4ff80daf8290e2dfec4474e7f9173 Mon Sep 17 00:00:00 2001 From: MHSanaei Date: Wed, 9 Aug 2023 00:37:05 +0330 Subject: [PATCH] add panel usage to main page --- web/html/xui/index.html | 43 ++++++++++++++++++++++------------ web/job/check_client_ip_job.go | 3 +-- web/service/server.go | 15 ++++++++++++ xray/process.go | 19 ++++++++++----- 4 files changed, 57 insertions(+), 23 deletions(-) diff --git a/web/html/xui/index.html b/web/html/xui/index.html index 0942b0ad..62e60297 100644 --- a/web/html/xui/index.html +++ b/web/html/xui/index.html @@ -108,21 +108,30 @@ - - - {{ i18n "pages.index.systemLoad" }}: [[ status.loads[0] ]] | [[ status.loads[1] ]] | [[ status.loads[2] ]] - - - - - - - {{ i18n "pages.index.operationHours" }}: - [[ formatSecond(status.uptime) ]] - - + {{ i18n "pages.index.operationHours" }}: + Xray: + [[ formatSecond(status.appStats.uptime) ]] + OS: + [[ formatSecond(status.uptime) ]] + + + + + {{ i18n "pages.index.systemLoad" }}: [[ status.loads[0] ]] | [[ status.loads[1] ]] | [[ status.loads[2] ]] + + + + + + + + + {{ i18n "usage"}}: + Memory [[ sizeFormat(status.appStats.mem) ]] - + Threads [[ status.appStats.threads ]] + @@ -361,6 +370,8 @@ this.tcpCount = 0; this.udpCount = 0; this.uptime = 0; + this.appUptime = 0; + this.appStats = {threads: 0, mem: 0, uptime: 0}; this.xray = { state: State.Stop, errorMsg: "", version: "", color: "" }; if (data == null) { @@ -379,6 +390,8 @@ this.tcpCount = data.tcpCount; this.udpCount = data.udpCount; this.uptime = data.uptime; + this.appUptime = data.appUptime; + this.appStats = data.appStats; this.xray = data.xray; switch (this.xray.state) { case State.Running: diff --git a/web/job/check_client_ip_job.go b/web/job/check_client_ip_job.go index 1c5dec83..74ef3e09 100644 --- a/web/job/check_client_ip_job.go +++ b/web/job/check_client_ip_job.go @@ -15,7 +15,7 @@ import ( "x-ui/xray" ) -type CheckClientIpJob struct {} +type CheckClientIpJob struct{} var job *CheckClientIpJob var disAllowedIps []string @@ -31,7 +31,6 @@ func NewCheckClientIpJob() *CheckClientIpJob { } func (j *CheckClientIpJob) Run() { - logger.Debug("Check Client IP Job...") // create files required for iplimit if not exists for i := 0; i < len(ipFiles); i++ { diff --git a/web/service/server.go b/web/service/server.go index 6319cab7..a56be10d 100644 --- a/web/service/server.go +++ b/web/service/server.go @@ -77,6 +77,11 @@ type Status struct { IPv4 string `json:"ipv4"` IPv6 string `json:"ipv6"` } `json:"publicIP"` + AppStats struct { + Threads uint32 `json:"threads"` + Mem uint64 `json:"mem"` + Uptime uint64 `json:"uptime"` + } `json:"appStats"` } type Release struct { @@ -220,6 +225,16 @@ func (s *ServerService) GetStatus(lastStatus *Status) *Status { status.Xray.ErrorMsg = s.xrayService.GetXrayResult() } status.Xray.Version = s.xrayService.GetXrayVersion() + var rtm runtime.MemStats + runtime.ReadMemStats(&rtm) + + status.AppStats.Mem = rtm.Sys + status.AppStats.Threads = uint32(runtime.NumGoroutine()) + if p.IsRunning() { + status.AppStats.Uptime = p.GetUptime() + } else { + status.AppStats.Uptime = 0 + } return status } diff --git a/xray/process.go b/xray/process.go index b860360f..315d3ed0 100644 --- a/xray/process.go +++ b/xray/process.go @@ -13,6 +13,7 @@ import ( "strings" "sync" "syscall" + "time" "x-ui/config" "x-ui/logger" @@ -101,16 +102,18 @@ type process struct { version string apiPort int - config *Config - lines *queue.Queue - exitErr error + config *Config + lines *queue.Queue + exitErr error + startTime time.Time } func newProcess(config *Config) *process { return &process{ - version: "Unknown", - config: config, - lines: queue.New(100), + version: "Unknown", + config: config, + lines: queue.New(100), + startTime: time.Now(), } } @@ -154,6 +157,10 @@ func (p *Process) GetConfig() *Config { return p.config } +func (p *Process) GetUptime() uint64 { + return uint64(time.Since(p.startTime).Seconds()) +} + func (p *process) refreshAPIPort() { for _, inbound := range p.config.InboundConfigs { if inbound.Tag == "api" {