Advertisement
President_2000

Example3

Dec 22nd, 2020
1,288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.     RoastedPresident2000
  3.     Example 3
  4.     This gamevalidation module is to verify requests made to the Script Showcase API
  5.     confirming they are within roblox servers
  6. */
  7.  
  8. import { getData, writeData, deleteData } from './faunadb';
  9.  
  10. const config = require('../workerconfig');
  11. const robloxSecret = config.robloxSecret;
  12. const placeId = config.placeId;
  13.  
  14. /*
  15.     this function validates an instance of a game and is valid within the roblox website
  16.     referenced from rocheck github
  17. */
  18. export async function validateRunningInstance(ip, jobId) {
  19.     try {
  20.         const req = await fetch(new Request(`https://assetgame.roblox.com/Game/PlaceLauncher.ashx?request=RequestGameJob&placeId=${placeId}&gameId=${jobId}`), {
  21.             method: "GET",
  22.             headers: {
  23.                 "cookie": `.ROBLOSECURITY=${robloxSecret}; path=/; domain=.roblox.com;`,
  24.                 "referer": `https://www.roblox.com/games/${placeId}/`,
  25.                 "origin": "https://www.roblox.com",
  26.                 "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:79.0) Gecko/20100101 Firefox/79.0",
  27.                 "connection": "keep-alive",
  28.             }
  29.         });
  30.  
  31.         // return to JSON
  32.         const reqJSON = await req.json();
  33.  
  34.         // Did it return the joinScriptUrl?
  35.         if (reqJSON && reqJSON.joinScriptUrl) {
  36.             // Grab URL
  37.             const joinScriptReq = await fetch(new Request(reqJSON.joinScriptUrl));
  38.             let joinScriptJSON = await joinScriptReq.text();
  39.             // Prevent json parse errors
  40.             joinScriptJSON = JSON.parse(joinScriptJSON.replace(/--.*\r\n/, ''));
  41.             // pretty much a sanity check on JobID to make sure it is valid
  42.             if (joinScriptJSON && joinScriptJSON.MachineAddress == ip && reqJSON.jobId == jobId) {
  43.                 return true;
  44.             }
  45.         }
  46.     } catch (ex) {
  47.         // return value for debugging
  48.         return ex;
  49.     }
  50.  
  51.     // return a false value if not validated
  52.     return false;
  53. }
  54.  
  55. /*
  56.     confirm and validate the passed instance from the game
  57.     this is to prevent spoofed requests made to the backend
  58. */
  59. export async function validateInstance(ip, jobId, GUID) {
  60.     // Checks if the instance exists in roblox
  61.     const validInstance = await validateRunningInstance(ip, jobId);
  62.     if (validInstance) {
  63.         // run a check on the given secret instance from game
  64.         const instanceExists = await getData("findInstance", [jobId, GUID, ip]);
  65.         // sanity check
  66.         if (instanceExists && instanceExists.GUID == GUID && instanceExists.jobId == jobId && instanceExists.ip == ip) {
  67.             return true;
  68.         }
  69.     }
  70.     // return a false value if not validated
  71.     return false;
  72. }
  73.  
  74. /*
  75.     Adds a instance after validation to the
  76.     database of known instances
  77. */
  78. export async function addInstance(ip, jobId, GUID) {
  79.     // Validate the instance first
  80.     const validInstance = await validateRunningInstance(ip, jobId);
  81.     if (validInstance) {
  82.         const exists = await getData("findInstanceIsRegistered", [ip, jobId]);
  83.         if (!exists) {
  84.             // Write to the database with the given GUID
  85.             const success = await writeData("servers", {
  86.                 "ip": ip,
  87.                 "jobId": jobId,
  88.                 "GUID": GUID
  89.             });
  90.  
  91.             // Return true on success, return false on failure
  92.             if (success) {
  93.                 return true;
  94.             }
  95.         }
  96.     }
  97.  
  98.     return false;
  99. }
  100.  
  101. /*
  102.     Removes instance from backend when game shuts down to
  103.     prevent failed requests
  104. */
  105. export async function removeInstance(ip, jobId, GUID) {
  106.     const deleted = await deleteData("findInstance", [jobId, GUID, ip]);
  107.     return deleted == true ? true : false;
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement