如何阻止indy中的未知客户端(Delphi) [英] how to block unknown clients in indy (Delphi)

查看:179
本文介绍了如何阻止indy中的未知客户端(Delphi)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个公共服务器(配置为indy 10)。一些未知客户端正在发送数千个无内容消息,将服务器的CPU使用率更改为50%。我的服务器上没有防火墙,所以我试图用这个代码来阻止这些不明客户端:



这是一个可以使用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;
如果ClientIPs [i] = StrIP然后
j:= j + 1;
结束
如果j> 10然后
begin
结果:= false;
退出;
结束
结果:= true;

这是我的计时码:

  //重置过滤措施
IX2:= 0;
SetLength(ClientIPs,0);

所以我在OnExecute事件中使用它:

  LogIP:= AContext.Connection.Socket.Binding.PeerIP; 

如果IPFilter(LogIP)<> true然后
begin
AContext.Connection.disconnect;
退出;
结束

//获取数据*********
数据:= AContext.Connection.IOHandler.ReadLn();

最后,如果客户端在短时间内发送了很多消息,则会断开连接。但是有一个问题。事实上,在客户端断开连接后,Onexecute事件仍然正常工作,我无法停止运行。完全不需要阻止一些IP。



谢谢


解决方案

跟随我之前的评论:

  function TForm1.IPFilter(const StrIP:string):Boolean; 
var
i,j:integer;
list:TList;
begin
j:= 0;
list:= IdTCPServer1.Contexts.LockList;
尝试
为i:= 0 to list.Count-1 do
begin
如果TIdContext(list [i]).Binding.PeerIP = StrIP然后
Inc (j);
结束
结果:= j <= 10;
finally
IdTCPServer1.Contexts.UnlockList;
结束
结束

程序TForm1.IdTCPServer1Execute(AContext:TIdContext);
begin
//强制断开连接并停止的最简单的方法
//调用线程是引发异常...
如果不是IPFilter(AContext.Binding.PeerIP )然后
中止();

//或者,如果您调用Disconnect(),请确保
// IOHandler的InputBuffer为空,否则
// AContext.Connection.Connected()将继续
//返回True!...
{if not IPFilter(AContext.Binding.PeerIP)then
begin
AContext.Connection.Disconnect;
AContext.Connection.IOHandler.InputBuffer.Clear;
退出;
end;}

//获取数据*********
数据:= AContext.Connection.IOHandler.ReadLn();
结束


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

解决方案

Followup to my earlier comment:

function TForm1.IPFilter(const StrIP: string): Boolean;
var 
  i, j: integer; 
  list: TList;
begin 
  j := 0; 
  list := IdTCPServer1.Contexts.LockList;
  try
    for i := 0 to list.Count-1 do 
    begin 
      if TIdContext(list[i]).Binding.PeerIP = StrIP then
        Inc(j); 
    end; 
    Result := j <= 10; 
  finally
    IdTCPServer1.Contexts.UnlockList;
  end;
end;

procedure TForm1.IdTCPServer1Execute(AContext: TIdContext);
begin
  // the simpliest way to force a disconnect and stop
  // the calling thread is to raise an exception...
  if not IPFilter(AContext.Binding.PeerIP) then
    Abort();

  // alternatively, if you call Disconnect(), make sure
  // the IOHandler's InputBuffer is empty, or else
  // AContext.Connection.Connected() will continue
  // returning True!...
  {if not IPFilter(AContext.Binding.PeerIP) then
  begin
    AContext.Connection.Disconnect;
    AContext.Connection.IOHandler.InputBuffer.Clear;
    Exit;
  end;}

  //Get Data ********* 
  Data := AContext.Connection.IOHandler.ReadLn(); 
end;

这篇关于如何阻止indy中的未知客户端(Delphi)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆