From 59e098ded5804f56d75db37a94d782c9a96e5593 Mon Sep 17 00:00:00 2001 From: e1732a364fed <75717694+e1732a364fed@users.noreply.github.com> Date: Sat, 1 Jan 2000 00:00:00 +0000 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E8=AE=A2=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- machine/machine.go | 42 ++++++++++++++++++++++++++++++++++-------- utils/strings.go | 11 +++++++++++ 2 files changed, 45 insertions(+), 8 deletions(-) diff --git a/machine/machine.go b/machine/machine.go index c6a84d3..bd647d7 100644 --- a/machine/machine.go +++ b/machine/machine.go @@ -15,6 +15,7 @@ import ( "sync" "time" + "github.com/dustin/go-humanize" "github.com/e1732a364fed/v2ray_simple" "github.com/e1732a364fed/v2ray_simple/httpLayer" "github.com/e1732a364fed/v2ray_simple/proxy" @@ -49,7 +50,7 @@ type M struct { callbacks - ticker *time.Ticker + stateReportTicker *time.Ticker } func New() *M { @@ -107,11 +108,17 @@ func (m *M) Start() { dm.StartListen() } - if m.ticker == nil { - m.ticker = time.NewTicker(time.Minute * 5) //每隔五分钟输出一次目前状态 + if m.stateReportTicker == nil { + m.stateReportTicker = time.NewTicker(time.Minute * 5) //每隔五分钟输出一次目前状态 + + var sw = utils.PrefixWriter{ + Writer: os.Stdout, + } go func() { - for range m.ticker.C { - m.PrintAllState(os.Stdout) + for range m.stateReportTicker.C { + sw.Prefix = []byte(time.Now().Format("2006-01-02 15:04:05.999 ")) + sw.Write([]byte("Current state:\n")) + m.PrintAllStateForHuman(&sw) } }() } @@ -153,9 +160,9 @@ func (m *M) Stop() { if dm := m.routingEnv.DnsMachine; dm != nil { dm.Stop() } - if m.ticker != nil { - m.ticker.Stop() - m.ticker = nil + if m.stateReportTicker != nil { + m.stateReportTicker.Stop() + m.stateReportTicker = nil } m.Unlock() } @@ -210,3 +217,22 @@ func (m *M) PrintAllState(w io.Writer) { } } + +// mimic PrintAllState +func (m *M) PrintAllStateForHuman(w io.Writer) { + if w == nil { + w = os.Stdout + } + fmt.Fprintln(w, "activeConnectionCount", m.ActiveConnectionCount) + fmt.Fprintln(w, "allDownloadBytesSinceStart", humanize.Bytes(m.AllDownloadBytesSinceStart)) + fmt.Fprintln(w, "allUploadBytesSinceStart", humanize.Bytes(m.AllUploadBytesSinceStart)) + + for i, s := range m.allServers { + fmt.Fprintln(w, "inServer", i, proxy.GetVSI_url(s, "")) + + } + for i, c := range m.allClients { + fmt.Fprintln(w, "outClient", i, proxy.GetVSI_url(c, "")) + } + +} diff --git a/utils/strings.go b/utils/strings.go index 7e4a703..2e74d18 100644 --- a/utils/strings.go +++ b/utils/strings.go @@ -2,6 +2,7 @@ package utils import ( "bytes" + "io" "math/rand" "os" "strings" @@ -89,3 +90,13 @@ func GetPurgedTomlBytes(v any) ([]byte, error) { return sb.Bytes(), nil } + +type PrefixWriter struct { + io.Writer + Prefix []byte +} + +func (lw *PrefixWriter) Write(p []byte) (n int, err error) { + lw.Writer.Write(lw.Prefix) + return lw.Writer.Write(p) +}