-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathRealmClient.js
100 lines (82 loc) · 2.83 KB
/
RealmClient.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
// Developed by: Amin.MasterkinG (https://masterking32.com)
// Github: https://github.com/masterking32/WoW-Server-Relay
// Year: 2024
import Net from "net";
import { RELAY_SERVER_CMD_WORLD } from "./opcodes.js";
class RealmClient {
constructor(config, client_ip, realm, logger, onStop, onData) {
this.ip = realm.realm_ip;
this.port = realm.realm_port;
this.logger = logger;
this.secret_key = config.secret_key;
this.client_ip = client_ip;
this.realm = realm;
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(`[RealmClient] Connected to ${this.ip}:${this.port}`);
if (this.config.send_relay_packet) {
this.logger.debug(
`[RealmClient] Sending Relay Packet: ${this.secret_key} ${this.client_ip}`
);
const secret_key = this.secret_key + "\0";
const client_ip = this.client_ip + "\0";
const packet_data_size = secret_key.length + client_ip.length + 10;
let relay_packet = Buffer.alloc(packet_data_size);
relay_packet.writeUint16BE(packet_data_size - 2); // Packet size (excluding size field but including opcode)
relay_packet.writeUint32LE(RELAY_SERVER_CMD_WORLD, 2);
relay_packet.write(secret_key, 6); // Terminal null byte is included in the secret key
relay_packet.write(client_ip, 6 + secret_key.length); // Terminal null byte is included in the client IP
this.socket.write(relay_packet);
this.logger.debug(
`[RealmClient] Sent Relay Packet: ${relay_packet.toString(
"hex"
)}, length: ${relay_packet.length} bytes`
);
}
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(`[RealmClient] Received ${data.length} bytes`);
this.onData(data);
}
onSocketError(error) {
this.logger.error(`[RealmClient] Error: ${error.message}`);
this.stop();
}
onSocketClose() {
this.logger.info("[RealmClient] Connection closed");
this.stop();
}
onSocketTimeout() {
this.logger.info("[RealmClient] Connection timeout");
this.stop();
}
WriteData(data) {
if (this.isEnded) {
return;
}
this.logger.debug(`[RealmClient] Sending ${data.length} bytes`);
this.socket.write(data);
}
stop() {
if (this.isEnded) {
return;
}
this.isEnded = true;
this.logger.info("[RealmClient] Stopping");
this.onStop();
this.socket.destroy();
}
}
export default RealmClient;