From 8cef0b62168f6c62216294e547d52fcf786ccdd6 Mon Sep 17 00:00:00 2001 From: hanhh <18330117883@163.com> Date: Fri, 27 Aug 2021 14:00:11 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=8D=A2ip=E5=9C=B0=E5=9D=80=E6=9F=A5?= =?UTF-8?q?=E8=AF=A2=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- back/config/util.ts | 31 +++++++++++++++++++------------ package.json | 1 + pnpm-lock.yaml | 3 ++- 3 files changed, 22 insertions(+), 13 deletions(-) diff --git a/back/config/util.ts b/back/config/util.ts index 28b652fd..7384151c 100644 --- a/back/config/util.ts +++ b/back/config/util.ts @@ -1,6 +1,7 @@ import * as fs from 'fs'; import * as path from 'path'; import got from 'got'; +import iconv from 'iconv-lite'; export function getFileContentByName(fileName: string) { if (fs.existsSync(fileName)) { @@ -156,18 +157,24 @@ export async function getNetIp(req: any) { ip = ''; } try { - const baiduApi = got.get( - `https://sp0.baidu.com/8aQDcjqpAAV3otqbppnN2DJv/api.php?query=${ip}&co=&resource_id=6006&t=1555898284898&ie=utf8&oe=utf8&format=json&tn=baidu`, - ); - const ipApi = got.get( - `https://whois.pconline.com.cn/ipJson.jsp?ip=${ip}&json=true`, - ); - const [{ data }, { addr, region, city }] = await ( - await Promise.all([baiduApi, ipApi]) - ).map((x) => JSON.parse(x.body)); - if (data[0] && data[0].location) { - return { address: data[0].location, ip }; - } else if (city && region) { + const baiduApi = got + .get(`https://www.cip.cc/${ip}`, { timeout: 100000 }) + .text(); + const ipApi = got + .get(`https://whois.pconline.com.cn/ipJson.jsp?ip=${ip}&json=true`, { + timeout: 100000, + }) + .buffer(); + const [data, ipApiBody] = await await Promise.all([baiduApi, ipApi]); + + const ipRegx = /.*IP :(.*)\n/; + const addrRegx = /.*数据二 :(.*)\n/; + if (data && ipRegx.test(data) && addrRegx.test(data)) { + const ip = data.match(ipRegx)[1]; + const addr = data.match(addrRegx)[1]; + return { address: addr, ip }; + } else if (ipApiBody) { + const { addr, ip } = JSON.parse(iconv.decode(ipApiBody, 'GBK')); return { address: `${addr}`, ip }; } else { return { address: `获取失败`, ip }; diff --git a/package.json b/package.json index 0fcbea85..260c7e94 100644 --- a/package.json +++ b/package.json @@ -33,6 +33,7 @@ "express-jwt": "^6.0.0", "express-urlrewrite": "^1.4.0", "got": "^11.8.2", + "iconv-lite": "^0.6.3", "jsonwebtoken": "^8.5.1", "lodash": "^4.17.21", "nedb": "^1.8.0", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 8d60b6d9..149d8c11 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -29,6 +29,7 @@ specifiers: express-jwt: ^6.0.0 express-urlrewrite: ^1.4.0 got: ^11.8.2 + iconv-lite: ^0.6.3 jsonwebtoken: ^8.5.1 lint-staged: ^10.0.7 lodash: ^4.17.21 @@ -68,6 +69,7 @@ dependencies: express-jwt: 6.0.0 express-urlrewrite: 1.4.0 got: 11.8.2 + iconv-lite: 0.6.3 jsonwebtoken: 8.5.1 lodash: 4.17.21 nedb: 1.8.0 @@ -4282,7 +4284,6 @@ packages: engines: {node: '>=0.10.0'} dependencies: safer-buffer: 2.1.2 - dev: true /identity-obj-proxy/3.0.0: resolution: {integrity: sha1-lNK9qWCERT7zb7xarsN+D3nx/BQ=}