假设我有一个 Node.JS 服务器应用程序,我想阻止连续 3 次登录失败的用户,正确的方法是什么?
我应该在服务器应用程序中处理阻塞部分,基本上是在用户连接但尚未登录之后,还是有一些较低级别的阶段我应该这样做,所以它甚至没有到达我的 Node.JS应用程序?
最佳答案
在你做之前
您应该考虑在以下情况下会发生什么:
有些用户有静态 IP 地址(他们无法更改)。
解决方案
正因为如此,我会阻止帐户和 IP 地址,然后我会向帐户所有者发送一封电子邮件,其中包含一个链接,该链接将解除对帐户的封锁,而这个 没有 更改密码的要求。帐户所有者还应回答有关解除 IP 地址封锁的问题,因为他知道自己的 IP 地址。
怎么做(阻止IP地址)
更可取的解决方案是在较低级别阻止 IP 地址 - 在它到达 Node.JS 之前,因为所有网络都发生在操作系统内核中,并且内核可以比 Node.JS 更有效、更快地在连接中阻止它它。
但首先要回答你的问题...
如何使用 Node.JS 阻止它
var blackList =
[
'77.88.99.1',
'88.77.99.1'
];
var http = require('http');
var server = http.createServer(function(req, res)
{
var ip = req.ip
|| req.connection.remoteAddress
|| req.socket.remoteAddress
|| req.connection.socket.remoteAddress;
if(blackList.indexOf(ip) > -1)
{
res.end(); // exit if it is a black listed ip
}
}).listen(80, '127.0.0.1');
和 Node.js http.Server
有一个 connection
事件。你也可以这样做:server.on('connection', function(socket)
{
// console.log(socket.remoteAddress);
// Put your logic here
});
如何用 Linux 阻止它(例如)Sometime it is necessary to block incoming connection or traffic from specific remote host. iptables is administration tool for IPv4 packet filtering and NAT under Linux kernel. Following tip will help you to block attacker or spammers IP address.
How do I block specific incoming ip address?
Following iptable rule will drop incoming connection from host/IP
202.54.20.22
:iptables -A INPUT -s 202.54.20.22 -j DROP iptables -A OUTPUT -d 202.54.20.22 -j DROP
A simple shell script to block lots of IP address
If you have lots of IP address use the following shell script:
A) Create a text file:
# vi /root/ip.blocked
Now append IP address:
# Ip address block file 202.54.20.22 202.54.20.1/24 #65.66.36.87
B) Create a script as follows or add following script line to existing iptables shell script:
BLOCKDB="/root/ip.blocked" IPS=$(grep -Ev "^#" $BLOCKDB) for i in $IPS do iptables -A INPUT -s $i -j DROP iptables -A OUTPUT -d $i -j DROP done
C) Save and close the file.
© Source Linux Iptables block incoming access to selected or specific ip address
关于javascript - 如何正确阻止 Node.JS 服务器应用程序中的 IP 地址?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63455999/