mirror of
https://github.com/shazow/ssh-chat.git
synced 2025-06-07 19:03:17 +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
|
For more, visit shazow.net or follow at twitter.com/shazow
|
||||||
`
|
`
|
||||||
|
|
||||||
|
const REQUIRED_WAIT time.Duration = time.Second / 2
|
||||||
|
|
||||||
type Client struct {
|
type Client struct {
|
||||||
Server *Server
|
Server *Server
|
||||||
Conn *ssh.ServerConn
|
Conn *ssh.ServerConn
|
||||||
@ -42,6 +44,7 @@ type Client struct {
|
|||||||
termWidth int
|
termWidth int
|
||||||
termHeight int
|
termHeight int
|
||||||
silencedUntil time.Time
|
silencedUntil time.Time
|
||||||
|
lastTX time.Time
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewClient(server *Server, conn *ssh.ServerConn) *Client {
|
func NewClient(server *Server, conn *ssh.ServerConn) *Client {
|
||||||
@ -52,11 +55,12 @@ func NewClient(server *Server, conn *ssh.ServerConn) *Client {
|
|||||||
Color: RandomColor(),
|
Color: RandomColor(),
|
||||||
Msg: make(chan string, MSG_BUFFER),
|
Msg: make(chan string, MSG_BUFFER),
|
||||||
ready: make(chan struct{}, 1),
|
ready: make(chan struct{}, 1),
|
||||||
|
lastTX: time.Now(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) ColoredName() string {
|
func (c *Client) ColoredName() string {
|
||||||
return ColorString(c.Color, c.Name)
|
return ColorString(c.Color, c.Name)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) Write(msg string) {
|
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)
|
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 {
|
if c.IsSilenced() || len(msg) > 1000 {
|
||||||
c.Msg <- fmt.Sprintf("-> Message rejected.")
|
c.Msg <- fmt.Sprintf("-> Message rejected.")
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
c.Server.Broadcast(msg, c)
|
c.Server.Broadcast(msg, c)
|
||||||
|
c.lastTX = time.Now()
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user