From 8241340f0e9c77a2a3267aae56ffb87313abc9cb Mon Sep 17 00:00:00 2001 From: Mia <49593536+mia-pi-git@users.noreply.github.com> Date: Thu, 24 Oct 2024 08:10:46 -0500 Subject: [PATCH] Send suspect data to smogon on test creation --- src/actions.ts | 33 +++++++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/src/actions.ts b/src/actions.ts index edf816c..69f79aa 100644 --- a/src/actions.ts +++ b/src/actions.ts @@ -77,6 +77,15 @@ const redundantFetch = async (targetUrl: string, data: RequestInit, attempts = 0 } }; +const smogonFetch = async (targetUrl: string, method: string, data: {[k: string]: any}) => { + const bodyText = JSON.stringify(data); + const hash = await signAsync("RSA-SHA1", bodyText, Config.privatekey); + return redundantFetch(`https://www.smogon.com/${targetUrl}`, { + method, + body: new URLSearchParams({data: bodyText, hash}), + }); +}; + export const actions: {[k: string]: QueryHandler} = { async register(params) { this.verifyCrossDomainRequest(); @@ -398,19 +407,12 @@ export const actions: {[k: string]: QueryHandler} = { // did not play games before the test began (ratingData?.first_played && ratingData.first_played > suspects[formatid].startDate) ) { - const data = JSON.stringify({ + void smogonFetch("tools/api/suspect-verify", "POST", { userid: rating.userid, format: formatid, reqs: {required: reqs, actual: userData}, suspectStartDate: suspects[formatid].startDate, }); - void redundantFetch("https://www.smogon.com/tools/api/suspect-verify", { - method: 'POST', - body: new URLSearchParams({ - data, - hash: await signAsync("RSA-SHA1", data, Config.privatekey), - }), - }); } } } @@ -1044,6 +1046,21 @@ export const actions: {[k: string]: QueryHandler} = { if (!(reqs.gxe || reqs.elo || reqs.coil) || Object.values(reqs).some(x => typeof x !== 'number')) { throw new ActionError("Invalid reqs sent."); } + // sim server should validate this already + if (!reqs.url?.trim().length) { + throw new ActionError("Invalid suspect URL provided."); + } + const start = time(); + try { + await smogonFetch("tools/api/suspect-create", "POST", { + url: reqs.url, + date: start + "", + reqs: JSON.stringify(reqs), + format: id, + }); + } catch (e: any) { + throw new ActionError("Failed to update Smogon suspect test record: " + e.message); + } suspects[id] = { startDate: time(), reqs,