如何阻止来自中国的请求?
我有一个比较流行的 API(每天 10,000 多个请求)。每天从某个 IP 地址发出 10 个请求后,我会返回一条消息,告诉用户如果想更多地使用该服务,则需要支付一些现金。
今天早上,我发现我的网络服务运行速度非常慢。我检查了数据库,发现我收到了来自中国 IP 地址的垃圾邮件。他们会使用一个 IP 地址 10 次,然后递增最后一个八位字节。悲伤的时刻。
为了保持系统的活力,我想限制或完全切断来自中国的请求。最好的方法是什么?在 PHP 中按国家/地区代码进行地理查找每个请求和禁令?这似乎是一种低效的方式。我在 htaccess 级别无能为力,是吗?
I have an API that is somewhat popular (10,000+ requests/day). After 10 requests per day from an IP address I return a message telling the user they need to cough of some cash if they want to use the service more.
This morning, I found that my web service was running terribly slow. I checked out the DB and I was getting absolutely spammed with requests from IP addresses originating in China. They would use an IP address 10 times and then increment the last octet. Sad times.
I'd like to limit or completely cut off requests from China, for the sake of keeping the system alive. What's the best way to do this? Geolookup each request and ban by country code in PHP? This seems like an inefficient way. There's nothing I can do at the htaccess level, is there?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
只需屏蔽整个中国IP范围即可:
在 .htaccess 中
Just block the entire china IP range:
in .htaccess
他们现在可能使用中国的 IP 地址,但禁止一个国家/地区,最终另一个国家/地区将成为问题。主要是因为国家与此无关;用户就是问题。您不应禁止 IP 范围,而应检测每当免费试用期结束后就增加 1 个八位组的 IP 地址。
They might be using Chinese IP addresses now, but ban one country and eventually another country will be the problem. Mostly because country has nothing to do with it; the user is the problem. Instead of banning IP ranges, you should detect IP addresses that are increasing by one octet each time they outlive a free trial.
封锁施虐者的整个子网,暂时解决问题。这些类型的用户也会出现在其他国家/地区,因此您最好的选择可能是需要注册和 API 密钥才能使用 API。
如果您仍想基于 IP 而不是 API 密钥进行阻止,请使用 whois(或 BGP)检查滥用子网有多大,并阻止整个 IP 范围。
Block the entire subnet of the abuser to solve the problem temporarily. These types of users will appear from other countries as well so your best bet may be to require a registration and an API key to use the API.
If you still want to block based on IP rather than API key, check how large the abusing subnet is using whois (or BGP) and block the entire IP range.
我使用 MaxMind GeoIP Web 服务: http://www.maxmind.com/en/web_services#country
只需 200 美元即可获得 2,000,000 次查找。效果很好,延迟低,而且您无需维护本地数据库。
I use the MaxMind GeoIP web service: http://www.maxmind.com/en/web_services#country
You get 2,000,000 lookups for $200. Works great, low latency, and you don't have to maintain a local database.