diff --git a/component/session/server.go b/component/session/server.go index 65911af..1ac8e5d 100644 --- a/component/session/server.go +++ b/component/session/server.go @@ -24,6 +24,10 @@ var ( type Server struct { *http.Server + + trafficUp int64 + trafficDown int64 + activeSessionMap sync.Map completedSessionQueue *queue.Queue } @@ -45,8 +49,8 @@ func (s *Server) handler(resp http.ResponseWriter, req *http.Request) { // Slice of completed sessions var completedSessions []*Session for _, item := range s.completedSessionQueue.Copy() { - if sess, ok := item.(*Session); ok { - completedSessions = append(completedSessions, sess) + if session, ok := item.(*Session); ok { + completedSessions = append(completedSessions, session) } } @@ -61,17 +65,17 @@ func (s *Server) handler(resp http.ResponseWriter, req *http.Request) { return sessions[i].SessionStart.After(sessions[j].SessionStart) }) - for _, sess := range sessions { + for _, session := range sessions { _, _ = fmt.Fprintf(w, "%v%v%v%v%v%v%v%v", - sess.Process, - sess.Network, - date(sess.SessionStart), - duration(sess.SessionStart, sess.SessionClose), - // sess.DialerAddr, - sess.ClientAddr, - sess.TargetAddr, - byteCountSI(atomic.LoadInt64(&sess.UploadBytes)), - byteCountSI(atomic.LoadInt64(&sess.DownloadBytes)), + session.Process, + session.Network, + date(session.SessionStart), + duration(session.SessionStart, session.SessionClose), + // session.DialerAddr, + session.ClientAddr, + session.TargetAddr, + byteCountSI(atomic.LoadInt64(&session.UploadBytes)), + byteCountSI(atomic.LoadInt64(&session.DownloadBytes)), ) } _, _ = fmt.Fprintf(w, "") @@ -88,10 +92,17 @@ table, th, td { }Go-tun2socks Sessions`) _, _ = fmt.Fprintf(w, "

Go-tun2socks %s

", C.Version) _, _ = fmt.Fprintf(w, "

Now: %s ; Uptime: %s

", now(), uptime()) - _, _ = fmt.Fprintf(w, "

Active sessions %d

", len(activeSessions)) + _, _ = fmt.Fprintf(w, "

Traffic

") + trafficUp := atomic.LoadInt64(&s.trafficUp) + trafficDown := atomic.LoadInt64(&s.trafficDown) + _, _ = fmt.Fprintf(w, "Total%sUp%sDown%s", + byteCountSI(trafficUp+trafficDown), + byteCountSI(trafficUp), + byteCountSI(trafficDown), + ) + _, _ = fmt.Fprintf(w, "

Active sessions: %d

", len(activeSessions)) tablePrint(w, activeSessions) - _, _ = fmt.Fprintf(w, "

") - _, _ = fmt.Fprintf(w, "

Recently completed sessions %d

", len(completedSessions)) + _, _ = fmt.Fprintf(w, "

Recently completed sessions: %d

", len(completedSessions)) tablePrint(w, completedSessions) _, _ = fmt.Fprintf(w, "") _ = w.Flush() @@ -131,17 +142,23 @@ func (s *Server) Stop() error { } func (s *Server) AddSession(key interface{}, session *Session) { - s.activeSessionMap.Store(key, session) + if session != nil { + s.activeSessionMap.Store(key, session) + } } func (s *Server) RemoveSession(key interface{}) { - if sess, ok := s.activeSessionMap.Load(key); ok { + if item, ok := s.activeSessionMap.Load(key); ok { + session := item.(*Session) + // delete first + s.activeSessionMap.Delete(key) + // record up & down traffic + atomic.AddInt64(&s.trafficUp, atomic.LoadInt64(&session.UploadBytes)) + atomic.AddInt64(&s.trafficDown, atomic.LoadInt64(&session.DownloadBytes)) // move to completed sessions - s.completedSessionQueue.Put(sess) + s.completedSessionQueue.Put(session) if s.completedSessionQueue.Len() > maxCompletedSessions { s.completedSessionQueue.Pop() } - // delete - s.activeSessionMap.Delete(key) } }