| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| const ALLOWED_ORIGINS = [ |
| "https://osintframework.com", |
| "https://www.osintframework.com", |
| ]; |
|
|
| const REPORT_THRESHOLD = 3; |
| const REPORT_DEDUP_TTL = 7 * 24 * 3600; |
|
|
| function corsHeaders(origin) { |
| const allowed = |
| ALLOWED_ORIGINS.includes(origin) || origin.endsWith(".osintframework.com") |
| ? origin |
| : ALLOWED_ORIGINS[0]; |
| return { |
| "Access-Control-Allow-Origin": allowed, |
| "Access-Control-Allow-Methods": "GET, POST, OPTIONS", |
| "Access-Control-Allow-Headers": "Content-Type", |
| "Access-Control-Max-Age": "86400", |
| }; |
| } |
|
|
| function jsonResponse(data, status, origin) { |
| return new Response(JSON.stringify(data), { |
| status, |
| headers: { |
| "Content-Type": "application/json", |
| ...corsHeaders(origin), |
| }, |
| }); |
| } |
|
|
| |
| |
| |
| |
| function validateCommon(tool_id, session_hash) { |
| if ( |
| typeof tool_id !== "string" || |
| tool_id.length === 0 || |
| tool_id.length > 200 |
| ) { |
| return "invalid tool_id"; |
| } |
| if ( |
| typeof session_hash !== "string" || |
| session_hash.length === 0 || |
| session_hash.length > 64 |
| ) { |
| return "invalid session_hash"; |
| } |
| return null; |
| } |
|
|
| |
| |
| |
| |
| async function isRateLimited(env, session_hash) { |
| const minute = Math.floor(Date.now() / 60000); |
| const rlKey = `ratelimit:${session_hash}:${minute}`; |
| const rlRaw = await env.CLICK_DATA.get(rlKey); |
| const rlCount = rlRaw ? parseInt(rlRaw, 10) : 0; |
| if (rlCount >= 60) return true; |
| await env.CLICK_DATA.put(rlKey, String(rlCount + 1), { expirationTtl: 120 }); |
| return false; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| async function handleTrack(request, env) { |
| const origin = request.headers.get("Origin") || ""; |
|
|
| let body; |
| try { |
| body = await request.json(); |
| } catch { |
| return jsonResponse({ ok: false, error: "invalid json" }, 400, origin); |
| } |
|
|
| const { tool_id, session_hash } = body; |
| const validationError = validateCommon(tool_id, session_hash); |
| if (validationError) { |
| return jsonResponse({ ok: false, error: validationError }, 400, origin); |
| } |
|
|
| if (await isRateLimited(env, session_hash)) { |
| return jsonResponse({ ok: false, error: "rate limited" }, 429, origin); |
| } |
|
|
| |
| const dedupKey = `dedup:${session_hash}:${tool_id}`; |
| const alreadyCounted = await env.CLICK_DATA.get(dedupKey); |
| if (alreadyCounted) { |
| return jsonResponse({ ok: true, counted: false }, 200, origin); |
| } |
|
|
| |
| const clickKey = `clicks:${tool_id}`; |
| const currentRaw = await env.CLICK_DATA.get(clickKey); |
| const current = currentRaw ? parseInt(currentRaw, 10) : 0; |
|
|
| await Promise.all([ |
| env.CLICK_DATA.put(clickKey, String(current + 1)), |
| env.CLICK_DATA.put(dedupKey, "1", { expirationTtl: 3600 }), |
| ]); |
|
|
| return jsonResponse({ ok: true, counted: true }, 200, origin); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| async function handleVote(request, env) { |
| const origin = request.headers.get("Origin") || ""; |
|
|
| let body; |
| try { |
| body = await request.json(); |
| } catch { |
| return jsonResponse({ ok: false, error: "invalid json" }, 400, origin); |
| } |
|
|
| const { tool_id, rating, session_hash } = body; |
| const validationError = validateCommon(tool_id, session_hash); |
| if (validationError) { |
| return jsonResponse({ ok: false, error: validationError }, 400, origin); |
| } |
|
|
| if (rating !== null) { |
| var validRatings = [0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5]; |
| if (typeof rating !== "number" || validRatings.indexOf(rating) === -1) { |
| return jsonResponse( |
| { ok: false, error: "rating must be 0-5 in 0.5 increments, or null" }, |
| 400, |
| origin |
| ); |
| } |
| } |
|
|
| if (await isRateLimited(env, session_hash)) { |
| return jsonResponse({ ok: false, error: "rate limited" }, 429, origin); |
| } |
|
|
| const userRatingKey = `rating:${session_hash}:${tool_id}`; |
| const sumKey = `ratings:sum:${tool_id}`; |
| const countKey = `ratings:count:${tool_id}`; |
|
|
| |
| const [prevRatingRaw, sumRaw, countRaw] = await Promise.all([ |
| env.CLICK_DATA.get(userRatingKey), |
| env.CLICK_DATA.get(sumKey), |
| env.CLICK_DATA.get(countKey), |
| ]); |
|
|
| const prevRating = prevRatingRaw ? parseFloat(prevRatingRaw) : null; |
| let sum = sumRaw ? parseFloat(sumRaw) : 0; |
| let count = countRaw ? parseInt(countRaw, 10) : 0; |
|
|
| |
| let newRating = rating; |
| if (rating !== null && rating === prevRating) { |
| newRating = null; |
| } |
|
|
| |
| if (prevRating !== null) { |
| sum = Math.max(0, sum - prevRating); |
| count = Math.max(0, count - 1); |
| } |
|
|
| |
| if (newRating !== null) { |
| sum += newRating; |
| count++; |
| } |
|
|
| |
| const writes = [ |
| env.CLICK_DATA.put(sumKey, String(sum)), |
| env.CLICK_DATA.put(countKey, String(count)), |
| ]; |
| if (newRating === null) { |
| writes.push(env.CLICK_DATA.delete(userRatingKey)); |
| } else { |
| writes.push(env.CLICK_DATA.put(userRatingKey, String(newRating))); |
| } |
| await Promise.all(writes); |
|
|
| const average = count > 0 ? Math.round((sum / count) * 10) / 10 : 0; |
| return jsonResponse( |
| { ok: true, average, count, userRating: newRating }, |
| 200, |
| origin |
| ); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| async function handleReport(request, env) { |
| const origin = request.headers.get("Origin") || ""; |
|
|
| let body; |
| try { |
| body = await request.json(); |
| } catch { |
| return jsonResponse({ ok: false, error: "invalid json" }, 400, origin); |
| } |
|
|
| const { tool_id, report_type, session_hash } = body; |
| const validationError = validateCommon(tool_id, session_hash); |
| if (validationError) { |
| return jsonResponse({ ok: false, error: validationError }, 400, origin); |
| } |
|
|
| const validTypes = ["dead_link", "paywalled", "incorrect_info"]; |
| if (!validTypes.includes(report_type)) { |
| return jsonResponse( |
| { ok: false, error: "report_type must be dead_link, paywalled, or incorrect_info" }, |
| 400, |
| origin |
| ); |
| } |
|
|
| if (await isRateLimited(env, session_hash)) { |
| return jsonResponse({ ok: false, error: "rate limited" }, 429, origin); |
| } |
|
|
| |
| const dedupKey = `reported:${session_hash}:${tool_id}:${report_type}`; |
| const alreadyReported = await env.CLICK_DATA.get(dedupKey); |
| if (alreadyReported) { |
| return jsonResponse({ ok: true, counted: false }, 200, origin); |
| } |
|
|
| |
| const countKey = `reportcount:${tool_id}:${report_type}`; |
| const countRaw = await env.CLICK_DATA.get(countKey); |
| const prevCount = countRaw ? parseInt(countRaw, 10) : 0; |
| const newCount = prevCount + 1; |
|
|
| await Promise.all([ |
| env.CLICK_DATA.put(countKey, String(newCount)), |
| env.CLICK_DATA.put(dedupKey, "1", { expirationTtl: REPORT_DEDUP_TTL }), |
| ]); |
|
|
| |
| if (newCount >= REPORT_THRESHOLD) { |
| const notifiedKey = `github_issue_created:${tool_id}:${report_type}`; |
| const alreadyNotified = await env.CLICK_DATA.get(notifiedKey); |
| if (!alreadyNotified && env.GITHUB_TOKEN) { |
| const created = await createGitHubIssue(env, tool_id, report_type, newCount); |
| if (created) { |
| await env.CLICK_DATA.put(notifiedKey, "1"); |
| } |
| } |
| } |
|
|
| return jsonResponse({ ok: true, counted: true }, 200, origin); |
| } |
|
|
| |
| |
| |
| |
| async function createGitHubIssue(env, tool_id, report_type, count) { |
| const typeLabels = { |
| dead_link: "dead link", |
| paywalled: "paywalled", |
| incorrect_info: "incorrect info", |
| }; |
| const typeLabel = typeLabels[report_type] || report_type; |
| const title = `[Community Report] ${tool_id} flagged as ${typeLabel}`; |
| const body = [ |
| `**Tool:** ${tool_id}`, |
| `**Report type:** ${typeLabel}`, |
| `**Report count:** ${count} unique reports`, |
| ``, |
| `This issue was automatically created by the OSINT Framework community reporting system.`, |
| `Community members have flagged this tool ${count} time(s) as \`${report_type}\`.`, |
| ``, |
| `Please review and take appropriate action (update URL, change pricing badge, correct description, etc.).`, |
| ].join("\n"); |
|
|
| try { |
| const resp = await fetch( |
| "https://api.github.com/repos/lockfale/OSINT-Framework/issues", |
| { |
| method: "POST", |
| headers: { |
| Authorization: `Bearer ${env.GITHUB_TOKEN}`, |
| "Content-Type": "application/json", |
| "User-Agent": "OSINT-Framework-Worker/1.0", |
| Accept: "application/vnd.github+json", |
| "X-GitHub-Api-Version": "2022-11-28", |
| }, |
| body: JSON.stringify({ |
| title, |
| body, |
| labels: ["community-report", "needs-review"], |
| }), |
| } |
| ); |
| return resp.ok; |
| } catch { |
| return false; |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| async function handleStats(request, env) { |
| const origin = request.headers.get("Origin") || ""; |
| const url = new URL(request.url); |
| const tool_id = url.searchParams.get("tool_id"); |
|
|
| if (!tool_id || tool_id.length === 0 || tool_id.length > 200) { |
| return jsonResponse({ ok: false, error: "invalid tool_id" }, 400, origin); |
| } |
|
|
| const [clicksRaw, sumRaw, countRaw] = await Promise.all([ |
| env.CLICK_DATA.get(`clicks:${tool_id}`), |
| env.CLICK_DATA.get(`ratings:sum:${tool_id}`), |
| env.CLICK_DATA.get(`ratings:count:${tool_id}`), |
| ]); |
|
|
| const clicks = clicksRaw ? parseInt(clicksRaw, 10) : 0; |
| const sum = sumRaw ? parseFloat(sumRaw) : 0; |
| const count = countRaw ? parseInt(countRaw, 10) : 0; |
| const average = count > 0 ? Math.round((sum / count) * 10) / 10 : 0; |
|
|
| return jsonResponse( |
| { tool_id, clicks, ratings: { average, count } }, |
| 200, |
| origin |
| ); |
| } |
|
|
| export default { |
| async fetch(request, env) { |
| const url = new URL(request.url); |
| const origin = request.headers.get("Origin") || ""; |
|
|
| |
| if (request.method === "OPTIONS") { |
| return new Response(null, { status: 204, headers: corsHeaders(origin) }); |
| } |
|
|
| if (url.pathname === "/api/track" && request.method === "POST") { |
| return handleTrack(request, env); |
| } |
|
|
| if (url.pathname === "/api/tool-stats" && request.method === "GET") { |
| return handleStats(request, env); |
| } |
|
|
| if (url.pathname === "/api/vote" && request.method === "POST") { |
| return handleVote(request, env); |
| } |
|
|
| if (url.pathname === "/api/report" && request.method === "POST") { |
| return handleReport(request, env); |
| } |
|
|
| |
| return env.ASSETS.fetch(request); |
| }, |
| }; |
|
|