From 5d2e809943af7117dff9a41faa77359da5dbbb0f Mon Sep 17 00:00:00 2001 From: halulu Date: Sat, 12 May 2018 13:56:03 +0800 Subject: [PATCH 1/4] count uploaded --- core/server.go | 53 ++++++++++++++++++++++++++++++-------------------- websocks.go | 3 +++ 2 files changed, 35 insertions(+), 21 deletions(-) diff --git a/core/server.go b/core/server.go index 674546b..7b0dba3 100644 --- a/core/server.go +++ b/core/server.go @@ -11,6 +11,8 @@ import ( "net/http/httputil" "net/url" + "sync/atomic" + "github.com/juju/loggo" "golang.org/x/net/websocket" ) @@ -23,49 +25,60 @@ type Server struct { CertPath string KeyPath string Proxy string -} -var opened = 0 -var closed = 0 -var t time.Time + CreatedAt time.Time -func handler(ws *websocket.Conn) { - opened++ - var err error - defer func() { - closed++ - if err != nil { - logger.Debugf(err.Error()) - } - }() + Opened uint64 + Closed uint64 + Uploaded uint64 +} + +func (server *Server) HandleWebSocket(ws *websocket.Conn) { + server.Opened++ defer ws.Close() dec := gob.NewDecoder(ws) req := Request{} - err = dec.Decode(&req) + err := dec.Decode(&req) if err != nil { + server.Closed++ + if err != nil { + logger.Debugf(err.Error()) + } return } logger.Debugf("Dial %s from %s", req.Addr, ws.RemoteAddr().String()) conn, err := net.Dial("tcp", req.Addr) if err != nil { + server.Closed++ + if err != nil { + logger.Debugf(err.Error()) + } return } defer conn.Close() go func() { - _, err = io.Copy(conn, ws) + _, err := io.Copy(conn, ws) if err != nil { - logger.Debugf(err.Error()) + server.Closed++ + if err != nil { + logger.Debugf(err.Error()) + } return } }() - _, err = io.Copy(ws, conn) + uploaded, err := io.Copy(ws, conn) + atomic.AddUint64(&server.Uploaded, uint64(uploaded)) if err != nil { + server.Closed++ + if err != nil { + logger.Debugf(err.Error()) + } return } } @@ -73,17 +86,15 @@ func handler(ws *websocket.Conn) { func (server *Server) Listen() (err error) { logger.SetLogLevel(server.LogLevel) - t = time.Now() - go func() { for { time.Sleep(time.Second) - logger.Debugf("%ds: opened%d, closed%d", int(time.Since(t).Seconds()), opened, closed) + logger.Debugf("%ds: opened%d, closed%d, uploaded%d bytes", int(time.Since(server.CreatedAt).Seconds()), server.Opened, server.Closed, server.Uploaded) } }() mux := http.NewServeMux() - mux.Handle(server.Pattern, websocket.Handler(handler)) + mux.Handle(server.Pattern, websocket.Handler(server.HandleWebSocket)) if server.Proxy != "" { mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { remote, err := url.Parse(server.Proxy) diff --git a/websocks.go b/websocks.go index 14e9df6..9cb61ac 100644 --- a/websocks.go +++ b/websocks.go @@ -9,6 +9,8 @@ import ( "io/ioutil" + "time" + "github.com/juju/loggo" "github.com/lzjluzijie/websocks/core" "github.com/urfave/cli" @@ -159,6 +161,7 @@ func main() { CertPath: certPath, KeyPath: keyPath, Proxy: proxy, + CreatedAt: time.Now(), } logger.Infof("Listening at %s", listenAddr) From aba56d418f357bc4a77032bec33ee7a5fb15f5c2 Mon Sep 17 00:00:00 2001 From: halulu Date: Sat, 12 May 2018 14:10:40 +0800 Subject: [PATCH 2/4] count downloaded --- core/server.go | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/core/server.go b/core/server.go index 7b0dba3..4ab14e9 100644 --- a/core/server.go +++ b/core/server.go @@ -28,9 +28,10 @@ type Server struct { CreatedAt time.Time - Opened uint64 - Closed uint64 - Uploaded uint64 + Opened uint64 + Closed uint64 + Uploaded uint64 + Downloaded uint64 } func (server *Server) HandleWebSocket(ws *websocket.Conn) { @@ -62,7 +63,8 @@ func (server *Server) HandleWebSocket(ws *websocket.Conn) { defer conn.Close() go func() { - _, err := io.Copy(conn, ws) + downloaded, err := io.Copy(conn, ws) + atomic.AddUint64(&server.Downloaded, uint64(downloaded)) if err != nil { server.Closed++ if err != nil { @@ -89,7 +91,7 @@ func (server *Server) Listen() (err error) { go func() { for { time.Sleep(time.Second) - logger.Debugf("%ds: opened%d, closed%d, uploaded%d bytes", int(time.Since(server.CreatedAt).Seconds()), server.Opened, server.Closed, server.Uploaded) + logger.Debugf("%ds: opened %d, closed %d, uploaded %d bytes, downloaded %d bytes", int(time.Since(server.CreatedAt).Seconds()), server.Opened, server.Closed, server.Uploaded, server.Downloaded) } }() From d412aed0ad458b7844908e2b7abd250a1d06567b Mon Sep 17 00:00:00 2001 From: halulu Date: Sat, 12 May 2018 14:17:47 +0800 Subject: [PATCH 3/4] web status --- core/server.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/core/server.go b/core/server.go index 4ab14e9..6740e26 100644 --- a/core/server.go +++ b/core/server.go @@ -13,6 +13,8 @@ import ( "sync/atomic" + "fmt" + "github.com/juju/loggo" "golang.org/x/net/websocket" ) @@ -85,6 +87,10 @@ func (server *Server) HandleWebSocket(ws *websocket.Conn) { } } +func (server *Server) Status(w http.ResponseWriter, r *http.Request) { + w.Write([]byte(fmt.Sprintf("%ds: opened %d, closed %d, uploaded %d bytes, downloaded %d bytes", int(time.Since(server.CreatedAt).Seconds()), server.Opened, server.Closed, server.Uploaded, server.Downloaded))) +} + func (server *Server) Listen() (err error) { logger.SetLogLevel(server.LogLevel) @@ -97,6 +103,7 @@ func (server *Server) Listen() (err error) { mux := http.NewServeMux() mux.Handle(server.Pattern, websocket.Handler(server.HandleWebSocket)) + mux.HandleFunc("/status", server.Status) if server.Proxy != "" { mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { remote, err := url.Parse(server.Proxy) From 1534973b80f9f09562f232d34308f0fbd787e053 Mon Sep 17 00:00:00 2001 From: halulu Date: Sat, 12 May 2018 14:24:30 +0800 Subject: [PATCH 4/4] https://zhuji.lu/tags/websocks --- README-zh.md | 3 +++ README.md | 28 +++++++++++++++------------- websocks.go | 2 +- 3 files changed, 19 insertions(+), 14 deletions(-) diff --git a/README-zh.md b/README-zh.md index ae98434..1cedfa2 100644 --- a/README-zh.md +++ b/README-zh.md @@ -1,5 +1,8 @@ # WebSocks +[教程资源](https://zhuji.lu/tags/websocks) +[TG群](https://t.me/websocks) + 一个基于 WebSocket 的代理工具 本项目目前还在开发中,更多功能仍在完善中。如果你对这个项目感兴趣,请star它来支持我,蟹蟹。 diff --git a/README.md b/README.md index f3e39b4..7245562 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,8 @@ # WebSocks +[教程资源](https://zhuji.lu/tags/websocks) [中文说明](https://github.com/lzjluzijie/websocks/blob/master/README-zh.md) +[Telegram Group](https://t.me/websocks) A secure proxy based on websocket. @@ -19,6 +21,19 @@ To-Do: ## Example +### Built-in TLS with fake server name and reversing proxy + +#### Server +``` +./websocks cert +./websocks server -l :2333 -p /password --proxy https://www.centos.org/ --tls +``` + +#### Local +``` +./websocks client -l :1080 -s wss://the-real-server.com:2333/password -n www.centos.com --insecure +``` + ### Caddy TLS #### Server @@ -39,16 +54,3 @@ https://server.com { } } ``` - -### Built-in TLS with fake server name and reversing proxy - -#### Server -``` -./websocks cert -./websocks server -l :2333 -p /password --proxy https://www.centos.org/ --tls -``` - -#### Local -``` -./websocks client -l :1080 -s wss://the-real-server.com:2333/password -n www.centos.com --insecure -``` diff --git a/websocks.go b/websocks.go index 9cb61ac..a7dfe2f 100644 --- a/websocks.go +++ b/websocks.go @@ -22,7 +22,7 @@ func main() { app := cli.NewApp() app.Name = "WebSocks" - app.Version = "0.3.0" + app.Version = "0.4.0" app.Usage = "A secure proxy based on WebSocket." app.Description = "See https://github.com/lzjluzijie/websocks" app.Author = "Halulu"