{"id":21275599,"url":"https://github.com/xin053/ipd","last_synced_at":"2025-07-11T08:30:29.798Z","repository":{"id":65315893,"uuid":"133937670","full_name":"xin053/ipd","owner":"xin053","description":"查询ip地理信息,多种查询模式,高效,具有缓存机制,并可使用elasticsearch构建自己的ip数据库","archived":false,"fork":false,"pushed_at":"2018-11-10T01:47:59.000Z","size":111342,"stargazers_count":26,"open_issues_count":0,"forks_count":6,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-06T01:41:28.291Z","etag":null,"topics":["bigcache","elasticsearch","geolite2","gin","go","ip","ip2region","ipd","qqwry"],"latest_commit_sha":null,"homepage":"https://xin053.github.io/ipd/","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/xin053.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-05-18T10:05:16.000Z","updated_at":"2025-01-03T21:47:08.000Z","dependencies_parsed_at":"2023-01-16T15:15:55.612Z","dependency_job_id":null,"html_url":"https://github.com/xin053/ipd","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/xin053/ipd","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xin053%2Fipd","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xin053%2Fipd/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xin053%2Fipd/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xin053%2Fipd/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/xin053","download_url":"https://codeload.github.com/xin053/ipd/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xin053%2Fipd/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":264765478,"owners_count":23660570,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["bigcache","elasticsearch","geolite2","gin","go","ip","ip2region","ipd","qqwry"],"created_at":"2024-11-21T09:34:23.057Z","updated_at":"2025-07-11T08:30:24.782Z","avatar_url":"https://github.com/xin053.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# ipd\r\n\r\n批量ip反查服务, 批量获取ip的地理位置信息,包括国家,省份,城市,ISP以及地理位置,支持国内外ip，目前仅支持ipv4.\r\n\r\n基于[`gin框架`](https://github.com/gin-gonic/gin)构建, 默认端口`6789`,可在`config.toml`中修改服务端口以及其他配置项\r\n\r\n## 从源代码构建\r\n\r\n1. 下载包管理器[`dep`](https://github.com/golang/dep)\r\n\r\n    ```shell\r\n    go get -u github.com/golang/dep/cmd/dep\r\n    ```\r\n\r\n    **确保`dep`在环境变量`PATH`中**\r\n2. 下载[`ipd`](https://github.com/xin053/ipd)源码\r\n\r\n    ```shell\r\n    export GOPATH=`pwd`\r\n    go get -d github.com/golang/xin053/ipd\r\n    ```\r\n\r\n3. 安装依赖\r\n\r\n    ```shell\r\n    dep ensure\r\n    ```\r\n\r\n4. 构建`ipd`可执行文件\r\n\r\n    ```shell\r\n    cd github.com/golang/xin053/ipd\r\n    go build ipd.go\r\n    ```\r\n\r\n    或者\r\n\r\n    ```shell\r\n    go build -ldflags \"-w -s\" ipd.go\r\n    ```\r\n\r\n5. 使用\r\n\r\n    ```shell\r\n    # 运行\r\n    ./ipd\r\n    # 停止\r\n    kill -2 pid\r\n    ```\r\n\r\n## 使用\r\n\r\n`ipd`提供四种ip反查的方式:\r\n\r\n1. 通过[纯真ip数据库](http://www.cz88.net/fox/ipdat.shtml)(目前更新到`2018-09-25`)\r\n\r\n   [*纯真ip数据库官网*](http://www.cz88.net/fox/ipdat.shtml),下载可能需要翻墙,下载后安装会释放出`qqwry.dat`文件,也可以使用 [`UniExtract2`](https://github.com/Bioruebe/UniExtract2) 直接解压出文件\r\n\r\n2. 通过[GeoLite2数据库](https://dev.maxmind.com/zh-hans/geoip/geoip2/geolite2-%E5%BC%80%E6%BA%90%E6%95%B0%E6%8D%AE%E5%BA%93/)\r\n3. 通过[ip2region数据库](https://github.com/lionsoul2014/ip2region),启动服务时会自动从github下载最新的数据库文件(*最优先*)\r\n4. 通过公开的 REST API方式, 目前支持四种接口:\r\n\r\n    1. 淘宝ip查询接口:`http://ip.taobao.com/service/getIpInfo.php?ip=`\r\n    2. 新浪ip查询接口:`http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json\u0026ip=`\r\n    3. 太平洋ip查询接口:`http://whois.pconline.com.cn/ipJson.jsp?ip=`\r\n    4. 百度ip查询接口:`http://api.map.baidu.com/location/ip?ak=yourAK\u0026ip=`\r\n\r\n### API\r\n\r\n1. 从公共API获取ip信息\r\n\r\n    **接口**\r\n\r\n    `POST /v1/api`\r\n\r\n    **请求头**\r\n\r\n    1. `Authorization` = `thisisaveryimportantkey`(key可在`config.go`中配置)\r\n    2. `Content-Type` = `application/json`\r\n\r\n    **Body**\r\n\r\n    ```json\r\n    {\r\n        \"ip\":\r\n            [\"111.111.111.111\", \"8.8.8.8\"]\r\n    }\r\n    ```\r\n\r\n    **返回**\r\n\r\n    ```json\r\n    [\r\n        {\r\n            \"ip\": \"8.8.8.8\",\r\n            \"country\": \"美国\",\r\n            \"region\": \"\",\r\n            \"city\": \"\",\r\n            \"isp\": \"Google公共DNS\",\r\n            \"geo_x\": 0,\r\n            \"geo_y\": 0\r\n        },\r\n        {\r\n            \"ip\": \"111.111.111.111\",\r\n            \"country\": \"\",\r\n            \"region\": \"\",\r\n            \"city\": \"\",\r\n            \"isp\": \"日本东京市KDDI通信公司\",\r\n            \"geo_x\": 0,\r\n            \"geo_y\": 0\r\n        }\r\n    ]\r\n    ```\r\n\r\n2. 从纯真ip数据库获取ip信息\r\n\r\n    **接口**\r\n\r\n    `POST /v1/db`\r\n\r\n    **请求头**\r\n\r\n    1. `Authorization` = `thisisaveryimportantkey`(key可在`config.go`中配置)\r\n    2. `Content-Type` = `application/json`\r\n\r\n    **Body**\r\n\r\n    ```json\r\n    {\r\n        \"ip\":\r\n            [\"111.111.111.111\", \"8.8.8.8\"]\r\n    }\r\n    ```\r\n\r\n    **返回**\r\n\r\n    ```json\r\n    [\r\n        {\r\n            \"ip\": \"8.8.8.8\",\r\n            \"country\": \"美国\",\r\n            \"region\": \"\",\r\n            \"city\": \"\",\r\n            \"isp\": \"Google公共DNS\",\r\n            \"geo_x\": 0,\r\n            \"geo_y\": 0\r\n        },\r\n        {\r\n            \"ip\": \"111.111.111.111\",\r\n            \"country\": \"\",\r\n            \"region\": \"\",\r\n            \"city\": \"\",\r\n            \"isp\": \"日本东京市KDDI通信公司\",\r\n            \"geo_x\": 0,\r\n            \"geo_y\": 0\r\n        }\r\n    ]\r\n    ```\r\n\r\n3. 从GeoLite2数据库获取ip信息\r\n\r\n    **接口**\r\n\r\n    `POST /v1/db2`\r\n\r\n    **请求头**\r\n\r\n    1. `Authorization` = `thisisaveryimportantkey`(key可在`config.go`中配置)\r\n    2. `Content-Type` = `application/json`\r\n\r\n    **Body**\r\n\r\n    ```json\r\n    {\r\n        \"ip\":\r\n            [\"111.111.111.111\", \"8.8.8.8\"]\r\n    }\r\n    ```\r\n\r\n    **返回**\r\n\r\n    ```json\r\n    [\r\n        {\r\n            \"ip\": \"8.8.8.8\",\r\n            \"country\": \"美国\",\r\n            \"region\": \"\",\r\n            \"city\": \"\",\r\n            \"isp\": \"Google公共DNS\",\r\n            \"geo_x\": 0,\r\n            \"geo_y\": 0\r\n        },\r\n        {\r\n            \"ip\": \"111.111.111.111\",\r\n            \"country\": \"\",\r\n            \"region\": \"\",\r\n            \"city\": \"\",\r\n            \"isp\": \"日本东京市KDDI通信公司\",\r\n            \"geo_x\": 0,\r\n            \"geo_y\": 0\r\n        }\r\n    ]\r\n    ```\r\n\r\n4. 从ip2region数据库获取ip信息\r\n\r\n    **接口**\r\n\r\n    `POST /v1/db3`\r\n\r\n    **请求头**\r\n\r\n    1. `Authorization` = `thisisaveryimportantkey`(key可在`config.go`中配置)\r\n    2. `Content-Type` = `application/json`\r\n\r\n    **Body**\r\n\r\n    ```json\r\n    {\r\n        \"ip\":\r\n            [\"111.111.111.111\", \"8.8.8.8\"]\r\n    }\r\n    ```\r\n\r\n    **返回**\r\n\r\n    ```json\r\n    [\r\n        {\r\n            \"ip\": \"8.8.8.8\",\r\n            \"country\": \"美国\",\r\n            \"region\": \"\",\r\n            \"city\": \"\",\r\n            \"isp\": \"Google公共DNS\",\r\n            \"geo_x\": 0,\r\n            \"geo_y\": 0\r\n        },\r\n        {\r\n            \"ip\": \"111.111.111.111\",\r\n            \"country\": \"\",\r\n            \"region\": \"\",\r\n            \"city\": \"\",\r\n            \"isp\": \"日本东京市KDDI通信公司\",\r\n            \"geo_x\": 0,\r\n            \"geo_y\": 0\r\n        }\r\n    ]\r\n    ```\r\n\r\n5. 整合以上四种方式获取ip信息,先异步查ip2region数据库(默认的主数据库),查不到的ip再查纯真, 再查GeoLite2数据库,最后通过api查询\r\n\r\n    主数据库可在配置文件中配置(`config.toml`中的`request_order`配置)\r\n\r\n    **接口**\r\n\r\n    `POST /v1/ip`\r\n\r\n    **请求头**\r\n\r\n    1. `Authorization` = `thisisaveryimportantkey`(key可在`config.go`中配置)\r\n    2. `Content-Type` = `application/json`\r\n\r\n    **Body**\r\n\r\n    ```json\r\n    {\r\n        \"ip\":\r\n            [\"111.111.111.111\", \"8.8.8.8\"]\r\n    }\r\n    ```\r\n\r\n    **返回**\r\n\r\n    ```json\r\n    [\r\n        {\r\n            \"ip\": \"8.8.8.8\",\r\n            \"country\": \"美国\",\r\n            \"region\": \"\",\r\n            \"city\": \"\",\r\n            \"isp\": \"Google公共DNS\",\r\n            \"geo_x\": 0,\r\n            \"geo_y\": 0\r\n        },\r\n        {\r\n            \"ip\": \"111.111.111.111\",\r\n            \"country\": \"\",\r\n            \"region\": \"\",\r\n            \"city\": \"\",\r\n            \"isp\": \"日本东京市KDDI通信公司\",\r\n            \"geo_x\": 0,\r\n            \"geo_y\": 0\r\n        }\r\n    ]\r\n    ```\r\n\r\n## 项目结构\r\n\r\n```\r\napi\\\r\n   |api.go                      # api 方式查询 ip 信息主文件\r\n   |baidu.go                    # 百度 ip API 服务解析\r\n   |base.go                     # 通用接口\r\n   |pconline.go                 # 太平洋 ip API 服务解析\r\n   |sina.go                     # 新浪 ip API 服务解析\r\n   |taobao.go                   # 淘宝 ip API 服务解析\r\nconfig\\\r\n      |config.go                # 从 config.toml 读取配置以及其他配置\r\nes\\\r\n  |es.go                        # elasticsearch 存储相关\r\ngeolite2\\\r\n        |geolite2.go            # db2 方式查询 ip 信息主文件\r\nip2region\\\r\n         |ip2region             # db3 方式查询 ip 信息主文件\r\n         |lib                   # 解析 ip2region.db 的库文件\r\nmiddleware\\\r\n          |auth.go              # 简单授权验证中间件\r\n          |cors.go              # cors 跨域中间件\r\n          |json_logger.go       # 日志服务中间件\r\n          |sentry.go            # sentry 服务\r\nqqwry\\\r\n     |qqwry.go                  # db 方式查询 ip 信息主文件\r\nserver\\\r\n     |server.go                 # /v1/ip 接口主文件\r\nutils\\\r\n     |utils_test.go             # 工具包测试\r\n     |utils.go                  # 工具包\r\nconfig.toml                     # ipd 服务使用的配置文件\r\nGeoLite2-City.mmdb              # geolite2 数据库二进制文件\r\nGopkg.lock                      # dep 包管理器 lock 文件\r\nGopkg.toml                      # dep 包管理器 toml 文件\r\nip2region.db                    # ip2region 数据库二进制文件\r\nipd.go                          # ipd 服务 main 包\r\nqqwry.dat                       # 纯真 ip 数据库二进制文件\r\nREADME.md                       # README\r\n```\r\n\r\n## 构建自己的ip信息数据库\r\n\r\nipd服务支持将查询过的ip信息添加到elasticsearch数据库,作后续其他的使用.默认启用elasticsearch存储\r\n\r\n1. 安装[elasticsearch](https://www.elastic.co/products/elasticsearch),建议安装最新版\r\n2. 修改`config.toml`中的`elasticsearch`段的`url`, 如果不想使用存储ip功能,将`elasticsearch`段注释即可\r\n\r\n## 其他事项\r\n\r\n1. 程序默认使用sentry服务, 修改`config.go`中的`dsn`以使用自己的sentry,也可以注释掉`config.toml`中的`sentry`段来禁用sentry\r\n2. 更多设置请查看`config.toml`的备注说明\r\n\r\n## TODO\r\n\r\n- [ ] 添加es接口，直接从es查询ip数据\r\n- [ ] 增加精准查询接口(多种查询方式同时 goroutine，获取接口分析取最优)\r\n- [x] 添加缓存机制","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxin053%2Fipd","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fxin053%2Fipd","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxin053%2Fipd/lists"}