refactor: replace autoconfig with native impl, remove openssl dependency

This commit is contained in:
rustmailer
2026-05-15 11:27:06 +08:00
parent 715858183d
commit 7afb1e29aa
8 changed files with 678 additions and 1118 deletions
+255
View File
@@ -0,0 +1,255 @@
//
// Copyright (c) 2025-2026 rustmailer.com (https://rustmailer.com)
//
// This file is part of the Bichon Email Archiving Project
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
use hickory_resolver::name_server::TokioConnectionProvider;
use hickory_resolver::proto::rr::RData;
use hickory_resolver::proto::rr::RecordType;
use hickory_resolver::TokioResolver;
use quick_xml::de::from_str;
use reqwest::Client;
use serde::Deserialize;
use crate::error::code::ErrorCode;
use crate::error::BichonResult;
use crate::raise_error;
/// Parsed result from Thunderbird-style autoconfig XML or DNS SRV fallback.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct MailConfig {
pub incoming: Vec<IncomingServer>,
pub outgoing: Vec<OutgoingServer>,
}
#[derive(Debug, Clone, Default, PartialEq, Eq, Deserialize)]
pub struct IncomingServer {
#[serde(rename = "@type")]
pub protocol: String,
pub hostname: String,
#[serde(default)]
pub port: u16,
#[serde(rename = "socketType")]
pub socket_type: String,
pub username: String,
}
#[derive(Debug, Clone, Default, PartialEq, Eq, Deserialize)]
pub struct OutgoingServer {
#[serde(rename = "@type")]
pub protocol: String,
pub hostname: String,
#[serde(default)]
pub port: u16,
#[serde(rename = "socketType")]
pub socket_type: String,
pub username: String,
}
// ---------------------------------------------------------------------------
// Internal XML wrapper structs matching the Thunderbird config-v1.1 schema:
// <clientConfig> → <emailProvider> → <incomingServer> / <outgoingServer>
// ---------------------------------------------------------------------------
#[derive(Debug, Deserialize)]
#[serde(rename = "clientConfig")]
struct ClientConfig {
#[serde(rename = "emailProvider", default)]
email_providers: Vec<EmailProvider>,
}
#[derive(Debug, Deserialize)]
struct EmailProvider {
#[serde(rename = "incomingServer", default)]
incoming_servers: Vec<IncomingServer>,
#[serde(rename = "outgoingServer", default)]
outgoing_servers: Vec<OutgoingServer>,
}
/// Parse Thunderbird autoconfig XML into a `MailConfig`.
/// Exposed for unit testing.
pub(crate) fn parse_autoconfig_xml(xml: &str) -> Option<MailConfig> {
let client_config: ClientConfig = from_str(xml).ok()?;
let provider = client_config.email_providers.into_iter().next()?;
Some(MailConfig {
incoming: provider.incoming_servers,
outgoing: provider.outgoing_servers,
})
}
// ---------------------------------------------------------------------------
// Network helpers
// ---------------------------------------------------------------------------
async fn fetch_xml(client: &Client, url: &str) -> Option<MailConfig> {
let resp = client.get(url).send().await.ok()?;
if !resp.status().is_success() {
return None;
}
let text = resp.text().await.ok()?;
parse_autoconfig_xml(&text)
}
async fn lookup_srv(domain: &str) -> Option<MailConfig> {
let resolver = TokioResolver::builder(TokioConnectionProvider::default())
.ok()?
.build();
let imap_srv = format!("_imaps._tcp.{}.", domain);
let imap_lookup = resolver.lookup(imap_srv, RecordType::SRV).await.ok()?;
let imap_record = imap_lookup.iter().next()?;
let (imap_host, imap_port) = match imap_record {
RData::SRV(srv) => {
let host = srv.target().to_string().trim_end_matches('.').to_string();
(host, srv.port())
}
_ => return None,
};
let smtp_srv = format!("_submission._tcp.{}.", domain);
let smtp_lookup = resolver.lookup(smtp_srv, RecordType::SRV).await.ok()?;
let smtp_record = smtp_lookup.iter().next()?;
let (smtp_host, smtp_port) = match smtp_record {
RData::SRV(srv) => {
let host = srv.target().to_string().trim_end_matches('.').to_string();
(host, srv.port())
}
_ => return None,
};
Some(MailConfig {
incoming: vec![IncomingServer {
protocol: "imap".to_string(),
hostname: imap_host,
port: imap_port,
socket_type: "SSL".to_string(),
username: "%EMAILADDRESS%".to_string(),
}],
outgoing: vec![OutgoingServer {
protocol: "smtp".to_string(),
hostname: smtp_host,
port: smtp_port,
socket_type: "STARTTLS".to_string(),
username: "%EMAILADDRESS%".to_string(),
}],
})
}
// ---------------------------------------------------------------------------
// Public API
// ---------------------------------------------------------------------------
/// Discover mail server configuration for a domain using the Thunderbird
/// autoconfig protocol (ISPDB) and DNS SRV fallback.
///
/// Probe order:
/// 1. `https://autoconfig.{domain}/mail/config-v1.1.xml`
/// 2. `https://{domain}/.well-known/autoconfig/mail/config-v1.1.xml`
/// 3. DNS SRV records (`_imaps._tcp` / `_submission._tcp`)
/// 4. Thunderbird central ISPDB (`https://autoconfig.thunderbird.net/v1.1/{domain}`)
pub async fn fetch(domain: &str) -> BichonResult<MailConfig> {
let client = Client::builder()
.timeout(std::time::Duration::from_secs(10))
.build()
.map_err(|e| raise_error!(format!("{:#?}", e), ErrorCode::InternalError))?;
// 1. Try autoconfig subdomain
if let Some(config) = fetch_xml(
&client,
&format!("https://autoconfig.{domain}/mail/config-v1.1.xml"),
)
.await
{
return Ok(config);
}
// 2. Try well-known path
if let Some(config) = fetch_xml(
&client,
&format!("https://{domain}/.well-known/autoconfig/mail/config-v1.1.xml"),
)
.await
{
return Ok(config);
}
// 3. Try DNS SRV records
if let Some(config) = lookup_srv(domain).await {
return Ok(config);
}
// 4. Fall back to Thunderbird central database
if let Some(config) = fetch_xml(
&client,
&format!("https://autoconfig.thunderbird.net/v1.1/{domain}"),
)
.await
{
return Ok(config);
}
Err(raise_error!(
format!("No autoconfig found for domain: {domain}"),
ErrorCode::InternalError
))
}
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test]
async fn test_fetch_valid_domain() {
let domains = vec![
// North America
("gmail.com", "Google Gmail"),
("outlook.com", "Microsoft Outlook"),
("hotmail.com", "Microsoft Hotmail"),
("yahoo.com", "Yahoo Mail"),
("icloud.com", "Apple iCloud"),
("aol.com", "AOL Mail"),
("protonmail.com", "ProtonMail"),
("zoho.com", "Zoho Mail"),
("fastmail.com", "FastMail"),
// Europe
("gmx.de", "GMX Germany"),
("gmx.net", "GMX International"),
("web.de", "Web.de Germany"),
("freenet.de", "Freenet Germany"),
("mail.ru", "Mail.ru Russia"),
("yandex.ru", "Yandex Russia"),
("orange.fr", "Orange France"),
("laposte.net", "La Poste France"),
("libero.it", "Libero Italy"),
("tiscali.it", "Tiscali Italy"),
("telenet.be", "Telenet Belgium"),
// Asia Pacific
("qq.com", "Tencent QQ"),
("163.com", "NetEase 163"),
("126.com", "NetEase 126"),
("sina.com", "Sina Mail"),
("naver.com", "Naver Korea"),
];
for (domain, label) in &domains {
let result = fetch(domain).await;
match result {
Ok(config) => println!("✅ [{label}] {domain}: {config:#?}"),
Err(e) => println!("⚠️ [{label}] {domain}: {e:?}"),
}
}
}
}
-14
View File
@@ -16,8 +16,6 @@
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
use autoconfig::config::OAuth2Config as XOAuth2Config;
use serde::{Deserialize, Serialize};
use crate::account::entity::Encryption;
@@ -55,18 +53,6 @@ pub struct OAuth2Config {
/// URL of the authorization server's token endpoint
pub token_url: String,
}
impl From<&XOAuth2Config> for OAuth2Config {
fn from(value: &XOAuth2Config) -> Self {
Self {
issuer: value.issuer().into(),
scope: value.scope().into_iter().map(Into::into).collect(),
auth_url: value.auth_url().into(),
token_url: value.token_url().into(),
}
}
}
#[derive(Debug, Clone, Default, Eq, PartialEq, Serialize, Deserialize)]
#[cfg_attr(feature = "web-api", derive(poem_openapi::Object))]
pub struct MailServerConfig {
+62 -64
View File
@@ -16,17 +16,50 @@
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
use crate::account::entity::Encryption;
use crate::autoconfig::client::{self, MailConfig};
use crate::autoconfig::entity::{MailServerConfig, ServerConfig};
use crate::autoconfig::CachedMailSettings;
use crate::error::code::ErrorCode;
use crate::{
raise_error,
{account::entity::Encryption, autoconfig::CachedMailSettings, error::BichonResult},
};
use autoconfig::config::{Server, ServerType};
use crate::error::BichonResult;
use crate::raise_error;
use email_address::EmailAddress;
use std::str::FromStr;
use tracing::error;
/// Map an autoconfig XML `socketType` value to our `Encryption` enum.
pub(crate) fn socket_type_to_encryption(raw: &str) -> Encryption {
match raw.to_ascii_uppercase().as_str() {
"SSL" | "TLS" => Encryption::Ssl,
"STARTTLS" => Encryption::StartTls,
_ => Encryption::None,
}
}
/// Convert the raw `MailConfig` discovered by `client::fetch` into a
/// `MailServerConfig` suitable for account provisioning.
pub(crate) fn mail_config_to_server_config(config: &MailConfig) -> Option<MailServerConfig> {
let imap = config.incoming.iter().find(|s| {
let p = s.protocol.to_ascii_lowercase();
p == "imap" || p == "imaps"
})?;
let encryption = socket_type_to_encryption(&imap.socket_type);
let port = if imap.port != 0 {
imap.port
} else {
match encryption {
Encryption::Ssl => 993,
_ => 143,
}
};
Some(MailServerConfig {
imap: ServerConfig::new(imap.hostname.clone(), port, encryption),
oauth2: None,
})
}
pub async fn resolve_autoconfig(email: impl AsRef<str>) -> BichonResult<Option<MailServerConfig>> {
let email = email.as_ref();
let email_address = EmailAddress::from_str(email).map_err(|error| {
@@ -37,73 +70,38 @@ pub async fn resolve_autoconfig(email: impl AsRef<str>) -> BichonResult<Option<M
})?;
let domain = email_address.domain();
// try read local cache first
// Try local cache first
if let Some(cached_entity) = CachedMailSettings::get(domain)? {
return Ok(Some(cached_entity.config));
}
let config = autoconfig::from_addr(email_address.email().as_ref())
.await
.map_err(|e| {
error!(email = %email, domain = %domain, error = ?e, "Autoconfig fetch failed");
raise_error!(
format!(
"Failed to fetch autoconfig for email '{}': {:#?}",
email_address.email(),
e
),
ErrorCode::AutoconfigFetchFailed
)
})?;
let imap_server = config
.email_provider()
.incoming_servers()
.into_iter()
.find(|s| matches!(s.server_type(), ServerType::Imap));
let imap_server = match imap_server {
Some(imap) => imap,
None => return Ok(None),
};
let get_encryption = |server: &Server| {
server
.security_type()
.map_or(Encryption::None, |encryption| match encryption {
autoconfig::config::SecurityType::Plain => Encryption::None,
autoconfig::config::SecurityType::Starttls => Encryption::StartTls,
autoconfig::config::SecurityType::Tls => Encryption::Ssl,
})
};
let get_port = |server: &Server, encryption: &Encryption, tls_port: u16, non_tls_port: u16| {
server.port().map_or_else(
|| match encryption {
Encryption::StartTls => tls_port,
_ => non_tls_port,
},
ToOwned::to_owned,
let config = client::fetch(domain).await.map_err(|e| {
error!(
email = %email,
domain = %domain,
error = ?e,
"Autoconfig fetch failed"
);
raise_error!(
format!(
"Failed to fetch autoconfig for email '{}': {:#?}",
email_address.email(),
e
),
ErrorCode::AutoconfigFetchFailed
)
};
})?;
let get_hostname = |server: &Server, default_prefix: &str| {
server.hostname().map_or_else(
|| format!("{}.{}", default_prefix, domain),
ToOwned::to_owned,
let result = mail_config_to_server_config(&config).ok_or_else(|| {
raise_error!(
format!(
"No IMAP server found in autoconfig for email: {}",
email_address.email()
),
ErrorCode::ResourceNotFound
)
};
})?;
let imap_encryption = get_encryption(imap_server);
let imap_config = ServerConfig::new(
get_hostname(imap_server, "imap"),
get_port(imap_server, &imap_encryption, 993, 143),
imap_encryption,
);
let result = MailServerConfig {
imap: imap_config,
oauth2: config.oauth2().map(|f| f.into()),
};
CachedMailSettings::add(domain.into(), result.clone())?;
Ok(Some(result))
}
+1
View File
@@ -22,6 +22,7 @@ use crate::database::{find_impl, MemDbModel};
use crate::{autoconfig::entity::MailServerConfig, error::BichonResult, utc_now};
use serde::{Deserialize, Serialize};
pub mod client;
pub mod entity;
pub mod load;
#[cfg(test)]
+288 -4
View File
@@ -16,9 +16,293 @@
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
use crate::account::entity::Encryption;
use crate::autoconfig::client::{self, IncomingServer, MailConfig};
use crate::autoconfig::load::{mail_config_to_server_config, socket_type_to_encryption};
#[tokio::test]
async fn test() {
let config = autoconfig::from_addr("test@gmail.com").await.unwrap();
println!("{:#?}", config);
// ---------------------------------------------------------------------------
// XML parsing tests
// ---------------------------------------------------------------------------
fn make_valid_xml() -> String {
r#"<?xml version="1.0" encoding="UTF-8"?>
<clientConfig version="1.1">
<emailProvider id="example.com">
<domain>example.com</domain>
<displayName>Example Mail</displayName>
<incomingServer type="imap">
<hostname>imap.example.com</hostname>
<port>993</port>
<socketType>SSL</socketType>
<username>%EMAILADDRESS%</username>
</incomingServer>
<outgoingServer type="smtp">
<hostname>smtp.example.com</hostname>
<port>587</port>
<socketType>STARTTLS</socketType>
<username>%EMAILADDRESS%</username>
</outgoingServer>
</emailProvider>
</clientConfig>"#
.to_string()
}
#[test]
fn parse_valid_xml() {
let xml = make_valid_xml();
let config = client::parse_autoconfig_xml(&xml).expect("should parse valid XML");
assert_eq!(config.incoming.len(), 1);
let imap = &config.incoming[0];
assert_eq!(imap.protocol, "imap");
assert_eq!(imap.hostname, "imap.example.com");
assert_eq!(imap.port, 993);
assert_eq!(imap.socket_type, "SSL");
assert_eq!(imap.username, "%EMAILADDRESS%");
assert_eq!(config.outgoing.len(), 1);
let smtp = &config.outgoing[0];
assert_eq!(smtp.protocol, "smtp");
assert_eq!(smtp.hostname, "smtp.example.com");
assert_eq!(smtp.port, 587);
assert_eq!(smtp.socket_type, "STARTTLS");
}
#[test]
fn parse_xml_empty_body() {
let xml = r#"<?xml version="1.0"?><clientConfig></clientConfig>"#;
let config = client::parse_autoconfig_xml(xml);
assert!(config.is_none(), "no emailProvider → None");
}
#[test]
fn parse_xml_no_incoming_servers() {
let xml = r#"<?xml version="1.0"?>
<clientConfig version="1.1">
<emailProvider id="example.com">
<domain>example.com</domain>
</emailProvider>
</clientConfig>"#;
let config = client::parse_autoconfig_xml(xml).expect("should parse");
assert!(config.incoming.is_empty());
assert!(config.outgoing.is_empty());
}
#[test]
fn parse_xml_garbage() {
let config = client::parse_autoconfig_xml("not xml at all");
assert!(config.is_none());
}
#[test]
fn parse_xml_missing_port_defaults_to_zero() {
let xml = r#"<?xml version="1.0"?>
<clientConfig version="1.1">
<emailProvider id="example.com">
<incomingServer type="imap">
<hostname>imap.example.com</hostname>
<socketType>SSL</socketType>
<username>%EMAILADDRESS%</username>
</incomingServer>
</emailProvider>
</clientConfig>"#;
let config = client::parse_autoconfig_xml(xml).expect("should parse");
assert_eq!(config.incoming[0].port, 0);
}
#[test]
fn parse_xml_multiple_providers_picks_first() {
let xml = r#"<?xml version="1.0"?>
<clientConfig version="1.1">
<emailProvider id="first.example.com">
<incomingServer type="imap">
<hostname>imap.first.example.com</hostname>
<port>993</port>
<socketType>SSL</socketType>
<username>%EMAILADDRESS%</username>
</incomingServer>
</emailProvider>
<emailProvider id="second.example.com">
<incomingServer type="imap">
<hostname>imap.second.example.com</hostname>
<port>143</port>
<socketType>STARTTLS</socketType>
<username>%EMAILADDRESS%</username>
</incomingServer>
</emailProvider>
</clientConfig>"#;
let config = client::parse_autoconfig_xml(xml).expect("should parse");
assert_eq!(config.incoming[0].hostname, "imap.first.example.com");
}
// ---------------------------------------------------------------------------
// socket_type → Encryption mapping tests
// ---------------------------------------------------------------------------
#[test]
fn encryption_ssl_uppercase() {
assert_eq!(socket_type_to_encryption("SSL"), Encryption::Ssl);
}
#[test]
fn encryption_ssl_lowercase() {
assert_eq!(socket_type_to_encryption("ssl"), Encryption::Ssl);
}
#[test]
fn encryption_tls() {
assert_eq!(socket_type_to_encryption("TLS"), Encryption::Ssl);
}
#[test]
fn encryption_starttls() {
assert_eq!(socket_type_to_encryption("STARTTLS"), Encryption::StartTls);
}
#[test]
fn encryption_starttls_lowercase() {
assert_eq!(socket_type_to_encryption("starttls"), Encryption::StartTls);
}
#[test]
fn encryption_starttls_mixed_case() {
assert_eq!(socket_type_to_encryption("StartTls"), Encryption::StartTls);
}
#[test]
fn encryption_plain() {
assert_eq!(socket_type_to_encryption("plain"), Encryption::None);
}
#[test]
fn encryption_empty_string() {
assert_eq!(socket_type_to_encryption(""), Encryption::None);
}
#[test]
fn encryption_unknown_value() {
assert_eq!(socket_type_to_encryption("WPA2-ENTERPRISE"), Encryption::None);
}
// ---------------------------------------------------------------------------
// MailConfig → MailServerConfig conversion tests
// ---------------------------------------------------------------------------
fn make_imap_server(host: &str, port: u16, socket_type: &str) -> IncomingServer {
IncomingServer {
protocol: "imap".to_string(),
hostname: host.to_string(),
port,
socket_type: socket_type.to_string(),
username: "%EMAILADDRESS%".to_string(),
}
}
#[test]
fn convert_basic_imap_ssl() {
let config = MailConfig {
incoming: vec![make_imap_server("imap.example.com", 993, "SSL")],
outgoing: vec![],
};
let result = mail_config_to_server_config(&config).expect("should convert");
assert_eq!(result.imap.host, "imap.example.com");
assert_eq!(result.imap.port, 993);
assert_eq!(result.imap.encryption, Encryption::Ssl);
assert!(result.oauth2.is_none());
}
#[test]
fn convert_imap_starttls_with_default_port() {
let config = MailConfig {
incoming: vec![make_imap_server("imap.example.com", 0, "STARTTLS")],
outgoing: vec![],
};
let result = mail_config_to_server_config(&config).expect("should convert");
assert_eq!(result.imap.port, 143, "default port for STARTTLS → 143");
assert_eq!(result.imap.encryption, Encryption::StartTls);
}
#[test]
fn convert_imap_ssl_with_default_port() {
let config = MailConfig {
incoming: vec![make_imap_server("imap.example.com", 0, "SSL")],
outgoing: vec![],
};
let result = mail_config_to_server_config(&config).expect("should convert");
assert_eq!(result.imap.port, 993, "default port for SSL → 993");
}
#[test]
fn convert_no_imap_only_pop3() {
let config = MailConfig {
incoming: vec![IncomingServer {
protocol: "pop3".to_string(),
hostname: "pop.example.com".to_string(),
port: 995,
socket_type: "SSL".to_string(),
username: "%EMAILADDRESS%".to_string(),
}],
outgoing: vec![],
};
assert!(mail_config_to_server_config(&config).is_none());
}
#[test]
fn convert_empty_incoming() {
let config = MailConfig {
incoming: vec![],
outgoing: vec![],
};
assert!(mail_config_to_server_config(&config).is_none());
}
#[test]
fn convert_picks_imap_over_pop3() {
let config = MailConfig {
incoming: vec![
IncomingServer {
protocol: "pop3".to_string(),
hostname: "pop.example.com".to_string(),
port: 995,
socket_type: "SSL".to_string(),
username: "%EMAILADDRESS%".to_string(),
},
make_imap_server("imap.example.com", 993, "SSL"),
],
outgoing: vec![],
};
let result = mail_config_to_server_config(&config).expect("should find IMAP");
assert_eq!(result.imap.host, "imap.example.com");
}
#[test]
fn convert_imaps_protocol_variant() {
let config = MailConfig {
incoming: vec![IncomingServer {
protocol: "imaps".to_string(),
hostname: "imap.example.com".to_string(),
port: 993,
socket_type: "SSL".to_string(),
username: "%EMAILADDRESS%".to_string(),
}],
outgoing: vec![],
};
let result = mail_config_to_server_config(&config).expect("should recognize 'imaps'");
assert_eq!(result.imap.host, "imap.example.com");
}
#[test]
fn convert_case_insensitive_protocol() {
let config = MailConfig {
incoming: vec![IncomingServer {
protocol: "IMAP".to_string(),
hostname: "imap.example.com".to_string(),
port: 143,
socket_type: "STARTTLS".to_string(),
username: "%EMAILADDRESS%".to_string(),
}],
outgoing: vec![],
};
let result = mail_config_to_server_config(&config).expect("should recognize 'IMAP'");
assert_eq!(result.imap.host, "imap.example.com");
}