Предотвратить флуд в TcpServer - PullRequest
0 голосов
/ 02 октября 2018

Как я могу предотвратить наводнение (отправить много заявок в TcpServer)?я полагаю, что они наблюдают за отправкой <-> от клиента к серверу и столько раз отправляют определенный пакет серверу, который его заполняет, например, они отправляют команду cmdScreenShotData в разное время, создавая множество файлов на компьютере.

Этомой метод OnExecute:

procedure TForm1.IdTCPServer1Execute(AContext: TIdContext);
type
  PTBytes   = ^TBytes;
  PTIdBytes = ^TIdBytes;
var
  LBuffer     : TIdBytes;
  LProtocol   : TProtocol;
  FTempBuffer : TIdBytes;

  Enviar    : TBytes;
  Protocolo : TProtocol;

  Conexao   : TClientContext;

  procedure AddToMemo(const AStr: string);
  begin
    TThread.Synchronize(nil,
      procedure
      begin
        Memo1.Lines.Add(AStr);
      end
    );
  end;
begin
  Conexao := TClientContext(AContext);

  AContext.Connection.IOHandler.ReadBytes(LBuffer, szProtocol, False);

  LProtocol := BytesToProtocol(PTBytes(@LBuffer)^);

  case LProtocol.Command of
    cmdConnect: begin
      Conexao.Client := LProtocol.Sender;

      if Conexao.Client.HWID = '' then begin
        InitProtocol(Protocolo);
        Protocolo.Command     := cmdHWID;
        Protocolo.Sender.HWID := GeraHWID(40);
        Enviar                := ProtocolToBytes(Protocolo);
        Conexao.Connection.IOHandler.Write(PTIdBytes(@Enviar)^);
        ClearBuffer(Enviar);

        { Seta HWID na conexão }

        Conexao.FClient.HWID := Protocolo.Sender.HWID;

        AddToMemo(Format('[%s] : [%s][%s]', ['Connect', AContext.Connection.Socket.Binding.PeerIP, Protocolo.Sender.HWID]));
      end
      else
      begin
        AddToMemo(Format('[%s] : [%s][%s]', ['Connect', AContext.Connection.Socket.Binding.PeerIP, LProtocol.Sender.HWID]));
      end;
    end;

    cmdDisconnect: begin
      AddToMemo(Format('[%s] : [%s][%s]', ['Disconnect', AContext.Connection.Socket.Binding.PeerIP, Conexao.Client.HWID]));
    end;

    cmdScreenShotData: begin
      AddToMemo(Format('[%s] : [%s][%s]', ['ScreenShotData', AContext.Connection.Socket.Binding.PeerIP, Conexao.Client.HWID]));

      TThread.Synchronize(nil,
        procedure
        var
          LStream : TMemoryStream;
          LPngImage : TPngImage;
        begin
          try
            AContext.Connection.IOHandler.ReadBytes(FTempBuffer, LProtocol.DataSize);

            LStream   := TMemoryStream.Create;
            LPngImage := TPngImage.Create;
            try
              LStream.Write(FTempBuffer[0], Length(FTempBuffer));

              LStream.Position := 0;

              LPngImage.LoadFromStream(LStream);
            finally
              LPngImage.SaveToFile(Format('Screen_%d_%s.png', [GetTickCount, Conexao.Client.HWID]));

              FreeAndNil(LStream);
              FreeAndNil(LPngImage);
              ClearBufferId(FTempBuffer);

              Memo1.Lines.Add('Received screenshot');
              Memo1.Lines.Add(Format('[%s] Pos Screen : [%s][%s]', ['Disconnect', AContext.Connection.Socket.Binding.PeerIP, Conexao.Client.HWID]));
            end;
          except
            FreeAndNil(LStream);
            FreeAndNil(LPngImage);
            ClearBufferId(FTempBuffer);

            Memo1.Lines.Add('Error on Receive ScreenShot');
          end;
        end
      );
    end;
  end;
end;

но они отправляют одни и те же пакеты, я думаю, может быть, используя WPE Pro, я не знаю, или другую флуд-программу для TcpServer.

И он использует такмного процессора на машине.

...