1
0
mirror of https://github.com/evilsocket/shieldwall.git synced 2021-09-07 00:28:37 +03:00

fix: do not reset firewall and apply rules if there were no changes

This commit is contained in:
Simone Margaritelli
2021-03-31 16:10:57 +02:00
parent c36338df0c
commit 1371b4849c
2 changed files with 33 additions and 12 deletions

View File

@@ -1,6 +1,8 @@
package main
import (
"crypto/sha256"
"encoding/json"
"flag"
"fmt"
"github.com/evilsocket/islazy/log"
@@ -8,6 +10,7 @@ import (
"github.com/evilsocket/shieldwall/version"
"os"
"os/signal"
"sort"
"syscall"
"time"
)
@@ -38,6 +41,26 @@ func addAllowRules(s *State) {
}
}
func hashObject(v interface{}) (string, error) {
if raw, err := json.Marshal(v); err != nil {
return "", err
} else {
return fmt.Sprintf("%x", sha256.Sum256(raw)), nil
}
}
func rulesHash(rules []firewall.Rule) string {
// make sure the order is always the same
sort.Slice(rules, func(i, j int) bool {
return rules[i].CreatedAt.Before(rules[j].CreatedAt)
})
hash, err := hashObject(rules)
if err != nil {
log.Warning("can't hash rules: %v", err)
}
return hash
}
func main() {
flag.Parse()
@@ -83,24 +106,22 @@ func main() {
api := NewAPI(conf.API)
// main loop
for {
prev := len(state.Rules)
prevHash := rulesHash(state.Rules)
if rules, err := api.FetchRules(); err != nil {
log.Error("error polling api: %v", err)
} else {
state.Rules = rules
if len(conf.Allow) > 0 {
addAllowRules(state)
}
num := len(state.Rules)
if num != prev {
log.Info("applying %d rules", num)
}
if err = firewall.Apply(state.Rules, conf.Drops); err != nil {
log.Fatal("%v", err)
newHash := rulesHash(state.Rules)
if prevHash != newHash {
log.Info("applying %d rules", len(state.Rules))
if err = firewall.Apply(state.Rules, conf.Drops); err != nil {
log.Fatal("%v", err)
}
} else {
log.Debug("no changes")
}
}

View File

@@ -1,3 +1,3 @@
package version
const Version = "1.2.1"
const Version = "1.2.0"