Name logicsrc apps in the User-Agent of CoinPay calls

logicsrc.com (Next), app.logicsrc.com (pwa) and the rotation CLI now send a
descriptive User-Agent on every server-side CoinPay request, so CoinPay's
logs and throttle can tell them apart from the other apps on dev2:

- logicsrc.com: logicsrc.com/<version> (+https://logicsrc.com) on merchant
  eligibility, checkout sessions, OAuth token and userinfo; x402-gateway
  ^0.18.2 names the site on crawl-gateway calls.
- app.logicsrc.com: app.logicsrc.com/<version> on the CoinPay OAuth calls and,
  through coinpay-onboard 0.3.1's fetch hook, on the auto-rotation client.
- logicsrc CLI rotators: logicsrc-cli/<version> (+https://logicsrc.com).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
riotcoder 2026-11-22 11:20:34 +00:00
parent d8a08c006b
commit 78bc2eb858
10 changed files with 44 additions and 12 deletions

View file

@ -8,6 +8,7 @@
// rotation_auto_paid_until NULL with the switch on means it was flipped by hand
// (a comp): it never expires and a payment does not cap it.
import { CoinPay, readWebhook } from "@profullstack/coinpay-onboard";
import { COINPAY_UA } from "./coinpay-ua.mjs";
import { createAutoRotation, sqlStore, resendMailer, DAY_MS } from "@profullstack/key-rotation";
import { get, all, run } from "../db.mjs";
import { config } from "../config.mjs";
@ -33,11 +34,19 @@ export const store = sqlStore({
),
});
/**
* coinpay-onboard 0.3.x has no userAgent option, so name this app on every
* CoinPay call through its fetch hook (CoinPay's logs and throttle key on it).
*/
export function coinpayFetch(url, init = {}) {
return fetch(url, { ...init, headers: { ...(init.headers ?? {}), "user-agent": COINPAY_UA } });
}
/** A CoinPay client on app.logicsrc.com's business key. */
export function coinpayClient(env = process.env) {
const token = env.COINPAY_API_KEY;
if (!token) throw new Error("COINPAY_API_KEY is not configured");
return new CoinPay({ token, base: env.COINPAY_API_URL || undefined });
return new CoinPay({ token, base: env.COINPAY_API_URL || undefined, fetch: coinpayFetch });
}
/** The module bound to this app; `origin` only changes the links (tests). */

View file

@ -0,0 +1,10 @@
import { readFileSync } from "node:fs";
// Sent on every server-side call to CoinPay so its logs and throttle can tell
// app.logicsrc.com apart from the other apps on the same box.
let version = "";
try {
version = JSON.parse(readFileSync(new URL("../../package.json", import.meta.url), "utf8")).version || "";
} catch {}
export const COINPAY_UA = `app.logicsrc.com${version ? `/${version}` : ""} (+https://app.logicsrc.com)`;

View file

@ -2,6 +2,7 @@
import { Router } from "express";
import crypto from "node:crypto";
import { config } from "../config.mjs";
import { COINPAY_UA } from "../lib/coinpay-ua.mjs";
import { token } from "../lib/crypto.mjs";
import { createSession, setCeremony, getCeremony, clearCeremony, takeNext } from "../lib/session.mjs";
import { userByCoinpay, createUserForCoinpay } from "../lib/users.mjs";
@ -39,7 +40,7 @@ coinpayRouter.get("/auth/coinpay/callback", async (req, res) => {
try {
const tokenRes = await fetch(config.coinpay.oauth.tokenUrl, {
method: "POST",
headers: { "content-type": "application/x-www-form-urlencoded" },
headers: { "content-type": "application/x-www-form-urlencoded", "user-agent": COINPAY_UA },
body: new URLSearchParams({
grant_type: "authorization_code",
code: String(req.query.code),
@ -52,7 +53,7 @@ coinpayRouter.get("/auth/coinpay/callback", async (req, res) => {
const tok = await tokenRes.json();
const infoRes = await fetch(config.coinpay.oauth.userinfoUrl, {
headers: { authorization: `Bearer ${tok.access_token}` },
headers: { authorization: `Bearer ${tok.access_token}`, "user-agent": COINPAY_UA },
});
if (!infoRes.ok) throw new Error(`userinfo ${infoRes.status}`);
const info = await infoRes.json();