diff options
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 } |