From c24d3e29437e75516d9b71b14aaa112095637f34 Mon Sep 17 00:00:00 2001 From: mojocn Date: Mon, 11 Nov 2024 18:16:05 +0800 Subject: [PATCH] Refactor proxy connection handling, improve error logging, and update WebSocket URL to secure connection --- shadowos/app.go | 58 ++++++++++++++++++-------------------------- shadowos/man/main.go | 2 +- 2 files changed, 25 insertions(+), 35 deletions(-) diff --git a/shadowos/app.go b/shadowos/app.go index b90eedb..e931034 100644 --- a/shadowos/app.go +++ b/shadowos/app.go @@ -142,7 +142,6 @@ func handshake(conn net.Conn, uuidS string) (connData []byte, err error) { } func (ss *ShadowosApp) handleConnection(conn net.Conn) { - defer log.Print("request done -->>") defer conn.Close() connBytes, err := handshake(conn, ss.UUID) if err != nil { @@ -202,9 +201,7 @@ func (ps ProxySession) Close() error { func (ps *ProxySession) doProxy(socks net.Conn) { go func() { - // tk := time.NewTicker(time.Second * 5) defer func() { - // tk.Stop() ps.ch <- struct{}{} }() for { @@ -242,44 +239,37 @@ func (ps *ProxySession) doProxy(socks net.Conn) { go func() { defer func() { - // tk.Stop() ps.ch <- struct{}{} }() for { - select { - - default: - buf := make([]byte, 1024) - n, err := socks.Read(buf) - if n > 0 { - log.Println("socks read N:", n) - if len(ps.connData) > 0 { - buf = append(ps.connData, buf[:n]...) - ps.connData = nil - } else { - buf = buf[:n] - } - err = ps.ws.WriteMessage(websocket.BinaryMessage, buf) - if err != nil { - log.Println("failed to write to websocket", err) - } - - } - //socks5 EOF - if err != io.EOF { - continue - } - if err != net.ErrClosed { - log.Print("socks5 closed") - return + buf := make([]byte, 1024) + n, err := socks.Read(buf) + if n > 0 { + log.Println("socks read N:", n) + if len(ps.connData) > 0 { + buf = append(ps.connData, buf[:n]...) + ps.connData = nil + } else { + buf = buf[:n] } - + err = ps.ws.WriteMessage(websocket.BinaryMessage, buf) if err != nil { - log.Printf("%T", err) - log.Println("failed to read from socks5 to websocket", err) - return + log.Println("failed to write to websocket", err) } } + //socks5 EOF + if err != io.EOF { + continue + } + if err != net.ErrClosed { + log.Print("socks5 closed") + return + } + if err != nil { + log.Printf("%T", err) + log.Println("failed to read from socks5 to websocket", err) + return + } } }() <-ps.ch diff --git a/shadowos/man/main.go b/shadowos/man/main.go index f8fb91b..ddf9bd0 100644 --- a/shadowos/man/main.go +++ b/shadowos/man/main.go @@ -8,7 +8,7 @@ import ( ) var ( - url = "ws://127.0.0.1:8787/53881505-c10c-464a-8949-e57184a576a9" + url = "wss://demo.libragen.cn/53881505-c10c-464a-8949-e57184a576a9" app = &shadowos.ShadowosApp{ AddrWs: url, AddrSocks5: "127.0.0.1:1080",