mirror of
https://github.com/hanzoai/mail.git
synced 2026-09-23 23:53:02 +00:00
Fork of github.com/mjl-/mox at 5656ca6d1b
(upstream v0.0.15 plus 55 commits).
Rename the module path in go.mod and the import paths in 268 Go files. Also
update the five scripts that "go install" the module (genwebsite.sh,
docker-release.sh, apidiff.sh, test-upgrade.sh, Dockerfile.release), which
would otherwise build upstream rather than this fork.
Add NOTICE recording the upstream project, author, license, and fork point,
and stating which license covers which files: MIT for the mox source and our
changes, MPL-2.0 for publicsuffix/public_suffix_list.txt alone, and the
vendored dependencies' own licenses under licenses/. Embed NOTICE so
"mail licenses" prints the lineage from the running binary. LICENSE.MIT,
LICENSE.MPLv2.0 and the licenses/ tree are preserved verbatim; no code is
relicensed.
The mox command name, config keys, data directory layout and website content
are unchanged. LLM.md records the fork point, the rename, what was left alone,
and the steps to stand the server up.
Co-authored-by: Hanzo Dev <[email protected]>
86 lines
2.3 KiB
Go
86 lines
2.3 KiB
Go
package dmarc_test
|
|
|
|
import (
|
|
"context"
|
|
"log"
|
|
"log/slog"
|
|
"net"
|
|
"strings"
|
|
|
|
"github.com/hanzoai/mail/dkim"
|
|
"github.com/hanzoai/mail/dmarc"
|
|
"github.com/hanzoai/mail/dns"
|
|
"github.com/hanzoai/mail/message"
|
|
"github.com/hanzoai/mail/spf"
|
|
)
|
|
|
|
func ExampleLookup() {
|
|
ctx := context.Background()
|
|
resolver := dns.StrictResolver{}
|
|
msgFrom, err := dns.ParseDomain("sub.example.com")
|
|
if err != nil {
|
|
log.Fatalf("parsing from domain: %v", err)
|
|
}
|
|
|
|
// Lookup DMARC DNS record for domain.
|
|
status, domain, record, txt, authentic, err := dmarc.Lookup(ctx, slog.Default(), resolver, msgFrom)
|
|
if err != nil {
|
|
log.Fatalf("dmarc lookup: %v", err)
|
|
}
|
|
|
|
log.Printf("status %s, domain %s, record %v, txt %q, dnssec %v", status, domain, record, txt, authentic)
|
|
}
|
|
|
|
func ExampleVerify() {
|
|
ctx := context.Background()
|
|
resolver := dns.StrictResolver{}
|
|
|
|
// Message to verify.
|
|
msg := strings.NewReader("From: <[email protected]>\r\nMore: headers\r\n\r\nBody\r\n")
|
|
msgFrom, _, _, err := message.From(slog.Default(), true, msg, nil)
|
|
if err != nil {
|
|
log.Fatalf("parsing message for from header: %v", err)
|
|
}
|
|
|
|
// Verify SPF, for use with DMARC.
|
|
args := spf.Args{
|
|
RemoteIP: net.ParseIP("10.11.12.13"),
|
|
MailFromDomain: dns.Domain{ASCII: "sub.example.com"},
|
|
}
|
|
spfReceived, spfDomain, _, _, err := spf.Verify(ctx, slog.Default(), resolver, args)
|
|
if err != nil {
|
|
log.Printf("verifying spf: %v", err)
|
|
}
|
|
|
|
// Verify DKIM-Signature headers, for use with DMARC.
|
|
smtputf8 := false
|
|
ignoreTestMode := false
|
|
dkimResults, err := dkim.Verify(ctx, slog.Default(), resolver, smtputf8, dkim.DefaultPolicy, msg, ignoreTestMode)
|
|
if err != nil {
|
|
log.Printf("verifying dkim: %v", err)
|
|
}
|
|
|
|
// Verify DMARC, based on DKIM and SPF results.
|
|
applyRandomPercentage := true
|
|
useResult, result := dmarc.Verify(ctx, slog.Default(), resolver, msgFrom.Domain, dkimResults, spfReceived.Result, &spfDomain, applyRandomPercentage)
|
|
|
|
// Print results.
|
|
log.Printf("dmarc status: %s", result.Status)
|
|
log.Printf("use result: %v", useResult)
|
|
if useResult && result.Reject {
|
|
log.Printf("should reject message")
|
|
}
|
|
log.Printf("result: %#v", result)
|
|
}
|
|
|
|
func ExampleParseRecord() {
|
|
txt := "v=DMARC1; p=reject; rua=mailto:[email protected]"
|
|
|
|
record, isdmarc, err := dmarc.ParseRecord(txt)
|
|
if err != nil {
|
|
log.Fatalf("parsing dmarc record: %v (isdmarc: %v)", err, isdmarc)
|
|
}
|
|
|
|
log.Printf("parsed record: %v", record)
|
|
}
|