I developed a socket system in Delphi. Is it possible to block an IP from connecting to my server?
Note: The components: SERVERSOCKET and CLIENTSOCKET were used.
I developed a socket system in Delphi. Is it possible to block an IP from connecting to my server?
Note: The components: SERVERSOCKET and CLIENTSOCKET were used.
I believe this is possible.
To block an address list (either in Memo
or StringList
) you can use a function like this:
{ Verifica se uma lista contem determinada string(IP bloqueado) }
function IpIsBlocked(List: TStrings; const IPBlocked: string): Boolean;
Var
I: Integer;
begin
Result := False;
for i := 0 to List.Count -1 do
if List.Strings[I] = IPBlocked then
Result := True;
end;
Now just use the IpIsBlocked function in the event OnClientConnect
of ServerSocket .
Example:
procedure TForm1.ServerSocket1ClientConnect(Sender: TObject; Socket: TCustomWinSocket);
begin
if IpIsBlocked(Memo1.Lines, 'XXX.XYX.XXX') then begin
ShowMessage(Format('O ip %s está bloqueado!', [socket.RemoteAddress]));
Socket.Close;
end;
end;
Well, that's basically it.
Yes, it is possible. You can intercept the client connection in the ClientConnect event of ServerSocket
procedure TForm1.ServerSocket1ClientConnect(Sender: TObject; Socket: TCustomWinSocket);
begin
if not ValidaAcesso(Socket.RemoteAddress, Socket.RemotePort) then
raise EConnException.Create('Endereço não autorizado!')
end;