git » chasquid » commit 008cd98

chasquid: Break connections after 10 errors

author Alberto Bertogli
2016-10-10 09:38:38 UTC
committer Alberto Bertogli
2016-10-21 21:13:39 UTC
parent aa0486b54e0f16810cdd99b863a011e9c90cb2f7

chasquid: Break connections after 10 errors

If a connection has accumulated 10 errors, it's very likely that
something has gone significantly wrong, or they're just probing/abusing
the service.

This patch makes chasquid break the connection after 10 errors.
The number is arbitrary, we may adjust it later.

chasquid.go +10 -1

diff --git a/chasquid.go b/chasquid.go
index a747bc6..e7bb41f 100644
--- a/chasquid.go
+++ b/chasquid.go
@@ -474,6 +474,7 @@ func (c *Conn) Handle() {
 
 	var cmd, params string
 	var err error
+	var errCount int
 
 loop:
 	for {
@@ -538,9 +539,17 @@ loop:
 		if code > 0 {
 			c.tr.Debugf("<- %d  %s", code, msg)
 
-			// Be verbose about errors, to help troubleshooting.
 			if code >= 400 {
+				// Be verbose about errors, to help troubleshooting.
 				c.tr.Errorf("%s failed: %d  %s", cmd, code, msg)
+
+				errCount++
+				if errCount > 10 {
+					// https://tools.ietf.org/html/rfc5321#section-4.3.2
+					c.tr.Errorf("too many errors, breaking connection")
+					c.writeResponse(421, "too many errors, bye")
+					break
+				}
 			}
 
 			err = c.writeResponse(code, msg)