git » 5medias » commit 94f7191

Set initial deadline to 15s

author Alberto Bertogli
2018-07-22 11:31:48 UTC
committer Alberto Bertogli
2018-07-22 11:31:48 UTC
parent bdd86e0b494bba9a81323fd6803f3323a244f22e

Set initial deadline to 15s

Currently, a client could open a connection and then never send any
requests or complete authentication for an indefinite amount of time.

This patch sets a deadline for the initial handshake and command of 15s,
so that if the client did not request a dial before then, the connection
is closed.

5medias.go +7 -0

diff --git a/5medias.go b/5medias.go
index bab4e4e..bafc907 100644
--- a/5medias.go
+++ b/5medias.go
@@ -69,6 +69,10 @@ func (c *Conn) Logf(f string, a ...interface{}) {
 func (c *Conn) Handle() {
 	defer c.conn.Close()
 
+	// Connetions should attempt to dial within 15s (this deadline is
+	// cancelled before dial below).
+	c.conn.SetDeadline(time.Now().Add(15 * time.Second))
+
 	c.Logf("connected")
 	if err := c.handshake(); err != nil {
 		c.Logf("handshake error: %v", err)
@@ -81,6 +85,9 @@ func (c *Conn) Handle() {
 		return
 	}
 
+	// Reset the deadline, now it'll be up to the dial.
+	c.conn.SetDeadline(time.Time{})
+
 	c.Logf("dial %q", dstAddr)
 	dstConn, err := net.DialTimeout("tcp", dstAddr, *dialTimeout)
 	if err != nil {