Files
Cloudflare-worker/alook-redirect
2026-04-05 17:50:12 +08:00

39 lines
1.3 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
export default {
async fetch(request) {
const url = new URL(request.url);
const shortUrl = url.searchParams.get("url");
if (!shortUrl) {
return new Response("缺少 url 参数", { status: 400 });
}
try {
const resp = await fetch(shortUrl, { redirect: "manual" });
const location = resp.headers.get("Location") || "";
let realUrl = null;
if (location.includes("viglink.com")) {
// 直接从原始 location 字符串里截取 u= 的值,不用 URL 解析(避免自动 decode
const match = location.match(/[?&]u=([^&]+)/);
if (match) {
realUrl = match[1]; // 保持原始编码,不 decode
}
} else if (location.includes("account.microsoft.com")) {
realUrl = location;
}
if (!realUrl) {
return new Response("无法解析链接:" + location, { status: 500 });
}
const alookUrl = "alook://" + realUrl.replace("https://", "").replace("https%3A%2F%2F", "");
return new Response(
`<html><body><script>window.location.href=${JSON.stringify(alookUrl)};</script></body></html>`,
{ headers: { "Content-Type": "text/html;charset=utf-8" } }
);
} catch (e) {
return new Response("错误:" + e.message, { status: 500 });
}
}
};