惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
云风的 BLOG
云风的 BLOG
小众软件
小众软件
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Apple Machine Learning Research
Apple Machine Learning Research
博客园 - 司徒正美
博客园 - 聂微东
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
美团技术团队
宝玉的分享
宝玉的分享
量子位
V
Visual Studio Blog
罗磊的独立博客
Vercel News
Vercel News
B
Blog
J
Java Code Geeks
S
SegmentFault 最新的问题
Recent Announcements
Recent Announcements
有赞技术团队
有赞技术团队
P
Proofpoint News Feed
GbyAI
GbyAI
G
Google Developers Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC

atpX

自建 S3 兼容对象存储服务 Garage 听风的歌 偶尔是深夜就好了 新玩具 AirPods Pro 3 公交车上的时间 从 WHOIS 到 RDAP 通过 WireGuard 访问 NFS 共享文件 自建音乐串流服务探索 我停止了探索 Fediverse 互联网背景噪音 我的数字生活降级 小城与确定性的墙 一只特立独行的猪 Fediverse 与社交 承认的勇气 mediaX - 轻量书影音记录管理工具 我的博客写作流程 网站新增 Misc 页面 擅长对线的鲍勃 在 Chroot 环境下使用 Rsync 同步 再见 JavaScript 当我玩博客时我在玩什么 为什么我的博客没有友链页面 小熊猫与大熊猫 (HDR 照片测试) 是时候为网站开启 HTTP/3 支持了吗 使用 AdGuard Home 搭建自用 DoH 服务 谈谈读书与消遣 2023 年终总结 西安两日游 从 AirPods「升级」到 EarPods
Build EchoIP service with Docker
ATP · 2021-05-13 · via atpX

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.atpx.com (no longer available) to see the demo.

echoIP

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.

Building

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.atpx.com
127.0.0.1
$ http ip.atpx.com
127.0.0.1
$ wget -qO- ip.atpx.com
127.0.0.1
$ fetch -qo- https://ip.atpx.com
127.0.0.1
$ bat -print=b ip.atpx.com/ip
127.0.0.1

Country and city lookup:

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

As json:

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