mirror of
https://github.com/shazow/ssh-chat.git
synced 2025-04-20 18:57:41 +03:00
Rate-limiting
This commit is contained in:
parent
e8ce9bfcff
commit
867b1ecde3
12
client.go
12
client.go
@ -30,6 +30,8 @@ const ABOUT_TEXT string = `-> ssh-chat is made by @shazow.
|
||||
For more, visit shazow.net or follow at twitter.com/shazow
|
||||
`
|
||||
|
||||
const REQUIRED_WAIT time.Duration = time.Second / 2
|
||||
|
||||
type Client struct {
|
||||
Server *Server
|
||||
Conn *ssh.ServerConn
|
||||
@ -42,6 +44,7 @@ type Client struct {
|
||||
termWidth int
|
||||
termHeight int
|
||||
silencedUntil time.Time
|
||||
lastTX time.Time
|
||||
}
|
||||
|
||||
func NewClient(server *Server, conn *ssh.ServerConn) *Client {
|
||||
@ -52,11 +55,12 @@ func NewClient(server *Server, conn *ssh.ServerConn) *Client {
|
||||
Color: RandomColor(),
|
||||
Msg: make(chan string, MSG_BUFFER),
|
||||
ready: make(chan struct{}, 1),
|
||||
lastTX: time.Now(),
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Client) ColoredName() string {
|
||||
return ColorString(c.Color, c.Name)
|
||||
return ColorString(c.Color, c.Name)
|
||||
}
|
||||
|
||||
func (c *Client) Write(msg string) {
|
||||
@ -229,11 +233,17 @@ func (c *Client) handleShell(channel ssh.Channel) {
|
||||
}
|
||||
|
||||
msg := fmt.Sprintf("%s: %s", c.ColoredName(), line)
|
||||
/* Rate limit */
|
||||
if time.Now().Sub(c.lastTX) < REQUIRED_WAIT {
|
||||
c.Msg <- fmt.Sprintf("-> Rate limiting in effect.")
|
||||
continue
|
||||
}
|
||||
if c.IsSilenced() || len(msg) > 1000 {
|
||||
c.Msg <- fmt.Sprintf("-> Message rejected.")
|
||||
continue
|
||||
}
|
||||
c.Server.Broadcast(msg, c)
|
||||
c.lastTX = time.Now()
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user