-
Notifications
You must be signed in to change notification settings - Fork 115
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement Rate Limit per remote #155
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
.env | ||
smtprelay |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
FROM golang:1.22-alpine AS build | ||
|
||
WORKDIR /app | ||
|
||
COPY go.mod ./ | ||
COPY go.sum ./ | ||
|
||
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 | ||
|
||
ENTRYPOINT ["./smtprelay"] |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,12 +10,16 @@ require ( | |
) | ||
|
||
require ( | ||
github.com/Hepri/rate_limiter v0.0.0-20170628005159-cf154f46b333 // indirect | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hm where is that dependency coming from? |
||
github.com/davecgh/go-spew v1.1.1 // indirect | ||
github.com/kr/pretty v0.3.1 // indirect | ||
github.com/pmezard/go-difflib v1.0.0 // indirect | ||
github.com/sethvargo/go-limiter v1.0.0 // indirect | ||
golang.org/x/sys v0.19.0 // indirect | ||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect | ||
gopkg.in/yaml.v3 v3.0.1 // indirect | ||
) | ||
|
||
go 1.20 | ||
go 1.22 | ||
|
||
toolchain go1.22.2 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -195,7 +195,7 @@ func mailHandler(peer smtpd.Peer, env smtpd.Envelope) error { | |
environ = append(environ, fmt.Sprintf("%s=%s", "SMTPRELAY_PEER", peerIP)) | ||
|
||
cmd := exec.Cmd{ | ||
Env: environ, | ||
Env: environ, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. whitespace change? |
||
Path: *command, | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,7 @@ | |
package main | ||
|
||
import ( | ||
"context" | ||
"crypto/tls" | ||
"encoding/base64" | ||
"errors" | ||
|
@@ -27,6 +28,8 @@ import ( | |
"net/smtp" | ||
"net/textproto" | ||
"strings" | ||
|
||
"github.com/chrj/smtpd" | ||
) | ||
|
||
// A Client represents a client connection to an SMTP server. | ||
|
@@ -321,6 +324,13 @@ var testHookStartTLS func(*tls.Config) // nil, except for tests | |
// functionality. Higher-level packages exist outside of the standard | ||
// library. | ||
func SendMail(r *Remote, from string, to []string, msg []byte) error { | ||
if r.RateLimiter != nil { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a copy from upstream Golang net/smtp and touching it should be avoided if possible. |
||
tokens, remaining, _, ok, err := (*r.RateLimiter).Take(context.Background(), "") | ||
if err != nil || !ok { | ||
return smtpd.Error{Code: 452, Message: "Rate limit reached"} | ||
} | ||
log.Debugf("Remaining %v tokens of %v", remaining, tokens) | ||
} | ||
if r.Sender != "" { | ||
from = r.Sender | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please respect #50 #54