/
pool.go
352 lines (336 loc) · 9.32 KB
/
pool.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
// vim: tabstop=2 shiftwidth=2
package main
import (
"encoding/hex"
"errors"
"io/ioutil"
"net/mail"
"os"
"path"
"strings"
"time"
//"github.com/codahale/blake2"
"github.com/Masterminds/log-go"
"github.com/crooks/yamn/crandom"
"github.com/crooks/yamn/keymgr"
"github.com/luksen/maildir"
)
// serverPoolOutboundSend is intended to be run concurrently with the server
// daemon. It sends messages from the pool at timed intervals.
func serverPoolOutboundSend() {
if cfg.Pool.Loop < 120 {
log.Warnf(
"Pool loop of %d Seconds is too short. "+
"Adjusting to minimum of 120 Seconds.",
cfg.Pool.Loop,
)
cfg.Pool.Loop = 120
}
sleepFor := time.Duration(cfg.Pool.Loop) * time.Second
for {
// Read dynamic mix of outbound files from the Pool
// filenames = dynamicMix()
// Read binomialMix of outbound files from the Pool
filenames := binomialMix()
for _, filename := range filenames {
emailPoolFile(filename)
}
time.Sleep(sleepFor)
}
}
// poolOutboundSend flushes the outbound pool. This should only be performed
// on clients, where all messages should be sent instantly after creation.
func poolOutboundSend() {
var err error
if cfg.Remailer.Daemon || flag.Daemon {
// This should never happen. If the server is started as a
// daemon, the serverPoolOutboundSend process is initiated and
// runs in an endless loop. This function would conflict with
// it.
err = errors.New("cannot flush pool when running as a daemon")
panic(err)
}
var filenames []string
// Read all the pool files
filenames, err = readDir(cfg.Files.Pooldir, "m")
if err != nil {
log.Warnf("Reading pool failed: %s", err)
return
}
if flag.Remailer {
// During normal operation, the pool shouldn't be flushed.
log.Warn("Flushing outbound remailer pool")
}
for _, filename := range filenames {
emailPoolFile(filename)
}
}
// emailPoolFile tries to email a given file from the Pool. If conditions are
// met, the file is then deleted.
func emailPoolFile(filename string) {
delFlag, err := mailPoolFile(path.Join(cfg.Files.Pooldir, filename))
if err != nil {
log.Warnf("Pool mailing failed: %s", err)
if delFlag {
// If delFlag is true, we delete the file, even though
// mailing failed.
poolDelete(filename)
}
} else {
stats.outMail++
poolDelete(filename)
}
}
// dynamicMix returns a dynamic Mix of filenames from the outbound pool.
func dynamicMix() []string {
var empty []string
poolFiles, err := readDir(cfg.Files.Pooldir, "m")
if err != nil {
log.Warnf("Unable to access pool: %s", err)
return empty
}
poolSize := len(poolFiles)
if poolSize < cfg.Pool.Size || poolSize == 0 {
// Pool isn't sufficiently populated
log.Tracef(
"Pool insufficiently populated to trigger sending."+
"Require=%d, Got=%d",
cfg.Pool.Size,
poolSize,
)
return empty
}
// Shuffle the slice of filenames now as we're going to return a
// setset of the overall pool.
crandom.Shuffle(poolFiles)
// Normal pool processing condition
numToSend := int((float32(poolSize) / 100.0) * float32(cfg.Pool.Rate))
log.Tracef("Processing %d pool messages.\n", poolSize)
return poolFiles[:numToSend]
}
// getBatchSize takes a Pool size and returns a corresponding batch size. This
// is intended for use with Binomial Mix Pools.
func getBatchSize(poolSize int) int {
/*
poolSize - Number of files in the pool
cfg.Pool.Size - Minimum messages to keep in pool
cfg.Pool.MinSend - Minimum number of messages to consider sending
cfg.Pool.Rate - Percentage of Pool in the batch
*/
if poolSize < (cfg.Pool.Size + cfg.Pool.MinSend) {
return 0
}
sendable := poolSize - cfg.Pool.Size
rate := float32(cfg.Pool.Rate) / 100
maxSend := max(1, int(float32(poolSize)*rate))
return min(sendable, maxSend)
}
// binomialMix returns a batched subset of Pool files to send using a
// Probability B/P method of selecting each file.
func binomialMix() (batch []string) {
poolFiles, err := readDir(cfg.Files.Pooldir, "m")
if err != nil {
log.Warnf("Unable to access pool: %s", err)
return
}
poolSize := len(poolFiles)
batchSize := getBatchSize(poolSize)
if batchSize == 0 {
log.Tracef("Binomial Mix Pool: Size=%d", poolSize)
// If the batch is empty, don't bother to process it.
return
}
// Shuffle the slice of filenames now as we're only going to consider a
// subset in the following loop.
crandom.Shuffle(poolFiles)
// Multiply probability by 255 as dice() returns 0-255.
prob := int((float32(batchSize) / float32(poolSize)) * 255)
// Test each pool filename against a biased coin-toss
for _, s := range poolFiles[:batchSize] {
if prob >= crandom.Dice() {
batch = append(batch, s)
}
}
log.Tracef(
"Binomial Mix Pool: Size=%d, Batch=%d, Prob=%d/255, Sending=%d",
poolSize,
batchSize,
prob,
len(batch),
)
return
}
// Delete a given file from the pool
func poolDelete(filename string) {
// Delete a pool file
err := os.Remove(path.Join(cfg.Files.Pooldir, filename))
if err != nil {
log.Errorf("Failed to remove %s from %s\n", filename, cfg.Files.Pooldir)
} else {
log.Tracef("Deleted %s from Pool", filename)
}
}
// processMail reads the Remailer's Maildir and processes the content
func processMail(secret *keymgr.Secring) (err error) {
dir := maildir.Dir(cfg.Files.Maildir)
// Get a list of Maildir keys from the directory
keys, err := dir.Unseen()
if err != nil {
return
}
newMsgs := len(keys)
if newMsgs == 0 {
// Nothing to do, move along!
return
}
log.Tracef(
"Reading %d messages from %s\n",
newMsgs,
cfg.Files.Maildir,
)
// Increment inbound Email counter
stats.inMail += newMsgs
// Fetch headers for each Maildir key
var head mail.Header
for _, key := range keys {
head, err = dir.Header(key)
if err != nil {
log.Warnf("%s: Getting headers failed with: %s", key, err)
continue
}
// The Subject determines if the message needs remailer-foo handling
subject := strings.TrimSpace(strings.ToLower(head.Get("Subject")))
if strings.HasPrefix(subject, "remailer-") {
// It's a remailer-foo request
err = remailerFoo(subject, head.Get("From"))
if err == nil {
// Increments stats counter
stats.inRemFoo++
} else {
log.Info(err)
}
err = dir.Purge(key)
if err != nil {
log.Warnf(
"Cannot delete remailer-foo mail: %s",
err,
)
}
// Nothing else to do, move on to the next message
continue
}
// It's not a remailer-foo request so assume a remailer message
var mailMsg *mail.Message
mailMsg, err := dir.Message(key)
if err != nil {
log.Warnf(
"%s: Reading message failed with: %s",
key,
err,
)
continue
}
var msg []byte
// Convert the armored Yamn message to its byte components
msg, err = stripArmor(mailMsg.Body)
if err != nil {
log.Info(err)
continue
}
if msg == nil {
log.Warn("Dearmor returned zero bytes")
continue
}
err = decodeMsg(msg, secret)
if err != nil {
log.Info(err)
}
err = dir.Purge(key)
if err != nil {
log.Warnf("Cannot delete mail: %s", err)
}
} // Maildir keys loop
return
}
// processInpool is similar to processMail but reads the Inbound Pool
func processInpool(prefix string, secret *keymgr.Secring) {
poolFiles, err := readDir(cfg.Files.Pooldir, prefix)
if err != nil {
log.Warnf("Unable to access inbound pool: %s", err)
return
}
poolSize := len(poolFiles)
processed := 0
for _, f := range poolFiles {
filename := path.Join(cfg.Files.Pooldir, f)
msg := make([]byte, messageBytes)
msg, err = ioutil.ReadFile(filename)
if err != nil {
log.Warnf("Failed to read %s from pool: %s", f, err)
continue
}
err = decodeMsg(msg, secret)
if err != nil {
log.Warn(err)
}
poolDelete(f)
processed++
}
if poolSize > 0 {
log.Tracef("Inbound pool processing complete. Read=%d, Decoded=%d",
poolSize, processed)
}
}
// randPoolFilename returns a random filename with a given prefix. This should
// be used in all instances where a new pool file is required.
func randPoolFilename(prefix string) (fqfn string) {
for {
outfileName := prefix + hex.EncodeToString(crandom.Randbytes(7))
fqfn = path.Join(cfg.Files.Pooldir, outfileName)
_, err := os.Stat(fqfn)
if err != nil {
// For once we want an error (indicating the file
// doesn't exist)
break
}
}
return
}
// newPoolFile opens a new file in Write mode and sets user-only permissions
func newPoolFile(prefix string) (f *os.File, err error) {
/*
Currently supported prefixs are:-
[ m Oubound message (final or intermediate) ]
[ i Inbound message (destined for this remailer ]
[ p Partial message chunk needing assembly ]
*/
fqfn := randPoolFilename(prefix)
f, err = os.OpenFile(fqfn, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0600)
return
}
// writeMessageToPool requires a recipient address (another remailer) and a
// payload (that gets Base64 armored).
func writeMessageToPool(sendTo string, payload []byte) {
f, err := newPoolFile("m")
if err != nil {
panic(err)
}
defer f.Close()
// Add mail headers to the pool file
writeInternalHeader(f)
writeMailHeaders(f, sendTo)
// Armor the payload
armor(f, payload)
}
// writePlainToPool writes a plaintext file to the pool and returns the filename
func writePlainToPool(payload []byte, prefix string) (filename string) {
f, err := newPoolFile(prefix)
if err != nil {
panic(err)
}
defer f.Close()
writeInternalHeader(f)
f.Write(payload)
_, filename = path.Split(f.Name())
return
}