Hacker News new | past | comments | ask | show | jobs | submit login

It's great to have services like this.

For the benefit of anyone interested: for a "self-hosted" solution, you can do this entirely within Nginx. Here's an example config:

    server {
      listen 80 default_server;
      listen [::]:80 default_server;

      listen 443 default_server;
      listen [::]:443 default_server;

      # Use Letsencrypt for SSL. This part will depend on your own setup.
      ssl_certificate /etc/letsencrypt/live/<my domain>/fullchain.pem;
      ssl_certificate_key /etc/letsencrypt/live/<my domain>/privkey.pem;

      server_name <my domain>;

      # Deny all access at all paths; useful if you're hosting other stuff behind
      # the same Nginx server (e.g. reverse proxy)
      location / {
        deny all;
      }

      # At /ip, return 200 with the client IP address in the body
      location = /ip {
        default_type text/plain;
        return 200 '$remote_addr';
      }
    }



It is even easier with Caddy's `respond` directive[0], placeholders[1], and automatic HTTPS.

Caddyfile:

  example.com {
      respond "{remote_host}"
  }
[0] https://caddyserver.com/docs/caddyfile/directives/respond

[1] https://caddyserver.com/docs/caddyfile/concepts#placeholders


One more reason to love that extraordinary web server. It is really wonderful, sad that it is not more used.

I work in IT (was a sysadmin for years, still administer my own servers) and I hated the configuration of the web servers (first Apache, then nginx) - mostly because I was too lazy to read the docs from beginning to end.

This changed with caddy. It is simple, fast, reliable, HTTPS first with LE. Great.


Along the same lines, if you want to make your own AWS Lambda /API Gateway version of this:

    def lambda_handler(event, context):
        return {
            'statusCode': '200',
            'headers': None,
            'body': event.get('requestContext', {}).get('identity', {}).get('sourceIp', 'unknown')
        }
I do this, though my lambda is a bit more complex in practice, since I have some triggers that say "if this thing reports a new IP, do something".

Of course, AWS provides this basic service as checkip.amazonaws.com


That's a brilliant way of adding and removing SSH security group rules for digital nomads


That's exactly what I use it for. I have a small program on my laptop that makes a request of my lambda every now and then (and if it senses a change of the network). It triggers a change in the firewall rules for a SSH server.

Between that and Mosh, I barely even notice when I change networks.


This can also be done with HAProxy

    listen whatismyip
        bind :::80 # listen on ipv4/ipv6
        bind :::443 ssl crt /etc/haproxy/ssl/fullchain.pem
        mode http

        http-request return status 200 content-type "text/plain" lf-string "%[src]" if { path /ip }
        http-request return status 200 content-type "application/json" lf-string "{\"ip\":\"%[src]\"}" if { path /json_ip }
        http-request deny


this service does a lot more than just return your remote_ip, which wont work behind a load-balancer or other proxy unless you configure realip module. and also need to add geoip module to do all the location stuff


Good points both.

That said, do you know of any software library that exposes the Geoip database (or at least Geoip Lite which you one easily obtain for free) in a nice API? Like how a lot of programming languages have tzinfo/tzdata libraries for querying the Tz database.


Maxmind do APIs for a bunch of language. I've used the python one and it works well (with Lite), but see here their list:

https://dev.maxmind.com/geoip/geoip2/web-services/


I should clarify that I was looking for an "offline" API/library that I can use against a local copy of the Lite database, but this is great stuff too.


Not sure what language you're looking for, but those exist too. Here's a Python one: https://maxminddb.readthedocs.io/en/latest/


If you click through one of the links (e.g. Python) you will see it does allow you to do so!

https://pypi.org/project/geoip2/


I thought maxmind has some kind of terms change that forced account signup and some other issues 'because of some privacy law' like gdpr - maybe the Cali one?

So it has ruined my second favorite wordpress security plugin - and MaxMind not really usable like it once was (?)


MaxMind offers a small version of their database API for free:

https://dev.maxmind.com/geoip/geoip2/geolite2/

In practice it's good enough for many purposes. It's actually the version of the database that ifconfig.co uses.


nginx with GeoIP2 module does exactly same for me for years by now.


You can even have the JSON version:

    location /json_ip {
        default_type application/json;
        return 200 "{\"ip\":\"$remote_addr\"}";
    }


been using this for few years. As far as I know, can't return IPv4/IPv6 only from nginx without using separate server block to enforce one of them




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: