#!/bin/bash
# 配置参数 - 正确的端口格式
TARGET_IP="192.168.1.1" # 需要保护的IP
OTHER_IP1="192.168.1.2" # 不限制的IP1
OTHER_IP2="192.168.1.3" # 不限制的IP2
PORT1="80" # 第一个端口
PORT2="443" # 第二个端口
PROTOCOL="tcp" # 协议
RULE_COMMENT="Cloudflare_Protection" # 规则标识
CF_IPV4_URL="https://www.cloudflare.com/ips-v4"
# 检查是否以root权限运行
if [ "$(id -u)" -ne 0 ]; then
echo "错误:请使用sudo运行此脚本,例如:sudo ./cloudflare_ufw_protect.sh" >&2
exit 1
fi
# 确保UFW已安装并启用
setup_ufw() {
# 检查UFW是否安装
if ! command -v ufw &> /dev/null; then
echo "正在安装UFW防火墙..."
apt update -qq > /dev/null
apt install -y -qq ufw > /dev/null
fi
# 启用UFW
if ! ufw status | grep -q "Status: active"; then
echo "正在启用UFW防火墙..."
ufw --force enable > /dev/null
# 设置默认策略
ufw default deny incoming > /dev/null
ufw default allow outgoing > /dev/null
fi
}
# 配置不受限制的IP
configure_unrestricted_ips() {
echo "配置不受限制的IP地址..."
# 允许所有IP访问OTHER_IP1的80端口
if ! ufw status | grep -q "ALLOW.*$PROTOCOL.*to.*$OTHER_IP1.*$PORT1"; then
ufw allow in to $OTHER_IP1 port $PORT1 proto $PROTOCOL comment "Unrestricted_$OTHER_IP1:$PORT1" > /dev/null
fi
# 允许所有IP访问OTHER_IP1的443端口
if ! ufw status | grep -q "ALLOW.*$PROTOCOL.*to.*$OTHER_IP1.*$PORT2"; then
ufw allow in to $OTHER_IP1 port $PORT2 proto $PROTOCOL comment "Unrestricted_$OTHER_IP1:$PORT2" > /dev/null
fi
# 允许所有IP访问OTHER_IP2的80端口
if ! ufw status | grep -q "ALLOW.*$PROTOCOL.*to.*$OTHER_IP2.*$PORT1"; then
ufw allow in to $OTHER_IP2 port $PORT1 proto $PROTOCOL comment "Unrestricted_$OTHER_IP2:$PORT1" > /dev/null
fi
# 允许所有IP访问OTHER_IP2的443端口
if ! ufw status | grep -q "ALLOW.*$PROTOCOL.*to.*$OTHER_IP2.*$PORT2"; then
ufw allow in to $OTHER_IP2 port $PORT2 proto $PROTOCOL comment "Unrestricted_$OTHER_IP2:$PORT2" > /dev/null
fi
}
# 清除旧的Cloudflare规则
cleanup_old_rules() {
echo "清除旧的Cloudflare规则..."
# 获取所有带指定注释的规则编号
RULE_NUMBERS=$(ufw status numbered | grep "$RULE_COMMENT" | awk -F'[][]' '{print $2}' | sort -nr)
# 逐个删除规则(从大到小)
for num in $RULE_NUMBERS; do
ufw --force delete $num > /dev/null
done
}
# 更新Cloudflare IP并添加新规则
update_cloudflare_rules() {
echo "[$(date)] 开始更新Cloudflare IP列表..."
# 创建临时文件存储IP列表
TEMP_FILE=$(mktemp)
# 下载Cloudflare IP列表
if ! curl -sSL $CF_IPV4_URL -o $TEMP_FILE; then
echo "错误:无法下载Cloudflare IP列表,请检查网络连接" >&2
rm -f $TEMP_FILE
return 1
fi
# 添加新规则(分别处理两个端口)
echo "正在添加新的访问规则..."
RULE_COUNT=0
while IFS= read -r ip; do
if [ -n "$ip" ]; then
# 添加80端口规则
ufw allow in from $ip to $TARGET_IP port $PORT1 proto $PROTOCOL comment "$RULE_COMMENT" > /dev/null
# 添加443端口规则
ufw allow in from $ip to $TARGET_IP port $PORT2 proto $PROTOCOL comment "$RULE_COMMENT" > /dev/null
((RULE_COUNT++))
fi
done < "$TEMP_FILE"
# 清理临时文件
rm -f $TEMP_FILE
echo "[$(date)] 成功添加 $RULE_COUNT 个Cloudflare IP段的规则(每个IP段包含两个端口规则)"
}
# 显示当前状态
show_status() {
echo "----------------------------------------"
echo "当前防护状态:"
echo "受保护IP: $TARGET_IP (仅允许Cloudflare访问$PORT1/$PORT2端口)"
echo "不受限制IP: $OTHER_IP1, $OTHER_IP2 (允许所有IP访问$PORT1/$PORT2端口)"
echo "----------------------------------------"
echo "已配置的Cloudflare规则数量:"
ufw status | grep "$RULE_COMMENT" | wc -l
echo "----------------------------------------"
}
# 主函数
main() {
# 初始化UFW
setup_ufw
# 配置不受限制的IP
configure_unrestricted_ips
# 清除旧规则并更新新规则
cleanup_old_rules
update_cloudflare_rules
# 显示状态
show_status
}
# 捕获中断信号,友好退出
trap 'echo "`date`: 脚本已停止"; exit 0' SIGINT SIGTERM
# 启动主程序
main
旧版本
在 Ubuntu 24 服务器上,只允许 Cloudflare 的 IP 地址访问 80 和 443 端口可以通过配置防火墙实现,以下是具体步骤:
### 步骤 1: 更新服务器
确保服务器是最新的,运行以下命令:
sudo apt update && sudo apt upgrade -y
### 步骤 2: 获取 Cloudflare 的 IP 地址列表
Cloudflare 的官方 IP 地址范围可以从以下页面获取:[Cloudflare IP Ranges](https://www.cloudflare.com/ips/)。你需要将这些 IP 地址用于防火墙配置。
运行以下命令以下载 Cloudflare 的 IP 列表:
curl https://www.cloudflare.com/ips-v4 -o cloudflare-ips-v4.txt
curl https://www.cloudflare.com/ips-v6 -o cloudflare-ips-v6.txt
### 步骤 3: 配置防火墙
#### 使用 UFW
如果你使用 UFW(Uncomplicated Firewall),可以按照以下步骤:
1. 允许 Cloudflare 的 IP 范围访问端口 80 和 443:
while read ip; do sudo ufw allow from $ip to any port 80; done < cloudflare-ips-v4.txt
while read ip; do sudo ufw allow from $ip to any port 443; done < cloudflare-ips-v4.txt
2. 拒绝其他来源访问 80 和 443 端口:
sudo ufw deny 80
sudo ufw deny 443
3. 启用或重新加载 UFW:
sudo ufw enable
sudo ufw reload
#### 使用 iptables
如果你使用的是 iptables,可以按照以下步骤:
1. 清除已有规则:
sudo iptables -F
sudo iptables -X
2. 添加 Cloudflare 的 IP 范围规则:
while read ip; do sudo iptables -A INPUT -p tcp -s $ip --dport 80 -j ACCEPT; done < cloudflare-ips-v4.txt
while read ip; do sudo iptables -A INPUT -p tcp -s $ip --dport 443 -j ACCEPT; done < cloudflare-ips-v4.txt
while read ip; do sudo ip6tables -A INPUT -p tcp -s $ip --dport 80 -j ACCEPT; done < cloudflare-ips-v6.txt
while read ip; do sudo ip6tables -A INPUT -p tcp -s $ip --dport 443 -j ACCEPT; done < cloudflare-ips-v6.txt
3. 拒绝其他来源访问 80 和 443 端口:
sudo iptables -A INPUT -p tcp --dport 80 -j DROP
sudo iptables -A INPUT -p tcp --dport 443 -j DROP
4. 保存规则:
sudo apt install iptables-persistent -y
sudo netfilter-persistent save
sudo netfilter-persistent reload
### 步骤 4: 验证配置
可以通过以下命令验证防火墙规则是否生效:
- 对于 UFW:
sudo ufw status
- 对于 iptables:
sudo iptables -L -n -v