Skip to content

Commit

Permalink
[i] deferred removal from whitelist
Browse files Browse the repository at this point in the history
  • Loading branch information
GenZmeY committed Oct 10, 2021
1 parent 80b2d2d commit 65b2679
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 8 deletions.
4 changes: 4 additions & 0 deletions cmd/kf2-antiddos/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func printHelp() {
output.Println(" -j, --jobs N allow N jobs at once")
output.Println(" -o, --output MODE self|proxy|all|quiet")
output.Println(" -t, --deny-time TIME minimum ip deny TIME (seconds)")
output.Println(" -a, --allow-time TIME ip whitelist time after disconnect (seconds)")
output.Println(" -c, --max-connections N Skip N connections before run deny script")
output.Println(" -v, --version Show version")
output.Println(" -h, --help Show help")
Expand All @@ -41,6 +42,9 @@ func parseArgs() config.Config {
gnuflag.UintVar(&rawCfg.DenyTime, "t", 0, "")
gnuflag.UintVar(&rawCfg.DenyTime, "deny-time", 0, "")

gnuflag.UintVar(&rawCfg.AllowTime, "a", 0, "")
gnuflag.UintVar(&rawCfg.AllowTime, "allow-time", 0, "")

gnuflag.UintVar(&rawCfg.MaxConn, "c", 0, "")
gnuflag.UintVar(&rawCfg.MaxConn, "max-connections", 0, "")

Expand Down
1 change: 1 addition & 0 deletions cmd/kf2-antiddos/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ func main() {
&banChan,
&resetChan,
cfg.MaxConn,
cfg.AllowTime,
))

// Action worker
Expand Down
2 changes: 1 addition & 1 deletion internal/action/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (a *Action) allow(unbanAll bool) {
unban := make([]string, 0)

for ip := range a.ips {
if unbanAll || bool(a.ips[ip]) { // aka if readyToUnban
if unbanAll || a.ips[ip] { // aka if readyToUnban
unban = append(unban, ip)
} else {
a.ips[ip] = true // mark readyToUnban next time
Expand Down
4 changes: 4 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type Config struct {
Jobs uint
OutputMode string
DenyTime uint
AllowTime uint
MaxConn uint

ShowVersion bool
Expand Down Expand Up @@ -79,4 +80,7 @@ func (cfg *Config) SetEmptyArgs() {
if cfg.DenyTime == 0 {
cfg.DenyTime = 20 * 60
}
if cfg.AllowTime == 0 {
cfg.AllowTime = 20 * 60
}
}
33 changes: 26 additions & 7 deletions internal/history/history.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,31 @@ package history

import (
"kf2-antiddos/internal/common"
"time"
)

type History struct {
ticker *time.Ticker
quit chan struct{}
eventChan *chan common.Event
banChan *chan string
resetChan *chan string
head byte
history map[byte]common.Event
ips map[string]uint // map[ip]conn_count
whitelist map[string]struct{}
whitelist map[string]bool
banned map[string]struct{}
maxConn uint
workerID uint
}

func New(workerID uint, eventChan *chan common.Event, banChan *chan string, resetChan *chan string, maxConn uint) *History {
func New(workerID uint, eventChan *chan common.Event, banChan *chan string, resetChan *chan string, maxConn uint, allowTime uint) *History {
return &History{
ticker: time.NewTicker(time.Duration(allowTime) * time.Second),
quit: make(chan struct{}),
ips: make(map[string]uint, 0),
history: make(map[byte]common.Event, 0),
whitelist: make(map[string]struct{}, 0),
whitelist: make(map[string]bool, 0),
banned: make(map[string]struct{}, 0),
eventChan: eventChan,
banChan: banChan,
Expand All @@ -42,7 +45,10 @@ func (h *History) Do() {
h.registerEvent(event)
case ip := <-*h.resetChan:
h.resetIp(ip)
case <-h.ticker.C:
h.unWhiteList()
case <-h.quit:
h.ticker.Stop()
return
}
}
Expand Down Expand Up @@ -88,16 +94,29 @@ func (h *History) registerConnect(ip string) {
}

func (h *History) registerNewPlayer(ip string) {
h.whitelist[ip] = struct{}{}
h.whitelist[ip] = false
}

func (h *History) registerEndPlayer(ip string) {
delete(h.whitelist, ip)
delete(h.ips, ip)
delete(h.banned, ip)
h.whitelist[ip] = true
}

func (h *History) resetIp(ip string) {
delete(h.ips, ip)
delete(h.banned, ip)
}

func (h *History) unWhiteList() {
toRemove := make([]string, 0)
for ip := range h.whitelist {
if h.whitelist[ip] {
toRemove = append(toRemove, ip)
}
}

for _, ip := range toRemove {
delete(h.whitelist, ip)
delete(h.ips, ip)
delete(h.banned, ip)
}
}

0 comments on commit 65b2679

Please sign in to comment.