Skip to content

Commit fa641a5

Browse files
committed
fix(ip-query): update IP API endpoint and response handling
- Changed the IP API endpoint from 'http://ip-api.com/json/' to 'https://freeipapi.com/api/json/' for improved reliability. - Updated the response structure to match the new API, including changes to field names and added handling for ASN organization. - Removed unnecessary timeout parameter from the getIp function to streamline the code. These changes enhance the functionality and reliability of the IP query feature. Signed-off-by: Innei <tukon479@gmail.com>
1 parent 8253442 commit fa641a5

File tree

2 files changed

+2
-2
lines changed

2 files changed

+2
-2
lines changed

apps/core/src/modules/serverless/pack/built-in/ip-query.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { BuiltInFunctionObject } from '../../function.types'
22

33
const ipQueryFnCode =
4-
"import { isIPv4, isIPv6 } from 'net'\n\nconst TIMEOUT = 5000\n\nexport default async function handler(ctx: Context, timeout = TIMEOUT) {\n const { ip } = ctx.req.query\n\n if (!ip) { ctx.res.throws(422, 'ip is empty') }\n const cache = ctx.storage.cache\n const hasCatch = await cache.get(ip)\n if (hasCatch) return hasCatch\n\n const result = await getIp(ctx, ip);\n await cache.set(ip, result)\n return result\n}\n\nasync function getIp(ctx: Context, ip: string, timeout = TIMEOUT) {\n const isV4 = isIPv4(ip)\n const isV6 = isIPv6(ip)\n const { axios } = await (ctx.getService('http'))\n if (!isV4 && !isV6) {\n ctx.throws(422, 'Invalid IP')\n }\n try {\n const data = await axios.get('http://ip-api.com/json/' + ip + '?lang=zh-CN').then(data => data.data) as Ip\n const res: FinalIpRecord = {\n cityName: data.city,\n countryName: data.country,\n ip: data.query,\n ispDomain: data.isp,\n ownerDomain: data.org,\n regionName: data.regionName\n\n }\n\n return res\n } catch (e) {\n ctx.throws(500, `IP API 调用失败,${e.message}`)\n }\n};\n\n\ninterface FinalIpRecord {\n cityName: string\n countryName: string\n ip: string\n ispDomain: string\n ownerDomain: string\n regionName: string\n}\ninterface Ip {\n country: string;\n countryCode: string;\n region: string;\n regionName: string;\n city: string;\n zip: string;\n lat: number;\n lon: number;\n timezone: string;\n isp: string;\n org: string;\n as: string;\n query: string;\n}"
4+
"import { isIPv4, isIPv6 } from 'net'\n\nconst TIMEOUT = 5000\n\nexport default async function handler(ctx: Context, timeout = TIMEOUT) {\n const { ip } = ctx.req.query\n\n if (!ip) { ctx.res.throws(422, 'ip is empty') }\n const cache = ctx.storage.cache\n const hasCatch = await cache.get(ip)\n if (hasCatch) return hasCatch\n\n const result = await getIp(ctx, ip);\n await cache.set(ip, result)\n return result\n}\n\nasync function getIp(ctx: Context, ip: string, timeout = TIMEOUT) {\n const isV4 = isIPv4(ip)\n const isV6 = isIPv6(ip)\n const { axios } = await (ctx.getService('http'))\n if (!isV4 && !isV6) {\n ctx.throws(422, 'Invalid IP')\n }\n try {\n const data = await axios.get('https://freeipapi.com/api/json/' + ip).then(data => data.data) as FreeIpApiResponse\n const res: FinalIpRecord = {\n cityName: data.cityName,\n countryName: data.countryName,\n ip: data.ipAddress,\n ispDomain: data.asnOrganization || '',\n ownerDomain: data.asnOrganization || '',\n regionName: data.regionName\n }\n\n return res\n } catch (e) {\n ctx.throws(500, `IP API 调用失败,${e.message}`)\n }\n};\n\n\ninterface FinalIpRecord {\n cityName: string\n countryName: string\n ip: string\n ispDomain: string\n ownerDomain: string\n regionName: string\n}\ninterface FreeIpApiResponse {\n ipVersion: number;\n ipAddress: string;\n latitude: number;\n longitude: number;\n countryName: string;\n countryCode: string;\n cityName: string;\n regionName: string;\n regionCode: string;\n continent: string;\n continentCode: string;\n asn: string;\n asnOrganization: string;\n isProxy: boolean;\n}"
55

66
export default {
77
code: ipQueryFnCode,

apps/core/src/utils/sandbox/sandbox-worker-code.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ async function requestBridgeCall(method, args) {
5555
5656
const BANNED_MODULES = new Set([
5757
'child_process', 'cluster', 'dgram', 'dns', 'fs', 'fs/promises',
58-
'http', 'http2', 'https', 'inspector', 'net', 'os', 'process',
58+
'inspector', 'os', 'process',
5959
'repl', 'sys', 'tls', 'v8', 'vm', 'worker_threads',
6060
// 防止通过 module.createRequire 绕过沙箱限制
6161
'module',

0 commit comments

Comments
 (0)