add connections track

This commit is contained in:
Jason
2019-08-10 19:00:18 +08:00
parent 870059087c
commit b5fe8489e9
5 changed files with 19 additions and 1 deletions

View File

@@ -21,6 +21,8 @@ import (
const MTU = 1500
var tcpConnections int64 = 0
var (
version = "unknown version"
description = "A tun2socks implementation written in Go."

View File

@@ -17,6 +17,9 @@ func init() {
args.UdpTimeout = flag.Duration("udpTimeout", 1*time.Minute, "UDP session timeout")
registerHandlerCreator("socks", func() {
//
socks.ActiveConnections = &tcpConnections
// Verify proxy server address.
proxyAddr, err := net.ResolveTCPAddr("tcp", *args.ProxyServer)
if err != nil {

View File

@@ -18,6 +18,8 @@ func init() {
session.StatsAddr = *args.StatsAddr
session.StatsVersion = version
session.ActiveConnections = &tcpConnections
sessionStater = session.NewSimpleSessionStater()
sessionStater.Start()
} else {

View File

@@ -19,6 +19,8 @@ import (
const maxCompletedSessions = 100
var ActiveConnections *int64
var (
StatsAddr = "localhost:6001"
StatsPath = "/stats/session/plain"
@@ -84,7 +86,7 @@ func (s *simpleSessionStater) Start() error {
}</style><title>Go-tun2socks Sessions</title></head>`)
_, _ = fmt.Fprintf(w, "<h2>Go-tun2socks %s</h2>", StatsVersion)
_, _ = fmt.Fprintf(w, "<h3>Now: %s ; Uptime: %s</h3>", now(), uptime())
_, _ = fmt.Fprintf(w, "<p>Active sessions %d</p>", len(sessions))
_, _ = fmt.Fprintf(w, "<p>Active sessions %d (%d)</p>", len(sessions), atomic.LoadInt64(ActiveConnections))
tablePrint(w, sessions)
_, _ = fmt.Fprintf(w, "<br/><br/>")
_, _ = fmt.Fprintf(w, "<p>Recently completed sessions %d</p>", len(s.completedSessions))

View File

@@ -5,6 +5,7 @@ import (
"net"
"strconv"
"sync"
"sync/atomic"
"time"
"golang.org/x/net/proxy"
@@ -16,6 +17,8 @@ import (
"github.com/xjasonlyu/tun2socks/core"
)
var ActiveConnections *int64
type tcpHandler struct {
sync.Mutex
@@ -110,6 +113,9 @@ func (h *tcpHandler) relay(localConn, remoteConn net.Conn, sess *stats.Session)
if h.sessionStater != nil {
h.sessionStater.RemoveSession(localConn)
}
// add -1
atomic.AddInt64(ActiveConnections, -1)
}
func (h *tcpHandler) Handle(localConn net.Conn, target *net.TCPAddr) error {
@@ -163,6 +169,9 @@ func (h *tcpHandler) Handle(localConn net.Conn, target *net.TCPAddr) error {
// relay connections
go h.relay(localConn, remoteConn, sess)
// add 1
atomic.AddInt64(ActiveConnections, 1)
log.Access(process, "proxy", target.Network(), localConn.LocalAddr().String(), targetAddr)
return nil
}