fix this proj too
This commit is contained in:
parent
0d952fed60
commit
4be1833d9f
|
@ -27,13 +27,13 @@ client.on("auth", () => {
|
|||
authenicated = true;
|
||||
});
|
||||
client.on("end", () => {
|
||||
console.log("RCON ended. Reconnecting in 1000ms.")
|
||||
console.log("RCON ended. Reconnecting in 2000ms.")
|
||||
if(rconReconnectTimeout) {
|
||||
clearTimeout(rconReconnectTimeout);
|
||||
}
|
||||
rconReconnectTimeout = setTimeout(() => {
|
||||
client.connect()
|
||||
}, 1000)
|
||||
}, 2000)
|
||||
})
|
||||
client.on("response", (a: string) => {
|
||||
if (playerRegex.test(a)) {
|
||||
|
|
104
rcon.ts
104
rcon.ts
|
@ -1,17 +1,17 @@
|
|||
import EventEmitter from 'events';
|
||||
import * as net from 'net';
|
||||
import * as dgram from 'dgram';
|
||||
import { Buffer } from 'buffer';
|
||||
import type TypedEmitter from "typed-emitter"
|
||||
import EventEmitter from "events";
|
||||
import * as net from "net";
|
||||
import * as dgram from "dgram";
|
||||
import { Buffer } from "buffer";
|
||||
import type TypedEmitter from "typed-emitter";
|
||||
|
||||
type Events = {
|
||||
error: (error: Error) => void,
|
||||
auth: () => void,
|
||||
response: (response: string) => void,
|
||||
connect: () => void,
|
||||
end: () => void,
|
||||
done: () => void
|
||||
}
|
||||
error: (error: Error) => void;
|
||||
auth: () => void;
|
||||
response: (response: string) => void;
|
||||
connect: () => void;
|
||||
end: () => void;
|
||||
done: () => void;
|
||||
};
|
||||
|
||||
export const PacketType = {
|
||||
COMMAND: 0x02,
|
||||
|
@ -49,7 +49,7 @@ export class Rcon extends (EventEmitter as new () => TypedEmitter<Events>) {
|
|||
this.outstandingData = null;
|
||||
this.tcp = options.tcp ? options.tcp : true;
|
||||
this.challenge = options.challenge ? options.challenge : true;
|
||||
this._challengeToken = '';
|
||||
this._challengeToken = "";
|
||||
}
|
||||
|
||||
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);
|
||||
} else {
|
||||
if (this.challenge && !this._challengeToken) {
|
||||
this.emit('error', new Error('Not authenticated'));
|
||||
this.emit("error", new Error("Not authenticated"));
|
||||
return;
|
||||
}
|
||||
let str = 'rcon ';
|
||||
if (this._challengeToken) str += this._challengeToken + ' ';
|
||||
if (this.password) str += this.password + ' ';
|
||||
str += data + '\n';
|
||||
let str = "rcon ";
|
||||
if (this._challengeToken) str += this._challengeToken + " ";
|
||||
if (this.password) str += this.password + " ";
|
||||
str += data + "\n";
|
||||
sendBuf = Buffer.alloc(4 + Buffer.byteLength(str));
|
||||
sendBuf.writeInt32LE(-1, 0);
|
||||
sendBuf.write(str, 4);
|
||||
|
@ -83,7 +83,7 @@ export class Rcon extends (EventEmitter as new () => TypedEmitter<Events>) {
|
|||
|
||||
private _sendSocket = (buf: Buffer) => {
|
||||
if (this._tcpSocket) {
|
||||
this._tcpSocket.write(buf.toString('binary'), 'binary');
|
||||
this._tcpSocket.write(buf.toString("binary"), "binary");
|
||||
} else if (this._udpSocket) {
|
||||
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 => {
|
||||
if (this.tcp) {
|
||||
this._tcpSocket = net.createConnection(this.port, this.host);
|
||||
this._tcpSocket
|
||||
.on('data', data => {
|
||||
this._tcpSocket.on("data", (data) => {
|
||||
this._tcpSocketOnData(data);
|
||||
})
|
||||
.on('connect', () => {
|
||||
});
|
||||
this._tcpSocket.on("connect", () => {
|
||||
this.socketOnConnect();
|
||||
})
|
||||
.on('error', err => {
|
||||
this.emit('error', err);
|
||||
})
|
||||
.on('end', () => {
|
||||
});
|
||||
this._tcpSocket.on("error", (err) => {
|
||||
//this.emit("error", err);
|
||||
this.socketOnEnd()
|
||||
});
|
||||
this._tcpSocket.on("end", () => {
|
||||
this.socketOnEnd();
|
||||
});
|
||||
} else {
|
||||
this._udpSocket = dgram.createSocket('udp4');
|
||||
this._udpSocket = dgram.createSocket("udp4");
|
||||
this._udpSocket
|
||||
.on('message', data => {
|
||||
.on("message", (data) => {
|
||||
this._udpSocketOnData(data);
|
||||
})
|
||||
.on('listening', () => {
|
||||
.on("listening", () => {
|
||||
this.socketOnConnect();
|
||||
})
|
||||
.on('error', err => {
|
||||
this.emit('error', err);
|
||||
.on("error", (err) => {
|
||||
this.emit("error", err);
|
||||
})
|
||||
.on('close', () => {
|
||||
.on("close", () => {
|
||||
this.socketOnEnd();
|
||||
});
|
||||
this._udpSocket.bind(0);
|
||||
|
@ -140,21 +140,23 @@ export class Rcon extends (EventEmitter as new () => TypedEmitter<Events>) {
|
|||
private _udpSocketOnData = (data: Buffer) => {
|
||||
const a = data.readUInt32LE(0);
|
||||
if (a === 0xffffffff) {
|
||||
const str = data.toString('utf-8', 4);
|
||||
const tokens = str.split(' ');
|
||||
const str = data.toString("utf-8", 4);
|
||||
const tokens = str.split(" ");
|
||||
if (
|
||||
tokens.length === 3 &&
|
||||
tokens[0] === 'challenge' &&
|
||||
tokens[1] === 'rcon'
|
||||
tokens[0] === "challenge" &&
|
||||
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.emit('auth');
|
||||
this.emit("auth");
|
||||
} else {
|
||||
this.emit('response', str.substring(1, str.length - 2));
|
||||
this.emit("response", str.substring(1, str.length - 2));
|
||||
}
|
||||
} 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 (!this.hasAuthed && type === PacketType.RESPONSE_AUTH) {
|
||||
this.hasAuthed = true;
|
||||
this.emit('auth');
|
||||
this.emit("auth");
|
||||
} else if (type === PacketType.RESPONSE_VALUE) {
|
||||
// Read just the body of the packet (truncate the last null byte)
|
||||
// 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.
|
||||
str = str.substring(0, str.length - 1);
|
||||
}
|
||||
|
||||
this.emit('response', str);
|
||||
this.emit("response", str);
|
||||
}
|
||||
} else {
|
||||
this.emit('error', new Error('Authentication failed'));
|
||||
this.emit("error", new Error("Authentication failed"));
|
||||
}
|
||||
|
||||
data = data.slice(12 + len - 8);
|
||||
|
@ -205,12 +207,12 @@ export class Rcon extends (EventEmitter as new () => TypedEmitter<Events>) {
|
|||
};
|
||||
|
||||
public socketOnConnect = (): void => {
|
||||
this.emit('connect');
|
||||
this.emit("connect");
|
||||
|
||||
if (this.tcp) {
|
||||
this.send(this.password, PacketType.AUTH);
|
||||
} else if (this.challenge) {
|
||||
const str = 'challenge rcon\n';
|
||||
const str = "challenge rcon\n";
|
||||
const sendBuf = Buffer.alloc(str.length + 4);
|
||||
sendBuf.writeInt32LE(-1, 0);
|
||||
sendBuf.write(str, 4);
|
||||
|
@ -222,12 +224,12 @@ export class Rcon extends (EventEmitter as new () => TypedEmitter<Events>) {
|
|||
this._sendSocket(sendBuf);
|
||||
|
||||
this.hasAuthed = true;
|
||||
this.emit('auth');
|
||||
this.emit("auth");
|
||||
}
|
||||
};
|
||||
|
||||
public socketOnEnd = (): void => {
|
||||
this.emit('end');
|
||||
this.emit("end");
|
||||
this.hasAuthed = false;
|
||||
};
|
||||
}
|
Loading…
Reference in a new issue