使用阿里云ESA边缘函数获取访问用户IP
async function handleRequest(request) {
const url = new URL(request.url);
const path = url.pathname;
const ip = request.headers.get('x-forwarded-for')?.split(',')[0]||request.headers.get('x-alicdn-security-xff')
if(path==='/'){
return new Response(ip, {
headers: {
"content-type": "text/html;charset=UTF-8",
},
})
} else if(path==='/json'){
return new Response(JSON.stringify({ ip }), {
headers: {
'content-type': 'application/json',
},
});
} else if(path==='/info'){
const info = {
ip,
geo: request.info
}
return new Response(JSON.stringify({ info }), {
headers: {
'content-type': 'application/json',
},
});
} else {
return new Response("404", {status: 404})
}
}
export default {
async fetch(request) {
return handleRequest(request);
}
};
阅读剩余
版权声明:
作者:lc_soul
链接:https://blog.lcsoul.cn/archives/568
文章版权归作者所有,未经允许请勿转载。
THE END