8

When a visitor submit a form, I'd like to assoc to inputs his IP adress.

(POST "/form" {params :params} (assoc params :ip-address the-ip)

How to do this?

Thought of doing this:

(POST "/form" {params    :params
               client-ip :remote-addr} 
      (->> params keywordize-keys (merge {:ip-address client-ip}) str)) 

But this returns {... :ip-address "0:0:0:0:0:0:0:1"}

3
  • well this is localhost in ipv6. where does your request originate from?
    – cfrick
    Commented May 3, 2015 at 19:15
  • @cfrick localhost. Yeah. I couldn't test this in prod so wasn't sure about :remote-addr form your answer, I suppose this is right.
    – leontalbot
    Commented May 3, 2015 at 19:24
  • BTW just found out I don't need to use a keywordize-keys fn. Just need wrap-keyword-params . stackoverflow.com/questions/26151801/…
    – leontalbot
    Commented May 3, 2015 at 19:35

3 Answers 3

9

From Arthur Ulfeldt's comment to Bill's answer, I guess we could write this:

(defn get-client-ip [req]
  (if-let [ips (get-in req [:headers "x-forwarded-for"])]
    (-> ips (clojure.string/split #",") first)
    (:remote-addr req)))
2
  • 1
    // , Will this return an IPv6 address, though? Commented Mar 9, 2016 at 3:18
  • @NathanBasanese If I get your question correctly, my answer would be I didn't test form an IPV6 source. I do not know if it will return such a string correctly..
    – leontalbot
    Commented Mar 9, 2016 at 21:05
6

Getting :remote-addr from the request object is typically the correct way to go unless you're sitting behind a load balancer. In that case your load balance may add a header e.g. x-forwarded-for to the request. Which would make something like this appropriate.

(or (get-in request [:headers "x-forwarded-for"]) (:remote-addr request))
1
  • 5
    This will fail for clients that are themselves behind a proxy, because the x-forwarded-for will be a comma separated list of IPs like "2.2.2.2,5.5.5.5" with the actual address you want as everything before the first , and keep in mind they can be IPv6 addresses as well even if your service is on IPv4 Commented May 4, 2015 at 17:05
1

I'm pretty sure you can get this information from the ring request object. Have a look at the ring.middleware.dump (handle-dump) package. If you set that up and a simple route to use it, you will get a nice web page showing all the available values provided in a ring request object. Pretty sure this includes the client IP address. As this information is obtained from the http headers, it will likely be more reliable than manually putting it into your request parameters - putting it into your request parameters either using a form or json could mean it is easier to forge. Besides, if it is already readily available as part of the request object, means one less ting you need to do.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.