# GoIP接口 **Repository Path**: liumou_site/go-ip-interface ## Basic Information - **Project Name**: GoIP接口 - **Description**: 使用Go实现的公网IP获取接口,可通过浏览器或者编程的方式获取当前设备的公网IP,支持IPV6&IPV4 - **Primary Language**: Go - **License**: AGPL-3.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2024-01-13 - **Last Updated**: 2026-07-29 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # 🌐 GoIP接口
[![Go](https://img.shields.io/badge/Go-1.19+-00ADD8?style=for-the-badge&logo=go)](https://golang.org/) [![License](https://img.shields.io/badge/License-MIT-green.svg?style=for-the-badge)](LICENSE) [![Release](https://img.shields.io/github/v/release/liumou_site/go-ip-interface?style=for-the-badge)](https://gitee.com/liumou_site/go-ip-interface/releases) _🔍 使用Go实现的高性能公网IP获取服务,支持IPV4/IPV6双栈_
## ✨ 功能特性
| 特性 | 状态 | 描述 | | ------------------ | ---- | ------------------------------ | | 🌍 **IPV4查询** | ✅ | 支持公网IPv4地址获取 | | 🌐 **IPV6查询** | ✅ | 支持公网IPv6地址获取 | | 🎨 **Web界面** | ✅ | 响应式设计,支持IP版本自动识别 | | 🔌 **RESTful API** | ✅ | 提供文本和JSON格式接口 | | ⚡ **一键服务** | ✅ | systemd服务一键安装/卸载 | | 🎭 **个性化** | ✅ | 支持自定义背景、图标、端口 | | 📦 **多平台** | ✅ | 独立二进制文件,跨平台支持 |
## 🚀 快速开始 ### 🪟 Windows系统 ```powershell # 1. 下载Windows版本 # 从 releases 页面下载 ips_windows-amd64.exe # 2. 直接运行 ips_windows-amd64.exe -p 8080 ``` ### 🐳 Docker(已支持) **📋 快速开始** ```bash # 使用腾讯云镜像一键启动 docker run -d -p 22010:22010 --name ip-service ccr.ccs.tencentyun.com/liumou/goip:latest # 或使用Docker Compose docker-compose up -d ``` **📖 完整文档** 查看 [DOCKER.md](DOCKER.md) 获取详细的Docker部署指南,包括: - ✅ IPv4/IPv6双栈支持配置 - ✅ 腾讯云镜像使用 - ✅ Docker Compose配置 - ✅ 环境变量和卷挂载 - ✅ 故障排除和性能优化 ### Linux系统(推荐) 1. **下载二进制文件** ```shell wget https://gitee.com/liumou_site/go-ip-interface/releases/download/1.9.0/ip_linux-arm64 -O ips chmod +x ips ``` 2. **安装为systemd服务** ```shell sudo ./ips -install ``` 效果 ```shell [root@bxy2 data]# wget https://gitee.com/liumou_site/go-ip-interface/releases/download/1.9.0/ip_linux-arm64 -O ips ``` --2025-09-09 11:04:03-- 正在解析主机 gitee.com (gitee.com)... 180.76.199.13, 180.76.198.77, 180.76.198.225 正在连接 gitee.com (gitee.com)|180.76.199.13|:443... 已连接。 已发出 HTTP 请求,正在等待回应... 302 Found 位置: [跟随至新的 URL] --2025-09-09 11:04:03-- 再次使用存在的到 gitee.com:443 的连接。 已发出 HTTP 请求,正在等待回应... 302 Found 位置: [跟随至新的 URL] --2025-09-09 11:04:04-- 正在解析主机 foruda.gitee.com (foruda.gitee.com)... 180.76.199.13, 180.76.198.77 正在连接 foruda.gitee.com (foruda.gitee.com)|180.76.199.13|:443... 已连接。 已发出 HTTP 请求,正在等待回应... 200 OK 长度:15931484 (15M) [application/x-executable] 正在保存至: “ips” ips 100%[================================================================================================================================================================>] 15.19M 3.16MB/s 用时 7.0s 2025-09-09 11:04:11 (2.16 MB/s) - 已保存 “ips” [15931484/15931484]) [root@bxy2 data]# chmod +x ips [root@bxy2 data]# ./ips -h Usage of ips: -bg string 设置背景图标,留空使用内置背景 -d 显示更多运行信息(debug) -f string 设置favicon.ico路径,留空使用内置图标 -install 安装systemd服务 -p int 设置服务器端口 (default 22010) -pid 写入pid文件(用于system管理) -t string 设置static路径,留空使用内置静态资源 -u4 string 设置IPV4专用URL (default "") -u6 string 设置IPV6专用URL (default "") -uninstall 卸载systemd服务 -v 显示版本号 -v4 监听IPV4端口-参数已作废 (default true) -v6 监听IPV6端口-参数已作废 [root@bxy2 data]# ./ips -install 服务安装成功 使用命令启动: systemctl start ips 查看状态: systemctl status ips [root@bxy2 data]# systemctl status ips ● ips.service - IP地址查询服务 Loaded: loaded (/etc/systemd/system/ips.service; enabled; vendor preset: disabled) Active: inactive (dead) [root@bxy2 data]# systemctl start ips [root@bxy2 data]# systemctl status ips ● ips.service - IP地址查询服务 Loaded: loaded (/etc/systemd/system/ips.service; enabled; vendor preset: disabled) Active: active (running) since Tue 2025-09-09 11:04:39 CST; 1s ago Main PID: 2195581 (ips) Tasks: 12 Memory: 19.3M CGroup: /system.slice/ips.service └─2195581 /opt/ips/ips 9月 09 11:04:39 bxy2 ips[2195581]: [2025-09-09 11:04:39] [INFO] [static:28] 使用内置favicon.ico 9月 09 11:04:39 bxy2 ips[2195581]: [GIN-debug] GET /json --> ip/model.(*Server).ApiJson.func1 (3 handlers) 9月 09 11:04:39 bxy2 ips[2195581]: [GIN-debug] GET /api --> ip/model.(*Server).Api.func1 (3 handlers) 9月 09 11:04:39 bxy2 ips[2195581]: [GIN-debug] GET / --> ip/model.(\*Server).IndexHtml.func1 (3 handlers) 9月 09 11:04:39 bxy2 ips[2195581]: [GIN-debug] [WARNING] You trusted all proxies, this is NOT safe. We recommend you to set a value. 9月 09 11:04:39 bxy2 ips[2195581]: Please check for details. 9月 09 11:04:39 bxy2 ips[2195581]: [GIN-debug] Listening and serving HTTP on :22010 [root@bxy2 data]# ```` 3. **访问服务** 打开浏览器访问: ### Windows系统 1. **下载可执行文件** 从 [releases页面](https://gitee.com/liumou_site/go-ip-interface/releases) 下载 `ips_windows-amd64.exe` 2. **直接运行** ```shell ips_windows-amd64.exe -p 8080 ```` ## 📥 安装教程 ### 🎯 下载地址
| 平台 | 下载链接 | 文件类型 | | ---------- | ----------------------------------------------------------------------- | ----------------------- | | 🐧 Linux | [最新版](https://gitee.com/liumou_site/go-ip-interface/releases/latest) | `ips_linux-amd64` | | 🪟 Windows | [最新版](https://gitee.com/liumou_site/go-ip-interface/releases/latest) | `ips_windows-amd64.exe` |
### 🛠️ 安装方式对比 | 方式 | 适用场景 | 优点 | 安装命令 | | ----------------- | ----------- | ---------------- | ---------------------- | | **Docker部署** 🐳 | 容器化环境 | 跨平台、易管理 | `docker-compose up -d` | | **二进制安装** 🔧 | Linux服务器 | 轻量级、快速部署 | `sudo ips -install` | | **手动运行** ▶️ | 测试/开发 | 灵活配置 | `./ips -p 8080` | ## 📝 使用说明 ### 🎛️ 命令行参数
| 参数 | 说明 | 默认值 | 示例 | | ------ | ------------ | ---------------------------------------- | ------------------------- | | `-p` | 服务器端口 | `22010` | `-p 8080` | | `-bg` | 背景图片路径 | `bg1.jpg` | `-bg /path/to/bg.jpg` | | `-f` | favicon路径 | `images/favicon.ico` | `-f /path/to/favicon.ico` | | `-u4` | IPV4专用URL | `https://ip4.liumou.site/` | `-u4 https://example.com` | | `-u6` | IPV6专用URL | `https://ip6.liumou.site/` | `-u6 https://example.com` | | `-d` | 调试模式 | 关闭 | `-d` | | `-pid` | 写入PID文件 | 关闭 | `-pid` |
### 🔧 服务管理命令 ```bash # 安装为systemd服务(Linux) sudo ips -install # 卸载systemd服务 sudo ips -uninstall # 查看版本 ips -v # 使用自定义端口启动 ips -p 8080 ``` ### 服务安装与管理 #### 二进制安装systemd服务(推荐) 对于Linux系统,可以直接使用二进制程序安装systemd服务: **安装服务:** ```shell sudo ips -install ``` **卸载服务:** ```shell sudo ips -uninstall ``` 安装完成后,服务会自动启用并设置为开机自启。 #### Deb安装包方式 ## 🚀 服务管理 ### ⚡ 二进制一键安装
```bash # 一键安装(推荐) sudo ips -install # 一键卸载 sudo ips -uninstall ```
> 💡 **特点**:自动配置systemd服务、设置开机自启、创建专用用户 ### 🎮 systemd服务管理
| 操作 | 命令 | 说明 | | ------- | ---------------------------- | ------------ | | ▶️ 启动 | `sudo systemctl start ips` | 立即启动服务 | | ⏹️ 停止 | `sudo systemctl stop ips` | 停止服务运行 | | 🔄 重启 | `sudo systemctl restart ips` | 重启服务 | | ✔️ 启用 | `sudo systemctl enable ips` | 开机自动启动 | | ❌ 禁用 | `sudo systemctl disable ips` | 取消开机启动 | | 📊 状态 | `sudo systemctl status ips` | 查看详细状态 |
## 效果图 ### 服务状态 ![STATUS](images/STATUS.png) ### 首页-IPV4 ![index](images/index.png) ### 首页-IPV6 ![index](images/index6.png) ### 局域网 ![lan](images/LAN.png) ### Api接口-文本 ![Api](images/api.png) ### Api接口-Json ![Api](images/json.png) ### 🚀 API使用示例
| 格式类型 | 请求URL | 返回示例 | 使用场景 | | --------------- | ----------- | ------------------------ | -------------------- | | 📄 **文本格式** | `GET /ip` | `192.168.1.100` | 脚本集成、命令行工具 | | 📊 **JSON格式** | `GET /json` | `{"ip":"192.168.1.100"}` | Web应用、API调用 |
#### 🎯 curl命令示例 ```bash # 📄 获取纯文本IP curl http://localhost:22010/ip # 输出: 192.168.1.100 # 📊 获取JSON格式IP curl http://localhost:22010/json # 输出: {"ip":"192.168.1.100"} ``` #### 🐍 Python集成示例 ```python import requests # 基础使用 ip = requests.get('http://localhost:22010/ip').text print(f"IP地址: {ip}") # JSON格式处理 response = requests.get('http://localhost:22010/json') data = response.json() print(f"IP地址: {data['ip']}") # 错误处理示例 try: response = requests.get('http://localhost:22010/json', timeout=5) response.raise_for_status() ip_data = response.json() print(f"成功获取IP: {ip_data['ip']}") except requests.exceptions.RequestException as e: print(f"获取IP失败: {e}") ``` ![req](images/req.png) ### 启动 ![run](images/run.png) ## 🌐 双栈获取机制 本项目支持 **IPv4/IPv6 双栈IP自动获取**,核心流程如下: 1. **本地检测**:前端请求当前服务的 `/dual` 接口,获取当前连接的IP地址 2. **配置下发**:后端通过 `/dual` 接口返回 `url4` 和 `url6` 专用栈URL配置 3. **跨栈获取**:如果仅检测到单栈(如只有IPv4),前端自动请求另一个栈的URL(如 `ip6.liumou.site/dual`)获取对端IP 4. **Cookie缓存**:获取到的对端IP会写入Cookie,后续访问可快速恢复 ### 配置专用栈URL ```bash # 命令行参数 ./ips -u4 https://ip4.liumou.site/ -u6 https://ip6.liumou.site/ # 环境变量(Docker) IP_URL4=https://ip4.liumou.site/ IP_URL6=https://ip6.liumou.site/ ``` > 💡 建议将 `ip4` 域名仅解析A记录,`ip6` 域名仅解析AAAA记录,以确保跨栈请求使用正确的协议 # 🌐 访问服务
## 🔗 访问地址 | 访问方式 | URL地址 | 说明 | | ----------------- | ------------------------------------------------ | -------------- | | 🏠 **本地访问** | [http://127.0.0.1:22010](http://127.0.0.1:22010) | 本机浏览器访问 | | 🌐 **局域网访问** | `http://<本机IP>:22010` | 其他设备访问 | | 📱 **手机访问** | `http://<本机IP>:22010` | 移动端浏览器 |
## ⚙️ 端口配置
| 配置方式 | 命令 | 说明 | | ------------------ | ----------------------------------------------------------------------- | ------------ | | **systemd服务** | `sudo sed -i 's/pid.*/pid -p 新端口/g' /etc/systemd/system/ips.service` | 修改服务端口 | | **二进制直接运行** | `./ips -p 新端口` | 临时指定端口 | | **Docker运行** | `docker run -p 新端口:22010 ...` | 容器端口映射 |
> 💡 **默认端口**: `22010` (可在启动时通过 `-p` 参数修改) # 📝 更新日志
## 🚀 v1.11.0 | 功能类别 | 更新内容 | 影响级别 | | --------------- | --------------------------------------------------------------------- | -------- | | 🌐 **双栈优化** | 前端检测到单栈时自动跨栈请求另一个栈URL获取对端IP | 🟢 新增 | | ⚙️ **后端配置** | `/dual`接口返回`url4`/`url6`配置,支持环境变量`IP_URL4`/`IP_URL6` | 🟢 新增 | | 🐳 **Docker** | docker-compose.yml 添加`IP_URL4`/`IP_URL6`环境变量 | 🟢 改进 | | 🎨 **前端改进** | 导航链接动态更新、跨栈获取状态提示、版本号显示优化 | 🟢 改进 | ## 📦 v2.0.0 > **发布日期**: 2024年12月 | 功能类别 | 更新内容 | 影响级别 | | --------------- | --------------------------------------------- | -------- | | 🔧 **服务管理** | 新增systemd服务一键安装/卸载功能 | 🟢 新增 | | 🎯 **算法优化** | 优化IP版本识别算法,支持更准确的IPv4/IPv6判断 | 🟢 改进 | | 🎨 **界面升级** | 改进Web界面,支持IP版本自动切换 | 🟢 改进 | | ✂️ **功能简化** | 移除多余的服务管理命令,简化操作 | 🟡 变更 | | 📦 **依赖优化** | 使用标准库替代第三方依赖,减少外部依赖 | 🟢 优化 | ## 📦 v1.8 (稳定版本) > **发布日期**: 2024年11月 | 功能类别 | 更新内容 | 影响级别 | | --------------- | ------------------ | -------- | | 🎨 **个性化** | 支持自定义背景图片 | 🟢 新增 | | 🔧 **进程管理** | 支持PID文件管理 | 🟢 新增 |
# 🌟 在线演示
## 🌐 实时体验 | 演示环境 | 访问地址 | 特色功能 | | --------------- | ------------------------------------------------------------------------------ | ------------------- | | 🌏 **IPv4演示** | [https://ip4.liumou.site](https://ip4.liumou.site) | 公网访问、HTTPS加密 | | 🌍 **IPv6演示** | [https://ip6.liumou.site](https://ip6.liumou.site) | IPv6原生支持 | ## 🎯 快速体验 ```bash # 一键测试IPv4 curl https://ip4.liumou.site/ip # 一键测试IPv6 curl https://ip6.liumou.site/ip ```