-
Notifications
You must be signed in to change notification settings - Fork 2
/
srs.go
279 lines (229 loc) · 6.85 KB
/
srs.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
package srs
import (
"crypto/hmac"
"crypto/sha1"
"encoding/base64"
"errors"
"math"
"net/mail"
"strings"
"time"
"unicode"
)
const (
hashLength = 4
sep = "="
timePrecision = float64(60 * 60 * 24)
timeSlots = float64(1024) // dont make mistakes like 2 ^ 10, since in go ^ is not power operator
maxAge = 21
)
// SRS engine
type SRS struct {
// Secret key, mandatory
Secret []byte
// Domain is localhost which will forward the emails
Domain string
// FirstSeparator after SRS0, optional, can be =+-, default is =
FirstSeparator string
defaultsChecked bool
}
// Forward returns SRS forward address or error
func (srs *SRS) Forward(email string) (string, error) {
srs.setDefaults()
var noDomain bool
if strings.HasSuffix(email, "@") {
email += srs.Domain
noDomain = true
}
local, hostname, err := parseEmail(email)
if err != nil {
return "", err
}
if noDomain {
hostname = ""
}
if hostname == srs.Domain {
return email, nil
}
if len(local) < 5 {
return srs.rewrite(local, hostname)
}
switch local[:5] {
case "SRS0=", "SRS0+", "SRS0-":
return srs.rewriteSRS0(local, hostname)
case "SRS1=", "SRS1+", "SRS1-":
return srs.rewriteSRS1(local, hostname)
default:
return srs.rewrite(local, hostname)
}
}
// rewrite email address
func (srs SRS) rewrite(local, hostname string) (string, error) {
ts := base32Encode(timestamp())
return "SRS0" + srs.FirstSeparator + srs.hash([]byte(strings.ToLower(ts+hostname+local))) + sep + ts + sep + hostname + sep + local + "@" + srs.Domain, nil
}
// rewriteSRS0 rewrites SRS0 address to SRS1
func (srs SRS) rewriteSRS0(local, hostname string) (string, error) {
srsLocal, srsHash, srsTimestamp, srsHost, srsUser, err := srs.parseSRS0(local)
if err != nil {
return "", errors.New("No user in SRS0 address")
}
hash := srs.hash([]byte(strings.ToLower(hostname + srsLocal)))
return "SRS1" + srs.FirstSeparator + hash + sep + hostname + sep + string(local[4]) + srsHash + sep + srsTimestamp + sep + srsHost + sep + srsUser + "@" + srs.Domain, nil
}
// parseSRS0 local part and return hash, ts, host and local
func (srs SRS) parseSRS0(local string) (srsLocal, srsHash, srsTimestamp, srsHost, srsUser string, err error) {
parts := strings.SplitN(local[5:], sep, 4)
if len(parts) < 4 {
return "", "", "", "", "", errors.New("No user in SRS0 address")
}
return local[4:], parts[0], parts[1], parts[2], parts[3], nil
}
// rewriteSRS1 rewrites SRS1 address to new SRS1
func (srs SRS) rewriteSRS1(local, hostname string) (string, error) {
srsLocal, _, srs1Host, srsHash, srsTimestamp, srsHost, srsUser, err := srs.parseSRS1(local)
if err != nil {
return "", err
}
hash := srs.hash([]byte(strings.ToLower(srs1Host + srsLocal)))
return "SRS1" + srs.FirstSeparator + hash + sep + srs1Host + sep + string(local[4]) + srsHash + sep + srsTimestamp + sep + srsHost + sep + srsUser + "@" + srs.Domain, nil
}
// parseSRS1 local part and return hash, ts, host and local
func (srs SRS) parseSRS1(local string) (srsLocal, srs1Hash, srs1Host, srsHash, srsTimestamp, srsHost, srsUser string, err error) {
var srs1Sep, srs1First, srs1Second string
for i := 0; i < len(local)-1; i++ {
sep := local[i : i+2]
if sep == "==" || sep == "=+" || sep == "=-" {
srs1Sep = string(local[i+1])
srs1First = local[0:i]
srs1Second = local[i+2:]
break
}
}
if srs1First == "" && srs1Second == "" {
return "", "", "", "", "", "", "", errors.New("No user in SRS1 address")
}
if len(srs1First) <= 8 {
return "", "", "", "", "", "", "", errors.New("Hash too short in SRS address")
}
srsLocal = srs1Sep + srs1Second
h := strings.SplitN(srs1First[5:], sep, 2)
if len(h) == 2 {
srs1Hash = h[0]
srs1Host = h[1]
}
parts := strings.SplitN(srs1Second, sep, 4)
if len(parts) < 4 {
return srsLocal, srs1Hash, srs1Host, "", "", "", "", nil
}
return srsLocal, srs1Hash, srs1Host, parts[0], parts[1], parts[2], parts[3], nil
}
// Reverse the SRS email address to regular email addresss or error
func (srs *SRS) Reverse(email string) (string, error) {
srs.setDefaults()
local, _, err := parseEmail(email)
if err != nil {
return "", errors.New("Not an SRS address")
}
if len(local) < 5 {
return "", errors.New("Not an SRS address")
}
switch local[:5] {
case "SRS0=", "SRS0+", "SRS0-":
_, srsHash, srsTimestamp, srsHost, srsUser, err := srs.parseSRS0(local)
if err != nil {
return "", err
}
if err := srs.checkTimestamp(srsTimestamp); err != nil {
return "", err
}
if srsHash != srs.hash([]byte(strings.ToLower(srsTimestamp+srsHost+srsUser))) {
return "", errors.New("Hash invalid in SRS address")
}
return srsUser + "@" + srsHost, nil
case "SRS1=", "SRS1+", "SRS1-":
srsLocal, srs1Hash, srs1Host, _, _, _, _, err := srs.parseSRS1(local)
if err != nil {
return "", err
}
if srs1Hash != srs.hash([]byte(strings.ToLower(srs1Host+srsLocal))) {
return "", errors.New("Hash invalid in SRS address")
}
return "SRS0" + srsLocal + "@" + srs1Host, nil
default:
return "", errors.New("Not an SRS address")
}
}
func (srs SRS) hash(input []byte) string {
mac := hmac.New(sha1.New, srs.Secret)
mac.Write(input)
s := base64.StdEncoding.EncodeToString(mac.Sum(nil))
return s[:hashLength]
}
// setDefaults parameters if not set
func (srs *SRS) setDefaults() {
if srs.defaultsChecked {
return
}
switch srs.FirstSeparator {
case "=", "+", "-":
default:
srs.FirstSeparator = "="
}
srs.defaultsChecked = true
}
// parseEmail and return username and domain name
func parseEmail(e string) (user, domain string, err error) {
if !strings.ContainsRune(e, '@') {
return "", "", errors.New("No at sign in sender address") // compatibility with postsrsd error message
}
addr, err := mail.ParseAddress(e)
if err != nil {
return "", "", errors.New("Bad formated email address")
}
parts := strings.SplitN(addr.Address, "@", 2)
if len(parts) != 2 {
return "", "", errors.New("No at sign in sender address")
}
return parts[0], parts[1], nil
}
// timestamp integer
func timestamp() int {
t := float64(time.Now().Unix())
x := math.Mod(t/timePrecision, timeSlots)
return int(x)
}
// checkTimestamp validity for illegal characters and out of date timestamp
func (srs *SRS) checkTimestamp(ts string) error {
// decode base32 encoded timestamp to `then``
then := 0
for _, c := range ts {
pos := strings.IndexRune(base32, unicode.ToUpper(c))
if pos == -1 {
return errors.New("Bad base32 character in timestamp")
}
then = then<<5 | pos
}
now := timestamp()
// mind the cycle of time slots
for now < then {
now = now + int(timeSlots)
}
if now <= then+maxAge {
return nil
}
return errors.New("Time stamp out of date")
}
const (
base32 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"
baseSize = 32
)
// base32Encode integer to string
func base32Encode(x int) (encoded string) {
for x > 0 {
r := x % baseSize
x /= baseSize
encoded = string(base32[r]) + encoded
}
return encoded
}