Browse Source

add connections track

pull/15/head
Jason 5 years ago
parent
commit
b5fe8489e9
  1. 2
      cmd/tun2socks/main.go
  2. 3
      cmd/tun2socks/main_socks.go
  3. 2
      cmd/tun2socks/main_stats.go
  4. 4
      common/stats/session/session.go
  5. 9
      proxy/socks/tcp.go

2
cmd/tun2socks/main.go

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

3
cmd/tun2socks/main_socks.go

@ -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 {

2
cmd/tun2socks/main_stats.go

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

4
common/stats/session/session.go

@ -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))

9
proxy/socks/tcp.go

@ -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
}

Loading…
Cancel
Save