mirror of
https://github.com/xjasonlyu/tun2socks.git
synced 2024-12-30 02:37:01 +00:00
cleanup code
This commit is contained in:
@@ -35,73 +35,80 @@ func NewSimpleSessionStater() stats.SessionStater {
|
||||
}
|
||||
}
|
||||
|
||||
func (s *simpleSessionStater) Start() {
|
||||
log.Debugf("Start session stater")
|
||||
sessionStatsHandler := func(resp http.ResponseWriter, req *http.Request) {
|
||||
// Make a snapshot.
|
||||
var activeSessions []*stats.Session
|
||||
s.activeSessionMap.Range(func(key, value interface{}) bool {
|
||||
activeSessions = append(activeSessions, value.(*stats.Session))
|
||||
return true
|
||||
func (s *simpleSessionStater) sessionStatsHandler(resp http.ResponseWriter, req *http.Request) {
|
||||
// Slice of active sessions
|
||||
var activeSessions []*stats.Session
|
||||
s.activeSessionMap.Range(func(key, value interface{}) bool {
|
||||
activeSessions = append(activeSessions, value.(*stats.Session))
|
||||
return true
|
||||
})
|
||||
|
||||
// Slice of completed sessions
|
||||
var completedSessions []*stats.Session
|
||||
for _, item := range s.completedSessionQueue.Copy() {
|
||||
if sess, ok := item.(*stats.Session); ok {
|
||||
completedSessions = append(completedSessions, sess)
|
||||
}
|
||||
}
|
||||
|
||||
tablePrint := func(w io.Writer, sessions []*stats.Session) {
|
||||
// Sort by session start time.
|
||||
sort.Slice(sessions, func(i, j int) bool {
|
||||
return sessions[i].SessionStart.Sub(sessions[j].SessionStart) < 0
|
||||
})
|
||||
_, _ = fmt.Fprintf(w, "<table style=\"border=4px solid\">")
|
||||
_, _ = fmt.Fprintf(w, "<tr><td>Process</td><td>Network</td><td>Date</td><td>Duration</td><td>Client Addr</td><td>Target Addr</td><td>Upload</td><td>Download</td></tr>")
|
||||
sort.Slice(sessions, func(i, j int) bool {
|
||||
return sessions[i].SessionStart.After(sessions[j].SessionStart)
|
||||
})
|
||||
|
||||
var completedSessions []*stats.Session
|
||||
for _, item := range s.completedSessionQueue.Copy() {
|
||||
if sess, ok := item.(*stats.Session); ok {
|
||||
completedSessions = append(completedSessions, sess)
|
||||
}
|
||||
for _, sess := range sessions {
|
||||
_, _ = fmt.Fprintf(w, "<tr><td>%v</td><td>%v</td><td>%v</td><td>%v</td><td>%v</td><td>%v</td><td>%v</td><td>%v</td></tr>",
|
||||
process(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)),
|
||||
)
|
||||
}
|
||||
_, _ = fmt.Fprintf(w, "</table>")
|
||||
}
|
||||
|
||||
tablePrint := func(w io.Writer, sessions []*stats.Session) {
|
||||
// Sort by session start time.
|
||||
sort.Slice(sessions, func(i, j int) bool {
|
||||
return sessions[i].SessionStart.Sub(sessions[j].SessionStart) < 0
|
||||
})
|
||||
_, _ = fmt.Fprintf(w, "<table style=\"border=4px solid\">")
|
||||
_, _ = fmt.Fprintf(w, "<tr><td>Process</td><td>Network</td><td>Date</td><td>Duration</td><td>Client Addr</td><td>Target Addr</td><td>Upload</td><td>Download</td></tr>")
|
||||
sort.Slice(sessions, func(i, j int) bool {
|
||||
return sessions[i].SessionStart.After(sessions[j].SessionStart)
|
||||
})
|
||||
|
||||
for _, sess := range sessions {
|
||||
_, _ = fmt.Fprintf(w, "<tr><td>%v</td><td>%v</td><td>%v</td><td>%v</td><td>%v</td><td>%v</td><td>%v</td><td>%v</td></tr>",
|
||||
process(sess.ProcessName),
|
||||
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)),
|
||||
)
|
||||
}
|
||||
_, _ = fmt.Fprintf(w, "</table>")
|
||||
}
|
||||
|
||||
w := bufio.NewWriter(resp)
|
||||
_, _ = fmt.Fprintf(w, "<html>")
|
||||
_, _ = fmt.Fprintf(w, `<head><style>table, th, td {
|
||||
w := bufio.NewWriter(resp)
|
||||
_, _ = fmt.Fprintf(w, "<html>")
|
||||
_, _ = fmt.Fprintf(w, `<head>
|
||||
<style>
|
||||
table, th, td {
|
||||
border: 1px solid black;
|
||||
border-collapse: collapse;
|
||||
text-align: right;
|
||||
padding: 4;
|
||||
}</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(activeSessions))
|
||||
tablePrint(w, activeSessions)
|
||||
_, _ = fmt.Fprintf(w, "<br/><br/>")
|
||||
_, _ = fmt.Fprintf(w, "<p>Recently completed sessions %d</p>", len(completedSessions))
|
||||
tablePrint(w, completedSessions)
|
||||
_, _ = fmt.Fprintf(w, "</html>")
|
||||
_ = w.Flush()
|
||||
}
|
||||
}</style>
|
||||
<title>Go-tun2socks Sessions</title>
|
||||
<meta http-equiv="refresh" content="1" >
|
||||
</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(activeSessions))
|
||||
tablePrint(w, activeSessions)
|
||||
_, _ = fmt.Fprintf(w, "<br/><br/>")
|
||||
_, _ = fmt.Fprintf(w, "<p>Recently completed sessions %d</p>", len(completedSessions))
|
||||
tablePrint(w, completedSessions)
|
||||
_, _ = fmt.Fprintf(w, "</html>")
|
||||
_ = w.Flush()
|
||||
}
|
||||
|
||||
func (s *simpleSessionStater) Start() {
|
||||
log.Debugf("Start session stater")
|
||||
mux := http.NewServeMux()
|
||||
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||
http.Redirect(w, r, StatsPath, 301)
|
||||
})
|
||||
mux.HandleFunc(StatsPath, sessionStatsHandler)
|
||||
mux.HandleFunc(StatsPath, s.sessionStatsHandler)
|
||||
s.server = &http.Server{Addr: StatsAddr, Handler: mux}
|
||||
go s.server.ListenAndServe()
|
||||
}
|
||||
|
||||
@@ -27,22 +27,72 @@ func process(name string) string {
|
||||
return name
|
||||
}
|
||||
|
||||
func duration(start, end time.Time) (t time.Duration) {
|
||||
func duration(start, end time.Time) string {
|
||||
var t time.Duration
|
||||
if end.IsZero() {
|
||||
t = time.Now().Sub(start)
|
||||
} else {
|
||||
t = end.Sub(start)
|
||||
}
|
||||
|
||||
if t < 1*time.Second {
|
||||
switch {
|
||||
case t < 1000*time.Millisecond:
|
||||
t = t.Round(time.Millisecond)
|
||||
} else {
|
||||
default:
|
||||
t = t.Round(time.Second)
|
||||
}
|
||||
return
|
||||
return t.String()
|
||||
}
|
||||
|
||||
func uptime() string {
|
||||
// Time difference function
|
||||
diff := func(a, b time.Time) (year, month, day, hour, min, sec int) {
|
||||
if a.Location() != b.Location() {
|
||||
b = b.In(a.Location())
|
||||
}
|
||||
if a.After(b) {
|
||||
a, b = b, a
|
||||
}
|
||||
y1, M1, d1 := a.Date()
|
||||
y2, M2, d2 := b.Date()
|
||||
|
||||
h1, m1, s1 := a.Clock()
|
||||
h2, m2, s2 := b.Clock()
|
||||
|
||||
year = int(y2 - y1)
|
||||
month = int(M2 - M1)
|
||||
day = int(d2 - d1)
|
||||
hour = int(h2 - h1)
|
||||
min = int(m2 - m1)
|
||||
sec = int(s2 - s1)
|
||||
|
||||
// Normalize negative values
|
||||
if sec < 0 {
|
||||
sec += 60
|
||||
min--
|
||||
}
|
||||
if min < 0 {
|
||||
min += 60
|
||||
hour--
|
||||
}
|
||||
if hour < 0 {
|
||||
hour += 24
|
||||
day--
|
||||
}
|
||||
if day < 0 {
|
||||
// days in month:
|
||||
t := time.Date(y1, M1, 32, 0, 0, 0, 0, time.UTC)
|
||||
day += 32 - t.Day()
|
||||
month--
|
||||
}
|
||||
if month < 0 {
|
||||
month += 12
|
||||
year--
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// Y M d h m s
|
||||
now := time.Now()
|
||||
year, month, day, hour, min, sec := diff(startTime, now)
|
||||
@@ -78,53 +128,6 @@ func uptime() string {
|
||||
return strings.Join([]string{Y, M, d, h, m, s}, "")
|
||||
}
|
||||
|
||||
func diff(a, b time.Time) (year, month, day, hour, min, sec int) {
|
||||
if a.Location() != b.Location() {
|
||||
b = b.In(a.Location())
|
||||
}
|
||||
if a.After(b) {
|
||||
a, b = b, a
|
||||
}
|
||||
y1, M1, d1 := a.Date()
|
||||
y2, M2, d2 := b.Date()
|
||||
|
||||
h1, m1, s1 := a.Clock()
|
||||
h2, m2, s2 := b.Clock()
|
||||
|
||||
year = int(y2 - y1)
|
||||
month = int(M2 - M1)
|
||||
day = int(d2 - d1)
|
||||
hour = int(h2 - h1)
|
||||
min = int(m2 - m1)
|
||||
sec = int(s2 - s1)
|
||||
|
||||
// Normalize negative values
|
||||
if sec < 0 {
|
||||
sec += 60
|
||||
min--
|
||||
}
|
||||
if min < 0 {
|
||||
min += 60
|
||||
hour--
|
||||
}
|
||||
if hour < 0 {
|
||||
hour += 24
|
||||
day--
|
||||
}
|
||||
if day < 0 {
|
||||
// days in month:
|
||||
t := time.Date(y1, M1, 32, 0, 0, 0, 0, time.UTC)
|
||||
day += 32 - t.Day()
|
||||
month--
|
||||
}
|
||||
if month < 0 {
|
||||
month += 12
|
||||
year--
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func byteCountSI(b int64) string {
|
||||
const unit = 1000
|
||||
if b < unit {
|
||||
|
||||
+13
-15
@@ -16,7 +16,7 @@ type SessionStater interface {
|
||||
}
|
||||
|
||||
type Session struct {
|
||||
ProcessName string
|
||||
Process string
|
||||
Network string
|
||||
DialerAddr string
|
||||
ClientAddr string
|
||||
@@ -30,22 +30,21 @@ type Session struct {
|
||||
// Track SessionConn
|
||||
type SessionConn struct {
|
||||
net.Conn
|
||||
*Session
|
||||
|
||||
once sync.Once
|
||||
once sync.Once
|
||||
session *Session
|
||||
}
|
||||
|
||||
func NewSessionConn(conn net.Conn, session *Session) net.Conn {
|
||||
return &SessionConn{
|
||||
Conn: conn,
|
||||
Session: session,
|
||||
session: session,
|
||||
}
|
||||
}
|
||||
|
||||
func (c *SessionConn) Read(b []byte) (n int, err error) {
|
||||
n, err = c.Conn.Read(b)
|
||||
if n > 0 {
|
||||
atomic.AddInt64(&c.DownloadBytes, int64(n))
|
||||
atomic.AddInt64(&c.session.DownloadBytes, int64(n))
|
||||
}
|
||||
return
|
||||
}
|
||||
@@ -53,14 +52,14 @@ func (c *SessionConn) Read(b []byte) (n int, err error) {
|
||||
func (c *SessionConn) Write(b []byte) (n int, err error) {
|
||||
n, err = c.Conn.Write(b)
|
||||
if n > 0 {
|
||||
atomic.AddInt64(&c.UploadBytes, int64(n))
|
||||
atomic.AddInt64(&c.session.UploadBytes, int64(n))
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (c *SessionConn) Close() error {
|
||||
c.once.Do(func() {
|
||||
c.SessionClose = time.Now()
|
||||
c.session.SessionClose = time.Now()
|
||||
})
|
||||
return c.Conn.Close()
|
||||
}
|
||||
@@ -68,22 +67,21 @@ func (c *SessionConn) Close() error {
|
||||
// Track SessionPacketConn
|
||||
type SessionPacketConn struct {
|
||||
net.PacketConn
|
||||
*Session
|
||||
|
||||
once sync.Once
|
||||
once sync.Once
|
||||
session *Session
|
||||
}
|
||||
|
||||
func NewSessionPacketConn(conn net.PacketConn, session *Session) net.PacketConn {
|
||||
return &SessionPacketConn{
|
||||
PacketConn: conn,
|
||||
Session: session,
|
||||
session: session,
|
||||
}
|
||||
}
|
||||
|
||||
func (c *SessionPacketConn) ReadFrom(b []byte) (n int, addr net.Addr, err error) {
|
||||
n, addr, err = c.PacketConn.ReadFrom(b)
|
||||
if n > 0 {
|
||||
atomic.AddInt64(&c.DownloadBytes, int64(n))
|
||||
atomic.AddInt64(&c.session.DownloadBytes, int64(n))
|
||||
}
|
||||
return
|
||||
}
|
||||
@@ -91,14 +89,14 @@ func (c *SessionPacketConn) ReadFrom(b []byte) (n int, addr net.Addr, err error)
|
||||
func (c *SessionPacketConn) WriteTo(b []byte, addr net.Addr) (n int, err error) {
|
||||
n, err = c.PacketConn.WriteTo(b, addr)
|
||||
if n > 0 {
|
||||
atomic.AddInt64(&c.UploadBytes, int64(n))
|
||||
atomic.AddInt64(&c.session.UploadBytes, int64(n))
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (c *SessionPacketConn) Close() error {
|
||||
c.once.Do(func() {
|
||||
c.SessionClose = time.Now()
|
||||
c.session.SessionClose = time.Now()
|
||||
})
|
||||
return c.PacketConn.Close()
|
||||
}
|
||||
|
||||
+1
-1
@@ -105,7 +105,7 @@ func (h *tcpHandler) Handle(conn net.Conn, target *net.TCPAddr) error {
|
||||
var process = lsof.GetProcessName(localConn.LocalAddr())
|
||||
if h.sessionStater != nil {
|
||||
sess := &stats.Session{
|
||||
ProcessName: process,
|
||||
Process: process,
|
||||
Network: localConn.LocalAddr().Network(),
|
||||
DialerAddr: remoteConn.LocalAddr().String(),
|
||||
ClientAddr: localConn.LocalAddr().String(),
|
||||
|
||||
+1
-1
@@ -83,7 +83,7 @@ func (h *udpHandler) Connect(conn core.UDPConn, target *net.UDPAddr) error {
|
||||
var process = lsof.GetProcessName(conn.LocalAddr())
|
||||
if h.sessionStater != nil {
|
||||
sess := &stats.Session{
|
||||
ProcessName: process,
|
||||
Process: process,
|
||||
Network: conn.LocalAddr().Network(),
|
||||
DialerAddr: remoteConn.LocalAddr().String(),
|
||||
ClientAddr: conn.LocalAddr().String(),
|
||||
|
||||
Reference in New Issue
Block a user