From 065418bf666fc731e3340c6ca760a5274f7c754e Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Fri, 10 Apr 2026 10:11:12 +0000 Subject: [PATCH 1/2] fix: resolve all 8 bugs (panic, uint32 compare, data races, typo, CRC order, needclose) Agent-Logs-Url: https://github.com/esrrhs/spp/sessions/c9b7bfc7-d3e2-45dd-8734-77c65a3abdef Co-authored-by: esrrhs <4083816+esrrhs@users.noreply.github.com> --- main.go | 28 +++++++++++----------- proxy/client.go | 3 ++- proxy/common.go | 38 +++++++++++++++--------------- proxy/default_DEBUG_2026-04-10.log | 8 +++++++ proxy/inputer.go | 2 +- proxy/outputer.go | 2 +- 6 files changed, 45 insertions(+), 36 deletions(-) create mode 100755 proxy/default_DEBUG_2026-04-10.log diff --git a/main.go b/main.go index 96db519..5916194 100644 --- a/main.go +++ b/main.go @@ -130,6 +130,13 @@ func main() { if *t == "proxy_client" || *t == "reverse_proxy_client" { + if !(len(fromaddr) == len(toaddr) && len(fromaddr) == len(proxyproto)) { + fmt.Println("[fromaddr] [toaddr] [proxyproto] len must be equal") + fmt.Println() + flag.Usage() + return + } + for i, _ := range proxyproto { if len(fromaddr[i]) == 0 || len(*server) == 0 || len(toaddr[i]) == 0 { fmt.Println("[proxy_client] or [reverse_proxy_client] need [server] [fromaddr] [toaddr] [proxyproto]") @@ -139,13 +146,6 @@ func main() { } } - if !(len(fromaddr) == len(toaddr) && len(fromaddr) == len(proxyproto)) { - fmt.Println("[fromaddr] [toaddr] [proxyproto] len must be equal") - fmt.Println() - flag.Usage() - return - } - if len(protos) == 0 { protos = append(protos, "tcp") } @@ -153,6 +153,13 @@ func main() { if *t == "socks5_client" || *t == "reverse_socks5_client" { + if !(len(fromaddr) == len(proxyproto)) { + fmt.Println("[fromaddr] [proxyproto] len must be equal") + fmt.Println() + flag.Usage() + return + } + for i, _ := range proxyproto { if len(fromaddr[i]) == 0 || len(*server) == 0 { fmt.Println("[socks5_client] or [reverse_socks5_client] need [server] [fromaddr] [proxyproto]") @@ -162,13 +169,6 @@ func main() { } } - if !(len(fromaddr) == len(proxyproto)) { - fmt.Println("[fromaddr] [proxyproto] len must be equal") - fmt.Println() - flag.Usage() - return - } - if len(protos) == 0 { protos = append(protos, "tcp") } diff --git a/proxy/client.go b/proxy/client.go index c1436c4..400e8e1 100644 --- a/proxy/client.go +++ b/proxy/client.go @@ -58,7 +58,7 @@ func NewClient(config *Config, serverproto string, server string, name string, c proxyproto = append(proxyproto, PROXY_PROTO(p)) } - wg := thread.NewGroup("Clent"+" "+clienttypestr, nil, nil) + wg := thread.NewGroup("Client"+" "+clienttypestr, nil, nil) c := &Client{ config: config, @@ -254,6 +254,7 @@ func (c *Client) processLoginRsp(wg *thread.Group, index int, f *ProxyFrame, sen err := c.iniService(wg, index, serverconn) if err != nil { + serverconn.needclose = true loggo.Error("processLoginRsp iniService fail %s %s", c.server, err) return } diff --git a/proxy/common.go b/proxy/common.go index 8702f0f..5f7bb34 100644 --- a/proxy/common.go +++ b/proxy/common.go @@ -65,8 +65,8 @@ type ProxyConn struct { established bool sendch *common.Channel // *ProxyFrame recvch *common.Channel // *ProxyFrame - actived int - pinged int + actived int32 + pinged int32 id string needclose bool } @@ -213,7 +213,7 @@ func recvFrom(wg *thread.Group, recvch *common.Channel, conn network.Conn, maxms } msglen := binary.LittleEndian.Uint32(bs) - if msglen > uint32(maxmsgsize)+MAX_PROTO_PACK_SIZE || msglen <= 0 { + if msglen > uint32(maxmsgsize)+MAX_PROTO_PACK_SIZE || msglen == 0 { loggo.Error("recvFrom len fail: %s %d", conn.Info(), msglen) return errors.New("msg len fail " + strconv.Itoa(int(msglen))) } @@ -233,11 +233,6 @@ func recvFrom(wg *thread.Group, recvch *common.Channel, conn network.Conn, maxms return err } - if loggo.IsDebug() { - loggo.Debug("recvFrom start Write %s", conn.Info()) - } - recvch.Write(f) - if f.Type != FRAME_TYPE_PING && f.Type != FRAME_TYPE_PONG && loggo.IsDebug() { loggo.Debug("recvFrom %s %s", conn.Info(), f.Type.String()) if f.Type == FRAME_TYPE_DATA { @@ -248,6 +243,11 @@ func recvFrom(wg *thread.Group, recvch *common.Channel, conn network.Conn, maxms } } + if loggo.IsDebug() { + loggo.Debug("recvFrom start Write %s", conn.Info()) + } + recvch.Write(f) + atomic.AddInt32(&gState.MainRecvNum, 1) atomic.AddInt64(&gState.MainRecvSize, int64(msglen)+4) } @@ -266,14 +266,14 @@ func sendTo(wg *thread.Group, sendch *common.Channel, conn network.Conn, compres for !wg.IsExit() { var f *ProxyFrame - if *pingflag > 0 { - *pingflag = 0 + if atomic.LoadInt32(pingflag) > 0 { + atomic.StoreInt32(pingflag, 0) f = &ProxyFrame{} f.Type = FRAME_TYPE_PING f.PingFrame = &PingFrame{} f.PingFrame.Time = time.Now().UnixNano() - } else if *pongflag > 0 { - *pongflag = 0 + } else if atomic.LoadInt32(pongflag) > 0 { + atomic.StoreInt32(pongflag, 0) f = &ProxyFrame{} f.Type = FRAME_TYPE_PONG f.PongFrame = &PongFrame{} @@ -304,7 +304,7 @@ func sendTo(wg *thread.Group, sendch *common.Channel, conn network.Conn, compres } msglen := uint32(len(mb)) - if msglen > uint32(maxmsgsize)+MAX_PROTO_PACK_SIZE || msglen <= 0 { + if msglen > uint32(maxmsgsize)+MAX_PROTO_PACK_SIZE || msglen == 0 { loggo.Error("sendTo len fail: %s %d", conn.Info(), msglen) return errors.New("msg len fail " + strconv.Itoa(int(msglen))) } @@ -501,14 +501,14 @@ func checkPingActive(wg *thread.Group, sendch *common.Channel, recvch *common.Ch // 2. 定时触发 Ping 逻辑 case <-pingTicker.C: // 检查心跳超时逻辑 - if proxyconn.pinged > pingintertimeout { + if atomic.LoadInt32(&proxyconn.pinged) > int32(pingintertimeout) { loggo.Info("checkPingActive ping pong timeout %s", proxyconn.conn.Info()) return errors.New("ping pong timeout") } // 发送心跳逻辑 atomic.AddInt32(pingflag, 1) - proxyconn.pinged++ + atomic.AddInt32(&proxyconn.pinged, 1) if showping { loggo.Info("ping %s", proxyconn.conn.Info()) } @@ -556,7 +556,7 @@ func processPing(f *ProxyFrame, sendch *common.Channel, proxyconn *ProxyConn, po func processPong(f *ProxyFrame, sendch *common.Channel, proxyconn *ProxyConn, showping bool) { elapse := time.Duration(time.Now().UnixNano() - f.PongFrame.Time) - proxyconn.pinged = 0 + atomic.StoreInt32(&proxyconn.pinged, 0) if showping { loggo.Info("pong %s %s", proxyconn.conn.Info(), elapse.String()) } @@ -597,11 +597,11 @@ func checkSonnyActive(wg *thread.Group, proxyconn *ProxyConn, estimeout int, tim // 2. 定时触发 Ping 逻辑 case <-activedTicker.C: - if proxyconn.actived == 0 { + if atomic.LoadInt32(&proxyconn.actived) == 0 { loggo.Error("checkSonnyActive timeout %s", proxyconn.conn.Info()) return errors.New("conn timeout") } - proxyconn.actived = 0 + atomic.StoreInt32(&proxyconn.actived, 0) } } @@ -633,7 +633,7 @@ func copySonnyRecv(wg *thread.Group, recvch *common.Channel, proxyConn *ProxyCon } } f.DataFrame.Id = proxyConn.id - proxyConn.actived++ + atomic.AddInt32(&proxyConn.actived, 1) father.sendch.Write(f) diff --git a/proxy/default_DEBUG_2026-04-10.log b/proxy/default_DEBUG_2026-04-10.log new file mode 100755 index 0000000..0f7b282 --- /dev/null +++ b/proxy/default_DEBUG_2026-04-10.log @@ -0,0 +1,8 @@ +[DEBUG] [2026-04-10T10:10:30.582451852Z] [common.go:126] [github.com/esrrhs/spp/proxy.MarshalSrpFrame] MarshalSrpFrame Compress from 110 49 +[DEBUG] [2026-04-10T10:10:30.582591062Z] [common.go:140] [github.com/esrrhs/spp/proxy.MarshalSrpFrame] MarshalSrpFrame Rc4 from 68fff6c2 c16dcedd +[DEBUG] [2026-04-10T10:10:30.582760595Z] [common.go:171] [github.com/esrrhs/spp/proxy.UnmarshalSrpFrame] UnmarshalSrpFrame Rc4 from c16dcedd 68fff6c2 +[DEBUG] [2026-04-10T10:10:30.582806489Z] [common.go:182] [github.com/esrrhs/spp/proxy.UnmarshalSrpFrame] UnmarshalSrpFrame Compress from 49 110 +[DEBUG] [2026-04-10T10:10:57.829981178Z] [common.go:126] [github.com/esrrhs/spp/proxy.MarshalSrpFrame] MarshalSrpFrame Compress from 110 49 +[DEBUG] [2026-04-10T10:10:57.830194674Z] [common.go:140] [github.com/esrrhs/spp/proxy.MarshalSrpFrame] MarshalSrpFrame Rc4 from 68fff6c2 c16dcedd +[DEBUG] [2026-04-10T10:10:57.830656787Z] [common.go:171] [github.com/esrrhs/spp/proxy.UnmarshalSrpFrame] UnmarshalSrpFrame Rc4 from c16dcedd 68fff6c2 +[DEBUG] [2026-04-10T10:10:57.830820325Z] [common.go:182] [github.com/esrrhs/spp/proxy.UnmarshalSrpFrame] UnmarshalSrpFrame Compress from 49 110 diff --git a/proxy/inputer.go b/proxy/inputer.go index 2c493f5..aaa1f93 100644 --- a/proxy/inputer.go +++ b/proxy/inputer.go @@ -99,7 +99,7 @@ func (i *Inputer) processDataFrame(f *ProxyFrame) { sonny.needclose = true loggo.Error("Inputer processDataFrame timeout sonnny %s %d", f.DataFrame.Id, len(f.DataFrame.Data)) } - sonny.actived++ + atomic.AddInt32(&sonny.actived, 1) loggo.Debug("Inputer processDataFrame %s %d", f.DataFrame.Id, len(f.DataFrame.Data)) } diff --git a/proxy/outputer.go b/proxy/outputer.go index 30cf1c0..ee5d547 100644 --- a/proxy/outputer.go +++ b/proxy/outputer.go @@ -81,7 +81,7 @@ func (o *Outputer) processDataFrame(f *ProxyFrame) { sonny.needclose = true loggo.Error("Outputer processDataFrame timeout sonnny %s %d", f.DataFrame.Id, len(f.DataFrame.Data)) } - sonny.actived++ + atomic.AddInt32(&sonny.actived, 1) loggo.Debug("Outputer processDataFrame %s %d", f.DataFrame.Id, len(f.DataFrame.Data)) } From 7d599e06a9a37f76b687f182b5a5e50f4c2cba1a Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Fri, 10 Apr 2026 10:17:04 +0000 Subject: [PATCH 2/2] chore: add *.log to .gitignore and untrack existing log file Agent-Logs-Url: https://github.com/esrrhs/spp/sessions/518cdc91-076d-40dd-b8aa-81d8c6a10de5 Co-authored-by: esrrhs <4083816+esrrhs@users.noreply.github.com> --- .gitignore | 3 +++ proxy/default_DEBUG_2026-04-10.log | 8 -------- 2 files changed, 3 insertions(+), 8 deletions(-) delete mode 100755 proxy/default_DEBUG_2026-04-10.log diff --git a/.gitignore b/.gitignore index 66fd13c..0e8bc89 100644 --- a/.gitignore +++ b/.gitignore @@ -11,5 +11,8 @@ # Output of the go coverage tool, specifically when used with LiteIDE *.out +# Log files +*.log + # Dependency directories (remove the comment below to include it) # vendor/ diff --git a/proxy/default_DEBUG_2026-04-10.log b/proxy/default_DEBUG_2026-04-10.log deleted file mode 100755 index 0f7b282..0000000 --- a/proxy/default_DEBUG_2026-04-10.log +++ /dev/null @@ -1,8 +0,0 @@ -[DEBUG] [2026-04-10T10:10:30.582451852Z] [common.go:126] [github.com/esrrhs/spp/proxy.MarshalSrpFrame] MarshalSrpFrame Compress from 110 49 -[DEBUG] [2026-04-10T10:10:30.582591062Z] [common.go:140] [github.com/esrrhs/spp/proxy.MarshalSrpFrame] MarshalSrpFrame Rc4 from 68fff6c2 c16dcedd -[DEBUG] [2026-04-10T10:10:30.582760595Z] [common.go:171] [github.com/esrrhs/spp/proxy.UnmarshalSrpFrame] UnmarshalSrpFrame Rc4 from c16dcedd 68fff6c2 -[DEBUG] [2026-04-10T10:10:30.582806489Z] [common.go:182] [github.com/esrrhs/spp/proxy.UnmarshalSrpFrame] UnmarshalSrpFrame Compress from 49 110 -[DEBUG] [2026-04-10T10:10:57.829981178Z] [common.go:126] [github.com/esrrhs/spp/proxy.MarshalSrpFrame] MarshalSrpFrame Compress from 110 49 -[DEBUG] [2026-04-10T10:10:57.830194674Z] [common.go:140] [github.com/esrrhs/spp/proxy.MarshalSrpFrame] MarshalSrpFrame Rc4 from 68fff6c2 c16dcedd -[DEBUG] [2026-04-10T10:10:57.830656787Z] [common.go:171] [github.com/esrrhs/spp/proxy.UnmarshalSrpFrame] UnmarshalSrpFrame Rc4 from c16dcedd 68fff6c2 -[DEBUG] [2026-04-10T10:10:57.830820325Z] [common.go:182] [github.com/esrrhs/spp/proxy.UnmarshalSrpFrame] UnmarshalSrpFrame Compress from 49 110