Skip to content

Commit

Permalink
1.2.0, support client ip info
Browse files Browse the repository at this point in the history
  • Loading branch information
metowolf committed Jul 12, 2021
1 parent 1a148aa commit 4a653e5
Show file tree
Hide file tree
Showing 6 changed files with 172 additions and 198 deletions.
22 changes: 7 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Open Source REST API for IP database, includes qqwry, ipipdotnet.
## Usage

```http
GET https://api.i-meto.com/ip/v1/qqwry/119.29.29.29
GET http://localhost:3000/v1/qqwry/119.29.29.29
```

```json
Expand All @@ -19,29 +19,21 @@ GET https://api.i-meto.com/ip/v1/qqwry/119.29.29.29
"region_name": "广东",
"city_name": "广州",
"owner_domain": "cloud.tencent.com",
"isp_domain": "",
"range": {
"from": "119.29.0.0",
"to": "119.29.127.255"
}
"isp_domain": ""
}
```

```http
GET https://api.i-meto.com/ip/v1/ipip/119.29.29.29
GET http://localhost:3000/v1/qqwry/me
```

```json
{
"ip": "119.29.29.29",
"country_name": "DNSPOD.COM",
"region_name": "DNSPOD.COM",
"ip": "127.0.0.1",
"country_name": "本机地址",
"region_name": "本机地址",
"city_name": "",
"owner_domain": "",
"isp_domain": "",
"range": {
"from": "119.29.29.0",
"to": "119.29.29.255"
}
"isp_domain": ""
}
```
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "docker-ipdb",
"version": "1.1.0",
"version": "1.2.0",
"main": "server.js",
"repository": "[email protected]:metowolf/docker-ipdb.git",
"author": "metowolf <[email protected]>",
Expand All @@ -18,11 +18,11 @@
"koa-pino-logger": "^3.0.0",
"koa-router": "^10.0.0",
"koa2-cors": "^2.0.6",
"pino": "^6.11.3",
"pino-pretty": "^5.1.0",
"qqwry.ipdb": "^2021.6.30"
"pino": "^6.12.0",
"pino-pretty": "^5.1.1",
"qqwry.ipdb": "^2021.7.7"
},
"devDependencies": {
"nodemon": "^2.0.9"
"nodemon": "^2.0.12"
}
}
4 changes: 3 additions & 1 deletion src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,7 @@ module.exports = {
port: parseInt(process.env.HTTP_PORT || 3000, 10),
host: process.env.HTTP_HOST || 'localhost',
prefix: process.env.HTTP_PREFIX || ''
}
},
client_header: process.env.CLIENT_HEADER || 'x-real-ip',
disable_range: (process.env.DISABLE_RANGE || 'true') === 'true'
}
12 changes: 9 additions & 3 deletions src/services/v1/ipip/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const Router = require('koa-router')
const IPDB = require('ipdb')
const ipdb_range = require('@ipdb/range')
const ipdb_database = require('@ipdb/database')
const config = require('../../../config')
const ipdb = new IPDB(ipdb_database, {
patches: [ipdb_range]
})
Expand All @@ -26,7 +27,10 @@ router.get('/version', async (ctx) => {
// find ip
router.get('/:ip', async (ctx) => {
try {
const ip = ctx.params.ip
let ip = ctx.params.ip
if (ip === 'me') {
ip = ctx.headers[config.client_header] || ctx.ip
}
const { code, data, message } = ipdb.find(ip)
if (code) {
throw new Error(message)
Expand All @@ -37,8 +41,10 @@ router.get('/:ip', async (ctx) => {
region_name: data.region_name,
city_name: data.city_name,
owner_domain: data.owner_domain || '',
isp_domain: data.isp_domain || '',
range: {
isp_domain: data.isp_domain || ''
}
if (!config.disable_range) {
result.range = {
from: data.range.from,
to: data.range.to
}
Expand Down
12 changes: 9 additions & 3 deletions src/services/v1/qqwry/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const Router = require('koa-router')
const IPDB = require('ipdb')
const ipdb_range = require('@ipdb/range')
const qqwry_ipdb = require('qqwry.ipdb')
const config = require('../../../config')
const ipdb = new IPDB(qqwry_ipdb, {
patches: [ipdb_range]
})
Expand All @@ -26,7 +27,10 @@ router.get('/version', async (ctx) => {
// find ip
router.get('/:ip', async (ctx) => {
try {
const ip = ctx.params.ip
let ip = ctx.params.ip
if (ip === 'me') {
ip = ctx.headers[config.client_header] || ctx.ip
}
const { code, data, message } = ipdb.find(ip)
if (code) {
throw new Error(message)
Expand All @@ -37,8 +41,10 @@ router.get('/:ip', async (ctx) => {
region_name: data.region_name,
city_name: data.city_name,
owner_domain: data.owner_domain || '',
isp_domain: data.isp_domain || '',
range: {
isp_domain: data.isp_domain || ''
}
if (!config.disable_range) {
result.range = {
from: data.range.from,
to: data.range.to
}
Expand Down
Loading

0 comments on commit 4a653e5

Please sign in to comment.