| 1 | const PROXYCHECK_API_KEY = process.env.PROXYCHECK_API_KEY; |
| 2 | const console = log.scoped("q+a"); |
| 3 | |
| 4 | export const app = new Hono(); |
| 5 | |
| 6 | // Main page |
| 7 | app.get("/q+a", async (c) => { |
| 8 | if (hasAdminToken(c)) return serveAsset(c, "/admin/q+a", 200); |
| 9 | return serveAsset(c, "/q+a", 200); |
| 10 | }); |
| 11 | |
| 12 | // Submit form |
| 13 | app.post("/q+a", async (c) => { |
| 14 | // Every submission is logged once, whatever happened to it, along with what |
| 15 | // is known about the request. The ip address is only ever written here, not |
| 16 | // stored with the question. `qmid` and `id` find the question it became. |
| 17 | const trace: Record<string, unknown> = { |
| 18 | ip: c.req.header("x-forwarded-for"), |
| 19 | userAgent: c.req.header("user-agent"), |
| 20 | language: c.req.header("accept-language"), |
| 21 | referer: c.req.header("referer"), |
| 22 | origin: c.req.header("origin"), |
| 23 | accept: c.req.header("accept"), |
| 24 | contentType: c.req.header("content-type"), |
| 25 | }; |
| 26 | const respond = <T>(outcome: string, response: T) => { |
| 27 | console.info(`submit ${outcome} ${JSON.stringify(trace)}`); |
| 28 | return response; |
| 29 | }; |
| 30 | |
| 31 | let form; |
| 32 | try { |
| 33 | form = await c.req.formData(); |
| 34 | } catch { |
| 35 | return respond("bad-form", await questionFailure(c, 400, "Bad Request")); |
| 36 | } |
| 37 | trace.fields = [...new Set(form.keys())]; |
| 38 | let text = form.get("text"); |
| 39 | if (typeof text !== "string") { |
| 40 | return respond("bad-text", await questionFailure(c, 400, "Bad Request")); |
| 41 | } |
| 42 | text = text.trim(); |
| 43 | trace.textLength = text.length; |
| 44 | // The values of the honeypot fields are kept to see what bots send. `null` |
| 45 | // is a field that was not sent at all. They are cut short, since nothing |
| 46 | // limits what a bot posts here. |
| 47 | const honeypot = (name: string) => { |
| 48 | const value = form.get(name); |
| 49 | return typeof value === "string" ? value.slice(0, 200) : null; |
| 50 | }; |
| 51 | const input = { |
| 52 | date: new Date(), |
| 53 | prompt: text, |
| 54 | sourceName: "unknown", |
| 55 | sourceLocation: "unknown", |
| 56 | sourceVPN: null as string | null, |
| 57 | sourceRisk: null as number | null, |
| 58 | robot: honeypot("robot"), |
| 59 | email: honeypot("email"), |
| 60 | }; |
| 61 | trace.robot = input.robot; |
| 62 | trace.email = input.email; |
| 63 | |
| 64 | input.date.setMilliseconds(0); |
| 65 | |
| 66 | if (text.length <= 0) { |
| 67 | return respond("too-short", await questionFailure(c, 400, "Content is too short", text)); |
| 68 | } |
| 69 | |
| 70 | if (text.length > 16000) { |
| 71 | return respond("too-long", await questionFailure(c, 400, "Content is too long", text)); |
| 72 | } |
| 73 | |
| 74 | // Ban patterns |
| 75 | if ( |
| 76 | text.includes("hams" + "terko" + "mbat" + ".expert") // 2025-02-18. 3 occurrences. All VPN |
| 77 | ) { |
| 78 | // To prevent known automatic spam-bots from noticing something automatic is |
| 79 | // happening, pretend that the question was successfully submitted. |
| 80 | return respond("banned", await sendSuccess(c, new Date())); |
| 81 | } |
| 82 | |
| 83 | const ipAddr = c.req.header("x-forwarded-for"); |
| 84 | if (ipAddr) { |
| 85 | input.sourceName = uniqueNamesGenerator({ |
| 86 | dictionaries: [adjectives, colors, animals], |
| 87 | separator: "-", |
| 88 | // names were once seeded with `ipAddr + PROXYCHECK_API_KEY`, which was |
| 89 | // never set in production. the suffix keeps those names unchanged. |
| 90 | seed: ipAddr + "undefined", |
| 91 | }); |
| 92 | } |
| 93 | trace.sourceName = input.sourceName; |
| 94 | |
| 95 | // Honeypots. The form shows an "i am a robot" checkbox which a person leaves |
| 96 | // alone, and an "email" input hidden by css which a person never sees. A bot |
| 97 | // that fills in every field trips them. Like the ban patterns, pretend it was |
| 98 | // submitted. The text is kept as a rejected question in case of a false hit. |
| 99 | if (form.has("robot") || form.get("email")) { |
| 100 | const date = questions.create("reject", JSON.stringify(input), input.date); |
| 101 | trace.qmid = date.getTime() / 60_000; |
| 102 | trace.id = formatQuestionId(date); |
| 103 | return respond("honeypot", await sendSuccess(c, date)); |
| 104 | } |
| 105 | |
| 106 | if (ipAddr && PROXYCHECK_API_KEY) { |
| 107 | const proxyCheck = await fetch( |
| 108 | `https://proxycheck.io/v3/?key=${PROXYCHECK_API_KEY}`, |
| 109 | { |
| 110 | method: "POST", |
| 111 | body: "ips=" + ipAddr, |
| 112 | headers: { |
| 113 | "Content-Type": "application/x-www-form-urlencoded", |
| 114 | }, |
| 115 | }, |
| 116 | ).then((res) => res.json()); |
| 117 | |
| 118 | if (ipAddr && proxyCheck[ipAddr]) { |
| 119 | const { detections, network, operator } = proxyCheck[ipAddr]; |
| 120 | trace.detections = detections; |
| 121 | trace.risk = detections?.risk; |
| 122 | |
| 123 | const risk = Number(detections?.risk); |
| 124 | if (Number.isFinite(risk)) input.sourceRisk = risk; |
| 125 | // hosting is any datacenter address, which no person is browsing from at home |
| 126 | if (detections?.proxy || detections?.vpn || detections?.tor || detections?.hosting) { |
| 127 | input.sourceVPN = operator?.name ?? network?.organisation ?? network?.provider ?? "unknown"; |
| 128 | trace.sourceVPN = input.sourceVPN; |
| 129 | } |
| 130 | |
| 131 | if (detections?.hosting && c.req.header("x-discord-user-id")) { |
| 132 | trace.discordId = c.req.header("x-discord-user-id"); |
| 133 | input.sourceName = uniqueNamesGenerator({ |
| 134 | dictionaries: [adjectives, colors, animals], |
| 135 | separator: "-", |
| 136 | seed: String(trace.discordId), |
| 137 | }); |
| 138 | input.sourceVPN = "Discord" |
| 139 | trace.sourceVPN = input.sourceVPN; |
| 140 | } else if (risk > 78) { |
| 141 | return respond( |
| 142 | "high-risk", |
| 143 | await questionFailure( |
| 144 | c, |
| 145 | 403, |
| 146 | "This IP address has been flagged as a high risk IP address. If " |
| 147 | + "you are using a VPN/Proxy, please disable it and try again.", |
| 148 | text, |
| 149 | ), |
| 150 | ); |
| 151 | } |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | const date = questions.create( |
| 156 | "pending", |
| 157 | JSON.stringify(input), |
| 158 | input.date, |
| 159 | ); |
| 160 | trace.qmid = date.getTime() / 60_000; |
| 161 | trace.id = formatQuestionId(date); |
| 162 | return respond("accepted", await sendSuccess(c, date)); |
| 163 | }); |
| 164 | async function sendSuccess(c: Context, date: Date) { |
| 165 | if (c.req.header("Accept")?.includes("application/json")) { |
| 166 | return c.json({ |
| 167 | success: true, |
| 168 | message: "ok", |
| 169 | date: date.getTime(), |
| 170 | id: formatQuestionId(date), |
| 171 | }, { status: 200 }); |
| 172 | } |
| 173 | c.res = await view.serveUncached(c, "q+a/success", { |
| 174 | permalink: `https://paperclover.net/q+a/${formatQuestionId(date)}`, |
| 175 | }); |
| 176 | } |
| 177 | // Question Permalink |
| 178 | app.get("/q+a/:id", async (c, next) => { |
| 179 | // from deadname era, the seconds used to be in the url. |
| 180 | // this was removed so that the url can be crafted by hand. |
| 181 | let id = c.req.param("id"); |
| 182 | if (id.length === 12 && /^\d+$/.test(id)) { |
| 183 | return c.redirect(`/q+a/${id.slice(0, 10)}`); |
| 184 | } |
| 185 | let image = false; |
| 186 | if (id.endsWith(".png")) { |
| 187 | image = true; |
| 188 | id = id.slice(0, -4); |
| 189 | } |
| 190 | |
| 191 | const timestamp = questionIdToTimestamp(id); |
| 192 | if (!timestamp) return next(); |
| 193 | const question = questions.getByDate(timestamp); |
| 194 | if (!question) return next(); |
| 195 | |
| 196 | if (image) { |
| 197 | return serveQuestionImage(c, question); |
| 198 | } |
| 199 | const ua = c.req.header("User-Agent") ?? ""; |
| 200 | if (ua.includes("clover-qa-image")) { |
| 201 | return view.serveCached(c, "q+a/image-embed", { question }); |
| 202 | } |
| 203 | return view.serveCached(c, "q+a/permalink", { question, isDiscord: ua.toLowerCase().includes("discord") }); |
| 204 | }); |
| 205 | |
| 206 | // Admin |
| 207 | app.get("/admin/q+a", async (c) => { |
| 208 | return serveAsset(c, "/admin/q+a", 200); |
| 209 | }); |
| 210 | app.get("/admin/q+a/inbox", async (c) => { |
| 211 | return view.serveCached(c, "q+a/backend-inbox", {}); |
| 212 | }); |
| 213 | app.delete("/admin/q+a/:id", async (c, next) => { |
| 214 | const id = c.req.param("id"); |
| 215 | const timestamp = questionIdToTimestamp(id); |
| 216 | if (!timestamp) return next(); |
| 217 | const question = questions.getByDate(timestamp); |
| 218 | if (!question) return next(); |
| 219 | const deleteFull = c.req.header("X-Delete-Full") === "true"; |
| 220 | if (deleteFull) { |
| 221 | questions.deleteByQmid(question.qmid); |
| 222 | } else { |
| 223 | questions.rejectByQmid(question.qmid); |
| 224 | } |
| 225 | return c.json({ success: true, message: "ok" }); |
| 226 | }); |
| 227 | app.patch("/admin/q+a/:id", async (c, next) => { |
| 228 | const id = c.req.param("id"); |
| 229 | const timestamp = questionIdToTimestamp(id); |
| 230 | if (!timestamp) return next(); |
| 231 | const question = questions.getByDate(timestamp); |
| 232 | if (!question) return next(); |
| 233 | const form = await c.req.raw.json(); |
| 234 | if (typeof form.text !== "string" || !Object.hasOwn(questions.types, form.type)) { |
| 235 | return questionFailure(c, 400, "Bad Request"); |
| 236 | } |
| 237 | questions.updateByQmid(question.qmid, form.text, form.type); |
| 238 | return c.json({ success: true, message: "ok" }); |
| 239 | }); |
| 240 | app.post("/admin/q+a/spell", async (c) => { |
| 241 | const { text } = await c.req.raw.json(); |
| 242 | if (typeof text !== "string") return questionFailure(c, 400, "Bad Request"); |
| 243 | return c.json({ issues: await cspell.checkText(text) }); |
| 244 | }); |
| 245 | app.get("/admin/q+a/:id", async (c, next) => { |
| 246 | const id = c.req.param("id"); |
| 247 | const timestamp = questionIdToTimestamp(id); |
| 248 | if (!timestamp) return next(); |
| 249 | const question = questions.getByDate(timestamp); |
| 250 | if (!question) return next(); |
| 251 | |
| 252 | let pendingInfo: null | questions.PendingData = null; |
| 253 | if (question.type === "pending") { |
| 254 | pendingInfo = JSON.parse(question.text) as questions.PendingData; |
| 255 | question.text = pendingInfo.prompt |
| 256 | .trim() |
| 257 | .split("\n") |
| 258 | .map((line) => (line.trim().length === 0 ? "" : `q: ${line.trim()}`)) |
| 259 | .join("\n") + "\n\n"; |
| 260 | question.type = "normal"; |
| 261 | } |
| 262 | |
| 263 | return view.serveCached(c, "q+a/editor", { |
| 264 | pendingInfo, |
| 265 | question, |
| 266 | }); |
| 267 | }); |
| 268 | |
| 269 | app.get("/q+a/things/random", async (c) => { |
| 270 | c.res = await view.serveUncached(c, "q+a/things-random", {}); |
| 271 | }); |
| 272 | |
| 273 | app.route("", lolZip.app); |
| 274 | |
| 275 | async function questionFailure( |
| 276 | c: Context, |
| 277 | status: ContentfulStatusCode, |
| 278 | message: string, |
| 279 | content?: string, |
| 280 | ) { |
| 281 | if (c.req.header("Accept")?.includes("application/json")) { |
| 282 | return c.json({ success: false, message, id: null }, { status }); |
| 283 | } |
| 284 | return await view.serveUncached(c, "q+a/fail", { |
| 285 | error: message, |
| 286 | content, |
| 287 | }); |
| 288 | } |
| 289 | |
| 290 | import { serveAsset } from "#sitegen/assets"; |
| 291 | import * as view from "#sitegen/view"; |
| 292 | import * as log from "@clo/lib/log"; |
| 293 | import { type Context, Hono } from "hono"; |
| 294 | import type { ContentfulStatusCode } from "hono/utils/http-status"; |
| 295 | import { adjectives, animals, colors, uniqueNamesGenerator } from "unique-names-generator"; |
| 296 | import { hasAdminToken } from "../admin.ts"; |
| 297 | import * as cspell from "../cspell.ts"; |
| 298 | import { formatQuestionId, questionIdToTimestamp } from "./format.ts"; |
| 299 | import { serveQuestionImage } from "./image.ts"; |
| 300 | import * as lolZip from "./lol.zip/backend.ts"; |
| 301 | import * as questions from "./models/questions.ts"; |