Link Admin Panel

const GITHUB_TOKEN = "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"; const OWNER = "rajarong"; const REPO = "iptv-database"; const FILE_PATH = "links.json"; const ADMIN_PASSWORD = "admin123"; // তোমার প্যানেলের পাসওয়ার্ড export default { async fetch(request, env) { const url = new URL(request.url); const path = url.pathname; // --- API: ADD NEW LINK --- if (path === "/api/add" && request.method === "POST") { try { const { key, url: longUrl, password } = await request.json(); if (password !== ADMIN_PASSWORD) { return new Response(JSON.stringify({ error: "Wrong Password" }), { status: 403 }); } const finalKey = key || Math.random().toString(36).substring(2, 7); const entryName = finalKey.endsWith(".m3u") ? finalKey : `${finalKey}.m3u`; const getUrl = `https://api.github.com/repos/${OWNER}/${REPO}/contents/${FILE_PATH}`; // 1. Fetch current file const res = await fetch(getUrl, { headers: { "Authorization": `token ${GITHUB_TOKEN}`, "User-Agent": "Cloudflare-Worker", "Accept": "application/vnd.github.v3+json" } }); let currentContent = {}; let sha = ""; if (res.ok) { const fileData = await res.json(); sha = fileData.sha; // Decode Base64 safely const decoded = Uint8Array.from(atob(fileData.content), c => c.charCodeAt(0)); currentContent = JSON.parse(new TextDecoder().decode(decoded)); } else if (res.status !== 404) { return new Response(JSON.stringify({ error: "GitHub Connection Error" }), { status: 500 }); } // 2. Add new data currentContent[entryName] = { u: longUrl, e: Date.now() + (30 * 24 * 60 * 60 * 1000) }; // 3. Update GitHub const updateRes = await fetch(getUrl, { method: "PUT", headers: { "Authorization": `token ${GITHUB_TOKEN}`, "Content-Type": "application/json", "User-Agent": "Cloudflare-Worker" }, body: JSON.stringify({ message: `Update: ${entryName}`, content: btoa(unescape(encodeURIComponent(JSON.stringify(currentContent, null, 2)))), sha: sha || undefined }) }); if (updateRes.ok) { return new Response(JSON.stringify({ success: "Saved to GitHub", key: entryName }), { status: 200, headers: { "Content-Type": "application/json" } }); } return new Response(JSON.stringify({ error: "Failed to push to GitHub" }), { status: 500 }); } catch (e) { return new Response(JSON.stringify({ error: "JSON Error: " + e.message }), { status: 500 }); } } // --- REDIRECT LOGIC --- const key = path.split("/")[1]; if (key && key !== "") { try { const getUrl = `https://api.github.com/repos/${OWNER}/${REPO}/contents/${FILE_PATH}`; const res = await fetch(getUrl, { headers: { "Authorization": `token ${GITHUB_TOKEN}`, "Accept": "application/vnd.github.v3.raw", "User-Agent": "Cloudflare-Worker" } }); if (res.ok) { const links = await res.json(); const data = links[key]; if (data) { const target = typeof data === 'object' ? data.u : data; return Response.redirect(target, 302); } } } catch (e) { // Silent fail for redirect } } return new Response("Admin Panel System Online", { status: 200 }); } };