Recent

Author Topic: Get client IP with fcl-web  (Read 2796 times)

Trenatos

  • Hero Member
  • *****
  • Posts: 535
    • MarcusFernstrom.com
Get client IP with fcl-web
« on: September 29, 2018, 03:03:12 am »
I'm replacing an Elixir based website with one built in FPC using fcl-web/fphttpserver.

It's working fine, but I can't figure out how to get the clients IP address from the request.

I'm implementing Google captcha, which needs to include the users IP address for server<->google communication.

I can't post the whole thing as it includes API keys, but I'm using an TFPHTTPConnectionRequest

I've found that I can access ARequest.Connection.Socket.RemoteAddress but I don't know how to get from here to an IPv4 string, anyone done this?

Trenatos

  • Hero Member
  • *****
  • Posts: 535
    • MarcusFernstrom.com
Re: Get client IP with fcl-web
« Reply #1 on: September 29, 2018, 04:06:22 am »
Because I use CloudFlare, I got the IP through the forwarded-for header.

In case someone else could use that info:

Code: Pascal  [Select][+][-]
  1. ARequest.GetCustomHeader('X-Forwarded-For')

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Get client IP with fcl-web
« Reply #2 on: September 29, 2018, 03:24:13 pm »
I haven't got time ATM to look into the fcl-web sources but ...

An IPv4 address is just a 32 bits unsigned integer (network endiannes), which means in code they ususally are either a DWord or a packed array of four bytes. Thus it should be easy to convert them to strings. For example:

Code: Pascal  [Select][+][-]
  1. type
  2.   TIPConvert = packed record
  3.   case Boolean of
  4.   True: (AsDWord: DWORD)
  5.   False: (AsBytes: packed array [0..3] of byte);
  6.   end;
  7.  
  8. function IPString(const IP: array of byte): String; overload;
  9. begin
  10.   Result := Format('%u"."%u"."%u"."%u',[IP[0],IP[1],IP[2],IP[3]]);
  11. end;
  12.  
  13. function IPString(const IP: DWord): String; overload;
  14. var
  15.   AnIPRec: TIPConvert;
  16. begin
  17.   AnIPRec.AsDWord := IP;
  18.   Result := IPString(AnIPRec.AsBytes)
  19. end;
  20.  
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

heejit

  • Full Member
  • ***
  • Posts: 245
Re: Get client IP with fcl-web
« Reply #3 on: September 29, 2018, 04:08:08 pm »
Try:

ARequest.RemoteAddress

 

TinyPortal © 2005-2018