举列子,获取访问者的ip地址,城市,区域。直接将此代码放到 cloudflare worker 中部署即可
JavaScript▼
export default {
async fetch(request, env, ctx) {
const ip = request.headers.get('CF-Connecting-IP') || request.headers.get('X-Forwarded-For') || '未知ip';
const city = request.cf?.city || '未知城市';
const region = request.cf?.region || '未知区域';
const html = `
<!DOCTYPE html>
<html lang="zh-cn">
<head>
<meta charset="UTF-8">
<title>IP 信息</title>
</head>
<body>
<p>IP 地址: ${ip}</p>
<p>城市: ${city}</p>
<p>区域: ${region}</p>
</body>
</html>
`;
return new Response(html, {
headers: { 'Content-Type': 'text/html;charset=UTF-8' }
});
},
};