Skip to content

Commit

Permalink
apiban-iptables-client.go: FLUSH
Browse files Browse the repository at this point in the history
  * add FLUSH config and processing
  * dont send if default key present

config.json: add FLUSH
  • Loading branch information
fredposner committed Jul 29, 2021
1 parent 7762c27 commit 1aa41aa
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 4 deletions.
Binary file modified clients/go/apiban-iptables-client
Binary file not shown.
33 changes: 31 additions & 2 deletions clients/go/apiban-iptables/apiban-iptables-client.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2020 Fred Posner (palner.com)
* Copyright (C) 2020-2021 Fred Posner (palner.com)
*
* This file is part of APIBAN.org.
*
Expand Down Expand Up @@ -29,6 +29,8 @@ import (
"log"
"os"
"runtime"
"strconv"
"time"

"github.com/coreos/go-iptables/iptables"
"github.com/palner/apiban/clients/go/apiban"
Expand All @@ -49,6 +51,7 @@ type ApibanConfig struct {
APIKEY string `json:"APIKEY"`
LKID string `json:"LKID"`
VERSION string `json:"VERSION"`
FLUSH string `json:"FLUSH"`

sourceFile string
}
Expand Down Expand Up @@ -82,7 +85,8 @@ func main() {
}

log.Print("** Started APIBAN CLIENT")
log.Print("Licensed under GPLv2. See LICENSE for details.")
log.Print("** Licensed under GPLv2. See LICENSE for details.")
now := time.Now()

// Open our config file
apiconfig, err := LoadConfig()
Expand All @@ -95,6 +99,10 @@ func main() {
log.Fatalln("Invalid APIKEY. Exiting.")
}

if apiconfig.APIKEY == "MY API KEY" {
log.Fatalln("Invalid APIKEY. Exiting. Go to apiban.org and get an api key.")
}

// allow cli of FULL to reset LKID to 100
if len(os.Args) > 1 {
arg1 := os.Args[1]
Expand All @@ -112,6 +120,13 @@ func main() {
apiconfig.LKID = "100"
}

// if no LKID, reset it to 100
if len(apiconfig.FLUSH) == 0 {
log.Print("Resetting FLUSH")
flushnow := now.Unix()
apiconfig.FLUSH = strconv.FormatInt(flushnow, 10)
}

// Go connect for IPTABLES
ipt, err := iptables.New()
if err != nil {
Expand All @@ -132,6 +147,20 @@ func main() {
apiconfig.LKID = "100"
}

flushtime, _ := strconv.ParseInt(apiconfig.FLUSH, 10, 64)
flushdiff := now.Unix() - flushtime
if flushdiff >= 604800 {
err = ipt.ClearChain("filter", "APIBAN")
if err != nil {
log.Print("Flushing APIBAN chain failed. ", err.Error())
} else {
log.Print("APIBAN chain flushed")
}

apiconfig.LKID = "100"
apiconfig.FLUSH = strconv.FormatInt(now.Unix(), 10)
}

// Get list of banned ip's from APIBAN.org
res, err := apiban.Banned(apiconfig.APIKEY, apiconfig.LKID)
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion clients/go/apiban-iptables/config.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"APIKEY":"MY API KEY",
"LKID":"100",
"VERSION":"0.6"
"VERSION":"0.7",
"FLUSH":"200"
}
2 changes: 1 addition & 1 deletion clients/go/apiban/apiban.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2020 Fred Posner (palner.com)
* Copyright (C) 2020-2021 Fred Posner (palner.com)
*
* This file is part of APIBAN.org.
*
Expand Down

0 comments on commit 1aa41aa

Please sign in to comment.