2

I have a dart web service application written with Shelf and Rpc apis.

I need to check the client Ip inside my api methods, but I cannot figure how.

The context method does not contain the HttpRequest .contentInfo property.

More, also the shelf Request object does not contain it, so also a middleware function would be of no use.

Anyone has an Idea on how to solve this problem?

J F
  • 1,058
  • 2
  • 10
  • 21
  • Just a question about the context. What are you trying to do with the client's IP address? As it is sometimes not a good idea to identify a client by its IP address. – Pacane Jan 21 '16 at 20:34
  • 1
    I'm writing a server application and I would like to accept user requests only if coming from authorized IPs. – J F Jan 22 '16 at 10:37

1 Answers1

0

You can do this by using the middleware concept of shelf. You can view an example logger middle ware here: https://www.crossdart.info/p/shelf/0.6.5+2/src/handlers/logger.dart.html. An example using the logger can be found here: https://github.com/dart-lang/shelf

In the middleware, you can find the ip address in the request object (which extends message). Message gives you access to the raw http header which contains the ip address. From there you can decide how you want to handle an invalid ip (throw an error, return a request with an error, etc.).

  • 3
    Perhaps it's just because I'm using cURL, but when I view the headers in the created Request object, I can't see any reference to the source IP. In fact, I only have User-Agent, Accept, and Host. Can you point me to where I can get the requester IP? – Zalán Meggyesi Jun 15 '17 at 12:57