答案1
您可以将 IPSET 与超时和计数器选项一起使用。这将看起来像这样:
#create ipset for accounting with default lifetime 300 secs
ipset create IP_QUOTA_SET hash:ip timeout 300 counters
#create separated rule chain
iptables --new-chain PER_IP_QUOTING
#send packets to chain
iptables -t filter -A INPUT \
-i <in-iface> --dst <ip> \
-p tcp --dport <dstport> \
-j PER_IP_QUOTING
#if ip doesn't exist in the set, add it
iptables -t filter -A PER_IP_QUOTING \
-m set ! --match-set IP_QUOTA_SET src \
-j SET --add-set IP_QUOTA_SET src --timeout 300
#if packet exists in the set, check bytes
#if byte counter > quota then close connection
#by sending of tcp-reset packet.
iptables -t filter -A PER_IP_QUOTING \
-m set --match-set IP_QUOTA_SET src \
--bytes-gt 1000 -j REJECT --reject-with tcp-rst
#pass other packets (for debug purpose)
iptables -t filter -A PER_IP_QUOTING \
-j RETURN
在这种情况下,你可以检查列表并通过 ipset 命令进行编辑。显示带有计数器和超时的当前列表:ipset list IP_QUOTA_SET。
有关详细信息,请阅读文档。