Advertisement
sqpp

Untitled

Aug 6th, 2023
816
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. const dgram = require('dgram');
  3.  
  4. function execRCon(gameServerAddress, gameServerPort, rconPassword, command) {
  5.   return new Promise((resolve, reject) => {
  6.     const client = dgram.createSocket('udp4');
  7.    
  8.     const passwordLength = Buffer.from([rconPassword.length]);
  9.     const commandLength = Buffer.alloc(2);
  10.     commandLength.writeUInt16LE(command.length);
  11.    
  12.     const rconPacket = Buffer.concat([
  13.       Buffer.from([0x01, 0x00, 0xF2]),
  14.       passwordLength,
  15.       Buffer.from(rconPassword),
  16.       commandLength,
  17.       Buffer.from(command)
  18.     ]);
  19.  
  20.     client.send(rconPacket, gameServerPort, gameServerAddress, (err) => {
  21.       if (err) {
  22.         client.close();
  23.         return reject(err);
  24.       }
  25.  
  26.       client.once('message', (response) => {
  27.         client.close();
  28.  
  29.         if (response.slice(0, 3).equals(Buffer.from([0x01, 0x00, 0xF0]))) {
  30.           const commandResultLength = response.readUInt16LE(3);
  31.           const commandResult = response.slice(5, 5 + commandResultLength).toString('utf8');
  32.           resolve(commandResult);
  33.         } else {
  34.           reject(new Error('Failed to execute remote command'));
  35.         }
  36.       });
  37.  
  38.       // Set a timeout to handle cases where the game server doesn't respond
  39.       setTimeout(() => {
  40.         client.close();
  41.         reject(new Error('Timeout waiting for RCON response'));
  42.       }, 5000);
  43.     });
  44.   });
  45. }
  46.  
  47. module.exports = { execRCon }
  48.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement