-
Notifications
You must be signed in to change notification settings - Fork 1
/
cache.go
53 lines (46 loc) · 1.34 KB
/
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
43
44
45
46
47
48
49
50
51
52
53
package srsmilter
import (
"time"
"blitiri.com.ar/go/spf"
"github.com/jellydator/ttlcache/v3"
)
func emptyTtlCache() *ttlcache.Cache[string, bool] {
return ttlcache.New[string, bool](
ttlcache.WithCapacity[string, bool](1*1024*1024*1024),
ttlcache.WithTTL[string, bool](30*time.Minute),
ttlcache.WithDisableTouchOnHit[string, bool](),
)
}
type Cache struct {
conf *Configuration
cache *ttlcache.Cache[string, bool]
}
func NewCache(conf *Configuration) *Cache {
return &Cache{
conf: conf,
cache: emptyTtlCache(),
}
}
func (c *Cache) IsLocalNotAllowedToSend(addr, asciiDomain string) bool {
if res := c.cache.Get(asciiDomain); res != nil {
return res.Value()
}
// Check if we are not authorized to send for `addr.Addr`
for _, ip := range c.conf.LocalIps {
result, _ := spf.CheckHostWithSender(ip, asciiDomain, addr)
// We rewrite when any of our IPs is not allowed to send
if result == spf.Fail || result == spf.SoftFail {
c.Set(asciiDomain, true)
return true
}
// if SPF record is empty or broken we quit early since checking with other IPs will not change result
if result == spf.None || result == spf.PermError {
break
}
}
c.Set(asciiDomain, false)
return false
}
func (c *Cache) Set(asciiDomain string, isLocalNotAllowedToSend bool) {
c.cache.Set(asciiDomain, isLocalNotAllowedToSend, ttlcache.DefaultTTL)
}