diff options
Diffstat (limited to 'index.ts')
| -rw-r--r-- | index.ts | 28 |
1 files changed, 18 insertions, 10 deletions
@@ -23,6 +23,20 @@ import rates from "$routes/rates"; import square from "$routes/square"; import users from "$routes/users"; +// Per-account limit. Runs at preHandler, after auth (preValidation) has set +// req.user, so the bucket is the real user id. Keying on a slice of the +// Authorization header doesn't work: every token starts with the same JWT +// header + `{"id":"` prefix, so all users would share one bucket. +const perUser = (name: string, max: number, timeWindow: number) => ({ + rateLimit: { + max, + timeWindow, + hook: "preHandler", + keyGenerator: (req) => + `${name}:${req.user?.id || (req.headers["cf-connecting-ip"] as string) || req.ip}`, + }, +}); + try { getLocations(); getFx(); @@ -46,7 +60,7 @@ app.get("/rates", rates.index); app.get("/locations", locations.list); -app.get("/invoice/:id", { config: { rateLimit: { max: 30, timeWindow: 10000, keyGenerator: (req) => { const ip = (req.headers["cf-connecting-ip"] as string) || req.ip; const token = (req.headers.authorization || "").slice(0, 50); return token ? `invoice:${token}` : `invoice:${ip}`; } } } }, invoices.get); +app.get("/invoice/:id", { config: { rateLimit: { max: 30, timeWindow: 10000, keyGenerator: (req) => `invoice:${(req.headers["cf-connecting-ip"] as string) || req.ip}` } } }, invoices.get); app.get("/invoices", auth, invoices.list); app.post("/invoice", { ...optional, config: { rateLimit: { max: 30, timeWindow: 10000, keyGenerator: (req) => `invoicecreate:${(req.headers["cf-connecting-ip"] as string) || req.ip}` } } }, invoices.create); app.post("/invoice/:id", { ...optional, config: { rateLimit: { max: 30, timeWindow: 10000, keyGenerator: (req) => `invoiceupdate:${(req.headers["cf-connecting-ip"] as string) || req.ip}` } } }, invoices.update); @@ -94,13 +108,7 @@ app.post( "/lightning/quote", { ...auth, - config: { - rateLimit: { - max: 20, - timeWindow: 10000, - keyGenerator: (req) => `lnquote:${(req.headers.authorization || "").slice(0, 50) || (req.headers["cf-connecting-ip"] as string) || req.ip}`, - }, - }, + config: perUser("lnquote", 20, 10000), }, payments.quote, ); @@ -124,8 +132,8 @@ app.get("/pay/:username/:amount", lnurl.pay); app.post("/freeze", payments.freeze); app.post("/confirm", payments.confirm); -app.post("/bitcoin/fee", auth, payments.fee); -app.post("/bitcoin/send", auth, payments.send); +app.post("/bitcoin/fee", { ...auth, config: perUser("btcfee", 10, 60000) }, payments.fee); +app.post("/bitcoin/send", { ...auth, config: perUser("btcsend", 10, 60000) }, payments.send); app.get("/account/:id", auth, users.account); app.post("/account/:id", auth, users.updateAccount); |
