实现原理
Autoconfiguration 来自 Thunderbrid, 使用了 Autodiscover 类似的实现方法, 但步骤和规范上有些区别.
发起 Autoconfiguration 的基本步骤:
- 从 Thunderbrid 在本地磁盘上的安装目录中读取配置:
C:/Program Files/Mozilla/Thunderbird/isp/example.com.xml
- 从电子邮件服务商获取配置:
- 尝试请求
http://autoconfig.emailaddressdomain/mail/[email protected]
或http://example.com/.well-known/autoconfig/mail/config-v1.1.xml
文件路径. - 尝试从 DNS 记录中寻找配置:
- 尝试查询用户名域名
example.com
中存在内容为https://www.example.com/mozilla.xml
的 TXT 记录依据记录进行请求配置文件. 未应用 - 另一种提议, 也是基于 DNS: 未应用
- 尝试解析用户名域名中的 MX 记录, 若存在多个 MX 记录则取最高优先级的记录; 如果不存在 MX 记录, 则查询并使用用户名域名中符合 SMTP 协议(RFC 2821[1])的 A 记录.
- 依据上方的查询结果, 从结果主机名中查询存在
mailconf=https://...
形式的 TXT 记录, 依据记录寻找配置文件, 如果 URL 错误则继续向下寻找. - 如果查询不到有效的 TXT 记录则选择第一条有效的 TXT 记录进行查找配置文件.
- 在对配置文件进行 GET 请求时中附带初始请求的用户邮件地址(
mailto:[email protected]
)为 referrer, 用于实现可能存在的动态配置文件下发. - 解析配置文件并让用户选择需要的协议和其他建议配置.
- 继续进行登录验证.
- 在对配置文件进行 GET 请求时中附带初始请求的用户邮件地址(
- 尝试解析用户名域名 DNS 从中寻找 SRV 记录获取配置(RFC 6186[2]). 未应用
- 尝试查询用户名域名
- 尝试请求
- 尝试从 Mozilla 服务器检索配置:
- 从
autoconfig.thunderbird.net
[3] 请求用户名域名中的服务商配置文件, 如:https://autoconfig.thunderbird.net/v1.1/google.com
. - 如果找不到公共配置文件, 则尝试使用一些通配字符和常用端口进行猜测, 如
imap.example.com
,pop.example.com
,pop3.example.com
,smtp.example.com
或mail.example.com
. - 继续失败, 则直接让用户进行手动配置.
- 从
以上标记有 "未应用" 的都代表存在这些提议, 但没有实际应用到 Thunderbird 邮件客户端和服务器系统中.
响应规范
从 MozillaWiki 中可以得到配置格式:
Thunderbird:Autoconfiguration:ConfigFileFormat - MozillaWiki
以下配置文件注释经过笔者翻译.
配置文件模板
1 |
|
对于配置文件中的 incomingServer
(传入服务器) 和 outgoingServer
(传出服务器) 可能出现多次. 但应该按照优先使用顺序来排序, 通常会使用第一个出现的服务器配置, 只有当实际服务器表现不满足配置中要求的选项时(比如配置指定使用 SSL 但实际无法通过 SSL 连接到服务器), 才尝试选择第二个服务器配置. 如果 IMAP 和 POP3 服务器同时存在, 客户端应该同时列出(依旧遵循配置中的顺序显示来表示服务商的偏好), 让用户自行选择需要的服务器协议.
除上面配置文件中提到的, 依据用户在客户端的输入内容, 还存在以下通用占位符(变量):
%EMAILADDRESS%
: 用户的完整电子邮件地址.%EMAILLOCALPART%
: 用户电子邮件地址中 "@" 前的部分.%EMAILDOMAIN%
: 用户电子邮件地址中 "@" 后的部分.%REALNAME%
: 真实名字(?)
依据这个规范编写的一个典型的 IMAP & POP3 & SMTP 服务器配置应该像这样:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32<?xml version="1.0"?>
<clientConfig version="1.1">
<emailProvider id="example.com">
<domain>example.com</domain>
<displayName>EXAMPLE Inc</displayName>
<displayShortName>CORG</displayShortName>
<incomingServer type="imap">
<hostname>imap.example.com</hostname>
<port>994</port>
<socketType>SSL</socketType>
<username>%EMAILADDRESS%</username>
<authentication>password-cleartext</authentication>
</incomingServer>
<incomingServer type="pop3">
<hostname>pop3.example.com</hostname>
<port>995</port>
<socketType>SSL</socketType>
<username>%EMAILADDRESS%</username>
<authentication>password-cleartext</authentication>
<pop3>
<leaveMessagesOnServer>true</leaveMessagesOnServer>
</pop3>
</incomingServer>
<outgoingServer type="smtp">
<hostname>smtp.example.com</hostname>
<port>465</port>
<socketType>SSL</socketType>
<username>%EMAILADDRESS%</username>
<authentication>password-cleartext</authentication>
</outgoingServer>
<clientConfigUpdate url="https://autoconfig.example.com/mail/config-v1.1.xml" />
</clientConfig>
对于不需要的配置如果不是需要保留为空值, 那么应该在配置文件中移除.
部署应用
标准实现
- 编写正确规范的 XML 配置文件.
将电子邮件地址使用的域名的子域名
autoconfig
解析到一个用于托管配置文件的网络服务器, 如:1
autoconfig.example.com 3600 IN A 1.0.0.1
将配置文件放入网络服务器, 使其路径为:
/mail/config-v1.1.xml
.
- 编写正确规范的 XML 配置文件.
将电子邮件域名直接解析至配置文件服务器:
1
example.com 3600 IN A 1.0.0.1
将配置文件放入网络服务器, 使其路径为:
/.well-known/autoconfig/mail/config-v1.1.xml
- 编写正确规范的 XML 配置文件.
将其电子邮件域名的子域名
www
解析至网络服务器, 并将 XML 文件放入该网络文件服务器, 使其为任意便于管理的路径, 如/autoconfig/example.com/config-v1.1.xml
.为电子邮箱域名添加一个 TXT 记录, 值为上一步放置的 XML 文件完整的 HTTP/HTTPS 网络路径:
1
example.com 3600 IN TXT "https://www.example.com/autoconfig/example.com/config-v1.1.xml"
多个配置文件理论并不会相互覆盖, 因为只会使用最先被发现的的那一个配置文件, 其目的是为了冗余. 但是不同客户端可能存在不同的行为, 所以建议同时设置多个方法且保证配置文件内容相同. 如果当前 DNS 解析状况导致解析已存在或不允许再次占用, 那么应该向后选择不冲突的那一种实现方式.
中心化实现
本文将 Autoconfiguration 实现方法中, 需要依赖第三方中心化服务实现自动发现的方式称为 "中心化实现". 比如这里要提到的 "尝试从 Mozilla 服务器检索配置".
Thunderbird 维护并托管的电子邮件服务商配置文件数据库称为 ISPDB, 可免费供给任何用户(客户)使用. 它包含了最多的电子邮件服务商配置, 大多市场份额超过 0.1% 的服务商都包含在内, 几乎囊括了全球 50% 的电子邮件账户.
但要注意, 并不是随便一个服务商就能进入这个数据库, 比如自托管的个人电子邮件服务器就不符合要求.
ISPDB 要求[4]:
If you are a big ISP (> 100,000 users) providing email addresses solely under a few domains like "
example.com
" and "example.de
", you may either submit the configuration to the ISPDB or set up a configuration server."如果您是大型 ISP(> 100,000 个用户), 仅在 "
example.com
" 和 "example.de
" 等几个域下提供电子邮件地址, 您可以将配置提交到 ISPDB 或设置配置服务器."If you are a small company installing Thunderbird on your employees' desktops, you can place a configuration file in the Thunderbird installation folder.
如果您是在员工桌面上安装 Thunderbird 的小公司, 您可以将配置文件放在 Thunderbird 安装文件夹中.
如果想要为自己的电子邮件服务能够通过 Mozilla 服务器查找到服务器配置, 那么除去需要在客户端支持从 ISPDB 中检索, 还要为该服务的源 Git 仓库提交你的 Autoconfiguration 配置文件:
thundernest/autoconfig: The ISPDB, Thunderbird's database of mail configuration files.
源仓库存在两分支的配置文件索引, 一个是 prod
分支, 直接用于 Thunderbird(https://autoconfig.thunderbird.net/autoconfig/v1.1/
); 另一个是 master
分支, 用于暂存服务器(https://autoconfig-stage.thunderbird.net/autoconfig/v1.1/
).
配置验证
配置完成建议模拟客户端协议流程对配置文件进行验证. 本小节将使用 HTTP 客户端模拟自动发现流程, 下方将使用 curl 进行流程演示.
- well-known 路径:
1 |
|
autoconfig 子域
1
curl https://autoconfig.example.com/mail/config-v1.1.xml
响应示例:
config-v1.1.xml 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31<?xml version="1.0"?>
<clientConfig version="1.1">
<emailProvider id="%EMAILDOMAIN%">
<domain>%EMAILDOMAIN%</domain>
<displayName>Email</displayName>
<displayShortName>Email</displayShortName>
<incomingServer type="imap">
<hostname>imap.example.com</hostname>
<port>993</port>
<socketType>SSL</socketType>
<username>%EMAILADDRESS%</username>
<authentication>password-cleartext</authentication>
</incomingServer>
<outgoingServer type="smtp">
<hostname>smtp.example.com</hostname>
<port>465</port>
<socketType>SSL</socketType>
<username>%EMAILADDRESS%</username>
<authentication>password-cleartext</authentication>
<addThisServer>true</addThisServer>
<useGlobalPreferredServer>false</useGlobalPreferredServer>
</outgoingServer>
<documentation url="https://mail.example.com/admin/client">
<descr lang="en">Configure your email client</descr>
</documentation>
</emailProvider>
</clientConfig>