Serversocket IP lock?

Asked

Viewed 495 times

4

I have developed a socket system in Delphi. It is possible to block an IP to not connect to my server?

Obs: The components were used: SERVERSOCKET and CLIENTSOCKET.

2 answers

2

Yes, it is possible. You can intercept the client connection in the Serversocket Clientconnect event

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;
  • Thanks friend. This VALIDA ACCESS function would be more or less like ? I could for example have a MEMO with multiple ips inside and it query inside that MEMO if there is IP blocked there ?

  • Oops! Let me give you a hint! @user7605 would be so. It’s a method you’ll specify as you need it. The IP address and Port are being passed as a parameter, so you write this method ValidaAcesso to verify that list of IP s and/or other checks you need.

  • I’ll set up this function so thanks friends for the help!

  • James or user7605, I created something like, but it didn’t work, what I did wrong ? Function Validaaccess(S: string): string; Begin s := Form6.Memo1.Text; end;

  • It’s already worked out here friends, thank you very much!

  • @user7605 Since it worked, just mark the answer as correct, so that the question leaves the line of unanswered.

Show 1 more comment

1


I believe this is possible.

To block an address list (be in a Memo or in a 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 function Ipisblocked at 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, basically this is it.

  • Thanks Friends!

  • Friend error on this line: if Form6.Listbox1.Strings[I] = Ipblocked then on STRINGS option.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.