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
。
验证步骤
测试隧道连通性和速度:
xfox@ClawHK:~$ sudo iperf -s
Server listening on TCP port 5001
TCP window size: 128 KByte (default)
[ 1] local 10.1.109.114 port 5001 connected with 125.41.115.202 port 4166 (icwnd/mss/irtt=13/1388/38975)
[ ID] Interval Transfer Bandwidth
[ 1] 0.0000-10.6193 sec 69.1 MBytes 54.6 Mbits/sec
[ 2] local 10.10.0.1 port 5001 connected with 10.10.0.2 port 55160 (icwnd/mss/irtt=13/1368/34509)
[ ID] Interval Transfer Bandwidth
[ 2] 0.0000-10.5885 sec 66.5 MBytes 52.7 Mbits/sec
两次测试,分别走公网访问和WireGuard,速度一致可以确定还没有受到运营商针对UDP或WG等特定协议的限制。
WireGuard遇到国内运营商对udp的qos就老实了(不过只是转发邮件的话影响应该不大),以及我记得WireGuard没有伪装功能,跨境组网有概率被阻断
不打算伪装,我暂时也没打算拿这个做其他用途。考虑到现在延迟有所降低,我可以直接搭建frp对本地服务进行穿透,不过我还是希望先试试wg再做打算。
😭被qos過,UDP組網聯機從40ms到999ping…
暂时没遇到。