From 825001493feaf151006546b64b39cb20514f61c0 Mon Sep 17 00:00:00 2001 From: spark Date: Fri, 20 Nov 2026 22:30:45 +0000 Subject: [PATCH] Speak TLS 1.2 to the Joga API so the worker can read the cup. Cloudflare blocks Node's TLS 1.3 handshake to app2030-prd.fpf.pt. --- apps/api/src/app.ts | 4 +- apps/api/src/sports/joga.test.ts | 4 ++ apps/api/src/sports/joga.ts | 72 +++++++++++++++++++++++++++++++- apps/api/src/worker.ts | 2 +- docs/ARCHITECTURE.md | 3 +- 5 files changed, 80 insertions(+), 5 deletions(-) diff --git a/apps/api/src/app.ts b/apps/api/src/app.ts index 1c858a6..89c68aa 100644 --- a/apps/api/src/app.ts +++ b/apps/api/src/app.ts @@ -63,7 +63,7 @@ import { FaceService } from "./faces/service.js"; import { moduleFromRecord, moduleFromUrl, requireAppModule } from "./modules/access.js"; import { registerOrgRoutes } from "./orgs/routes.js"; import { registerSportsRoutes } from "./sports/routes.js"; -import { jogaClientFromConfig } from "./sports/joga.js"; +import { jogaClientFromConfig, jogaFetch } from "./sports/joga.js"; import { SportsService } from "./sports/service.js"; import { attachRealtime, type Broadcaster, silentBroadcaster } from "./realtime.js"; import { LocalStorage, UrlSigner } from "./storage/storage.js"; @@ -121,7 +121,7 @@ export async function buildApp({ config, db, realtime, fetchImpl, reporter: give signer, log: app.log, fetch: fetchImpl ?? fetch, - joga: jogaClientFromConfig(config, fetchImpl ?? fetch), + joga: jogaClientFromConfig(config, fetchImpl ?? jogaFetch), }); // Archive clouds: the API connects them, matches galleries and streams originals; the worker walks and indexes. const archive = new ArchiveService({ db, secrets, log: app.log, syncSquads: (id) => sports.syncEventSquads(id), ligaSeason: (season) => sports.importLigaSeason(season), storageRoot: config.STORAGE_ROOT, minFreeBytes: config.ARCHIVE_MIN_FREE_GB * 1e9, storage, ...(fetchImpl && { fetchImpl }) }); diff --git a/apps/api/src/sports/joga.test.ts b/apps/api/src/sports/joga.test.ts index 758e94d..c9ba8e2 100644 --- a/apps/api/src/sports/joga.test.ts +++ b/apps/api/src/sports/joga.test.ts @@ -7,6 +7,7 @@ import { chooseClub, clubMatchScore, clubsMatch, + JOGA_TLS, jogaClientFromConfig, jogaPlayerName, jogaSquadPlan, @@ -143,6 +144,9 @@ describe("Joga client", () => { expect(jogaClientFromConfig({}, fetchImpl)).toBeNull(); expect(jogaClientFromConfig({ FPF_JOGA_API_KEY: "k", FPF_JOGA_ACCESS_TOKEN: " " }, fetchImpl)).toBeNull(); expect(jogaClientFromConfig({ FPF_JOGA_API_KEY: "k", FPF_JOGA_ACCESS_TOKEN: "token" }, fetchImpl)).toBeInstanceOf(Joga); + expect(jogaClientFromConfig({ FPF_JOGA_API_KEY: "k", FPF_JOGA_ACCESS_TOKEN: "token" })).toBeInstanceOf(Joga); + expect(JOGA_TLS.maxVersion).toBe("TLSv1.2"); + expect(JOGA_TLS.ALPNProtocols).toEqual(["http/1.1"]); }); it("pages a squad until a short page and refreshes the access token once", async () => { diff --git a/apps/api/src/sports/joga.ts b/apps/api/src/sports/joga.ts index c99ec52..a34f300 100644 --- a/apps/api/src/sports/joga.ts +++ b/apps/api/src/sports/joga.ts @@ -1,3 +1,4 @@ +import https from "node:https"; import { slugify } from "./codegen.js"; import { type Fetch, type FixtureData, type PlayerData, ProviderHttpError, lisbonTime, seasonOf } from "./providers.js"; @@ -453,9 +454,78 @@ export class JogaClient { } } +/** + * Cloudflare answers Node's usual TLS 1.3 handshake to app2030-prd.fpf.pt with a block page. + * TLS 1.2 over HTTP/1.1 is accepted. + */ +export const JOGA_TLS = { + ALPNProtocols: ["http/1.1"], + minVersion: "TLSv1.2", + maxVersion: "TLSv1.2", +} as const; + +export const jogaFetch: Fetch = (url, init) => + new Promise((resolve, reject) => { + let settled = false; + const fail = (err: Error) => { + if (settled) { + return; + } + settled = true; + reject(err); + }; + const options: https.RequestOptions & { ALPNProtocols: readonly string[] } = { + method: init?.method ?? "GET", + headers: init?.headers, + signal: init?.signal, + ALPNProtocols: JOGA_TLS.ALPNProtocols, + minVersion: JOGA_TLS.minVersion, + maxVersion: JOGA_TLS.maxVersion, + }; + const req = https.request( + url, + options, + (res) => { + const chunks: Buffer[] = []; + res.on("data", (chunk: Buffer) => { + chunks.push(chunk); + }); + res.on("error", fail); + res.on("end", () => { + if (settled) { + return; + } + settled = true; + const body = Buffer.concat(chunks); + const status = res.statusCode ?? 0; + let ok = false; + if (status >= 200 && status < 300) { + ok = true; + } + resolve({ + ok, + status, + json: async () => JSON.parse(body.toString("utf8")) as unknown, + text: async () => body.toString("utf8"), + arrayBuffer: async () => { + const copy = new ArrayBuffer(body.byteLength); + new Uint8Array(copy).set(body); + return copy; + }, + }); + }); + }, + ); + req.on("error", fail); + if (init?.body) { + req.write(init.body); + } + req.end(); + }); + export function jogaClientFromConfig( config: { FPF_JOGA_API_KEY?: string | undefined; FPF_JOGA_ACCESS_TOKEN?: string | undefined; FPF_JOGA_REFRESH_TOKEN?: string | undefined }, - fetcher: Fetch, + fetcher: Fetch = jogaFetch, ): JogaClient | null { const apiKey = config.FPF_JOGA_API_KEY?.trim() ?? ""; const accessToken = config.FPF_JOGA_ACCESS_TOKEN?.trim() ?? ""; diff --git a/apps/api/src/worker.ts b/apps/api/src/worker.ts index 3990f31..6435f69 100644 --- a/apps/api/src/worker.ts +++ b/apps/api/src/worker.ts @@ -104,7 +104,7 @@ if (!renderEngine) log.warn("RENDER_BIN not set: edited photos get no new thumbn const develop = new DevelopService({ db, storage, signer, broadcaster, log, engine: renderEngine }); const renders = renderEngine ? new RenderWorker(develop, { concurrency: capacity.render, intervalMs: 500, log }) : undefined; -const joga = jogaClientFromConfig(config, fetch); +const joga = jogaClientFromConfig(config); if (!joga) log.info("joga: no credentials, FPF squads stay on the HTML site"); const sportsService = new SportsService({ db, storage, signer, log, fetch, joga }); // Archive clouds: walked for new galleries, each image indexed as a preview whose original stays in the cloud. diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md index a6a41e0..2383e8e 100644 --- a/docs/ARCHITECTURE.md +++ b/docs/ARCHITECTURE.md @@ -351,7 +351,8 @@ every 6 hours (`SPORTS_SYNC=off` turns it off). It is shared by every organisati challenge blocks datacentre IPs. When `FPF_JOGA_API_KEY` and `FPF_JOGA_ACCESS_TOKEN` are set, the worker also reads the cup from the Joga+ app API (`app2030-prd.fpf.pt`): the calendar, and squads with federation headshots for FPF teams that do not have one yet. A Liga squad stays as Liga published it. Joga sends no shirt numbers, - so those names are not written onto a Liga team. The access token stays in the server `.env`, never in git. + so those names are not written onto a Liga team. The client speaks TLS 1.2 over HTTP/1.1, because Cloudflare + answers Node's usual TLS 1.3 handshake to this host with a block page. The access token stays in the server `.env`, never in git. - **EHF** (the JSON `ehfcl`, `ehfel` and `ehfec.eurohandball.com` read; not a documented API): men's and women's Champions League, European League and European Cup. Fixtures with venue, and squads from the match sheet once it is published: the nominated players (number, position, photo) and the bench staff (letters A, B, …; the sheet