-
Go-tun2socks %s
`, C.Version)
- // calculate traffic
- trafficUp := atomic.LoadInt64(&s.trafficUp)
- trafficDown := atomic.LoadInt64(&s.trafficDown)
- for _, session := range activeSessions {
- trafficUp += session.UploadBytes
- trafficDown += session.DownloadBytes
- }
- // statistics
- _, _ = fmt.Fprintf(w, `
-
-
- | Last Refresh Time | Platform Version | CPU | MEM | Uptime | Total | Upload | Download |
-
- | %v | %v | %v | %v | %v | %v | %v | %v |
-
-
-
`,
- runtime.NumGoroutine(),
- date(time.Now()),
- platform(),
- cpu(),
- mem(),
- uptime(),
- byteCountSI(trafficUp+trafficDown),
- byteCountSI(trafficUp),
- byteCountSI(trafficDown),
- )
- // Session table
- _, _ = fmt.Fprintf(w, "\n", len(activeSessions))
- tablePrint(w, activeSessions)
- _, _ = fmt.Fprintf(w, "\n", len(closedSessions))
- tablePrint(w, closedSessions)
- _, _ = fmt.Fprintf(w, "
\n")
- _ = w.Flush()
-}
-
-func (s *Server) Start() error {
- _, port, err := net.SplitHostPort(s.ServeAddr)
- if port == "0" || port == "" || err != nil {
- return errors.New("address format error")
- }
-
- tcpAddr, err := net.ResolveTCPAddr("tcp", s.ServeAddr)
- if err != nil {
- return err
- }
-
- c, err := net.ListenTCP("tcp", tcpAddr)
- if err != nil {
- return err
- }
-
- mux := http.NewServeMux()
- mux.HandleFunc("/", s.serveHTML)
- mux.HandleFunc("/json", s.serveJSON)
-
- box := packr.New("CSSBox", "./css")
- mux.Handle("/css/", http.StripPrefix("/css/", http.FileServer(box)))
-
- s.Server = &http.Server{Addr: s.ServeAddr, Handler: mux}
- go func() {
- s.Serve(c)
- }()
-
- return nil
-}
-
-func (s *Server) Stop() error {
- return s.Close()
-}
-
-func (s *Server) AddSession(key interface{}, session *Session) {
- if session != nil {
- s.activeSessionMap.Store(key, session)
- }
-}
-
-func (s *Server) RemoveSession(key interface{}) {
- 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 closed sessions
- s.Lock()
- s.closedSessionList = append(s.closedSessionList, *session)
- if len(s.closedSessionList) > maxClosedSessions {
- s.closedSessionList = s.closedSessionList[1:]
- }
- s.Unlock()
- }
-}
diff --git a/component/session/session.go b/component/session/session.go
deleted file mode 100644
index 8ce0aef..0000000
--- a/component/session/session.go
+++ /dev/null
@@ -1,103 +0,0 @@
-package session
-
-import (
- "net"
- "sync"
- "sync/atomic"
- "time"
-)
-
-type Monitor interface {
- Start() error
- Stop() error
-
- // METHODS
- AddSession(key interface{}, session *Session)
- RemoveSession(key interface{})
-}
-
-type Status struct {
- Platform string `json:"platform"`
- Version string `json:"version"`
- CPU string `json:"cpu"`
- MEM string `json:"mem"`
- Uptime string `json:"uptime"`
- Total int64 `json:"total"`
- Upload int64 `json:"upload"`
- Download int64 `json:"download"`
- Goroutines int `json:"goroutines"`
- ActiveSessions []Session `json:"activeSessions"`
- ClosedSessions []Session `json:"closedSessions"`
-}
-
-type Session struct {
- Process string `json:"process"`
- Network string `json:"network"`
- DialerAddr string `json:"dialerAddr"`
- ClientAddr string `json:"clientAddr"`
- TargetAddr string `json:"targetAddr"`
- UploadBytes int64 `json:"upload"`
- DownloadBytes int64 `json:"download"`
- SessionStart time.Time `json:"sessionStart"`
- SessionClose time.Time `json:"sessionClose"`
-}
-
-// Track SessionConn
-type Conn struct {
- *Session
- net.Conn
- once sync.Once
-}
-
-func (c *Conn) Read(b []byte) (n int, err error) {
- n, err = c.Conn.Read(b)
- if n > 0 {
- atomic.AddInt64(&c.DownloadBytes, int64(n))
- }
- return
-}
-
-func (c *Conn) Write(b []byte) (n int, err error) {
- n, err = c.Conn.Write(b)
- if n > 0 {
- atomic.AddInt64(&c.UploadBytes, int64(n))
- }
- return
-}
-
-func (c *Conn) Close() error {
- c.once.Do(func() {
- c.SessionClose = time.Now()
- })
- return c.Conn.Close()
-}
-
-// Track SessionPacketConn
-type PacketConn struct {
- *Session
- net.PacketConn
- once sync.Once
-}
-
-func (c *PacketConn) 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))
- }
- return
-}
-
-func (c *PacketConn) 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))
- }
- return
-}
-
-func (c *PacketConn) Close() error {
- c.once.Do(func() {
- c.SessionClose = time.Now()
- })
- return c.PacketConn.Close()
-}
diff --git a/component/session/utils.go b/component/session/utils.go
deleted file mode 100644
index 623aa0c..0000000
--- a/component/session/utils.go
+++ /dev/null
@@ -1,174 +0,0 @@
-package session
-
-import (
- "fmt"
- "strings"
- "time"
-
- C "github.com/shirou/gopsutil/cpu"
- H "github.com/shirou/gopsutil/host"
- M "github.com/shirou/gopsutil/mem"
-)
-
-var startTime time.Time
-
-func init() {
- startTime = time.Now()
-}
-
-func cpu() string {
- c, err := C.Percent(0, false)
- if err != nil || len(c) != 1 {
- return "N/A"
- }
- return fmt.Sprintf("%.1f%%", c[0])
-}
-
-func platform() string {
- h, err := H.Info()
- if err != nil {
- return "N/A"
- }
- return fmt.Sprintf("%s-%s", h.Platform, h.KernelVersion)
-}
-
-func mem() string {
- m, err := M.VirtualMemory()
- if err != nil {
- return "N/A"
- }
- return fmt.Sprintf("%.1f%%", m.UsedPercent)
-}
-
-func date(t time.Time) string {
- return t.Format("Mon Jan 2 15:04:05")
-}
-
-func duration(start, end time.Time) string {
- var t time.Duration
- if end.IsZero() {
- t = time.Now().Sub(start)
- } else {
- t = end.Sub(start)
- }
-
- switch {
- case t < 1000*time.Millisecond:
- t = t.Round(time.Millisecond)
- default:
- t = t.Round(time.Second)
- }
- 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)
-
- var Y, M, d, h, m, s string
-
- // Y M d
- if year != 0 {
- Y = fmt.Sprintf("%dY,", year)
- }
-
- if month != 0 {
- M = fmt.Sprintf("%dM,", month)
- }
-
- if day != 0 {
- d = fmt.Sprintf("%dd,", day)
- }
-
- // h m s
- if hour != 0 {
- h = fmt.Sprintf("%dh", hour)
- }
-
- if min != 0 {
- m = fmt.Sprintf("%dm", min)
- }
-
- if sec != 0 {
- s = fmt.Sprintf("%ds", sec)
- }
-
- return strings.Join([]string{Y, M, d, h, m, s}, "")
-}
-
-func byteCountSI(b int64) string {
- const unit = 1000
- if b < unit {
- return fmt.Sprintf("%d B", b)
- }
- div, exp := int64(unit), 0
- for n := b / unit; n >= unit; n /= unit {
- div *= unit
- exp++
- }
- return fmt.Sprintf("%.1f %cB",
- float64(b)/float64(div), "kMGTPE"[exp])
-}
-
-func byteCountIEC(b int64) string {
- const unit = 1024
- if b < unit {
- return fmt.Sprintf("%d B", b)
- }
- div, exp := int64(unit), 0
- for n := b / unit; n >= unit; n /= unit {
- div *= unit
- exp++
- }
- return fmt.Sprintf("%.1f %ciB",
- float64(b)/float64(div), "KMGTPE"[exp])
-}
diff --git a/constant/version.go b/constant/version.go
deleted file mode 100644
index 32a3a4f..0000000
--- a/constant/version.go
+++ /dev/null
@@ -1,5 +0,0 @@
-package constant
-
-var (
- Version = "v0.0.0-0-00000000"
-)
diff --git a/core/addr.go b/core/addr.go
deleted file mode 100755
index 436c04d..0000000
--- a/core/addr.go
+++ /dev/null
@@ -1,46 +0,0 @@
-package core
-
-/*
-#cgo CFLAGS: -I./c/include
-#include "lwip/tcp.h"
-#include