Checking IP address in a login method

I would like to check the IP address from where a login request comes from to check it against a whitelist.
The server is written in Delphi using a TROIndyHTTPServer…
The FAQ at http://wiki.remobjects.com/wiki/Server_FAQs_(RemObjects_SDK)#How_can_I_get_IP_address_of_the_remote_client_from_the_service_code.3F only mentions .NET, is it possible to have the same example in Delphi ?

have you saw the next faq ?

from Dispatch Notifier sample:

procedure TDispatchNotifierService.GetDispatchInfo(
  const aTransport: IROTransport; const aMessage: IROMessage);
var
  tcpinfo: IROTCPTransport;
begin
  if Supports(aTransport, IROTCPtransport, tcpinfo) then DispatchNotifierServerMainForm.Log('Client ' + tcpinfo.GetClientAddress + ' connected!');

Sure, but inside the Login implementation method, how can I access the aTransport property to get the client address ? I need the ip address avaiable inside the login method to cross check it with the login name, some users are allowed to log from everywhere, some other only from local network.

why you can’t store ip in private service variable and use it later in login method?

For “hystorical” reasons the login service is a singletone one, i.e. its classfactory is TROSingletonClassFactory, is it guaranteed that the GetDispatchinfo method is called just before the login method and without no possibilities of overlapping between different clients ?

GetDispatchinfo method is called before calling an actual method - see BeforeInvoke call in TROInvoker.CustomHandleMessage. You can store <client_id>= pair in private TStringList or other properties of your login service

@digitaldomus: You could use a thread local variable in that case. Singletons do get called from multiple threads at once, but the GetDispatchInfo is always called before the actual invoke.

With a thread local variabile you mean a private variabile in the service ?

Create a global variable like:

threadvar
  UserIP: String[100]; // delphi doesn't do regular strings as threadvar

a threadvar is a global var that’s unique per thread. Since RO always executes GetDispatchInfo and your login method in order, this var will always contain the right ip. Another option is to make your login class not a singleton and just use a private var in the class.

1 Like

It works thanks a lot!