aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--index.ts28
-rw-r--r--routes/payments.ts19
2 files changed, 30 insertions, 17 deletions
diff --git a/index.ts b/index.ts
index 81cb2743..1658dee4 100644
--- a/index.ts
+++ b/index.ts
@@ -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);
diff --git a/routes/payments.ts b/routes/payments.ts
index 80ef56bb..011aabb4 100644
--- a/routes/payments.ts
+++ b/routes/payments.ts
@@ -747,13 +747,18 @@ export default {
try {
res.send(await build({ ...body, user }));
} catch (e) {
- warn(
- "problem estimating fee",
- e.message,
- user.username,
- body.amount,
- body.address,
- );
+ // Bots retry the same doomed withdrawal every few seconds; log once
+ // per user per 10 minutes.
+ const n = quiet(`feefail:${user.id}`);
+ if (n !== null)
+ warn(
+ "problem estimating fee",
+ e.message,
+ user.username,
+ body.amount,
+ body.address,
+ suppressed(n),
+ );
let msg = e.message;
if (msg.includes("500")) msg = "";
bail(res, `Failed to prepare transaction ${msg}`);