TypechoJoeTheme

尘遇 logo

当我遇上你
排行榜
统计

网站只允许cloudflare的IP访问

醉书生站长
3周前
0 评论
251 阅读
检测收录...
05/03

iptables只允许cloudflare的IP访问,参考这个https://developers.cloudflare.com/fundamentals/get-started/setup/allow-cloudflare-ip-addresses/

首先,要确定有iptables和ip6tables,然后,iptables创建一个链:

iptables -N CLOUDFLARE
ip6tables -N CLOUDFLARE

让INPUT引用

iptables -A INPUT -j CLOUDFLARE
ip6tables -A INPUT -j CLOUDFLARE

然后把CF的IP加进链里

for ip in `curl -s https://www.cloudflare.com/ips-v4`;do
iptables  -A CLOUDFLARE -p tcp -m multiport --dports http,https -s $ip -j ACCEPT
done
for ip in `curl -s https://www.cloudflare.com/ips-v6`;do
ip6tables  -A CLOUDFLARE -p tcp -m multiport --dports http,https -s $ip -j ACCEPT
done

不允许其他

iptables -A INPUT -p tcp -m multiport --dport http,https -j DROP
ip6tables -A INPUT -p tcp -m multiport --dport http,https -j DROP

搞过一次之后,也就是定时执行的脚本如下
清空链,然后重新加一遍IP
以下保存为脚本,定时执行即可

#先删掉"不允许所有",避免在下面命令执行期间GG
iptables -D INPUT -p tcp -m multiport --dport http,https -j DROP
ip6tables -D INPUT -p tcp -m multiport --dport http,https -j DROP
#清除规则(旧的CF IP)
iptables -F CLOUDFLARE
ip6tables -F CLOUDFLARE
#添加CF IP,下面可以对curl的结果做一次判断,可以避免网络问题可能出现的问题,自己写
for ip in `curl -s https://www.cloudflare.com/ips-v4`;do
iptables -A CLOUDFLARE -s $i -j ACCEPT
done
for ip in `curl -s https://www.cloudflare.com/ips-v6`;do
ip6tables -A CLOUDFLARE -s $i -j ACCEPT
done
mkdir -p /etc/iptables/
iptables-save > /etc/iptables/rules.v4
ip6tables-save > /etc/iptables/rules.v6
#禁用其他IP
iptables -A INPUT -p tcp -m multiport --dport http,https -j DROP
ip6tables -A INPUT -p tcp -m multiport --dport http,https -j DROP

如果想清空上面设置过的规则

iptables -F CLOUDFLARE
ip6tables -F CLOUDFLARE
iptables -D INPUT -j CLOUDFLARE
ip6tables -D INPUT -j CLOUDFLARE
iptables -X CLOUDFLARE
ip6tables -X CLOUDFLARE
iptables -D INPUT -p tcp --dport http,https -j DROP
ip6tables -D INPUT -p tcp --dport http,https -j DROP
> /etc/iptables/rules.v4
> /etc/iptables/rules.v6

补上iptables规则持久化的设置,以免重启后就无了!

#保存规则
mkdir -p /etc/iptables/
iptables-save > /etc/iptables/rules.v4
ip6tables-save > /etc/iptables/rules.v6
#引用规则
iptables-restore < /etc/iptables/rules.v4
ip6tables-restore < /etc/iptables/rules.v6

以上保存规则设置到关机执行的命令(也可以不用),引用规则设置到开机执行的命令。或者网卡关闭和启动。
还有,上面整完新的CF IP后要保存一下规则(我已经写上去了)!

本文来自Hostloc MJJ 好鸭!

本文共255个字数,您已阅读:平均阅读时长≈1分钟。
朗读
点赞
0
赞赏
海报
人生
欢迎
要时间嘉许,等春风得意,我正好遇上你!

微信/QQ/支付宝三合一收款码

微信
QQ
支付宝
打开微信/QQ/支付宝扫一扫,即可进行扫码打赏!
版权属于:

尘遇

最后修改:

2022 年 05 月 03 日 09 : 22 PM

本文链接:

https://chenyu.me/1542.html(转载时请注明本文出处及文章链接)

暂无评论

光阴者,百代之过客

今日已经过去小时
这周已经过去
本月已经过去
今年已经过去个月

最新回复

  1. 1秀才
    2022-05-28

    {!{data:image/webp;base64,UklGRmwGAABXRUJQVlA4WAoAAAAwAAAAAQMAKwEASUNDUBgCAAAAAAIYAAAAAAQwAABtbnRyUkdCIFhZWiAAAAAAAAAAAAAAAABhY3NwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAQAA9tYAAQAAAADTLQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAlkZXNjAAAA8AAAAHRyWFlaAAABZAAAABRnWFlaAAABeAAAABRiWFlaAAABjAAAABRyVFJDAAABoAAAAChnVFJDAAABoAAAAChiVFJDAAABoAAA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+YBpZvv3OMBRF4tvcKEP36Hk0CwnW9wVFw/aV6DUu5eXnr9u1oD9etA997J6iBnSdu3b3zAsA7cP78MVD/q//V/+p/9b/6X/2v/lf/q//V/+p/9b/6X/2v/lf/q//V/+p/9b/6X/2v/lf/q//V/+p/9f8POQBWUDgg0gEAAFA1AJ0BKgIDLAE/cbjZZbSvK6cgCAKQLglpbuF3YRtACewD32ych77ZOQ99snIe+2TkPfbJyHvtk5D32ych77ZOQ99snIe+2TkPfbJyHvtk5D32ych77ZOQ99snIe+2TkPfbJyHvtk5D32ych77ZOQ99snIe+2TkPfbJyHvtk5D32ych77ZOQ99snIe+2TkPfbJyHvtk5D32ych77ZOQ99snIe+2TkPfbJyHvtk5D32ych77ZOQ99snIe+2TkPfbJyHvtk5D32ych77ZOQ99snIe+2TkPfbJyHvtk5D32ych77ZOQ99snIe+2TkPfbJyHvtk5D32ych77ZOQ99snIe+2TkPfbJyHvtk5D32ych77ZOQ99snIe+2TkPfbJyHvtk5D32ych77ZOQ99snIe+2TkPfbJyHvtk5D32ych77ZOQ99snIe+2TkPfbJyHvtk5D32ych77ZOQ99snIe+2TkPfbJyHvtk5D32ych77ZOQ99snIe+2TkPfbJyHvtk5D32ych77ZOQ99snIe+2TkPfbJyHvtk5D32ych77ZOQ99snIe+2TkPfbJyHvtk5D32ycKAAD+/60eAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=}!}

  2. dingding秀才
    2022-05-27
  3. 秀才
    2022-05-26
  4. 清泓秀才
    2022-05-26

  5. 清泓秀才
    2022-05-26