Skip to content

Commit a3c3417

Browse files
author
seanchann
committed
1. add recover when parse data panic.
1 parent e811387 commit a3c3417

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

pkg/connection/tcp/ssclient/ssclient.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,8 @@ func (c *Client) ParseTcpReq() (*protocol.SSProtocol, error) {
158158
return ssProtocol, nil
159159
}
160160

161-
func (c *Client) ParseReqData(buf []byte) ([]byte, error) {
161+
//ParseReqData parse request data
162+
func (c *Client) ParseReqData(buf []byte) (out []byte, err error) {
162163
const (
163164
dataLenLen = 2
164165
hmacSha1Len = 10
@@ -172,7 +173,7 @@ func (c *Client) ParseReqData(buf []byte) ([]byte, error) {
172173
buf = make([]byte, headerLen)
173174
}
174175

175-
if _, err := io.ReadFull(c, buf[:headerLen]); err != nil {
176+
if _, err = io.ReadFull(c, buf[:headerLen]); err != nil {
176177
return nil, err
177178
}
178179
dataLen := binary.BigEndian.Uint16(buf[:dataLenLen])
@@ -183,6 +184,14 @@ func (c *Client) ParseReqData(buf []byte) ([]byte, error) {
183184
buf = make([]byte, dataLen)
184185
}
185186

187+
defer func() {
188+
if r := recover(); r != nil {
189+
glog.Infof("bufferlen (%v) datalen(%v) headerlen(%v) dataend(%v)\r\n", len(buf), dataLen, headerLen, dataEndIdx)
190+
glog.Errorf("panic information %v\r\n", r)
191+
err = fmt.Errorf("panic in parse data")
192+
}
193+
}()
194+
186195
if _, err := io.ReadFull(c, buf[headerLen:dataEndIdx]); err != nil {
187196
return nil, err
188197
}

pkg/connection/tcp/tcp_server.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ func (tcpSrv *TCPServer) handleRequest(ctx context.Context, acceptConn net.Conn)
8585
if limitConn > uint(tcpSrv.Config.MaxConnection) {
8686
glog.Errorf("limit connection, refuse this connection(%v)", acceptConn)
8787
acceptConn.Close()
88+
return
8889
}
8990
}
9091
limitConn++

0 commit comments

Comments
 (0)