-
Notifications
You must be signed in to change notification settings - Fork 11
/
RealmSession.js
70 lines (59 loc) · 1.54 KB
/
RealmSession.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
// Developed by: Amin.MasterkinG (https://masterking32.com)
// Github: https://github.com/masterking32/WoW-Server-Relay
// Year: 2024
import RealmClient from "./RealmClient.js";
class RealmSession {
constructor(realm_id, realm, config, socket, logger) {
this.config = config;
this.socket = socket;
this.logger = logger;
this.isEnded = false;
this.realm_id = realm_id;
this.realm = realm;
}
run() {
this.client = new RealmClient(
this.config,
this.socket.remoteAddress.includes("::ffff:")
? this.socket.remoteAddress.replace("::ffff:", "")
: this.socket.remoteAddress,
this.realm,
this.logger,
this.stop.bind(this),
(data) => {
this.socket.write(data);
}
);
this.client.run();
this.socket.on("data", (data) => {
if (this.isEnded) {
return;
}
if (!this.client) {
this.logger.error("[RealmSession] Client is not ready yet");
return;
}
this.logger.debug(`[RealmSession] Received ${data.length} bytes`);
this.client.socket.write(data);
});
this.socket.on("close", () => {
this.stop();
});
this.socket.on("error", (error) => {
this.logger.error("[RealmSession] Socket error", error);
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 RealmSession;