fix this proj too

This commit is contained in:
Soph :3 2024-08-27 22:28:29 +03:00
parent 0d952fed60
commit 4be1833d9f
Signed by: sophie
GPG key ID: EDA5D222A0C270F2
2 changed files with 61 additions and 59 deletions

View file

@ -27,13 +27,13 @@ client.on("auth", () => {
authenicated = true; authenicated = true;
}); });
client.on("end", () => { client.on("end", () => {
console.log("RCON ended. Reconnecting in 1000ms.") console.log("RCON ended. Reconnecting in 2000ms.")
if(rconReconnectTimeout) { if(rconReconnectTimeout) {
clearTimeout(rconReconnectTimeout); clearTimeout(rconReconnectTimeout);
} }
rconReconnectTimeout = setTimeout(() => { rconReconnectTimeout = setTimeout(() => {
client.connect() client.connect()
}, 1000) }, 2000)
}) })
client.on("response", (a: string) => { client.on("response", (a: string) => {
if (playerRegex.test(a)) { if (playerRegex.test(a)) {

112
rcon.ts
View file

@ -1,17 +1,17 @@
import EventEmitter from 'events'; import EventEmitter from "events";
import * as net from 'net'; import * as net from "net";
import * as dgram from 'dgram'; import * as dgram from "dgram";
import { Buffer } from 'buffer'; import { Buffer } from "buffer";
import type TypedEmitter from "typed-emitter" import type TypedEmitter from "typed-emitter";
type Events = { type Events = {
error: (error: Error) => void, error: (error: Error) => void;
auth: () => void, auth: () => void;
response: (response: string) => void, response: (response: string) => void;
connect: () => void, connect: () => void;
end: () => void, end: () => void;
done: () => void done: () => void;
} };
export const PacketType = { export const PacketType = {
COMMAND: 0x02, COMMAND: 0x02,
@ -49,7 +49,7 @@ export class Rcon extends (EventEmitter as new () => TypedEmitter<Events>) {
this.outstandingData = null; this.outstandingData = null;
this.tcp = options.tcp ? options.tcp : true; this.tcp = options.tcp ? options.tcp : true;
this.challenge = options.challenge ? options.challenge : true; this.challenge = options.challenge ? options.challenge : true;
this._challengeToken = ''; this._challengeToken = "";
} }
public send = (data: string, cmd?: number, id?: number): void => { public send = (data: string, cmd?: number, id?: number): void => {
@ -67,13 +67,13 @@ export class Rcon extends (EventEmitter as new () => TypedEmitter<Events>) {
sendBuf.writeInt16LE(0, length + 12); sendBuf.writeInt16LE(0, length + 12);
} else { } else {
if (this.challenge && !this._challengeToken) { if (this.challenge && !this._challengeToken) {
this.emit('error', new Error('Not authenticated')); this.emit("error", new Error("Not authenticated"));
return; return;
} }
let str = 'rcon '; let str = "rcon ";
if (this._challengeToken) str += this._challengeToken + ' '; if (this._challengeToken) str += this._challengeToken + " ";
if (this.password) str += this.password + ' '; if (this.password) str += this.password + " ";
str += data + '\n'; str += data + "\n";
sendBuf = Buffer.alloc(4 + Buffer.byteLength(str)); sendBuf = Buffer.alloc(4 + Buffer.byteLength(str));
sendBuf.writeInt32LE(-1, 0); sendBuf.writeInt32LE(-1, 0);
sendBuf.write(str, 4); sendBuf.write(str, 4);
@ -83,7 +83,7 @@ export class Rcon extends (EventEmitter as new () => TypedEmitter<Events>) {
private _sendSocket = (buf: Buffer) => { private _sendSocket = (buf: Buffer) => {
if (this._tcpSocket) { if (this._tcpSocket) {
this._tcpSocket.write(buf.toString('binary'), 'binary'); this._tcpSocket.write(buf.toString("binary"), "binary");
} else if (this._udpSocket) { } else if (this._udpSocket) {
this._udpSocket.send(buf, 0, buf.length, this.port, this.host); this._udpSocket.send(buf, 0, buf.length, this.port, this.host);
} }
@ -92,32 +92,32 @@ export class Rcon extends (EventEmitter as new () => TypedEmitter<Events>) {
public connect = (): void => { public connect = (): void => {
if (this.tcp) { if (this.tcp) {
this._tcpSocket = net.createConnection(this.port, this.host); this._tcpSocket = net.createConnection(this.port, this.host);
this._tcpSocket this._tcpSocket.on("data", (data) => {
.on('data', data => { this._tcpSocketOnData(data);
this._tcpSocketOnData(data); });
}) this._tcpSocket.on("connect", () => {
.on('connect', () => { this.socketOnConnect();
this.socketOnConnect(); });
}) this._tcpSocket.on("error", (err) => {
.on('error', err => { //this.emit("error", err);
this.emit('error', err); this.socketOnEnd()
}) });
.on('end', () => { this._tcpSocket.on("end", () => {
this.socketOnEnd(); this.socketOnEnd();
}); });
} else { } else {
this._udpSocket = dgram.createSocket('udp4'); this._udpSocket = dgram.createSocket("udp4");
this._udpSocket this._udpSocket
.on('message', data => { .on("message", (data) => {
this._udpSocketOnData(data); this._udpSocketOnData(data);
}) })
.on('listening', () => { .on("listening", () => {
this.socketOnConnect(); this.socketOnConnect();
}) })
.on('error', err => { .on("error", (err) => {
this.emit('error', err); this.emit("error", err);
}) })
.on('close', () => { .on("close", () => {
this.socketOnEnd(); this.socketOnEnd();
}); });
this._udpSocket.bind(0); this._udpSocket.bind(0);
@ -140,21 +140,23 @@ export class Rcon extends (EventEmitter as new () => TypedEmitter<Events>) {
private _udpSocketOnData = (data: Buffer) => { private _udpSocketOnData = (data: Buffer) => {
const a = data.readUInt32LE(0); const a = data.readUInt32LE(0);
if (a === 0xffffffff) { if (a === 0xffffffff) {
const str = data.toString('utf-8', 4); const str = data.toString("utf-8", 4);
const tokens = str.split(' '); const tokens = str.split(" ");
if ( if (
tokens.length === 3 && tokens.length === 3 &&
tokens[0] === 'challenge' && tokens[0] === "challenge" &&
tokens[1] === 'rcon' tokens[1] === "rcon"
) { ) {
this._challengeToken = tokens[2].substring(0, tokens[2].length - 1).trim(); this._challengeToken = tokens[2]
.substring(0, tokens[2].length - 1)
.trim();
this.hasAuthed = true; this.hasAuthed = true;
this.emit('auth'); this.emit("auth");
} else { } else {
this.emit('response', str.substring(1, str.length - 2)); this.emit("response", str.substring(1, str.length - 2));
} }
} else { } else {
this.emit('error', new Error('Received malformed packet')); this.emit("error", new Error("Received malformed packet"));
} }
}; };
@ -178,21 +180,21 @@ export class Rcon extends (EventEmitter as new () => TypedEmitter<Events>) {
if (id === this.rconId) { if (id === this.rconId) {
if (!this.hasAuthed && type === PacketType.RESPONSE_AUTH) { if (!this.hasAuthed && type === PacketType.RESPONSE_AUTH) {
this.hasAuthed = true; this.hasAuthed = true;
this.emit('auth'); this.emit("auth");
} else if (type === PacketType.RESPONSE_VALUE) { } else if (type === PacketType.RESPONSE_VALUE) {
// Read just the body of the packet (truncate the last null byte) // Read just the body of the packet (truncate the last null byte)
// See https://developer.valvesoftware.com/wiki/Source_RCON_Protocol for details // See https://developer.valvesoftware.com/wiki/Source_RCON_Protocol for details
let str = data.toString('utf8', 12, 12 + len - 10); let str = data.toString("utf8", 12, 12 + len - 10);
if (str.charAt(str.length - 1) === '\n') { if (str.charAt(str.length - 1) === "\n") {
// Emit the response without the newline. // Emit the response without the newline.
str = str.substring(0, str.length - 1); str = str.substring(0, str.length - 1);
} }
this.emit('response', str); this.emit("response", str);
} }
} else { } else {
this.emit('error', new Error('Authentication failed')); this.emit("error", new Error("Authentication failed"));
} }
data = data.slice(12 + len - 8); data = data.slice(12 + len - 8);
@ -205,12 +207,12 @@ export class Rcon extends (EventEmitter as new () => TypedEmitter<Events>) {
}; };
public socketOnConnect = (): void => { public socketOnConnect = (): void => {
this.emit('connect'); this.emit("connect");
if (this.tcp) { if (this.tcp) {
this.send(this.password, PacketType.AUTH); this.send(this.password, PacketType.AUTH);
} else if (this.challenge) { } else if (this.challenge) {
const str = 'challenge rcon\n'; const str = "challenge rcon\n";
const sendBuf = Buffer.alloc(str.length + 4); const sendBuf = Buffer.alloc(str.length + 4);
sendBuf.writeInt32LE(-1, 0); sendBuf.writeInt32LE(-1, 0);
sendBuf.write(str, 4); sendBuf.write(str, 4);
@ -222,12 +224,12 @@ export class Rcon extends (EventEmitter as new () => TypedEmitter<Events>) {
this._sendSocket(sendBuf); this._sendSocket(sendBuf);
this.hasAuthed = true; this.hasAuthed = true;
this.emit('auth'); this.emit("auth");
} }
}; };
public socketOnEnd = (): void => { public socketOnEnd = (): void => {
this.emit('end'); this.emit("end");
this.hasAuthed = false; this.hasAuthed = false;
}; };
} }