tailscale 解决阿里云服务器受影响的问题

  1. 创建监听脚本 (/usr/local/bin/fix-ts-whitelist.sh)
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
sudo tee /usr/local/bin/fix-ts-whitelist.sh << 'EOF'
#!/bin/bash
# 功能:定期检查并修复 tailscale ts-input 链中的白名单规则

CONFIG_FILE="/etc/tailscale/whitelist.conf"
CHAIN="ts-input"
TABLE="filter"

# 如果配置文件不存在,静默退出
if [[ ! -f "$CONFIG_FILE" ]]; then
exit 0
fi

# 检查 ts-input 链是否存在(tailscale 未运行时可能不存在)
if ! iptables -t $TABLE -L $CHAIN -n >/dev/null 2>&1; then
# 链不存在,无需修复,直接退出
exit 0
fi

# 读取配置文件,忽略空行和注释
# 修正1:使用 || [[ -n "$entry" ]] 防止最后一行无换行符时被丢弃
while IFS= read -r entry || [[ -n "$entry" ]]; do
# 修正2:移除行尾注释(# 及之后的内容),避免非法的 iptables 参数
entry="${entry%%#*}"

# 去除前后空白
entry="$(echo "$entry" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')"

# 跳过空行(注释行已被清理或原本就是空行)
[[ -z "$entry" ]] && continue

# 检查规则是否已存在
if ! iptables -t $TABLE -C $CHAIN -s "$entry" -j ACCEPT 2>/dev/null; then
# 插入到链的最顶部(优先级最高)
# 注意:若文件中有多条规则,最后读取的条目会成为第一条,
# 即配置文件中后写的规则会优先匹配,可视需求调整
iptables -t $TABLE -I $CHAIN 1 -s "$entry" -j ACCEPT 2>/dev/null
if [[ $? -eq 0 ]]; then
logger -t "fix-ts-whitelist" "Added ACCEPT rule for $entry"
fi
fi
done < "$CONFIG_FILE"

exit 0
EOF
sudo chmod +x /usr/local/bin/fix-ts-whitelist.sh
  1. 创建 systemd service
1
2
3
4
5
6
7
8
9
sudo tee /etc/systemd/system/fix-ts-whitelist.service << 'EOF'
[Unit]
Description=Ensure Tailscale whitelist iptables rules
[Service]
Type=oneshot
ExecStart=/usr/local/bin/fix-ts-whitelist.sh
StandardOutput=journal
StandardError=journal
EOF
  1. 创建 systemd timer(每 1 分钟执行)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
sudo tee /etc/systemd/system/fix-ts-whitelist.timer << 'EOF'
[Unit]
Description=Run fix-ts-whitelist every 1 minutes
Requires=fix-ts-whitelist.service

[Timer]
OnBootSec=1min
OnUnitActiveSec=1min
# 随机延迟 0-30 秒,避免集中运行
RandomizedDelaySec=30

[Install]
WantedBy=timers.target
EOF

参数解释

  • OnBootSec=1min:系统启动 1 分钟后首次运行。
  • OnUnitActiveSec=1min:每次执行完成后间隔 1 分钟再次运行。
  • RandomizedDelaySec=30:随机延迟最多 30 秒,减轻系统瞬间负载。
  1. 启用 timer 并测试
1
2
3
4
5
6
7
8
9
10
11
12
# 重载 systemd 配置
sudo systemctl daemon-reload

# 启用并启动 timer(开机自启)
sudo systemctl enable fix-ts-whitelist.timer
sudo systemctl start fix-ts-whitelist.timer

# 查看 timer 状态
sudo systemctl status fix-ts-whitelist.timer

# 列出所有活跃的 timer
sudo systemctl list-timers | grep fix-ts

手动测试(可选):

1
2
3
4
# 手动运行一次 service,检查能否正确添加规则
sudo systemctl start fix-ts-whitelist.service
# 查看 iptables 规则
sudo iptables -L ts-input -n -v
  1. 配置白名单文件(示例)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
sudo mkdir -p /etc/tailscale
sudo tee /etc/tailscale/whitelist.conf << 'EOF'
# 需要放通的 IP 或网段,每行一个
100.100.35.30
100.100.98.28
100.100.0.4
100.100.100.200
100.100.2.136
100.100.2.138
100.115.33.49
100.115.33.47
100.115.61.9
100.100.2.148
EOF

参考阿里云文档 配置云助手Agent网络权限 获取需要允许的部分IP数据

✅ 最终效果

  • 开机后 1 分钟:自动添加规则。
  • 运行期间:每 5 分钟检查一次,若规则被 tailscale down/up 清除,最迟 5 分钟内会自动恢复(实际由于 timer 间隔,平均恢复时间约 2.5 分钟)。
  • 无侵入性:不会干扰 tailscale 的正常运行,也不需要禁用其防火墙管理。

📝 小贴士

  • 日志查看journalctl -u fix-ts-whitelist.service -f
  • 调整频率:若希望更短间隔(如 1 分钟),修改 OnUnitActiveSec=1min 即可。
  • 性能影响:iptables 规则添加极轻量,每 5 分钟运行一次对系统无负担。

参考