Skip to content

Commit 2858c23

Browse files
authored
Merge pull request #40 from esrrhs/copilot/check-for-bugs
Fix 8 bugs in proxy + exclude log files from git
2 parents bc41272 + 7d599e0 commit 2858c23

6 files changed

Lines changed: 40 additions & 36 deletions

File tree

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,8 @@
1111
# Output of the go coverage tool, specifically when used with LiteIDE
1212
*.out
1313

14+
# Log files
15+
*.log
16+
1417
# Dependency directories (remove the comment below to include it)
1518
# vendor/

main.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,13 @@ func main() {
130130

131131
if *t == "proxy_client" ||
132132
*t == "reverse_proxy_client" {
133+
if !(len(fromaddr) == len(toaddr) && len(fromaddr) == len(proxyproto)) {
134+
fmt.Println("[fromaddr] [toaddr] [proxyproto] len must be equal")
135+
fmt.Println()
136+
flag.Usage()
137+
return
138+
}
139+
133140
for i, _ := range proxyproto {
134141
if len(fromaddr[i]) == 0 || len(*server) == 0 || len(toaddr[i]) == 0 {
135142
fmt.Println("[proxy_client] or [reverse_proxy_client] need [server] [fromaddr] [toaddr] [proxyproto]")
@@ -139,20 +146,20 @@ func main() {
139146
}
140147
}
141148

142-
if !(len(fromaddr) == len(toaddr) && len(fromaddr) == len(proxyproto)) {
143-
fmt.Println("[fromaddr] [toaddr] [proxyproto] len must be equal")
144-
fmt.Println()
145-
flag.Usage()
146-
return
147-
}
148-
149149
if len(protos) == 0 {
150150
protos = append(protos, "tcp")
151151
}
152152
}
153153

154154
if *t == "socks5_client" ||
155155
*t == "reverse_socks5_client" {
156+
if !(len(fromaddr) == len(proxyproto)) {
157+
fmt.Println("[fromaddr] [proxyproto] len must be equal")
158+
fmt.Println()
159+
flag.Usage()
160+
return
161+
}
162+
156163
for i, _ := range proxyproto {
157164
if len(fromaddr[i]) == 0 || len(*server) == 0 {
158165
fmt.Println("[socks5_client] or [reverse_socks5_client] need [server] [fromaddr] [proxyproto]")
@@ -162,13 +169,6 @@ func main() {
162169
}
163170
}
164171

165-
if !(len(fromaddr) == len(proxyproto)) {
166-
fmt.Println("[fromaddr] [proxyproto] len must be equal")
167-
fmt.Println()
168-
flag.Usage()
169-
return
170-
}
171-
172172
if len(protos) == 0 {
173173
protos = append(protos, "tcp")
174174
}

proxy/client.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func NewClient(config *Config, serverproto string, server string, name string, c
5858
proxyproto = append(proxyproto, PROXY_PROTO(p))
5959
}
6060

61-
wg := thread.NewGroup("Clent"+" "+clienttypestr, nil, nil)
61+
wg := thread.NewGroup("Client"+" "+clienttypestr, nil, nil)
6262

6363
c := &Client{
6464
config: config,
@@ -254,6 +254,7 @@ func (c *Client) processLoginRsp(wg *thread.Group, index int, f *ProxyFrame, sen
254254

255255
err := c.iniService(wg, index, serverconn)
256256
if err != nil {
257+
serverconn.needclose = true
257258
loggo.Error("processLoginRsp iniService fail %s %s", c.server, err)
258259
return
259260
}

proxy/common.go

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ type ProxyConn struct {
6565
established bool
6666
sendch *common.Channel // *ProxyFrame
6767
recvch *common.Channel // *ProxyFrame
68-
actived int
69-
pinged int
68+
actived int32
69+
pinged int32
7070
id string
7171
needclose bool
7272
}
@@ -213,7 +213,7 @@ func recvFrom(wg *thread.Group, recvch *common.Channel, conn network.Conn, maxms
213213
}
214214

215215
msglen := binary.LittleEndian.Uint32(bs)
216-
if msglen > uint32(maxmsgsize)+MAX_PROTO_PACK_SIZE || msglen <= 0 {
216+
if msglen > uint32(maxmsgsize)+MAX_PROTO_PACK_SIZE || msglen == 0 {
217217
loggo.Error("recvFrom len fail: %s %d", conn.Info(), msglen)
218218
return errors.New("msg len fail " + strconv.Itoa(int(msglen)))
219219
}
@@ -233,11 +233,6 @@ func recvFrom(wg *thread.Group, recvch *common.Channel, conn network.Conn, maxms
233233
return err
234234
}
235235

236-
if loggo.IsDebug() {
237-
loggo.Debug("recvFrom start Write %s", conn.Info())
238-
}
239-
recvch.Write(f)
240-
241236
if f.Type != FRAME_TYPE_PING && f.Type != FRAME_TYPE_PONG && loggo.IsDebug() {
242237
loggo.Debug("recvFrom %s %s", conn.Info(), f.Type.String())
243238
if f.Type == FRAME_TYPE_DATA {
@@ -248,6 +243,11 @@ func recvFrom(wg *thread.Group, recvch *common.Channel, conn network.Conn, maxms
248243
}
249244
}
250245

246+
if loggo.IsDebug() {
247+
loggo.Debug("recvFrom start Write %s", conn.Info())
248+
}
249+
recvch.Write(f)
250+
251251
atomic.AddInt32(&gState.MainRecvNum, 1)
252252
atomic.AddInt64(&gState.MainRecvSize, int64(msglen)+4)
253253
}
@@ -266,14 +266,14 @@ func sendTo(wg *thread.Group, sendch *common.Channel, conn network.Conn, compres
266266

267267
for !wg.IsExit() {
268268
var f *ProxyFrame
269-
if *pingflag > 0 {
270-
*pingflag = 0
269+
if atomic.LoadInt32(pingflag) > 0 {
270+
atomic.StoreInt32(pingflag, 0)
271271
f = &ProxyFrame{}
272272
f.Type = FRAME_TYPE_PING
273273
f.PingFrame = &PingFrame{}
274274
f.PingFrame.Time = time.Now().UnixNano()
275-
} else if *pongflag > 0 {
276-
*pongflag = 0
275+
} else if atomic.LoadInt32(pongflag) > 0 {
276+
atomic.StoreInt32(pongflag, 0)
277277
f = &ProxyFrame{}
278278
f.Type = FRAME_TYPE_PONG
279279
f.PongFrame = &PongFrame{}
@@ -304,7 +304,7 @@ func sendTo(wg *thread.Group, sendch *common.Channel, conn network.Conn, compres
304304
}
305305

306306
msglen := uint32(len(mb))
307-
if msglen > uint32(maxmsgsize)+MAX_PROTO_PACK_SIZE || msglen <= 0 {
307+
if msglen > uint32(maxmsgsize)+MAX_PROTO_PACK_SIZE || msglen == 0 {
308308
loggo.Error("sendTo len fail: %s %d", conn.Info(), msglen)
309309
return errors.New("msg len fail " + strconv.Itoa(int(msglen)))
310310
}
@@ -501,14 +501,14 @@ func checkPingActive(wg *thread.Group, sendch *common.Channel, recvch *common.Ch
501501
// 2. 定时触发 Ping 逻辑
502502
case <-pingTicker.C:
503503
// 检查心跳超时逻辑
504-
if proxyconn.pinged > pingintertimeout {
504+
if atomic.LoadInt32(&proxyconn.pinged) > int32(pingintertimeout) {
505505
loggo.Info("checkPingActive ping pong timeout %s", proxyconn.conn.Info())
506506
return errors.New("ping pong timeout")
507507
}
508508

509509
// 发送心跳逻辑
510510
atomic.AddInt32(pingflag, 1)
511-
proxyconn.pinged++
511+
atomic.AddInt32(&proxyconn.pinged, 1)
512512
if showping {
513513
loggo.Info("ping %s", proxyconn.conn.Info())
514514
}
@@ -556,7 +556,7 @@ func processPing(f *ProxyFrame, sendch *common.Channel, proxyconn *ProxyConn, po
556556

557557
func processPong(f *ProxyFrame, sendch *common.Channel, proxyconn *ProxyConn, showping bool) {
558558
elapse := time.Duration(time.Now().UnixNano() - f.PongFrame.Time)
559-
proxyconn.pinged = 0
559+
atomic.StoreInt32(&proxyconn.pinged, 0)
560560
if showping {
561561
loggo.Info("pong %s %s", proxyconn.conn.Info(), elapse.String())
562562
}
@@ -597,11 +597,11 @@ func checkSonnyActive(wg *thread.Group, proxyconn *ProxyConn, estimeout int, tim
597597

598598
// 2. 定时触发 Ping 逻辑
599599
case <-activedTicker.C:
600-
if proxyconn.actived == 0 {
600+
if atomic.LoadInt32(&proxyconn.actived) == 0 {
601601
loggo.Error("checkSonnyActive timeout %s", proxyconn.conn.Info())
602602
return errors.New("conn timeout")
603603
}
604-
proxyconn.actived = 0
604+
atomic.StoreInt32(&proxyconn.actived, 0)
605605
}
606606
}
607607

@@ -633,7 +633,7 @@ func copySonnyRecv(wg *thread.Group, recvch *common.Channel, proxyConn *ProxyCon
633633
}
634634
}
635635
f.DataFrame.Id = proxyConn.id
636-
proxyConn.actived++
636+
atomic.AddInt32(&proxyConn.actived, 1)
637637

638638
father.sendch.Write(f)
639639

proxy/inputer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ func (i *Inputer) processDataFrame(f *ProxyFrame) {
9999
sonny.needclose = true
100100
loggo.Error("Inputer processDataFrame timeout sonnny %s %d", f.DataFrame.Id, len(f.DataFrame.Data))
101101
}
102-
sonny.actived++
102+
atomic.AddInt32(&sonny.actived, 1)
103103
loggo.Debug("Inputer processDataFrame %s %d", f.DataFrame.Id, len(f.DataFrame.Data))
104104
}
105105

proxy/outputer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ func (o *Outputer) processDataFrame(f *ProxyFrame) {
8181
sonny.needclose = true
8282
loggo.Error("Outputer processDataFrame timeout sonnny %s %d", f.DataFrame.Id, len(f.DataFrame.Data))
8383
}
84-
sonny.actived++
84+
atomic.AddInt32(&sonny.actived, 1)
8585
loggo.Debug("Outputer processDataFrame %s %d", f.DataFrame.Id, len(f.DataFrame.Data))
8686
}
8787

0 commit comments

Comments
 (0)