如何在 indy (Delphi) 中阻止未知客户端
我有一个公共服务器(配置有 indy 10)。一些未知的客户端正在发送数千条无内容消息,它将服务器的 CPU 使用率更改为 50% 。我的服务器上没有防火墙,所以我尝试使用以下代码阻止未知客户端:
这是一个与计时器一起使用的函数:
var
i, j: integer;
begin
IX2 := IX2 + 1;
SetLength(ClientIPs, IX2);
ClientIPs[IX2 - 1] := StrIP;
j := 0;
for i := low(ClientIPs) to high(ClientIPs) do
begin
Application.ProcessMessages;
if ClientIPs[i] = StrIP then
j := j + 1;
end;
if j > 10 then
begin
Result := false;
exit;
end;
Result := true;
这是我的计时器代码:
//Reset filtering measures
IX2 := 0;
SetLength(ClientIPs, 0);
所以我在 OnExecute 事件中使用它:
LogIP := AContext.Connection.Socket.Binding.PeerIP;
if IPFilter(LogIP) <> true then
begin
AContext.Connection.disconnect;
exit;
end;
//Get Data *********
Data := AContext.Connection.IOHandler.ReadLn();
最后,如果客户端发送短时间内发很多消息,就会被断开。但有一个问题。事实上,在客户端断开连接后,Onexecute 事件仍在工作,我无法完全停止操作。无论如何,我需要完全阻止一些 IP。
谢谢
I have a public server(configured with indy 10) . some unknown clients are sending thousands of no content messages that it change the server's cpu usage to 50% . i have no firewall on my server , so i tried to block the unknown clients with this codes :
This is a function that works with a Timer :
var
i, j: integer;
begin
IX2 := IX2 + 1;
SetLength(ClientIPs, IX2);
ClientIPs[IX2 - 1] := StrIP;
j := 0;
for i := low(ClientIPs) to high(ClientIPs) do
begin
Application.ProcessMessages;
if ClientIPs[i] = StrIP then
j := j + 1;
end;
if j > 10 then
begin
Result := false;
exit;
end;
Result := true;
And it's my Timer code :
//Reset filtering measures
IX2 := 0;
SetLength(ClientIPs, 0);
So i use it in OnExecute event :
LogIP := AContext.Connection.Socket.Binding.PeerIP;
if IPFilter(LogIP) <> true then
begin
AContext.Connection.disconnect;
exit;
end;
//Get Data *********
Data := AContext.Connection.IOHandler.ReadLn();
finally , if a client sends many message in a short time , it will be disconnect . but there is a problem . in fact , after client disconnection , the Onexecute event is still working and i can not stop the operation Fully .anyway i need to block some IPs completely .
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
OnConnect 事件将是断开黑名单 IP 的更好位置。在 OnExecute 事件中进行检查的唯一原因是,直到 OnConnect 被触发之后 IP 才被列入黑名单。
至于为什么 OnExecute 在您断开连接后继续运行 - 唯一可能发生的情况是,如果您的 OnExecute 处理程序有一个 try.. except 块,该块正在捕获并丢弃 Indy 的内部通知。您所做的任何异常处理都需要重新引发 EIdException 派生的异常,以便服务器可以处理它们。
The OnConnect event would be a better place to disconnect blacklisted IPs. The only reason to do the check in the OnExecute event is if the IP is not being blacklisted until after OnConnect has already been fired.
As for why OnExecute keeps running after you disconnect - the only way that can happen is if your OnExecute handler has a try..except block that is catching and discarding Indy's internal notifications. Any exception handling you do needs to re-raise EIdException-derived exceptions so the server can process them.
我之前的评论的后续:
Followup to my earlier comment: