forked from EnriqCG/rcon-srcds
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrcon.ts
211 lines (179 loc) · 6.77 KB
/
rcon.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import { createConnection, Socket } from 'net'
import protocol from './protocol'
import * as packets from './packet'
class RCON {
host: string
port: number
maxPacketSize: number
encoding: packets.EncodingOptions
timeout: number
connection!: Socket
connected: boolean
authenticated: boolean
/**
* Source RCON (https://developer.valvesoftware.com/wiki/Source_RCON)
* @param options Connection options
*/
constructor(options: RCONOptions) {
this.host = options.host || '127.0.0.1'
this.port = options.port || 27015
this.maxPacketSize = options.maxPacketSize || 4096
this.encoding = options.encoding || 'ascii'
this.timeout = options.timeout || 1000
this.authenticated = false
this.connected = false
}
/**
* Authenticates the connection
* @param password Password string
*/
async authenticate(password: string): Promise<boolean> {
if (!this.connected) {
await this.connect()
}
return new Promise((resolve, reject) => {
if (this.authenticated) {
reject(Error('Already authenticated'))
return
}
this.write(protocol.SERVERDATA_AUTH, protocol.ID_AUTH, password)
.then((data) => {
if (data === true) {
this.authenticated = true
resolve(true)
} else {
this.disconnect()
reject(Error('Unable to authenticate'))
}
}).catch(reject)
})
}
/**
* Executes command on the server
* @param command Command to execute
*/
execute(command: string): Promise<string | boolean> {
return new Promise((resolve, reject) => {
if (!this.connected) {
reject(Error('Already disconnected. Please reauthenticate.'))
return
}
const packetId = Math.floor(Math.random() * (256 - 1) + 1)
if (!this.connection.writable) {
reject(Error('Unable to write to socket'))
return
}
if (!this.authenticated) {
reject(Error('Not authorized'))
return
}
this.write(protocol.SERVERDATA_EXECCOMMAND, packetId, command)
.then(resolve)
.catch(reject)
})
}
/**
* Creates a connection to the socket
*/
private connect(): Promise<void> {
return new Promise((resolve, reject) => {
this.connection = createConnection({
host: this.host,
port: this.port
}, () => {
if (this.connection) this.connection.removeListener('error', reject)
this.connected = true
resolve()
})
this.connection.once('error', reject)
this.connection.setTimeout(this.timeout)
})
}
/**
* Destroys the socket connection
*/
disconnect(): Promise<void> {
this.authenticated = false
this.connected = false
this.connection.destroy()
return new Promise((resolve, reject) => {
const onError = (e: Error): void => {
this.connection.removeListener('close', onClose)
reject(e)
}
const onClose = (): void => {
this.connection.removeListener('error', onError)
resolve()
}
this.connection.once('close', onClose)
this.connection.once('error', onError)
})
}
isConnected(): boolean {
return this.connected
}
isAuthenticated(): boolean {
return this.authenticated
}
/**
* Writes to socket connection
* @param type Packet Type
* @param id Packet ID
* @param body Packet payload
*/
private write(type: number, id: number, body: string): Promise<string | boolean> {
return new Promise((resolve, reject) => {
let response = ''
const onData = (packet: Buffer): void => {
const decodedPacket = packets.decode(packet, this.encoding)
// Server will respond twice (0x00 and 0x02) if we send an auth packet (0x03)
// but we need 0x02 to confirm
if (type === protocol.SERVERDATA_AUTH && decodedPacket.type !== protocol.SERVERDATA_AUTH_RESPONSE) {
return
} else if (type === protocol.SERVERDATA_AUTH && decodedPacket.type === protocol.SERVERDATA_AUTH_RESPONSE) {
if (decodedPacket.id === protocol.ID_AUTH) {
resolve(true)
} else {
resolve(false)
}
this.connection.removeListener('data', onData)
} else if (id === decodedPacket.id || decodedPacket.id === protocol.ID_TERM) {
// don't add the termination packet.
if (decodedPacket.id != protocol.ID_TERM) {
response = response.concat(decodedPacket.body.replace(/\n$/, '\n')) // remove last line break
}
// Hack to cope with multipacket responses.
// see https://developer.valvesoftware.com/wiki/Talk:Source_RCON_Protocol#How_to_receive_split_response?
if (decodedPacket.size > 3700) {
let encodedTerminationPacket = packets.encode(protocol.SERVERDATA_RESPONSE_VALUE, protocol.ID_TERM, '');
this.connection.write(encodedTerminationPacket);
} else if (decodedPacket.size <= 3700) { // no need to check for ID_TERM here, since this packet will always be < 3700
this.connection.removeListener('data', onData);
resolve(response);
}
}
this.connection.removeListener('error', onError)
}
const onError = (e: Error): void => {
this.connection.removeListener('data', onData)
reject(e)
}
const encodedPacket = packets.encode(type, id, body, this.encoding)
if (this.maxPacketSize > 0 && encodedPacket.length > this.maxPacketSize) {
reject(Error('Packet size too big'))
return
}
this.connection.on('data', onData)
this.connection.on('error', onError)
this.connection.write(encodedPacket)
})
}
}
interface RCONOptions {
host?: string
port?: number
maxPacketSize?: number
encoding?: packets.EncodingOptions
timeout?: number
}
export default RCON