commit 3be8783

delthas  ·  2022-11-04 13:57:09 +0000 UTC
parent b3377c5
Add a 10s timeout to connect + TLS handshake
1 files changed,  +11, -6
M app.go
M app.go
+11, -6
 1@@ -13,11 +13,11 @@ import (
 2 	"unicode"
 3 	"unicode/utf8"
 4 
 5-	"golang.org/x/net/proxy"
 6-
 7 	"git.sr.ht/~taiite/senpai/irc"
 8 	"git.sr.ht/~taiite/senpai/ui"
 9 	"github.com/gdamore/tcell/v2"
10+	"golang.org/x/net/context"
11+	"golang.org/x/net/proxy"
12 )
13 
14 const eventChanSize = 1024
15@@ -383,9 +383,14 @@ func (app *App) tryConnect() (conn net.Conn, err error) {
16 		}
17 	}
18 
19-	conn, err = proxy.FromEnvironment().Dial("tcp", addr)
20+	ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
21+
22+	dialer := &net.Dialer{
23+		Timeout: 10 * time.Second,
24+	}
25+	conn, err = proxy.FromEnvironmentUsing(dialer).(proxy.ContextDialer).DialContext(ctx, "tcp", addr)
26 	if err != nil {
27-		return
28+		return nil, fmt.Errorf("connect: %v", err)
29 	}
30 
31 	if tcpConn, ok := conn.(*net.TCPConn); ok {
32@@ -399,10 +404,10 @@ func (app *App) tryConnect() (conn net.Conn, err error) {
33 			ServerName: host,
34 			NextProtos: []string{"irc"},
35 		})
36-		err = conn.(*tls.Conn).Handshake()
37+		err = conn.(*tls.Conn).HandshakeContext(ctx)
38 		if err != nil {
39 			conn.Close()
40-			return nil, err
41+			return nil, fmt.Errorf("tls handshake: %v", err)
42 		}
43 	}
44