diff --git a/cmd/tun2socks/main.go b/cmd/tun2socks/main.go index 5051ece..b9ab8a2 100644 --- a/cmd/tun2socks/main.go +++ b/cmd/tun2socks/main.go @@ -21,8 +21,6 @@ import ( const MTU = 1500 -var tcpConnections int64 = 0 - var ( version = "unknown version" description = "A tun2socks implementation written in Go." diff --git a/cmd/tun2socks/main_socks.go b/cmd/tun2socks/main_socks.go index 0f3c5bc..d4c2b61 100644 --- a/cmd/tun2socks/main_socks.go +++ b/cmd/tun2socks/main_socks.go @@ -17,9 +17,6 @@ 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 { diff --git a/cmd/tun2socks/main_stats.go b/cmd/tun2socks/main_stats.go index 59dda82..9a53f2f 100644 --- a/cmd/tun2socks/main_stats.go +++ b/cmd/tun2socks/main_stats.go @@ -18,8 +18,6 @@ func init() { session.StatsAddr = *args.StatsAddr session.StatsVersion = version - session.ActiveConnections = &tcpConnections - sessionStater = session.NewSimpleSessionStater() sessionStater.Start() } else { diff --git a/common/stats/session/session.go b/common/stats/session/session.go index 4dc4222..475916c 100644 --- a/common/stats/session/session.go +++ b/common/stats/session/session.go @@ -19,7 +19,7 @@ import ( const maxCompletedSessions = 100 -var ActiveConnections *int64 +var ActiveTCPConnections *int64 var ( StatsAddr = "localhost:6001" @@ -86,7 +86,7 @@ func (s *simpleSessionStater) Start() error { }
Active sessions %d (%d)
", len(sessions), atomic.LoadInt64(ActiveConnections)) + _, _ = fmt.Fprintf(w, "Active sessions %d (%d)
", len(sessions), atomic.LoadInt64(ActiveTCPConnections)) tablePrint(w, sessions) _, _ = fmt.Fprintf(w, "Recently completed sessions %d
", len(s.completedSessions)) diff --git a/proxy/socks/tcp.go b/proxy/socks/tcp.go index 35d6f62..441d00f 100644 --- a/proxy/socks/tcp.go +++ b/proxy/socks/tcp.go @@ -14,10 +14,15 @@ import ( "github.com/xjasonlyu/tun2socks/common/log" "github.com/xjasonlyu/tun2socks/common/lsof" "github.com/xjasonlyu/tun2socks/common/stats" + "github.com/xjasonlyu/tun2socks/common/stats/session" "github.com/xjasonlyu/tun2socks/core" ) -var ActiveConnections *int64 +var activeTCPConnections int64 + +func init() { + session.ActiveTCPConnections = &activeTCPConnections +} type tcpHandler struct { sync.Mutex @@ -115,7 +120,7 @@ func (h *tcpHandler) relay(localConn, remoteConn net.Conn, sess *stats.Session) } // add -1 - atomic.AddInt64(ActiveConnections, -1) + atomic.AddInt64(&activeTCPConnections, -1) } func (h *tcpHandler) Handle(localConn net.Conn, target *net.TCPAddr) error { @@ -170,7 +175,7 @@ func (h *tcpHandler) Handle(localConn net.Conn, target *net.TCPAddr) error { go h.relay(localConn, remoteConn, sess) // add 1 - atomic.AddInt64(ActiveConnections, 1) + atomic.AddInt64(&activeTCPConnections, 1) log.Access(process, "proxy", target.Network(), localConn.LocalAddr().String(), targetAddr) return nil