-
Notifications
You must be signed in to change notification settings - Fork 2
/
RedisBackend.go
173 lines (150 loc) · 3.95 KB
/
RedisBackend.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package gospam
import (
"context"
"encoding/json"
"fmt"
"log"
"strings"
"time"
"github.com/emersion/go-smtp"
"github.com/go-redis/redis/v8"
)
type RedisBackend struct {
client *redis.Client
acceptedDomains []string
acceptSubdomains bool
ctx context.Context
expiration time.Duration
}
func NewRedisBackend(addr string, password string, db int, acceptedDomains []string, acceptSubdomains bool, retentionHours int) Backend {
backend := &RedisBackend{
acceptedDomains: acceptedDomains,
acceptSubdomains: acceptSubdomains,
ctx: context.Background(),
expiration: time.Duration(retentionHours) * time.Hour,
}
backend.client = redis.NewClient(&redis.Options{
Addr: addr,
Password: password,
DB: db,
})
return backend
}
func (backend *RedisBackend) AnonymousLogin(c *smtp.ConnectionState) (smtp.Session, error) {
log.Printf("Anonymous login from %s\n", c.RemoteAddr.String())
return &Session{
remote: c.RemoteAddr,
backend: backend,
}, nil
}
func (backend *RedisBackend) NewSession(c smtp.ConnectionState, _ string) (smtp.Session, error) {
log.Printf("New Session with: %s\n", c.RemoteAddr.String())
return &Session{
remote: c.RemoteAddr,
backend: backend,
}, nil
}
func (b *RedisBackend) GetEmailById(id int) *EMail {
scanComplete := false
for cursor := uint64(0); !scanComplete; {
var keys []string
var err error
keys, cursor, err = b.client.Scan(b.ctx, cursor, fmt.Sprintf("*:%d", id), 100).Result()
if err != nil {
break
}
for _, key := range keys {
mailData := b.client.Get(b.ctx, key).Val()
email := &EMail{}
err := json.Unmarshal([]byte(mailData), email)
if err != nil {
log.Printf("Error unmarshalling email: %s\n", err)
continue
}
return email
}
if cursor == 0 {
scanComplete = true
}
}
return nil
}
func (b *RedisBackend) GetEmailsByAlias(alias string) []*EMail {
emails := make([]*EMail, 0)
scanComplete := false
for cursor := uint64(0); !scanComplete; {
var keys []string
var err error
keys, cursor, err = b.client.Scan(b.ctx, cursor, fmt.Sprintf("%s:*", alias), 100).Result()
if err != nil {
log.Printf("Error GetEmailsByAlias(): %s\n", err)
break
}
for _, key := range keys {
mailData := b.client.Get(b.ctx, key).Val()
email := &EMail{}
err := json.Unmarshal([]byte(mailData), email)
if err != nil {
log.Printf("Error unmarshalling email: %s\n", err)
continue
}
emails = append(emails, email)
}
if cursor == 0 {
scanComplete = true
}
}
return emails
}
func (b *RedisBackend) GetProcessedEmails() int {
currentId, err := b.client.Get(b.ctx, "email_id").Int()
if err != nil {
return 0
}
return currentId
}
func (b *RedisBackend) IsAcceptedDomain(email string) bool {
if len(b.acceptedDomains) == 0 {
return true
}
emailParts := strings.Split(email, "@")
domain := emailParts[len(emailParts)-1]
for _, d := range b.acceptedDomains {
if strings.EqualFold(d, domain) {
return true
} else if b.acceptSubdomains && strings.HasSuffix(domain, "."+d) {
return true
}
}
return false
}
func (backend *RedisBackend) Login(_ *smtp.ConnectionState, username, password string) (smtp.Session, error) {
return nil, smtp.ErrAuthUnsupported
}
func (b *RedisBackend) SaveEmail(email *EMail) {
emailId, err := b.client.Incr(b.ctx, "email_id").Result()
if err != nil {
log.Printf("RedisBackend error: %s\n", err)
return
}
email.ID = int(emailId)
mailData, err := json.Marshal(email)
if err != nil {
log.Printf("Error marshalling email: %s\n", err)
return
}
for _, to := range email.To {
alias := getAlias(to)
err = b.client.Set(b.ctx, fmt.Sprintf("%s:%d", alias, email.ID), string(mailData), b.expiration).Err()
if err != nil {
log.Printf("RedisBackend error: %s\n", err)
return
}
}
}
func getAlias(email string) string {
return strings.Split(email, "@")[0]
}
func (b *RedisBackend) Cleanup(deadline time.Time) {
// Nothing todo redis took care of this
}