Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const dgram = require('dgram');
- function execRCon(gameServerAddress, gameServerPort, rconPassword, command) {
- return new Promise((resolve, reject) => {
- const client = dgram.createSocket('udp4');
- const passwordLength = Buffer.from([rconPassword.length]);
- const commandLength = Buffer.alloc(2);
- commandLength.writeUInt16LE(command.length);
- const rconPacket = Buffer.concat([
- Buffer.from([0x01, 0x00, 0xF2]),
- passwordLength,
- Buffer.from(rconPassword),
- commandLength,
- Buffer.from(command)
- ]);
- client.send(rconPacket, gameServerPort, gameServerAddress, (err) => {
- if (err) {
- client.close();
- return reject(err);
- }
- client.once('message', (response) => {
- client.close();
- if (response.slice(0, 3).equals(Buffer.from([0x01, 0x00, 0xF0]))) {
- const commandResultLength = response.readUInt16LE(3);
- const commandResult = response.slice(5, 5 + commandResultLength).toString('utf8');
- resolve(commandResult);
- } else {
- reject(new Error('Failed to execute remote command'));
- }
- });
- // Set a timeout to handle cases where the game server doesn't respond
- setTimeout(() => {
- client.close();
- reject(new Error('Timeout waiting for RCON response'));
- }, 5000);
- });
- });
- }
- module.exports = { execRCon }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement