更换ip地址查询接口

This commit is contained in:
hanhh 2021-08-27 14:00:11 +08:00
parent 930541d5d6
commit 8cef0b6216
3 changed files with 22 additions and 13 deletions

View File

@ -1,6 +1,7 @@
import * as fs from 'fs'; import * as fs from 'fs';
import * as path from 'path'; import * as path from 'path';
import got from 'got'; import got from 'got';
import iconv from 'iconv-lite';
export function getFileContentByName(fileName: string) { export function getFileContentByName(fileName: string) {
if (fs.existsSync(fileName)) { if (fs.existsSync(fileName)) {
@ -156,18 +157,24 @@ export async function getNetIp(req: any) {
ip = ''; ip = '';
} }
try { try {
const baiduApi = got.get( const baiduApi = got
`https://sp0.baidu.com/8aQDcjqpAAV3otqbppnN2DJv/api.php?query=${ip}&co=&resource_id=6006&t=1555898284898&ie=utf8&oe=utf8&format=json&tn=baidu`, .get(`https://www.cip.cc/${ip}`, { timeout: 100000 })
); .text();
const ipApi = got.get( const ipApi = got
`https://whois.pconline.com.cn/ipJson.jsp?ip=${ip}&json=true`, .get(`https://whois.pconline.com.cn/ipJson.jsp?ip=${ip}&json=true`, {
); timeout: 100000,
const [{ data }, { addr, region, city }] = await ( })
await Promise.all<any>([baiduApi, ipApi]) .buffer();
).map((x) => JSON.parse(x.body)); const [data, ipApiBody] = await await Promise.all<any>([baiduApi, ipApi]);
if (data[0] && data[0].location) {
return { address: data[0].location, ip }; const ipRegx = /.*IP :(.*)\n/;
} else if (city && region) { 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 }; return { address: `${addr}`, ip };
} else { } else {
return { address: `获取失败`, ip }; return { address: `获取失败`, ip };

View File

@ -33,6 +33,7 @@
"express-jwt": "^6.0.0", "express-jwt": "^6.0.0",
"express-urlrewrite": "^1.4.0", "express-urlrewrite": "^1.4.0",
"got": "^11.8.2", "got": "^11.8.2",
"iconv-lite": "^0.6.3",
"jsonwebtoken": "^8.5.1", "jsonwebtoken": "^8.5.1",
"lodash": "^4.17.21", "lodash": "^4.17.21",
"nedb": "^1.8.0", "nedb": "^1.8.0",

View File

@ -29,6 +29,7 @@ specifiers:
express-jwt: ^6.0.0 express-jwt: ^6.0.0
express-urlrewrite: ^1.4.0 express-urlrewrite: ^1.4.0
got: ^11.8.2 got: ^11.8.2
iconv-lite: ^0.6.3
jsonwebtoken: ^8.5.1 jsonwebtoken: ^8.5.1
lint-staged: ^10.0.7 lint-staged: ^10.0.7
lodash: ^4.17.21 lodash: ^4.17.21
@ -68,6 +69,7 @@ dependencies:
express-jwt: 6.0.0 express-jwt: 6.0.0
express-urlrewrite: 1.4.0 express-urlrewrite: 1.4.0
got: 11.8.2 got: 11.8.2
iconv-lite: 0.6.3
jsonwebtoken: 8.5.1 jsonwebtoken: 8.5.1
lodash: 4.17.21 lodash: 4.17.21
nedb: 1.8.0 nedb: 1.8.0
@ -4282,7 +4284,6 @@ packages:
engines: {node: '>=0.10.0'} engines: {node: '>=0.10.0'}
dependencies: dependencies:
safer-buffer: 2.1.2 safer-buffer: 2.1.2
dev: true
/identity-obj-proxy/3.0.0: /identity-obj-proxy/3.0.0:
resolution: {integrity: sha1-lNK9qWCERT7zb7xarsN+D3nx/BQ=} resolution: {integrity: sha1-lNK9qWCERT7zb7xarsN+D3nx/BQ=}