1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
| const HTML = <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>🔗 go.yourdomain.com</title> <link rel="icon" type="image/png" href="https://yourdomain.com/favicon.png" /> <meta name="viewport" content="width=device-width, initial-scale=1"> <style> body { font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'PingFang SC', 'Microsoft YaHei', sans-serif; max-width: 600px; margin: 50px auto; padding: 20px; line-height: 1.6; } h1 { text-align: center; margin-bottom: 30px; } input, button { padding: 12px; width: 100%; margin: 10px 0; box-sizing: border-box; border: 1px solid #ccc; border-radius: 4px; } button { background: #007bff; color: white; cursor: pointer; font-size: 16px; } button:hover { background: #0069d9; } #result { margin-top: 20px; padding: 12px; background: #f8f9fa; border: 1px solid #e9ecef; border-radius: 4px; word-break: break-all; } a { color: #007bff; text-decoration: none; } a:hover { text-decoration: underline; } </style> </head> <body> <h1>短链接生成</h1> <input id="longUrl" placeholder="请输入长链接(例如:https://...)" /> <button onclick="createShortLink()">生成短链接</button> <div id="result"></div>
<script> async function createShortLink() { const longUrl = document.getElementById('longUrl').value.trim(); if (!longUrl) { alert("请输入一个有效的网址"); return; }
const shortCode = Math.random().toString(36).substring(2, 8);
try { const res = await fetch('/api/create-public', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ longUrl, shortCode }) });
const data = await res.json(); const resultDiv = document.getElementById('result'); if (data.ok) { resultDiv.innerHTML = '<strong>您的短链接:</strong><br>' + '<a href="' + data.shortUrl + '" target="_blank">' + data.shortUrl + '</a>'; } else { resultDiv.innerText = "错误:" + (data.error "未知错误"); } } catch (err) { resultDiv.innerText = "网络错误:" + err.message; } } </script> </body> </html>;
export default { async fetch(request, env) { const url = new URL(request.url); const { pathname } = url;
// 首页 if (pathname === "/") { return new Response(HTML, { headers: { "Content-Type": "text/html; charset=utf-8" } }); }
// CORS 预检 if (request.method === "OPTIONS") { return new Response(null, { headers: { "Access-Control-Allow-Origin": "", "Access-Control-Allow-Methods": "POST, OPTIONS", "Access-Control-Allow-Headers": "Content-Type, Authorization" } }); }
// ─────────────────────────────────────── // 公开创建接口:/api/create-public(无需 Token) // ─────────────────────────────────────── if (pathname === "/api/create-public" && request.method === "POST") { try { const { longUrl, shortCode } = await request.json(); if (!longUrl !shortCode) { return new Response(JSON.stringify({ error: "缺少 longUrl 或 shortCode" }), { status: 400, headers: { "Content-Type": "application/json", "Access-Control-Allow-Origin": "" } }); } await env.URLS.put(shortCode, longUrl); return new Response(JSON.stringify({ ok: true, shortUrl: "https://go.yourdomain.com/" + shortCode }), { headers: { "Content-Type": "application/json", "Access-Control-Allow-Origin": "" } }); } catch (e) { return new Response(JSON.stringify({ error: "服务器内部错误" }), { status: 500, headers: { "Content-Type": "application/json", "Access-Control-Allow-Origin": "" } }); } }
// ─────────────────────────────────────── // 受控创建接口:/api/create(需要 API_TOKEN) // ─────────────────────────────────────── if (pathname === "/api/create" && request.method === "POST") { const expectedToken = env.API_TOKEN; if (!expectedToken) { return new Response(JSON.stringify({ error: "服务器未配置 API_TOKEN Secret" }), { status: 500, headers: { "Content-Type": "application/json", "Access-Control-Allow-Origin": "" } }); }
const authHeader = request.headers.get("Authorization"); if (!authHeader !authHeader.startsWith("Bearer ")) { return new Response(JSON.stringify({ error: "缺少或无效的 Authorization 头。格式应为:Bearer <API_TOKEN>" }), { status: 401, headers: { "Content-Type": "application/json", "Access-Control-Allow-Origin": "" } }); }
const token = authHeader.substring(7); if (token !== expectedToken) { return new Response(JSON.stringify({ error: "API Token 无效" }), { status: 403, headers: { "Content-Type": "application/json", "Access-Control-Allow-Origin": "" } }); }
try { const { longUrl, shortCode } = await request.json(); if (!longUrl !shortCode) { return new Response(JSON.stringify({ error: "缺少 longUrl 或 shortCode" }), { status: 400, headers: { "Content-Type": "application/json", "Access-Control-Allow-Origin": "" } }); } await env.URLS.put(shortCode, longUrl); return new Response(JSON.stringify({ ok: true, shortUrl: "https://go.yourdomain.com/" + shortCode }), { headers: { "Content-Type": "application/json", "Access-Control-Allow-Origin": "" } }); } catch (e) { return new Response(JSON.stringify({ error: "服务器内部错误" }), { status: 500, headers: { "Content-Type": "application/json", "Access-Control-Allow-Origin": "" } }); } }
// 短链接跳转 const code = pathname.slice(1); if (code) { const target = await env.URLS.get(code); if (target) { return Response.redirect(target, 302); } }
// 未找到 return new Response("短链接不存在", { status: 404 }); } };
|