Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- RoastedPresident2000
- Example 3
- This gamevalidation module is to verify requests made to the Script Showcase API
- confirming they are within roblox servers
- */
- import { getData, writeData, deleteData } from './faunadb';
- const config = require('../workerconfig');
- const robloxSecret = config.robloxSecret;
- const placeId = config.placeId;
- /*
- this function validates an instance of a game and is valid within the roblox website
- referenced from rocheck github
- */
- export async function validateRunningInstance(ip, jobId) {
- try {
- const req = await fetch(new Request(`https://assetgame.roblox.com/Game/PlaceLauncher.ashx?request=RequestGameJob&placeId=${placeId}&gameId=${jobId}`), {
- method: "GET",
- headers: {
- "cookie": `.ROBLOSECURITY=${robloxSecret}; path=/; domain=.roblox.com;`,
- "referer": `https://www.roblox.com/games/${placeId}/`,
- "origin": "https://www.roblox.com",
- "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:79.0) Gecko/20100101 Firefox/79.0",
- "connection": "keep-alive",
- }
- });
- // return to JSON
- const reqJSON = await req.json();
- // Did it return the joinScriptUrl?
- if (reqJSON && reqJSON.joinScriptUrl) {
- // Grab URL
- const joinScriptReq = await fetch(new Request(reqJSON.joinScriptUrl));
- let joinScriptJSON = await joinScriptReq.text();
- // Prevent json parse errors
- joinScriptJSON = JSON.parse(joinScriptJSON.replace(/--.*\r\n/, ''));
- // pretty much a sanity check on JobID to make sure it is valid
- if (joinScriptJSON && joinScriptJSON.MachineAddress == ip && reqJSON.jobId == jobId) {
- return true;
- }
- }
- } catch (ex) {
- // return value for debugging
- return ex;
- }
- // return a false value if not validated
- return false;
- }
- /*
- confirm and validate the passed instance from the game
- this is to prevent spoofed requests made to the backend
- */
- export async function validateInstance(ip, jobId, GUID) {
- // Checks if the instance exists in roblox
- const validInstance = await validateRunningInstance(ip, jobId);
- if (validInstance) {
- // run a check on the given secret instance from game
- const instanceExists = await getData("findInstance", [jobId, GUID, ip]);
- // sanity check
- if (instanceExists && instanceExists.GUID == GUID && instanceExists.jobId == jobId && instanceExists.ip == ip) {
- return true;
- }
- }
- // return a false value if not validated
- return false;
- }
- /*
- Adds a instance after validation to the
- database of known instances
- */
- export async function addInstance(ip, jobId, GUID) {
- // Validate the instance first
- const validInstance = await validateRunningInstance(ip, jobId);
- if (validInstance) {
- const exists = await getData("findInstanceIsRegistered", [ip, jobId]);
- if (!exists) {
- // Write to the database with the given GUID
- const success = await writeData("servers", {
- "ip": ip,
- "jobId": jobId,
- "GUID": GUID
- });
- // Return true on success, return false on failure
- if (success) {
- return true;
- }
- }
- }
- return false;
- }
- /*
- Removes instance from backend when game shuts down to
- prevent failed requests
- */
- export async function removeInstance(ip, jobId, GUID) {
- const deleted = await deleteData("findInstance", [jobId, GUID, ip]);
- return deleted == true ? true : false;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement