Configurable rate limiting for sshd
authorAndrey Petrov <andrey.petrov@shazow.net>
Fri, 16 Jan 2015 20:35:57 +0000 (12:35 -0800)
committerAndrey Petrov <andrey.petrov@shazow.net>
Fri, 16 Jan 2015 20:36:34 +0000 (12:36 -0800)
cmd.go
sshd/net.go

diff --git a/cmd.go b/cmd.go
index 9a222092eda75fb98dd3e48aa2ea383e18ba624a..a763885a649646f8c7c0d75e0a351b761994d5f9 100644 (file)
--- a/cmd.go
+++ b/cmd.go
@@ -102,6 +102,7 @@ func main() {
                os.Exit(4)
        }
        defer s.Close()
+       s.RateLimit = true
 
        fmt.Printf("Listening for connections on %v\n", s.Addr().String())
 
index 7ded1e72cf4abde54db83190966c604686c3e43b..5e782d8c98a7211a531233c489815d2a1cad059d 100644 (file)
@@ -11,7 +11,8 @@ import (
 // Container for the connection and ssh-related configuration
 type SSHListener struct {
        net.Listener
-       config *ssh.ServerConfig
+       config    *ssh.ServerConfig
+       RateLimit bool
 }
 
 // Make an SSH listener socket
@@ -20,13 +21,17 @@ func ListenSSH(laddr string, config *ssh.ServerConfig) (*SSHListener, error) {
        if err != nil {
                return nil, err
        }
-       l := SSHListener{socket, config}
+       l := SSHListener{Listener: socket, config: config}
        return &l, nil
 }
 
 func (l *SSHListener) handleConn(conn net.Conn) (*Terminal, error) {
+       if l.RateLimit {
+               // TODO: Configurable Limiter?
+               conn = ReadLimitConn(conn, rateio.NewGracefulLimiter(1000, time.Minute*2, time.Second*3))
+       }
+
        // Upgrade TCP connection to SSH connection
-       conn = ReadLimitConn(conn, rateio.NewGracefulLimiter(1000, time.Minute*2, time.Second*3))
        sshConn, channels, requests, err := ssh.NewServerConn(conn, l.config)
        if err != nil {
                return nil, err