-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
introduce a way to remain below a threshold for the mail throughput
- Loading branch information
Showing
10 changed files
with
202 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
FROM golang:1.22-alpine AS build | ||
WORKDIR /app | ||
|
||
|
||
|
||
COPY go.mod ./ | ||
COPY go.sum ./ | ||
|
||
RUN apk add git | ||
|
||
RUN go mod download | ||
|
||
COPY *.go ./ | ||
|
||
RUN go build -o smtprelay | ||
|
||
FROM golang:1.22-alpine | ||
|
||
WORKDIR /app | ||
|
||
COPY --from=build /app/smtprelay ./ | ||
|
||
EXPOSE 25 | ||
|
||
CMD ["./smtprelay", "-config", "smtprelay.ini"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"os" | ||
"strings" | ||
"sync" | ||
"time" | ||
|
||
"github.com/maypok86/otter" | ||
) | ||
|
||
var lock = &sync.Mutex{} | ||
|
||
type single struct { | ||
context context.Context | ||
cache otter.Cache[string, string] | ||
r *Remote | ||
} | ||
|
||
var remoteCache map[*Remote]*single = make(map[*Remote]*single) | ||
|
||
func getContext(r *Remote) *single { | ||
|
||
if remoteCache[r] == nil { | ||
lock.Lock() | ||
defer lock.Unlock() | ||
if remoteCache[r] == nil { | ||
remoteCache[r] = &single{} | ||
|
||
remoteCache[r].context = context.Background() | ||
remoteCache[r].r = r | ||
|
||
cache, err := otter.MustBuilder[string, string](10_000). | ||
CollectStats(). | ||
Cost(func(key string, value string) uint32 { | ||
return 1 | ||
}).DeletionListener(func(key, value string, cause otter.DeletionCause) { | ||
log.Infof("Evicted %s %s %v ", key, value, cause) | ||
|
||
parts := strings.Split(value, ";") | ||
if len(parts) < 3 { | ||
log.Info("Should have had at least three parts") | ||
} else { | ||
msg, err := os.ReadFile("/tmp/" + key + ".mail") | ||
if err != nil { | ||
log.Errorf("cannot read file %s", key+".mail") | ||
|
||
} else { | ||
from := parts[1] | ||
to := parts[2:] | ||
SendMail(r, from, to, msg) | ||
os.Remove("/tmp/" + key + ".mail") | ||
} | ||
} | ||
}). | ||
WithTTL(time.Minute). | ||
Build() | ||
|
||
if err != nil { | ||
panic(err) | ||
} | ||
remoteCache[r].cache = cache | ||
} | ||
} | ||
return remoteCache[r] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#! /bin/bash | ||
swaks --to [email protected] --cc [email protected] --from [email protected] --header "Subject: Test Email" --body "This is a test email sent using swaks." --server 127.0.0.1 --port 1025 | ||
swaks --to [email protected] --cc [email protected] --from [email protected] --header "Subject: Test Email" --body "This is a test email sent using swaks." --server 127.0.0.1 --port 1025 | ||
swaks --to [email protected] --cc [email protected] --from [email protected] --header "Subject: Test Email" --body "This is a test email sent using swaks." --server 127.0.0.1 --port 1025 | ||
swaks --to [email protected] --cc [email protected] --from [email protected] --header "Subject: Test Email" --body "This is a test email sent using swaks." --server 127.0.0.1 --port 1025 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,20 +8,20 @@ | |
;logfile = | ||
|
||
; Log format: default, plain (no timestamp), json | ||
;log_format = default | ||
log_format = default | ||
|
||
; Log level: panic, fatal, error, warn, info, debug, trace | ||
;log_level = info | ||
log_level = info | ||
|
||
; Hostname for this SMTP server | ||
;hostname = localhost.localdomain | ||
hostname = localhost.localdomain | ||
|
||
; Welcome message for clients | ||
;welcome_msg = <hostname> ESMTP ready. | ||
|
||
; Listen on the following addresses for incoming | ||
; unencrypted connections. | ||
;listen = 127.0.0.1:25 [::1]:25 | ||
listen = 127.0.0.1:1025 | ||
|
||
; STARTTLS and TLS are also supported but need a | ||
; SSL certificate and key. | ||
|
@@ -37,7 +37,6 @@ | |
; Only use remotes where FROM EMail address in received | ||
; EMail matches remote_sender. | ||
;strict_sender = false | ||
|
||
; Socket timeout for read operations | ||
; Duration string as sequence of decimal numbers, | ||
; each with optional fraction and a unit suffix. | ||
|
@@ -126,5 +125,10 @@ | |
; Multiple remotes, space delimited | ||
;remotes = smtp://127.0.0.1:1025 starttls://user:[email protected]:587 | ||
|
||
; rate limit | ||
; remotes = smtp://127.0.0.1:2525?rate=99/21m | ||
remotes = smtp://127.0.0.1:2525?rate=1/1m smtp://127.0.0.1:2527?rate=6/1m | ||
|
||
|
||
; Pipe messages to external command | ||
;command = /usr/local/bin/script |