A TCP connection monitoring tool written in Go.
A TCP connection monitoring tool written in Go.
This is a tool(a library and a command line) for monitoring and debugging network services.
I built this for the purpose of facilitating debugging and monitoring the network connections of IoT devices. At this stage, it is just an experimental tool.
Note: For each tcp connection, an additional goroutine is used for monitoring, and uses a MonConn struct to store the monitored info, which takes up a bit more memory. For 5000 long connections, it takes up about 80~100MB of memory.
So it's not recommended to use it in high performance production environment.
features
library:
go get -u -v github.com/gihnius/monconn
command line:
go get -u -v github.com/gihnius/monconn/cli/monconn
then run: monconn -h
import "github.com/gihnius/monconn"
// --------------- global setup
monconn.Debug = true // enable debug log
monconn.DebugFunc = func(format string, v ...interface{}) {
// custom log function
}
// set net.Conn buffer size, default 4k
monconn.ReadBufSize = 4 << 10
monconn.WriteBufSize = 4 << 10
// set how many concurrency clients(ip) can connect to a service
// default 64
monconn.MaxIPLimit = 1000
// --------------- service setup
// set net.Conn read write timeout
service.ReadTimeout = 600
service.WriteTimeout = 600
// close wait timeout
service.WaitTimeout = 60
// max idle check in seconds
service.MaxIdle = 900
// connections count to a service, default 0, no limit
service.ConnLimit = 0 // ConnLimit changes need to restart service
// ips count to service, default 0, no limit
service.IPLimit = 0
// set tcp connection keepalive, default true
service.KeepAlive = true
// Print read write bytes in hex format, default false
service.PrintBytes = false
please checkout monconn/cli/monconn/config.yaml
.
simple usage as a port forwarder:
# use as ssh and web frontend to monitor traffics
$ monconn -p :22=127.0.0.1:2222 -p 0.0.0.0:80=127.0.0.1:8080
# here 2222 and 8080 are the real ssh and web services
more configurable options:
# monconn config.yaml
service_1: # service name
# service listen on
listen_ip: "127.0.0.1"
# monconn listen on 80
listen_port: 80
# forward requests to this backend
backend_ip: "127.0.0.1"
# the real web listen on 8080
backend_port: 8080
# service config
read_timeout: 600 # 10 minutes
write_timeout: 600 # 10 minutes
wait_timeout: 60 # 1 minute
max_idle: 900 # 15 minutes
conn_limit: 0 # zero means no limit
ip_limit: 0 # zero means no limit
keepalive: true
print_bytes: true
ip_blacklist:
- 1.1.1.1
- 2.2.2.2
// create service by given a sid: "127.0.0.1:1234"
// a service is binding to a tcp listener, so normally
// use a listen address as sid to identify a service
// you can also choose a uniqe string as sid.
service := monconn.NewService("127.0.0.1:1234")
// configure service like above
// service.ReadTimeout = ...
// listen a tcp port
// start the service monitor on ln
service.Listen("tcp", "127.0.0.1:1234")
// accept connection and monitor the connection
conn, err := service.AcquireConn()
if err != nil {
// handle err
} else {
go HandleConn(conn)
}
// when everything done, usually before program exit,
// call service.Stop() to stop the listener as well as service
service.Close()
// or call Shutdown() to Stop all services if there are multiple started.
monconn.Shutdown()
// or pls checkout the example code in examples/
// checkout the API to see how to grab the monitored infomation.
// ... that's all
see godoc
In development (alpha).