Search code examples
indydelphi-10.3-rio

Block incoming IPs in TidHTTPServer


I want to block certain client "OnConnect" to my Server, but I am not sure which event is best to use and how to find the remote IP...


Solution

  • In your app's code, using the OnConnect event is the simplest choice. You can get the client's IP from the Binding.PeerIP property of the provided AContext parameter, eg:

    procedure TMyForm.IdHTTPServer1Connect(AContext: TIdContext);
    begin
      if (AContext.Binding.PeerIP is blacklisted) then
        AContext.Connection.Disconnect; // or raise an Exception...
    end;
    

    However, a better choice is to put your server app behind a firewall that blocks connections by the desired IPs from reaching TIdHTTPServer in the first place.