diff options
author | Kevin Chabowski <kevin@kch42.de> | 2014-03-27 15:17:37 +0100 |
---|---|---|
committer | Kevin Chabowski <kevin@kch42.de> | 2014-03-27 23:51:08 +0100 |
commit | ccdba88b73eae78984e31831079ff8798c2ddd59 (patch) | |
tree | b3a40da8b6d55dc173b05bb4b98e7a3ef205fdbd /websock.go | |
parent | addb75b981a2044a47adb7ff26850d6ff12b6144 (diff) | |
download | simplechat-ccdba88b73eae78984e31831079ff8798c2ddd59.tar.gz simplechat-ccdba88b73eae78984e31831079ff8798c2ddd59.tar.bz2 simplechat-ccdba88b73eae78984e31831079ff8798c2ddd59.zip |
Prevent users from flooding the server
Diffstat (limited to 'websock.go')
-rw-r--r-- | websock.go | 14 |
1 files changed, 14 insertions, 0 deletions
@@ -5,6 +5,7 @@ import ( "github.com/gorilla/mux" "github.com/kch42/simplechat/chat" "net/http" + "time" ) type JoinResponse struct { @@ -13,6 +14,11 @@ type JoinResponse struct { Buddies []string `json:"buddies,omitempty"` } +const ( + floodstopReset = 1 * time.Second + floodstopMax = 3 +) + func AcceptWebSock(rw http.ResponseWriter, req *http.Request) { vars := mux.Vars(req) roomname := vars["chatroom"] @@ -45,6 +51,9 @@ func AcceptWebSock(rw http.ResponseWriter, req *http.Request) { exit := make(chan struct{}) + floodstop := NewFloodstop(floodstopReset, floodstopMax) + defer floodstop.Stop() + go func() { var s string for { @@ -52,6 +61,11 @@ func AcceptWebSock(rw http.ResponseWriter, req *http.Request) { break } + if !floodstop.Ask() { + // User sent too many requests in a short amount of time. Kick them! + break + } + if s == "" { continue } |