-
Notifications
You must be signed in to change notification settings - Fork 11
/
AuthSession.js
307 lines (273 loc) · 8.51 KB
/
AuthSession.js
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
// Developed by: Amin.MasterkinG (https://masterking32.com)
// Github: https://github.com/masterking32/WoW-Server-Relay
// Year: 2024
import AuthClient from "./AuthClient.js";
import {
CMD_AUTH_LOGON_CHALLENGE,
CMD_AUTH_LOGON_PROOF,
CMD_AUTH_RECONNECT_CHALLENGE,
CMD_AUTH_RECONNECT_PROOF,
CMD_REALM_LIST,
CMD_SURVEY_RESULT,
CMD_XFER_ACCEPT,
CMD_XFER_CANCEL,
CMD_XFER_RESUME,
RELAY_SERVER_CMD_AUTH,
} from "./opcodes.js";
class AuthSession {
constructor(config, socket, logger) {
this.config = config;
this.socket = socket;
this.logger = logger;
this.status = "init";
this.ClientIP = socket.remoteAddress.includes("::ffff:")
? socket.remoteAddress.replace("::ffff:", "")
: socket.remoteAddress;
this.isEnded = false;
}
run() {
this.socket.on("data", this.onSocketData.bind(this));
this.socket.on("close", this.onSocketClose.bind(this));
this.socket.on("error", this.onSocketError.bind(this));
this.socket.on("timeout", this.onSocketTimeout.bind(this));
}
async onSocketData(data) {
let bytes = data.length;
this.logger.debug(`[AuthSession] Received ${data.length} bytes`);
let position = 0;
while (bytes > 0) {
const opcode = data.readUInt8(position);
position += 1;
bytes -= 1;
const new_position = await this.HandleOpcode(
opcode,
data.slice(position)
);
if (!new_position) {
break;
}
position += new_position;
bytes -= new_position;
}
}
async HandleOpcode(opcode, data) {
let position = 0;
switch (opcode) {
case CMD_AUTH_LOGON_CHALLENGE:
case CMD_AUTH_RECONNECT_CHALLENGE:
const ChallengeResponse = await this.HandleAuthLogonChallenge(data);
if (!ChallengeResponse) {
this.stop();
}
position = ChallengeResponse.position;
let AuthChallengePayload = Buffer.alloc(
ChallengeResponse.payload.length + 1
);
AuthChallengePayload.writeUInt8(opcode, 0);
ChallengeResponse.payload.copy(AuthChallengePayload, 1);
this.onClientStop = () => {
this.stop();
};
this.onClientData = (data) => {
this.socket.write(data);
this.logger.debug(`[AuthSession] Sent ${data.toString("hex")}`);
};
this.client = new AuthClient(
this.config,
this.ClientIP,
AuthChallengePayload,
this.logger,
this.onClientStop,
this.onClientData
);
this.client.run();
break;
case CMD_AUTH_LOGON_PROOF:
case CMD_AUTH_RECONNECT_PROOF:
this.logger.debug("[AuthSession] Auth logon proof");
if (this.client) {
const packet = Buffer.alloc(data.length + 1);
packet.writeUInt8(opcode, 0);
data.copy(packet, 1);
this.client.WriteData(packet);
} else {
this.logger.error(
"[AuthSession] Auth logon proof received without client"
);
this.stop();
}
position = data.length;
break;
case CMD_REALM_LIST:
this.logger.debug("[AuthSession] Realm list");
const packet = Buffer.alloc(data.length + 1);
packet.writeUInt8(CMD_REALM_LIST, 0);
data.copy(packet, 1);
this.client.WriteData(packet);
position = data.length;
break;
case RELAY_SERVER_CMD_AUTH:
this.logger.debug("[AuthSession] Relay server command");
const RelayServerResponse = await this.HandleRelayServerCommand(data);
if (!RelayServerResponse) {
this.stop();
}
position = RelayServerResponse.position;
break;
case CMD_XFER_ACCEPT:
case CMD_XFER_RESUME:
case CMD_XFER_CANCEL:
case CMD_SURVEY_RESULT:
position = data.length;
if (this.client) {
const packet = Buffer.alloc(data.length + 1);
packet.writeUInt8(opcode, 0);
data.copy(packet, 1);
this.client.WriteData(packet);
} else {
this.stop();
}
break;
default:
this.logger.error(`[AuthSession] Unknown opcode ${opcode}`);
this.stop();
}
return position;
}
async HandleAuthLogonChallenge(data) {
let position = 1;
const protocol_version = await data.readUInt8(0x01 - position);
const packet_size = await data.readUInt16LE(0x02 - position);
const game_name = await data.toString(
"utf8",
0x04 - position,
0x08 - position
);
const versionArray = [];
for (let i = 0; i < 3; i++) {
const versionByte = await data.readUInt8(0x08 + i - position);
versionArray.push(versionByte);
}
const version = versionArray.join(".");
const build = await data.readUInt16LE(0x0b - position);
const platform = await data.toString(
"utf8",
0x0d - position,
0x0d + 4 - position
);
const os = await data.toString(
"utf8",
0x11 - position,
0x11 + 4 - position
);
const locale = await data.toString(
"utf8",
0x15 - position,
0x15 + 4 - position
);
const timezone_bias = await data.readInt32LE(0x19 - position);
const ip = await data.readUInt32LE(0x1d - position);
const username_length = await data.readUInt8(0x21 - position);
const username = await data.toString(
"utf8",
0x22 - position,
0x22 + username_length - position
);
this.logger.debug(
`[AuthSession] Protocol Version: ${protocol_version}, Packet Size: ${packet_size}, Game Name: ${game_name}, Version: ${version}, Build: ${build}, Platform: ${platform
.split("")
.reverse()
.join("")}, OS: ${os.split("").reverse().join("")}, Locale: ${locale
.split("")
.reverse()
.join(
""
)}, Timezone Bias: ${timezone_bias}, IP: ${ip}, Username Length: ${username_length}, Username: ${username}`
);
if (version !== this.config.game_version) {
this.logger.error(`[AuthSession] Invalid version: ${version}`);
return false;
}
if (build !== this.config.build) {
this.logger.error(`[AuthSession] Invalid build: ${build}`);
return false;
}
if (!username_length) {
this.logger.error(
`[AuthSession] Invalid username length: ${username_length}`
);
return false;
}
if (username_length + 0x22 - 4 !== packet_size) {
this.logger.error(`[AuthSession] Invalid packet size: ${packet_size}`);
return false;
}
position = 0x22 + username_length - position;
const output = {
protocol_version: protocol_version,
packet_size: packet_size,
game_name: game_name,
version: version,
build: build,
platform: platform,
os: os,
locale: locale,
timezone_bias: timezone_bias,
ip: ip,
position: position,
payload: data,
};
return output;
}
// ? With this custom packet we can get the real user IP and forward it to the main server
// ? Then if you ban the user, you can ban the user by IP without any problems!
async HandleRelayServerCommand(data) {
const secret_key_length = await data.readUInt16LE(0);
const client_ip_length = await data.readUInt16LE(2);
const secret_key = await data.toString("utf8", 4, 4 + secret_key_length);
const client_ip = await data.toString(
"utf8",
4 + secret_key_length,
4 + secret_key_length + client_ip_length
);
this.logger.debug(
`[AuthSession] Secret Key Length: ${secret_key_length}, Secret Key: ${secret_key}, Client IP Length: ${client_ip_length}, Client IP: ${client_ip}`
);
if (secret_key !== this.config.secret_key) {
this.logger.error("[AuthSession] Invalid secret key");
this.stop();
}
const output = {
secret_key_length: secret_key_length,
secret_key: secret_key,
client_ip_length: client_ip_length,
client_ip: client_ip,
position: 4 + secret_key_length + client_ip_length,
};
return output;
}
onSocketClose() {
this.logger.debug("[AuthSession] Connection closed");
this.stop();
}
onSocketError(error) {
this.logger.debug("[AuthSession] Connection error: " + error.message);
this.stop();
}
onSocketTimeout() {
this.logger.debug("[AuthSession] Connection timeout");
this.stop();
}
stop() {
if (this.isEnded) {
return;
}
this.logger.debug("[AuthSession] Stopping session");
this.isEnded = true;
if (this.client) {
this.client.stop();
}
this.socket.destroy();
}
}
export default AuthSession;