php 阻止ip代码,php – 如何阻止100,000个独立的IP地址

你可以尝试的一些事情是保留你想要在

text file中阻止的IP地址的列表或将其转换为

dbm hash file,然后使用mod_rewrite的RewriteMap。你必须在你的服务器/ vhost配置中设置。您不能在htaccess文件中初始化映射。

RewriteEngine On

RewriteMap deny_ips txt:/path/to/deny_ips.txt

RewriteCond ${deny_ips:%{REMOTE_ADDR}|0} !=0

RewriteRule ^ - [L,F]

/path/to/deny_ips.txt文件看起来像这样:

12.34.56.78 1

11.22.33.44 1

etc.

基本上,你想要拒绝的IP和空格,然后“1”。此文本文件中的任何IP将导致服务器返回403 Forbidden。为了加快速度,你可以使用httxt2dbm生成一个dbm哈希,然后定义映射如下:

RewriteMap deny_ips dbm:/path/to/deny_ips.dbm

我不知道什么性能命中是使用mod_rewrite像这样与许多IPs,但在linache 2.2运行在3Ghz i686上的快速基准测试,列表中的5个IP与102418之间的差异是可以忽略不计。根据ab的输出,他们几乎相同。

解决具体问题:

Is it possible for htaccess to get the list from database (Redis,Crunchbase,Mongo, MySQL or even Sqlite) … any

使用重写映射,可以使用“prg”映射类型为映射类型运行外部程序。然后,您可以编写perl,php等脚本与数据库通话,以查找IP地址。另请注意,“注意”下列出的警告。然后,您将像任何其他地图一样使用此地图(RewriteCond $ {deny_ips:%{REMOTE_ADDR} | 0}!= 0)。这本质上会为所有请求创建瓶颈。不是与数据库通信的最佳解决方案。

在apache 2.4中,有一个dbd/fastdbd地图类型,它允许你通过mod_dbd创建查询。这是一个更好的选择,mod_dbd模块管理与数据库,池连接等的连接。所以地图定义看起来像:

RewriteMap deny_ips "fastdbd:SELECT active FROM deny_ips WHERE source = %s"

假设你有一个表“deny_ips”,有2列“源”(IP地址)和“活动”(1表示活动,0表示不活动)。

Is there a visible solution to manage such kind of issue in production

如果要将所有阻止的IP存储在数据库中,则需要管理数据库表的内容。如果您使用dbm映射类型,我至少知道perl has a DBI用于管理dbm文件,因此您可以使用它来从拒绝列表中添加/删除IP条目。我从来没有使用它,所以我不能真正地说很多。管理一个平面文本文件将是一个很麻烦,特别是如果你打算删除条目,而不是只是追加到它。在使用数据库和apache 2.4的mod_dbd之外,我不认为任何这些解决方案是开箱即用或生产准备。它需要自定义工作。

I know the best solution is Block the IPs at the firewall level is there any way to pragmatically add/remove IP to the firewall

对于IPtables,有一个perl interface标记为Beta,但我从来没有使用它。有libiptc但根据netfilter’s faq:

Is there an C/C++ API for adding/removing rules?

The answer unfortunately is: No.

Now you might think ‘but what about libiptc?’. As has been pointed out numerous times on the mailinglist(s), libiptc was NEVER meant to be used as a public interface. We don’t guarantee a stable interface, and it is planned to remove it in the next incarnation of linux packet filtering. libiptc is way too low-layer to be used reasonably anyway.

We are well aware that there is a fundamental lack for such an API, and we are working on improving that situation. Until then, it is recommended to either use system() or open a pipe into stdin of iptables-restore. The latter will give you a way better performance.

所以我不知道libiptc解决方案是否可行,如果没有API稳定性。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值