-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathAuthClient.js
298 lines (257 loc) · 8.3 KB
/
AuthClient.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
// Developed by: Amin.MasterkinG (https://masterking32.com)
// Github: https://github.com/masterking32/WoW-Server-Relay
// Year: 2024
import Net from "net";
import {
CMD_AUTH_LOGON_CHALLENGE,
CMD_AUTH_LOGON_PROOF,
CMD_AUTH_RECONNECT_CHALLENGE,
CMD_AUTH_RECONNECT_PROOF,
CMD_REALM_LIST,
CMD_XFER_DATA,
CMD_XFER_INITIATE,
RELAY_SERVER_CMD_AUTH,
} from "./opcodes.js";
class AuthClient {
constructor(config, client_ip, authChallengePayload, logger, onStop, onData) {
this.ip = config.main_server_auth.host;
this.port = config.main_server_auth.port;
this.logger = logger;
this.secret_key = config.secret_key;
this.client_ip = client_ip;
this.authChallengePayload = authChallengePayload;
this.send_relay_packet = config.send_relay_packet;
this.onStop = onStop;
this.onData = onData;
this.isReady = false;
this.isEnded = false;
this.config = config;
}
run() {
this.socket = Net.createConnection(this.port, this.ip, () => {
this.logger.info(`[AuthClient] Connected to ${this.ip}:${this.port}`);
if (this.send_relay_packet) {
let relay_packet = Buffer.alloc(
this.secret_key.length + this.client_ip.length + 5
);
relay_packet.writeUInt8(RELAY_SERVER_CMD_AUTH, 0);
relay_packet.writeUint16LE(this.secret_key.length, 1);
relay_packet.writeUint16LE(this.client_ip.length, 3);
relay_packet.write(this.secret_key, 5);
relay_packet.write(this.client_ip, 5 + this.secret_key.length);
this.socket.write(relay_packet);
this.logger.debug(
`[AuthClient] Sent Relay Packet: ${relay_packet.toString(
"hex"
)}, length: ${relay_packet.length} bytes`
);
}
this.socket.write(this.authChallengePayload);
this.logger.debug(
`[AuthClient] Sent Auth Challenge Payload: ${this.authChallengePayload.toString()}`
);
this.isReady = true;
});
this.socket.on("data", this.onSocketData.bind(this));
this.socket.on("error", this.onSocketError.bind(this));
this.socket.on("close", this.onSocketClose.bind(this));
this.socket.on("timeout", this.onSocketTimeout.bind(this));
}
async onSocketData(data) {
this.logger.debug(`[AuthClient] Received ${data.length} bytes`);
let bytes = data.length;
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;
}
this.logger.debug(`[AuthClient] Remaining bytes: ${bytes}`);
}
async HandleOpcode(opcode, data) {
let position = 0;
this.logger.debug(`[AuthClient] Opcode: ${opcode}`);
switch (opcode) {
case CMD_AUTH_LOGON_CHALLENGE:
case CMD_AUTH_RECONNECT_CHALLENGE:
const ChallengeResponse = await this.HandleAuthLogonChallenge(
opcode,
data
);
if (!ChallengeResponse) {
this.stop();
}
position = ChallengeResponse.position;
break;
case CMD_AUTH_LOGON_PROOF:
case CMD_AUTH_RECONNECT_PROOF:
this.logger.debug(`[AuthClient] Received Auth Logon Proof`);
const AuthLogonProofResponse = await this.HandleAuthLogonProof(
opcode,
data
);
if (!AuthLogonProofResponse) {
this.stop();
}
position = AuthLogonProofResponse.position;
break;
case CMD_REALM_LIST:
this.logger.debug(`[AuthClient] Received Realm List`);
const RealmListResponse = await this.HandleRealmList(data);
if (!RealmListResponse) {
this.stop();
}
position = RealmListResponse.position;
break;
case CMD_XFER_INITIATE:
case CMD_XFER_DATA:
const packet = Buffer.alloc(1 + data.length);
packet.writeUInt8(opcode, 0);
data.copy(packet, 1, 0);
this.onData(packet);
position = data.length;
break;
default:
this.logger.error(`[AuthClient] Unknown opcode: ${opcode}`);
this.stop();
position = false;
break;
}
return position;
}
async HandleAuthLogonProof(opcode, data) {
this.logger.debug("[AuthClient] Handling Auth Logon Proof");
const packet = Buffer.alloc(1 + data.length);
packet.writeUInt8(opcode, 0);
data.copy(packet, 1, 0);
this.onData(packet);
const result = data.readUInt8(0x0);
this.logger.debug(`[AuthClient] Auth Logon Proof Result: ${result}`);
if (result === 0x00) {
// success
let position = data.length;
return { position: position, payload: packet };
} else {
// fail
return false;
}
}
async HandleAuthLogonChallenge(opcode, data) {
this.logger.debug("[AuthClient] Handling Auth Logon Challenge");
let position = 1;
let result = data.readUInt8(0x1 - position);
if (CMD_AUTH_LOGON_CHALLENGE === opcode) {
result = data.readUInt8(0x2 - position);
}
this.logger.debug(`[AuthClient] Result: ${result}`);
const payload = Buffer.alloc(data.length + 1);
payload.writeUInt8(opcode, 0);
data.copy(payload, 1, 0);
this.logger.debug(
`[AuthClient] Auth Logon Challenge Payload: ${payload.toString("hex")}`
);
this.onData(payload);
if (result === 0x00) {
// Success
return { position: payload.length - 1, payload: payload };
}
return false;
}
async HandleRealmList(data) {
this.logger.debug("[AuthClient] Handling Realm List");
let position = 1;
const size = data.readUInt8(0x1 - position);
const realm_count = data.readUInt8(0x7 - position);
this.logger.debug(
`[AuthClient] Realm Count: ${realm_count}, size: ${size}, packetSize: ${data.length}`
);
let new_data = data;
position = 0x7;
for (let i = 0; i < realm_count; i++) {
let realmName = "";
position = 0x4 + position;
while (data.readUInt8(position) !== 0) {
realmName += String.fromCharCode(data.readUInt8(position));
position++;
}
position++;
let address_port = "";
let start_address_position = position;
let end_address_position = position;
while (data.readUInt8(position) !== 0) {
address_port += String.fromCharCode(data.readUInt8(position));
if (String.fromCharCode(data.readUInt8(position)) == ":") {
end_address_position = position;
}
position++;
}
position += 7;
const realm_id = data.readUInt8(position);
position += 1;
this.logger.debug(
`[AuthClient] Realm Name: ${realmName}, Address: ${address_port}, Realm ID: ${realm_id}`
);
let new_address_port = this.config.relay_ip;
let different_length =
new_address_port.length -
(end_address_position - start_address_position);
let temp_packet = Buffer.alloc(new_data.length + different_length);
new_data.copy(temp_packet, 0, 0, start_address_position);
temp_packet.write(new_address_port, start_address_position);
new_data.copy(
temp_packet,
start_address_position + new_address_port.length,
end_address_position
);
temp_packet.writeUInt8(temp_packet.length - 2, 0);
new_data = temp_packet;
if (size <= position) {
break;
}
position -= 1;
}
const new_packet = Buffer.alloc(new_data.length + 1);
new_packet.writeUInt8(CMD_REALM_LIST, 0);
new_data.copy(new_packet, 1, 0);
this.onData(new_packet);
return { position: data.length, payload: new_packet };
}
onSocketError(error) {
this.logger.error(`[AuthClient] Error: ${error.message}`);
this.stop();
}
onSocketClose() {
this.logger.info("[AuthClient] Connection closed");
this.stop();
}
onSocketTimeout() {
this.logger.info("[AuthClient] Connection timeout");
this.stop();
}
WriteData(data) {
if (this.isEnded) {
return;
}
this.logger.debug(`[AuthClient] Sending ${data.length} bytes`);
this.socket.write(data);
}
stop() {
if (this.isEnded) {
return;
}
this.isEnded = true;
this.logger.info("[AuthClient] Stopping");
this.onStop();
this.socket.destroy();
}
}
export default AuthClient;