IP Blocking ServerSocket?

4

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.

    
asked by anonymous 17.04.2014 / 03:23

2 answers

1

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.

    
17.04.2014 / 07:33
2

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;
    
17.04.2014 / 03:53