generated from template/slidev-template
update
This commit is contained in:
41
agent/ip.ts
Normal file
41
agent/ip.ts
Normal file
@@ -0,0 +1,41 @@
|
||||
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;
|
||||
}
|
||||
Reference in New Issue
Block a user