1package tlsrpt_test
2
3import (
4 "context"
5 "log"
6 "log/slog"
7 "strings"
8
9 "github.com/mjl-/mox/dns"
10 "github.com/mjl-/mox/tlsrpt"
11)
12
13func ExampleLookup() {
14 ctx := context.Background()
15 resolver := dns.StrictResolver{}
16 domain, err := dns.ParseDomain("domain.example")
17 if err != nil {
18 log.Fatalf("parsing domain: %v", err)
19 }
20
21 // Lookup TLSRPT record in DNS, and parse it.
22 record, txt, err := tlsrpt.Lookup(ctx, slog.Default(), resolver, domain)
23 if err != nil {
24 log.Fatalf("looking up tlsrpt record: %v", err)
25 }
26
27 log.Printf("TLSRPT record: %s", txt)
28 log.Printf("Parsed: %v", record)
29}
30
31func ExampleParseMessage() {
32 // Message, as received over SMTP.
33 msg := `From: <tlsrpt@mail.sender.example.com>
34To: <mts-sts-tlsrpt@example.net>
35Subject: Report Domain: example.net
36Report-ID: <735ff.e317+bf22029@example.net>
37TLS-Report-Domain: example.net
38TLS-Report-Submitter: mail.sender.example.com
39MIME-Version: 1.0
40Content-Type: application/tlsrpt+gzip
41Content-Transfer-Encoding: base64
42Content-Disposition: attachment;
43 filename="mail.sender.example!example.com!1013662812!1013749130.json.gz"
44
45H4sIAPZreGUAA51UbW/aMBD+DL/Cyr5NdeokJIVI0za1aPswdRWgiXWqImMbai2JI9tBMMR/n52Y
46lwkx2KQocXx3vud57s6bbscTcoFL/gtrLkpY4oJ5KfDuRVHhcg2n3o1xoVgzKHG5sLZNt9PxlMZS
47Q7uveRsRoiCBqAdRMEEobZ5nG9zxWEnPeYZRGg/M8+x1O1ubiYhSY6IhL+fC+iqtoGSVkJqXiw/E
48oVr5bIWLKmcNutYOObUBMUriXnhHYBjRCPbuCIazhCE46CUMI9YnvVkbVYmcE86UCfphUFpWbnPt
49SO7/oV5XzKFpKB0sSksDzJ1h95dMKiNkCsaT8TJw3h2vEJSlQDNleRx2Vyl46xeY5/6O2vqYWuuE
50VxlemOh+0kPIa3Zf/kRBhTmjtAjPHWNSwVeh9BHSs4nbDPa9bYI9VRcFlkeyaKFxDlVNCFNqXpul
51+dr2IaIubY44CpObY9+5SVVLduIYoego0c6LMm1Wag924yBLpupc78tBmGmLOSe2O9mq4pLRvWrK
52dJ2RGhYaQ161bYeClM76KZ4RarozCNP0UCDJCOPLJqJVajcJxSq4VCELm9ETbgFCjUNL7hyJZpJ0
53rmAptJG0sr38bzyiK3mEl3gcYneZIh/5QRD5cXKJrEG188CUcnuZmLLbMZZFc7XYA1+1rlR6e9tO
54rPJP5tlZMhsH3gNOwTvgJhoQAEEYARq9AWOn2aPQ451iwLtC7CXOOW1vOtdrfxE6GPT9OPBNGf0k
55vEak/jVVgDNMftbVf/ZUdGy3oyIZVo2dNudPYzTIvmXD0Sh7Gn2dfs+ePk4+Z1+Gj5/MZzi9Hw4f
56hg9Oqa4bc7N46W67vwF2Eq+hDAYAAA==
57`
58 msg = strings.ReplaceAll(msg, "\n", "\r\n")
59
60 // Parse the email message, and the TLSRPT report within.
61 reportJSON, err := tlsrpt.ParseMessage(slog.Default(), strings.NewReader(msg))
62 if err != nil {
63 log.Fatalf("parsing tlsrpt report in message: %v", err)
64 }
65
66 log.Printf("report: %#v", reportJSON)
67}
68