Message rate limiting, input length, and ignore empty lines.

This commit is contained in:
Andrey Petrov 2015-01-18 19:11:35 -08:00
parent 0c21486992
commit 6c972e6e58

20
host.go
View File

@ -5,11 +5,15 @@ import (
"fmt" "fmt"
"io" "io"
"strings" "strings"
"time"
"github.com/shazow/rateio"
"github.com/shazow/ssh-chat/chat" "github.com/shazow/ssh-chat/chat"
"github.com/shazow/ssh-chat/sshd" "github.com/shazow/ssh-chat/sshd"
) )
const maxInputLength int = 1024
// GetPrompt will render the terminal prompt string based on the user. // GetPrompt will render the terminal prompt string based on the user.
func GetPrompt(user *chat.User) string { func GetPrompt(user *chat.User) string {
name := user.Name() name := user.Name()
@ -96,6 +100,7 @@ func (h *Host) Connect(term *sshd.Terminal) {
// Should the user be op'd on join? // Should the user be op'd on join?
member.Op = h.isOp(term.Conn) member.Op = h.isOp(term.Conn)
ratelimit := rateio.NewSimpleLimiter(3, time.Second*3)
for { for {
line, err := term.ReadLine() line, err := term.ReadLine()
@ -106,6 +111,21 @@ func (h *Host) Connect(term *sshd.Terminal) {
logger.Errorf("Terminal reading error: %s", err) logger.Errorf("Terminal reading error: %s", err)
break break
} }
err = ratelimit.Count(1)
if err != nil {
user.Send(chat.NewSystemMsg("Message rejected: Rate limiting is in effect.", user))
continue
}
if len(line) > maxInputLength {
user.Send(chat.NewSystemMsg("Message rejected: Input too long.", user))
continue
}
if line == "" {
// Silently ignore empty lines.
continue
}
m := chat.ParseInput(line, user) m := chat.ParseInput(line, user)
// FIXME: Any reason to use h.room.Send(m) instead? // FIXME: Any reason to use h.room.Send(m) instead?