Browse Source

Apply a timeout to upstream dials in the server.

In my testing locally, these dials would time out after about 30 seconds
anyway:
	2021/04/20 23:26:46 begin session 54cafb53
	2021/04/20 23:26:47 begin stream 54cafb53:3
	2021/04/20 23:27:19 stream 54cafb53:3 handleStream: stream 54cafb53:3 connect upstream: dial tcp X.X.X.X:YYYY: connect: connection timed out
	2021/04/20 23:27:19 end stream 54cafb53:3
Which is in line with the documentation for net.Dialer:
	https://golang.org/pkg/net/#Dialer
	With or without a timeout, the operating system may impose its
	own earlier timeout. For instance, TCP timeouts are often around
	3 minutes.
But may as well be explicit.

This commit has the side effect of changing the error message from
"connection timed out" to "i/o timeout".
	2021/04/20 23:28:08 begin session 05b0a46e
	2021/04/20 23:28:09 begin stream 05b0a46e:3
	2021/04/20 23:28:39 stream 05b0a46e:3 handleStream: stream 05b0a46e:3 connect upstream: dial tcp X.X.X.X:YYYY: i/o timeout
	2021/04/20 23:28:39 end stream 05b0a46e:3
David Fifield 5 years ago
parent
commit
23759e203f
1 changed files with 7 additions and 1 deletions
  1. 7 1
      dnstt-server/main.go

+ 7 - 1
dnstt-server/main.go

@@ -71,6 +71,9 @@ const (
 	// to be the query timeout of the Quad9 DoH server.
 	// https://dnsencryption.info/imc19-doe.html Section 4.2, Finding 2.4
 	maxResponseDelay = 1 * time.Second
+
+	// How long to wait for a TCP connection to upstream to be established.
+	upstreamDialTimeout = 30 * time.Second
 )
 
 var (
@@ -182,7 +185,10 @@ func readKeyFromFile(filename string) ([]byte, error) {
 // handleStream bidirectionally connects a client stream with a TCP socket
 // addressed by upstream.
 func handleStream(stream *smux.Stream, upstream string, conv uint32) error {
-	upstreamConn, err := net.Dial("tcp", upstream)
+	dialer := net.Dialer{
+		Timeout: upstreamDialTimeout,
+	}
+	upstreamConn, err := dialer.Dial("tcp", upstream)
 	if err != nil {
 		return fmt.Errorf("stream %08x:%d connect upstream: %v", conv, stream.ID(), err)
 	}