generated from template/slidev-template
41 lines
883 B
TypeScript
41 lines
883 B
TypeScript
const baseURLv4 = 'https://4.ipw.cn/';
|
|
const baseURLv6 = 'https://6.ipw.cn/';
|
|
|
|
|
|
import { app } from './app.ts';
|
|
|
|
|
|
app.route({
|
|
path: 'ip',
|
|
key: 'v4',
|
|
description: '获取当前公网IPv4地址',
|
|
}).define(async (ctx) => {
|
|
const response = await fetch(baseURLv4);
|
|
const ip = (await response.text()).trim();
|
|
if(!isIpv4(ip)) {
|
|
ctx.throw?.('获取地址失败');
|
|
}
|
|
ctx.body = { ip };
|
|
}).addTo(app);
|
|
|
|
app.route({
|
|
path: 'ip',
|
|
key: 'v6',
|
|
description: '获取当前公网IPv6地址',
|
|
}).define(async (ctx) => {
|
|
const response = await fetch(baseURLv6);
|
|
const ip = (await response.text()).trim();
|
|
if(!isIpv6(ip)) {
|
|
ctx.throw?.('获取地址失败');
|
|
}
|
|
ctx.body = { ip };
|
|
}).addTo(app);
|
|
|
|
|
|
export const isIpv6 = (ip: string): boolean => {
|
|
return ip.includes(':');
|
|
}
|
|
|
|
export const isIpv4 = (ip: string): boolean => {
|
|
return ip.split('.').length === 4;
|
|
} |