chore: options to show stat

This commit is contained in:
kevin
2022-07-08 23:52:28 +08:00
parent 65491a7375
commit cf4e22aeb9
6 changed files with 49 additions and 13 deletions
+3 -1
View File
@@ -17,11 +17,12 @@ const (
serverSide = "SERVER"
clientSide = "CLIENT"
useOfClosedConn = "use of closed network connection"
statInterval = time.Second * 5
)
var (
errClientCanceled = errors.New("client canceled")
stat = NewStatPrinter(time.Second * 5)
stat Stater
)
type PairedConnection struct {
@@ -107,6 +108,7 @@ func (c *PairedConnection) stop() {
}
func startListener() error {
stat = NewStatPrinter(statInterval)
go stat.Start()
conn, err := net.Listen("tcp", fmt.Sprintf("%s:%d", settings.LocalHost, settings.LocalPort))
+2 -1
View File
@@ -17,6 +17,7 @@ func main() {
remote = flag.String("r", "", "Remote address (host:port) to connect")
delay = flag.Duration("d", 0, "the delay to relay packets")
protocol = flag.String("t", "", "The type of protocol, currently support grpc")
stat = flag.Bool("s", false, "Enable statistics")
quiet = flag.Bool("q", false,
"Quiet mode, only prints connection open/close and stats, default false")
)
@@ -27,7 +28,7 @@ func main() {
}
flag.Parse()
saveSettings(*localHost, *localPort, *remote, *delay, *protocol, *quiet)
saveSettings(*localHost, *localPort, *remote, *delay, *protocol, *stat, *quiet)
if len(settings.Remote) == 0 {
fmt.Fprintln(os.Stderr, color.HiRedString("[x] Remote target required"))
+3 -1
View File
@@ -8,11 +8,12 @@ type Settings struct {
LocalPort int
Delay time.Duration
Protocol string
Stat bool
Quiet bool
}
func saveSettings(localHost string, localPort int, remote string, delay time.Duration,
protocol string, quiet bool) {
protocol string, stat, quiet bool) {
if localHost != "" {
settings.LocalHost = localHost
}
@@ -24,5 +25,6 @@ func saveSettings(localHost string, localPort int, remote string, delay time.Dur
}
settings.Delay = delay
settings.Protocol = protocol
settings.Stat = stat
settings.Quiet = quiet
}
+23
View File
@@ -0,0 +1,23 @@
//go:build !linux
package main
import (
"net"
"time"
)
type StatPrinter struct{}
func NewStatPrinter(_ time.Duration) Stater {
return StatPrinter{}
}
func (p StatPrinter) AddConn(_ string, _ *net.TCPConn) {
}
func (p StatPrinter) DelConn(_ string) {
}
func (p StatPrinter) Start() {
}
+13 -9
View File
@@ -1,5 +1,3 @@
//go:build !linux
package main
import (
@@ -7,17 +5,23 @@ import (
"time"
)
type StatPrinter struct{}
func NewStatPrinter(_ time.Duration) StatPrinter {
return StatPrinter{}
type Stater interface {
AddConn(key string, conn *net.TCPConn)
DelConn(key string)
Start()
}
func (p StatPrinter) AddConn(_ string, _ *net.TCPConn) {
type NilPrinter struct{}
func NewNilPrinter(_ time.Duration) Stater {
return NilPrinter{}
}
func (p StatPrinter) DelConn(_ string) {
func (p NilPrinter) AddConn(_ string, _ *net.TCPConn) {
}
func (p StatPrinter) Start() {
func (p NilPrinter) DelConn(_ string) {
}
func (p NilPrinter) Start() {
}
+5 -1
View File
@@ -19,7 +19,11 @@ type StatPrinter struct {
lock sync.RWMutex
}
func NewStatPrinter(duration time.Duration) *StatPrinter {
func NewStatPrinter(duration time.Duration) Stater {
if !settings.Stat {
return NilPrinter{}
}
return &StatPrinter{
duration: duration,
conns: make(map[string]*net.TCPConn),