-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added a connection limit so only one user can connect to the flipper at a time. Allowing multiple connections simultaneously doesn't make much sense for this tool.
- Loading branch information
Showing
2 changed files
with
57 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package main | ||
|
||
import ( | ||
"errors" | ||
"sync" | ||
|
||
"github.com/charmbracelet/wish" | ||
"github.com/gliderlabs/ssh" | ||
) | ||
|
||
type connLimiter struct { | ||
sync.Mutex | ||
conns int | ||
maxConns int | ||
} | ||
|
||
func newConnLimiter(maxConns int) *connLimiter { | ||
return &connLimiter{ | ||
maxConns: maxConns, | ||
} | ||
} | ||
|
||
func (u *connLimiter) Add() error { | ||
u.Lock() | ||
defer u.Unlock() | ||
if u.conns >= u.maxConns { | ||
return errors.New("max connections reached") | ||
} | ||
u.conns++ | ||
return nil | ||
} | ||
|
||
func (u *connLimiter) Remove() { | ||
u.Lock() | ||
defer u.Unlock() | ||
u.conns-- | ||
if u.conns < 0 { | ||
u.conns = 0 | ||
} | ||
} | ||
|
||
func connLimit(limiter *connLimiter) wish.Middleware { | ||
return func(sh ssh.Handler) ssh.Handler { | ||
return func(s ssh.Session) { | ||
if err := limiter.Add(); err != nil { | ||
wish.Fatalf(s, "max connections reached\n") | ||
return | ||
} | ||
sh(s) | ||
limiter.Remove() | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters