Skip to content

Commit

Permalink
skip i/o timeout logs
Browse files Browse the repository at this point in the history
  • Loading branch information
z0rr0 committed Sep 21, 2024
1 parent 68610cf commit 96ba894
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 25 deletions.
33 changes: 25 additions & 8 deletions conn/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package conn

import (
"context"
"errors"
"fmt"
"log"
"net"
Expand All @@ -25,22 +26,38 @@ func newIdleTimeoutConn(conn net.Conn, timeout time.Duration, logger *log.Logger

// Read reads data from the connection.
func (c *idleTimeoutConn) Read(b []byte) (int, error) {
if c.timeout > 0 {
if err := c.Conn.SetReadDeadline(time.Now().Add(c.timeout)); err != nil {
c.logger.Printf("failed to set read deadline: %v", err)
var netErr net.Error
n, err := c.Conn.Read(b)

if err != nil {
if errors.As(err, &netErr) && netErr.Timeout() {
c.logger.Printf("idleTimeoutConn read timeout from %s to %s", c.Conn.RemoteAddr(), c.Conn.LocalAddr())
}
} else if c.timeout > 0 {
if deadlineErr := c.Conn.SetReadDeadline(time.Now().Add(c.timeout)); deadlineErr != nil {
c.logger.Printf("idleTimeoutConn failed to set read deadline: %v", deadlineErr)
}
}
return c.Conn.Read(b)

return n, err
}

// Write writes data to the connection.
func (c *idleTimeoutConn) Write(b []byte) (int, error) {
if c.timeout > 0 {
if err := c.Conn.SetWriteDeadline(time.Now().Add(c.timeout)); err != nil {
c.logger.Printf("failed to set write deadline: %v", err)
var netErr net.Error
n, err := c.Conn.Write(b)

if err != nil {
if errors.As(err, &netErr) && netErr.Timeout() {
c.logger.Printf("idleTimeoutConn write timeout from %s to %s", c.Conn.RemoteAddr(), c.Conn.LocalAddr())
}
} else if c.timeout > 0 {
if deadlineErr := c.Conn.SetWriteDeadline(time.Now().Add(c.timeout)); deadlineErr != nil {
c.logger.Printf("idleTimeoutConn failed to set write deadline: %v", deadlineErr)
}
}
return c.Conn.Write(b)

return n, err
}

// Dial creates a new DialType.
Expand Down
4 changes: 2 additions & 2 deletions gsocks5.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ func main() {
debugMode bool
connections uint32 = 1024
port uint16 = 1080
timeoutIdle = 20 * time.Second
timeoutIdle = 30 * time.Second
timeoutDNS = 5 * time.Second
timeoutKeepAlive = 90 * time.Second
timeoutConn = 5 * time.Second
timeoutConn = 10 * time.Second
)
defer func() {
if r := recover(); r != nil {
Expand Down
9 changes: 5 additions & 4 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"log"
"net"
"os"
"strings"
"sync"
"time"

Expand Down Expand Up @@ -132,6 +133,7 @@ func (s *Server) start(p *Params, connections <-chan net.Conn, semaphore <-chan
}

func (s *Server) handle(p *Params, conn net.Conn, semaphore <-chan struct{}) {
const skipError = "i/o timeout"
var (
t = time.Now()
client = conn.RemoteAddr().String()
Expand All @@ -153,11 +155,10 @@ func (s *Server) handle(p *Params, conn net.Conn, semaphore <-chan struct{}) {
}()

if err = s.S.ServeConn(conn); err != nil {
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)
if errMsg := err.Error(); strings.HasSuffix(errMsg, skipError) {
s.logDebug.Printf("connection from %s is closed due to timeout: %v", client, err)
} else {
s.logInfo.Printf("failed to serve connection from client %q: %v", client, err)
s.logInfo.Printf("failed to serve connection from client %q [%T]: %v", client, err, err)
}
} else {
s.logDebug.Printf("connection served from %s during %v", client, time.Since(t))
Expand Down
27 changes: 16 additions & 11 deletions server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,17 @@ import (
"github.com/z0rr0/gsocks5/conn"
)

const timeout = 200 * time.Millisecond
const timeout = 250 * time.Millisecond

var logger = log.New(os.Stdout, "[test] ", log.LstdFlags|log.Lshortfile|log.Lmicroseconds)

func run(t *testing.T, s *Server, i, port int, isErr bool) (string, chan os.Signal) {
func run(t *testing.T, s *Server, i, port int, isErr bool, count uint32) (string, chan os.Signal) {
params := &Params{
Addr: net.JoinHostPort("localhost", strconv.Itoa(port)),
Connections: 1,
Connections: count,
Done: make(chan struct{}),
Sigint: make(chan os.Signal),
Timeout: timeout,
}

go func() {
Expand All @@ -48,30 +49,34 @@ func TestNew(t *testing.T) {
cases := []struct {
name string
port int
count uint32
hosts []testHost
err bool
}{
{name: "one", port: 1080, hosts: []testHost{{host: "github.com", port: 443}}},
{name: "one", port: 1080, count: 1, hosts: []testHost{{host: "github.com", port: 443}}},
{
name: "two",
port: 1080,
name: "two",
port: 1080,
count: 2,
hosts: []testHost{
{host: "github.com", port: 443},
{host: "leetcode.com", port: 443, close: true},
},
},
{
name: "three",
port: 1080,
name: "three",
port: 1080,
count: 3,
hosts: []testHost{
{host: "github.com", port: 443, close: true},
{host: "leetcode.com", port: 443, close: true},
{host: "leetcode.com", port: 80},
},
},
{
name: "many",
port: 1080,
name: "many",
port: 1080,
count: 10,
hosts: []testHost{
{host: "github.com", port: 443, close: true},
{host: "github.com", port: 80},
Expand All @@ -93,7 +98,7 @@ func TestNew(t *testing.T) {
tt.Errorf("case [%d] %s: unexpected error: %v", i, c.name, err)
}

addr, sigint := run(tt, s, i, c.port, c.err)
addr, sigint := run(tt, s, i, c.port, c.err, c.count)
defer close(sigint)

if c.err {
Expand Down

0 comments on commit 96ba894

Please sign in to comment.