Skip to content

Commit

Permalink
Send suspect data to smogon on test creation
Browse files Browse the repository at this point in the history
  • Loading branch information
mia-pi-git committed Oct 24, 2024
1 parent f5ce9fb commit 8241340
Showing 1 changed file with 25 additions and 8 deletions.
33 changes: 25 additions & 8 deletions src/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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),
}),
});
}
}
}
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 8241340

Please sign in to comment.