Skip to content

Commit

Permalink
timeout correction
Browse files Browse the repository at this point in the history
  • Loading branch information
z0rr0 committed Sep 21, 2024
1 parent 44df462 commit 68610cf
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
6 changes: 3 additions & 3 deletions gsocks5.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ func main() {
port uint16 = 1080
timeoutIdle = 20 * time.Second
timeoutDNS = 5 * time.Second
timeoutKeepAlive = 30 * time.Second
timeoutConn = 10 * time.Second
timeoutKeepAlive = 90 * time.Second
timeoutConn = 5 * time.Second
)
defer func() {
if r := recover(); r != nil {
Expand Down Expand Up @@ -122,7 +122,7 @@ func main() {
addr, customDNS, connections, debugMode, authFile,
)

params := &server.Params{Addr: addr, Connections: connections, Sigint: sigint}
params := &server.Params{Addr: addr, Connections: connections, Sigint: sigint, Timeout: timeoutConn}
if err = s.ListenAndServe(params); err != nil {
logInfo.Printf("server listen error: %s", err)
}
Expand Down
19 changes: 13 additions & 6 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type Params struct {
Connections uint32
Done chan struct{} // only for testing
Sigint chan os.Signal
Timeout time.Duration
setReady sync.Once
wg sync.WaitGroup
listener net.Listener
Expand Down Expand Up @@ -86,7 +87,7 @@ func (s *Server) listen(ctx context.Context, p *Params, done chan<- struct{}) (<
go func() {
for {
semaphore <- struct{}{} // limit connections, Server.handle will release it
if conn, e := s.accept(listener); e != nil {
if conn, e := s.accept(listener, p); e != nil {
if errors.Is(e, net.ErrClosed) {
break
}
Expand All @@ -106,13 +107,19 @@ func (s *Server) listen(ctx context.Context, p *Params, done chan<- struct{}) (<
}

// accept accepts a new connection.
func (s *Server) accept(listener net.Listener) (net.Conn, error) {
func (s *Server) accept(listener net.Listener, p *Params) (net.Conn, error) {
conn, err := listener.Accept()
if err != nil {
return nil, fmt.Errorf("failed to accept connection: %w", err)
}

s.logDebug.Printf("accepted connection from %s", conn.RemoteAddr().String())
if p.Timeout > 0 {
if err = conn.SetReadDeadline(time.Now().Add(p.Timeout)); err != nil {
return nil, fmt.Errorf("failed to set read deadline for connection: %w", err)
}
}

s.logDebug.Printf("accepted connection from %s with timeout %v", conn.RemoteAddr().String(), p.Timeout)
return conn, nil
}

Expand Down Expand Up @@ -146,9 +153,9 @@ func (s *Server) handle(p *Params, conn net.Conn, semaphore <-chan struct{}) {
}()

if err = s.S.ServeConn(conn); err != nil {
var nErr net.Error
if errors.As(err, &nErr) && nErr.Timeout() {
s.logDebug.Printf("connection from %s is closed due to timeout: %v", client, err)
var netErr net.Error
if errors.As(err, &netErr) && netErr.Timeout() {
s.logDebug.Printf("connection from %s is closed due to timeout [%T]: %v", client, err, err)
} else {
s.logInfo.Printf("failed to serve connection from client %q: %v", client, err)
}
Expand Down

0 comments on commit 68610cf

Please sign in to comment.