重构代码结构,优化连接管理和日志记录
This commit is contained in:
@@ -253,7 +253,13 @@ func (s *Server) handleConnect(ctx context.Context, conn net.Conn, req *Request)
|
||||
}
|
||||
|
||||
slog.Info("需要向 " + req.DestAddr.Address() + " 建立连接")
|
||||
s.Conn <- ProxyConn{conn, req.realDestAddr.Address()}
|
||||
select {
|
||||
case <-s.ctx.Done():
|
||||
if conn != nil {
|
||||
utils.Close(conn)
|
||||
}
|
||||
case s.Conn <- ProxyConn{conn, req.realDestAddr.Address()}:
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
@@ -122,12 +122,13 @@ func (s *Server) Run() error {
|
||||
for loop := true; loop; {
|
||||
select {
|
||||
case <-s.ctx.Done():
|
||||
slog.Debug("服务主动停止")
|
||||
slog.Debug("socks 服务主动停止")
|
||||
loop = false
|
||||
case conn, ok := <-connCh:
|
||||
if !ok {
|
||||
err = errors.New("意外错误,无法获取连接")
|
||||
loop = false
|
||||
s.Close()
|
||||
break
|
||||
}
|
||||
s.wg.Add(1)
|
||||
@@ -141,9 +142,6 @@ func (s *Server) Run() error {
|
||||
}()
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
s.Close()
|
||||
}
|
||||
|
||||
// 关闭服务
|
||||
timeout, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
@@ -157,10 +155,7 @@ func (s *Server) Run() error {
|
||||
case <-wgCh:
|
||||
}
|
||||
|
||||
if s.Conn != nil {
|
||||
close(s.Conn)
|
||||
}
|
||||
|
||||
close(s.Conn)
|
||||
return err
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user