Linux用户站邮局服务规划
应用选型:Mailu
架构规划
主要设备
服务器名称 | 硬件 | 网络 | 用途 |
---|---|---|---|
境内Server A | EliteDesk800G3 SSF ,G4600,2*8GB RAM | 无公网IP | 运行mailu 容器 |
境外Server B | RackNerd VPS,1*Vcpu,768MB RAM | 有公网IP | 提供公网IPv4 地址 |
网络规划
考虑到方案1可能违反SakuraFrp的用户协议,暂定方案2。
方案 | 实施 | 风险 |
---|---|---|
方案1 | 在A上运行FRP,开出公网端口用于与B的通信;在B上运行socat转发必要端口到A,实现A-B双向通信。 | 依赖第三方服务(如FRP)的稳定性,可能存在性能瓶颈或配置复杂性。 |
方案2 | 在A、B上运行WireGuard,实现10.10.0.0/24虚拟组网。 | 配置较为复杂,涉及跨境组网,网络波动可能导致通信失败。 |
具体操作
A的配置文件(wg0.conf)应该设置自己的内网IP(比如10.10.0.2/24),并指定B的公网IP作为Endpoint。B的配置文件则设置自己的内网IP(比如10.10.0.1/24),并指定A的连接信息。同时,B需要配置NAT转发,通过iptables将来自公网的邮件端口流量转发到A的WireGuard IP地址,比如10.10.0.2的25端口。
WireGuard 组网方案概述
- 目标:通过WireGuard建立加密隧道,使A与B处于同一虚拟内网(如
10.10.0.0/24
),B将公网流量转发到A。 - 优势:低延迟、高性能、端到端加密,适合长期稳定的内网穿透。
- 流量路径:
公网请求 -> B的公网IP(25/465等端口) -> WireGuard隧道 -> A的Mailu服务
具体步骤
1. 安装 WireGuard
在 A 和 B 上安装WireGuard:
# Ubuntu/Debian
sudo apt update && sudo apt install wireguard resolvconf
# CentOS/RHEL
sudo yum install epel-release
sudo yum install wireguard-tools
# 启用内核转发(在B上执行)
echo "net.ipv4.ip_forward=1" | sudo tee /etc/sysctl.d/99-wireguard.conf
sudo sysctl -p
2. 生成密钥对
在 A 和 B 上分别生成密钥:
# 生成私钥
wg genkey | sudo tee /etc/wireguard/privatekey
# 生成公钥
sudo cat /etc/wireguard/privatekey | wg pubkey | sudo tee /etc/wireguard/publickey
3. 配置 WireGuard
服务器A(无公网IP)配置
编辑 /etc/wireguard/wg0.conf
:
[Interface]
PrivateKey = <A的私钥>
Address = 10.10.0.2/24 # A的虚拟内网IP
DNS = 8.8.8.8 # 可选,解决A的DNS解析问题
[Peer]
PublicKey = <B的公钥>
Endpoint = <B的公网IP>:51820 # B的公网IP和WireGuard端口
AllowedIPs = 10.10.0.0/24 # 允许访问B所在虚拟网段
PersistentKeepalive = 25 # 维持NAT连接
服务器B(有公网IP)配置
编辑 /etc/wireguard/wg0.conf
:
[Interface]
PrivateKey = <B的私钥>
Address = 10.10.0.1/24 # B的虚拟内网IP
ListenPort = 51820 # 监听的公网端口
[Peer]
PublicKey = <A的公钥>
AllowedIPs = 10.10.0.2/32 # 允许A的IP访问
4. 启动 WireGuard 隧道
在 A 和 B 上启动服务:
sudo systemctl enable wg-quick@wg0
sudo systemctl start wg-quick@wg0
验证隧道状态:
sudo wg show
# 应看到 "latest handshake" 和传输数据量
5. 配置服务器B的流量转发
在 B 上设置NAT转发,将公网流量通过隧道转发到A:
# 允许IP转发
sudo sysctl -w net.ipv4.ip_forward=1
# 配置iptables规则(示例:转发SMTP 25端口)
sudo iptables -t nat -A PREROUTING -p tcp --dport 25 -j DNAT --to-destination 10.10.0.2:25
sudo iptables -t nat -A POSTROUTING -o wg0 -j MASQUERADE # 伪装出站流量
# 保存规则(Ubuntu使用iptables-persistent)
sudo apt install iptables-persistent
sudo netfilter-persistent save
6. 配置Mailu服务
在 A 上运行Mailu时,确保服务监听在WireGuard虚拟IP (10.10.0.2
) 或 0.0.0.0
:
# mailu.yml 配置片段
services:
smtp:
ports:
- "10.10.0.2:25:25"
- "10.10.0.2:465:465"
networks:
default:
ipv4_address: 10.10.0.2
7. DNS与邮件服务配置
• MX记录:指向B的公网IP(mail.example.com A <B的公网IP>
)。
• SPF记录:包含B的公网IP(v=spf1 ip4:<B公网IP> -all
)。
• PTR反向解析:联系B的ISP或云服务商,将B的公网IP反向解析为 mail.example.com
。
验证步骤
测试隧道连通性:
# 在A上ping B的虚拟IP ping 10.10.0.1 # 在B上ping A的虚拟IP ping 10.10.0.2
测试端口转发:
# 从外部测试SMTP端口是否可达 telnet <B的公网IP> 25 # 应连接到A的Mailu服务
邮件收发测试:
# 使用swaks发送测试邮件 swaks --to test@example.com --server <B的公网IP> --port 25
注意事项
- 防火墙规则:确保B的防火墙放行UDP 51820(WireGuard端口)和邮件相关TCP端口(25/465/587等)。
- 隧道稳定性:若A位于NAT后(如家庭网络),需在A的配置中启用
PersistentKeepalive
。 - 动态IP支持:如果B的公网IP可能变动,可使用动态DNS(如
ddns.net
)并更新A的Endpoint配置。 - 加密流量:Mailu应启用TLS(Let's Encrypt),避免明文传输邮件。
通过以上步骤,服务器A的Mailu服务即可通过WireGuard隧道,借助服务器B的公网IP稳定收发邮件。