-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCobaltClient.js
More file actions
217 lines (207 loc) · 6.92 KB
/
CobaltClient.js
File metadata and controls
217 lines (207 loc) · 6.92 KB
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
process.env.DEBUG = "cobalt";
const debug = require("debug")("cobalt-debug");
const release = require("debug")("cobalt-release");
release("Starting Cobalt...");
debug("Debugging enabled!");
const protocol = require("./clientHandling");
const { defaultOptions } = require("bedrock-protocol/src/options.js");
const DefaultSkinData = require("./skindata.json");
const assert = require("node:assert");
const { EventEmitter } = require("node:events");
class CobaltClient extends EventEmitter {
#players = {};
/** @type {import("bedrock-protocol/index").Client} */
client;
/** @type {import("bedrock-protocol/index").ClientOptions} */
options;
/**
* @param {import("bedrock-protocol").ClientOptions && { SkinData: typeof DefaultSkinData }} ClientOptions
*/
constructor(ClientOptions) {
super();
this.options = {
...defaultOptions,
viewDistance: 9e20,
...ClientOptions,
// these should never be changed as these are the quickest options
raknetBackend: "raknet-native",
useRaknetWorker: true,
skinData: {
...DefaultSkinData,
...(ClientOptions.skinData ?? {}),
},
};
this.client = protocol.createClient(this.options);
debug("Registering listeners...");
this.RegisterListeners();
debug("Listeners registered!");
release(
`Cobalt was initialized for ${this.options.realms?.realmInvite ?? `${this.options.host}:${this.options.port}`}!`,
);
debug(
`Cobalt was initialized for ${this.options.realms?.realmInvite ?? `${this.options.host}:${this.options.port}`}!`,
);
}
RegisterListeners() {
const client = this.client;
debug("Connecting to realm...");
client.on("play_status", async ({ status }) => {
if (status !== "login_success") return;
debug("Successfully logged in!");
release(
`Connected to ${client.options.realms?.realmInvite ?? `${client.options.host}:${client.options.port}`} with ${client.profile.name}`,
);
});
client.on("disconnect", async ({ reason, message }) => {
if (
client.lastDisconnected &&
Date.now() - client.lastDisconnected < 5000
)
return;
client.lastDisconnected = Date.now();
release(
`The client was disconnected: ${reason} - ${message}\nSince this was unexpected, the client will now reconnect...`,
);
debug(
`The client was disconnected: ${reason} - ${message}\nSince this was unexpected, the client will now reconnect...`,
);
await new Promise((r) => setTimeout(r, 5000));
this.reconnect();
});
client.on("play_status", async ({ status }) => {
if (status !== "login_success") return;
await new Promise((resolve) => setTimeout(resolve, 11));
release(
`Connected to ${client.options.realms?.realmInvite ?? `${client.options.host}:${client.options.port}`} with ${client.profile.name}`,
);
client.write("text", {
needs_translation: true,
platform_chat_id: "",
xuid: "",
type: "chat",
message: [
"",
`§7§l${"-".repeat(12)}`,
`§r§d» §rHello, I'm a bot known as §d§lCobalt§r§d.`,
`§r§d» §rIf you're seeing this message, it means that your realm is currently being used for beta testing.`,
"§r§d» §rPlease be patient as we work to improve the bot and make it more stable.",
"§r§d» §rIf you have any questions or concerns, please contact §b§ltrippleawap§r on Discord.",
"",
].join("\n"),
source_name: "",
});
// can run extra stuff here
});
this.RegisterChecks();
}
RegisterChecks() {
const client = this.client;
client.on(
"player_list",
async ({ records: { records: players, type } }) => {
if (type !== "add") return;
for (const player of players ?? []) {
if (this.#players[player.username]?.passed) continue; // .filter then loop would be slower because well, it'd be 2 loops
if (!player.username) {
// this should never occur with the type check so prob flag the user here!
debug("Player has no username, BANNING!", player);
// use realm api to ban the xuid from the realm maybe.. ( this could be abused prob dont but idk how you should handle this );
continue;
}
if (player.username === client.profile.name) continue;
// dont know actual username length cap but 16 should be fine
if (player.username.length > 16) {
debug(
`${player.username.substring(0, 16)}'s username is too long, total length: ${player.username.length}!`,
);
client.write("command_request", {
command: `kick "${player.username}" umm you failed a check!`,
internal: true,
version: 66,
origin: {
type: 0,
uuid: "",
request_id: "",
},
});
this.#players[player.username] = {
...player,
passed: false
};
continue;
}
const result = await CobaltClient.VerifySkinData(player.skin_data);
if (result) {
debug(`${player.username} passed all checks!`);
this.#players[player.username] = { ...player, passed: true };
continue;
}
release(`${player.username.substring(0, 16)} failed a check!`);
debug(`${player.username.substring(0, 16)} failed a check!`);
// havent tested this but im 99% this is correct..
client.write("command_request", {
command: `kick "${player.username}" umm you failed a check!`,
internal: true,
version: 66,
origin: {
type: 0,
uuid: "",
request_id: "",
},
});
this.#players[player.username] = {
...player,
passed: false
};
}
},
);
}
async close() {
this.client.connection?.close();
await new Promise((r) => setTimeout(r, 50));
this.#players = {};
return this;
}
async deleteClient() {
await this.close();
this.client = null;
return this;
}
async reconnect() {
assert(this.client.connection, "The client has not been initialized yet!");
this.client.connection?.close();
await new Promise((r) => setTimeout(r, 50));
this.removeAllListeners();
this.client.removeAllListeners();
this.client = protocol.createClient(this.options);
debug("Registering listeners...");
this.RegisterListeners();
debug("Listeners registered!");
}
get players() {
return this.#players;
}
/**
* @param {import("./types").Skin} skinData
* @returns {boolean} returns 0 if the skin data is valid else returns the index of the failed check.
*/
static VerifySkinData(skinData) {
try {
if (Buffer.from(skinData.skin_data.data, "base64").at(0) === undefined)
return false;
Buffer.from(skinData.cape_data.data, "base64"); // this is usually empty so we dont check if 0 === undefined;
if (skinData.geometry_data.length < 1) return false;
if (throwskinData.skin_id === "")
if (skinData.geometry_data_version === "") return false;
if (typeof skinData.animations !== "object") return false;
if (skinData.full_skin_id === "") return false;
JSON.parse(skinData.skin_resource_pack);
// handle stuff like failing to parse buffers ect.
} catch (e) {
return false;
}
return true;
}
}
module.exports = { CobaltClient };