Can I get get request remote address from Scripted REST API?

jason_lau
Tera Contributor

I'm using a scripted rest API and in my script I would like to log the request source IP and possible perform source Ip address filtering (as the use case for this interface I do not want to enforce authentication), as well as other logic based on source IP address of the request. 

Since I'm not enforcing authentication, I cannot use session: 

gs.getSession().getClientIP().toString(); 

That does work in the script, but only for authenticated sessions. I really want to pull this from request object ideally, on a per-request basis versus per-session.

Any thoughts? 

 

 

 

1 ACCEPTED SOLUTION

ChrisBurks
Mega Sage

If I remember correctly it should be possible to grab the ip address with the "x-forwarded-for" header.
For example in your scripted REST script doing this:  request.headers["x-forwarded-for"]

Log it by something like this: gs.log("IP address: " + request.headers["x-forwarded-for"], "HEADERS")

But if this is to replace authentication it may not be a good idea as headers can be spoofed. 

View solution in original post

2 REPLIES 2

ChrisBurks
Mega Sage

If I remember correctly it should be possible to grab the ip address with the "x-forwarded-for" header.
For example in your scripted REST script doing this:  request.headers["x-forwarded-for"]

Log it by something like this: gs.log("IP address: " + request.headers["x-forwarded-for"], "HEADERS")

But if this is to replace authentication it may not be a good idea as headers can be spoofed. 

jason_lau
Tera Contributor

Thanks Chris! I updated my script to log IP using x-forwarded-for header (spoofable) and changed authentication to use an API key.