/
cache.go
42 lines (34 loc) · 817 Bytes
/
cache.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package api
import (
"encoding/json"
"github.com/evilsocket/islazy/log"
"github.com/evilsocket/shieldwall/database"
"github.com/evilsocket/shieldwall/firewall"
"gorm.io/datatypes"
"sync"
"time"
)
type cachedRules struct {
CachedAt time.Time
Rules datatypes.JSON
}
var cacheByAgentToken = sync.Map{}
func (api *API) expireRules(jsonbRules datatypes.JSON, doLog bool) (datatypes.JSON, int, error) {
var rules []firewall.Rule
expired := 0
notExpired := make([]firewall.Rule, 0)
if err := json.Unmarshal(jsonbRules, &rules); err != nil {
return nil, 0, err
}
for _, rule := range rules {
if rule.Expired() {
if doLog {
log.Info("rule expired %#v", rule)
}
expired++
} else {
notExpired = append(notExpired, rule)
}
}
return database.ToJSONB(notExpired), expired, nil
}