caelum0x 173b60097d feat(mail): OCI relay pool, direct MTA (parked), inbound HTTPS ingest, Cloudflare inbound forwarder
- mail-oci: OCI Email Delivery client (signed REST), per-customer domain/DKIM/sender onboarding, suppression mirror, sending limits
- mail-mta: RelayPool transport with OCI primary, optional secondary, health/back-off/quota failover; direct MTA kept but off
- mail-inbound: DSN/ARF/DMARC/TLS-RPT parsing + HMAC-signed POST /inbound/raw ingest (no port 25)
- workers/inbound-forwarder: Cloudflare Email Routing worker forwarding raw mail over HTTPS

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-11-13 01:14:52 +03:00

230 lines
8.2 KiB
Rust

//! Hand-written sample reports used by the `feed` example.
use std::io::Write;
fn base64(bytes: &[u8]) -> String {
const T: &[u8; 64] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
let mut out = String::with_capacity(bytes.len() * 4 / 3 + 4);
for chunk in bytes.chunks(3) {
let b = [chunk[0], *chunk.get(1).unwrap_or(&0), *chunk.get(2).unwrap_or(&0)];
let n = (u32::from(b[0]) << 16) | (u32::from(b[1]) << 8) | u32::from(b[2]);
out.push(T[(n >> 18) as usize & 63] as char);
out.push(T[(n >> 12) as usize & 63] as char);
out.push(if chunk.len() > 1 { T[(n >> 6) as usize & 63] as char } else { '=' });
out.push(if chunk.len() > 2 { T[n as usize & 63] as char } else { '=' });
}
let mut wrapped = String::with_capacity(out.len() + out.len() / 76 * 2);
for line in out.as_bytes().chunks(76) {
wrapped.push_str(std::str::from_utf8(line).unwrap_or(""));
wrapped.push_str("\r\n");
}
wrapped
}
fn gzip(data: &[u8]) -> Vec<u8> {
let mut enc = flate2::write::GzEncoder::new(Vec::new(), flate2::Compression::default());
enc.write_all(data).and_then(|_| enc.finish()).unwrap_or_default()
}
pub fn dsn(domain: &str) -> Vec<u8> {
format!(
"From: Mail Delivery Subsystem <MAILER-DAEMON@mx.example.net>\r\n\
To: <bounces+abc@{domain}>\r\n\
Subject: Delivery Status Notification (Failure)\r\n\
Date: Thu, 13 Nov 2026 01:02:03 +0000\r\n\
Message-ID: <dsn-1@mx.example.net>\r\n\
MIME-Version: 1.0\r\n\
Content-Type: multipart/report; report-type=delivery-status; boundary=\"b1\"\r\n\
\r\n\
--b1\r\n\
Content-Type: text/plain; charset=us-ascii\r\n\
\r\n\
The following address failed: user@example.net (550 5.1.1 user unknown)\r\n\
--b1\r\n\
Content-Type: message/delivery-status\r\n\
\r\n\
Reporting-MTA: dns; mx.example.net\r\n\
Arrival-Date: Thu, 13 Nov 2026 01:02:00 +0000\r\n\
\r\n\
Final-Recipient: rfc822; user@example.net\r\n\
Original-Recipient: rfc822;user@example.net\r\n\
Action: failed\r\n\
Status: 5.1.1\r\n\
Remote-MTA: dns; mx2.example.net\r\n\
Diagnostic-Code: smtp; 550 5.1.1 <user@example.net>: Recipient address rejected:\r\n\
User unknown in virtual mailbox table\r\n\
\r\n\
--b1\r\n\
Content-Type: message/rfc822\r\n\
\r\n\
Return-Path: <bounces+abc@{domain}>\r\n\
From: noreply@{domain}\r\n\
To: user@example.net\r\n\
Subject: original\r\n\
X-Mail-Id: abc\r\n\
Message-ID: <orig-1@{domain}>\r\n\
\r\n\
original body\r\n\
--b1--\r\n"
)
.into_bytes()
}
pub fn arf(domain: &str) -> Vec<u8> {
format!(
"From: <feedback@isp.example>\r\n\
To: <abuse@{domain}>\r\n\
Subject: FW: Earn money\r\n\
Date: Thu, 13 Nov 2026 01:05:00 +0000\r\n\
Message-ID: <arf-1@isp.example>\r\n\
MIME-Version: 1.0\r\n\
Content-Type: multipart/report; report-type=feedback-report; boundary=\"part1_13d.2e68ed54_boundary\"\r\n\
\r\n\
--part1_13d.2e68ed54_boundary\r\n\
Content-Type: text/plain; charset=US-ASCII\r\n\
Content-Transfer-Encoding: 7bit\r\n\
\r\n\
This is an email abuse report for an email message received from IP 203.0.113.9.\r\n\
\r\n\
--part1_13d.2e68ed54_boundary\r\n\
Content-Type: message/feedback-report\r\n\
\r\n\
Feedback-Type: abuse\r\n\
User-Agent: SomeGenerator/1.0\r\n\
Version: 1\r\n\
Original-Mail-From: <bounces+cmp42@{domain}>\r\n\
Original-Rcpt-To: <complainer@isp.example>\r\n\
Arrival-Date: Thu, 13 Nov 2026 00:59:00 +0000\r\n\
Source-IP: 203.0.113.9\r\n\
Reported-Domain: {domain}\r\n\
Authentication-Results: mail.isp.example; dkim=pass header.d={domain}\r\n\
\r\n\
--part1_13d.2e68ed54_boundary\r\n\
Content-Type: message/rfc822\r\n\
Content-Disposition: inline\r\n\
\r\n\
Return-Path: <bounces+cmp42@{domain}>\r\n\
From: Sender <marketing@{domain}>\r\n\
To: complainer@isp.example\r\n\
Subject: Earn money\r\n\
X-Mail-Id: cmp42\r\n\
Message-ID: <orig-42@{domain}>\r\n\
Date: Thu, 13 Nov 2026 00:58:00 +0000\r\n\
\r\n\
Spam Spam Spam\r\n\
--part1_13d.2e68ed54_boundary--\r\n"
)
.into_bytes()
}
pub fn dmarc_gzip(domain: &str) -> Vec<u8> {
let xml = format!(
r#"<?xml version="1.0" encoding="UTF-8" ?>
<feedback>
<report_metadata>
<org_name>google.com</org_name>
<email>noreply-dmarc-support@google.com</email>
<report_id>1762992000000000001</report_id>
<date_range><begin>1762905600</begin><end>1762991999</end></date_range>
</report_metadata>
<policy_published>
<domain>{domain}</domain><adkim>r</adkim><aspf>r</aspf><p>reject</p><sp>reject</sp><pct>100</pct>
</policy_published>
<record>
<row>
<source_ip>130.61.180.42</source_ip><count>120</count>
<policy_evaluated><disposition>none</disposition><dkim>pass</dkim><spf>pass</spf></policy_evaluated>
</row>
<identifiers><header_from>{domain}</header_from><envelope_from>{domain}</envelope_from></identifiers>
<auth_results>
<dkim><domain>{domain}</domain><result>pass</result><selector>s1</selector></dkim>
<spf><domain>{domain}</domain><result>pass</result><scope>mfrom</scope></spf>
</auth_results>
</record>
<record>
<row>
<source_ip>198.51.100.7</source_ip><count>3</count>
<policy_evaluated><disposition>reject</disposition><dkim>fail</dkim><spf>fail</spf></policy_evaluated>
</row>
<identifiers><header_from>{domain}</header_from></identifiers>
<auth_results>
<dkim><domain>spoofer.example</domain><result>fail</result></dkim>
<spf><domain>spoofer.example</domain><result>softfail</result></spf>
</auth_results>
</record>
</feedback>
"#
);
let gz = gzip(xml.as_bytes());
let name = format!("google.com!{domain}!1762905600!1762991999.xml.gz");
format!(
"From: noreply-dmarc-support@google.com\r\n\
To: dmarc@{domain}\r\n\
Subject: Report domain: {domain} Submitter: google.com Report-ID: 1762992000000000001\r\n\
Date: Thu, 13 Nov 2026 01:10:00 +0000\r\n\
Message-ID: <dmarc-1@google.com>\r\n\
MIME-Version: 1.0\r\n\
Content-Type: multipart/mixed; boundary=\"dm1\"\r\n\
\r\n\
--dm1\r\n\
Content-Type: text/plain; charset=UTF-8\r\n\
\r\n\
This is an aggregate report from google.com.\r\n\
--dm1\r\n\
Content-Type: application/gzip; name=\"{name}\"\r\n\
Content-Disposition: attachment; filename=\"{name}\"\r\n\
Content-Transfer-Encoding: base64\r\n\
\r\n\
{}\
--dm1--\r\n",
base64(&gz)
)
.into_bytes()
}
pub fn tlsrpt(domain: &str) -> Vec<u8> {
let json = format!(
r#"{{"organization-name":"Google Inc.","date-range":{{"start-datetime":"2026-11-12T00:00:00Z","end-datetime":"2026-11-12T23:59:59Z"}},"contact-info":"smtp-tls-reporting@google.com","report-id":"2026-11-12T00:00:00Z_{domain}","policies":[{{"policy":{{"policy-type":"sts","policy-string":["version: STSv1","mode: enforce","mx: mx.{domain}","max_age: 86400"],"policy-domain":"{domain}","mx-host":["mx.{domain}"]}},"summary":{{"total-successful-session-count":5326,"total-failure-session-count":2}},"failure-details":[{{"result-type":"certificate-expired","sending-mta-ip":"2001:db8::1","receiving-mx-hostname":"mx.{domain}","failed-session-count":2}}]}}]}}"#
);
format!(
"From: tlsrpt@google.com\r\n\
To: tls-rpt@{domain}\r\n\
Subject: Report Domain: {domain} Submitter: google.com Report-ID: <2026-11-12T00:00:00Z_{domain}>\r\n\
Date: Thu, 13 Nov 2026 01:12:00 +0000\r\n\
Message-ID: <tlsrpt-1@google.com>\r\n\
TLS-Report-Domain: {domain}\r\n\
TLS-Report-Submitter: google.com\r\n\
MIME-Version: 1.0\r\n\
Content-Type: multipart/report; report-type=tlsrpt; boundary=\"tr1\"\r\n\
\r\n\
--tr1\r\n\
Content-Type: text/plain; charset=UTF-8\r\n\
\r\n\
This is an aggregate TLS report from google.com.\r\n\
--tr1\r\n\
Content-Type: application/tlsrpt+json\r\n\
Content-Disposition: attachment; filename=\"google.com!{domain}!1762905600!1762991999!001.json\"\r\n\
Content-Transfer-Encoding: base64\r\n\
\r\n\
{}\
--tr1--\r\n",
base64(json.as_bytes())
)
.into_bytes()
}
pub fn plain_bounce(domain: &str) -> Vec<u8> {
format!(
"From: Mail Delivery System <mailer-daemon@legacy.example>\r\n\
To: bounces+xyz@{domain}\r\n\
Subject: Undelivered Mail Returned to Sender\r\n\
Date: Thu, 13 Nov 2026 01:15:00 +0000\r\n\
Message-ID: <plain-1@legacy.example>\r\n\
\r\n\
This is the mail system at host legacy.example.\r\n\
\r\n\
<someone@legacy.example>: host mx.legacy.example said: 550 5.1.1 No such user\r\n"
)
.into_bytes()
}