Sometimes I need to check my public IP address and information about it. So I Google "my ip" and click some websites, most of them are full of Ads and the connetction speed is not satisfied. After searching, I find it does have some nice websites like ifconfig.co, ifconfig.me. But for further, how about build one by myself?

I find the source code of ifconfig.co is available on GitHub. So I decide to build a Docker image with it. You can access https://ip.zatp.com to see the demo.

You also can use the Docker Image I build for test:

docker pull hurt/echoip
docker run -d --rm --name echoip -p8080:8080 hurt/echoip

GeoIP database version: 20210511

Now I will show you how to build the image by yourself. It's also available on GitHub.

Build

Clone this project

git clone https://github.com/scenery/EchoIPDocker.git echoip
cd echoip

Download GeoIP data

You can get the newest GeoIP database from MAXMIND At least three files are required as follows:

  • GeoLite2-ASN.mmdb
  • GeoLite2-City.mmdb
  • GeoLite2-Country.mmdb

Then extract and place the files into geoip folder or you can edit run.sh to choose what you need.

Do not forget chmod +x run.sh.

Usage of echoip:
  -C int
        Size of response cache. Set to 0 to disable
  -H value
        Header to trust for remote IP, if present (e.g. X-Real-IP)
  -a string
        Path to GeoIP ASN database
  -c string
        Path to GeoIP city database
  -f string
        Path to GeoIP country database
  -l string
        Listening address (default ":8080")
  -p    Enable port lookup
  -r    Perform reverse hostname lookups
  -t string
        Path to template directory (default "html")

Build the container

docker build -t echoip .

Run

docker run -d --name echoip -p 8080:8080 echoip

Nginx configuration

Now the EchoIP service is running in background. You can run this service with your own domain, just use Nginx to proxy port 8080. There is the main config content:

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

    location / {
        if ($http_user_agent !~* (curl|wget)) {
            return 301 https://$server_name$request_uri;
        }
        proxy_set_header  Host $host;
        proxy_set_header  X-Real-IP $remote_addr;
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header  X-Forwarded-Proto $scheme;
        proxy_pass  http://localhost:8080;
    }
}

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    ssl_certificate /path/to/ssl/chain.pem; 
    ssl_certificate_key /path/to/ssl/private.key;
    ...

    location / {
        proxy_set_header  Host $host;
        proxy_set_header  X-Real-IP $remote_addr;
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header  X-Forwarded-Proto $scheme;
        proxy_pass  http://localhost:8080;
    }
}

Usage

In addition to direct access to the website, there are more convenient usage:

IP lookup:

$ curl ip.zatp.com
127.0.0.1
$ http ip.zatp.com
127.0.0.1
$ wget -qO- ip.zatp.com
127.0.0.1
$ fetch -qo- https://ip.zatp.com
127.0.0.1
$ bat -print=b ip.zatp.com/ip
127.0.0.1

Country and city lookup:

$ curl ip.zatp.com/country
Elbonia
$ curl ip.zatp.com/country-iso
EB
$ curl ip.zatp.com/city
Bornyasherk
$ curl ip.zatp.com/asn
AS59795

As json:

$ curl ip.zatp.com/json
{
  "city": "Bornyasherk",
  "country": "Elbonia",
  "country_iso": "EB",
  "ip": "127.0.0.1",
  "ip_decimal": 2130706433,
  "asn": "AS59795",
  "asn_org": "Hosting4Real"
}