-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.js
More file actions
executable file
·293 lines (254 loc) · 11.4 KB
/
Copy pathproxy.js
File metadata and controls
executable file
·293 lines (254 loc) · 11.4 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
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
const mc = require("minecraft-protocol");
const formatter = require("./formatter.js");
const CommandHandler = require("./modules/commandHandler.js");
const HypixelHandler = require("./modules/hypixelHandler.js");
const QueueStatsHandler = require("./modules/queueStatsHandler.js");
const EntityManager = require("./modules/entityManager.js");
const TabManager = require("./modules/tabManager.js");
const TabAlerter = require("./modules/tabAlerter.js");
const AutoGGHandler = require("./modules/autoGGHandler.js");
const GametrackApiHandler = require("./modules/gametrackApiHandler.js");
const GametrackClientHandler = require("./modules/gametrackClientHandler.js");
const path = require("path");
const fs = require("fs");
const discordRpc = require('./modules/discordRpcHandler.js');
const iconPath = process.env.ICON_PATH || path.join(__dirname, "server-icon.png");
const icon = fs.readFileSync(iconPath);
const favicon = "data:image/png;base64," + icon.toString("base64");
function replaceNamesInComponent(component, nicknames) {
if (!component) return;
if (typeof component === 'string') return;
const realName = Object.keys(nicknames).find(name => (component.text && component.text.includes(name)));
if (realName) {
component.text = component.text.replace(new RegExp(realName, 'g'), nicknames[realName]);
}
if (component.extra && Array.isArray(component.extra)) {
component.extra.forEach(part => replaceNamesInComponent(part, nicknames));
}
}
class JagProx {
constructor(config, env) {
this.config = config;
this.env = env;
this.client = null;
this.target = null;
this.lastPlayCommand = null;
this.mc_uuid = null;
this.gametrackApiHandler = new GametrackApiHandler(this.env.jwt);
this.gametrackClientHandler = null;
this.hypixel = new HypixelHandler(this);
this.commands = new CommandHandler(this);
this.queueStats = new QueueStatsHandler(this);
this.entityManager = new EntityManager(this);
this.tabManager = new TabManager(this);
this.tabAlerter = new TabAlerter(this);
this.autoGG = new AutoGGHandler(this);
if (this.config.discord_rpc && this.config.discord_rpc.enabled) {
discordRpc.login();
}
this.server = mc.createServer({
"online-mode": true,
port: this.config.port || 25565,
version: '1.8.9',
motd: ' '.repeat(20) + '§8[§5jag§dprox§8] §d1.8-1.21\n' + ' '.repeat(13) + '§5§lHypixel Proxy §8- §7by §dJagHack',
favicon: favicon
});
this.server.on("login", (client) => {
if (this.client) {
client.end('Ein Spieler ist bereits mit diesem Proxy verbunden.');
return;
}
this.client = client;
this.handleLogin();
});
this.server.on("error", (err) => formatter.log(`Proxy server error: ${err.message}`));
formatter.log(`JagProx server started on port ${this.config.port || 25565}.`);
}
handleLogin() {
formatter.log(`Client connected to proxy: ${this.client.username}`);
this.mc_uuid = this.client.uuid;
this.gametrackClientHandler = new GametrackClientHandler(this, this.client.uuid, this.client.username);
this.lastPlayCommand = null;
this.autoGG.reset();
discordRpc.setActivity({
details: 'Playing',
state: 'In Game',
largeImageKey: 'icon',
largeImageText: 'JagProx',
instance: false,
startTimestamp: new Date()
});
const userDataPath = process.env.USER_DATA_PATH || '.';
const cacheFolder = this.config.cache_folder || './cache/profiles';
const cachePath = path.isAbsolute(this.config.cache_folder)
? this.config.cache_folder
: path.join(userDataPath, this.config.cache_folder);
if (!fs.existsSync(cachePath)) {
fs.mkdirSync(cachePath, { recursive: true });
}
this.target = mc.createClient({
host: "mc.hypixel.net",
port: 25565,
username: this.client.username,
auth: "microsoft",
version: '1.8.9',
profile: this.client.profile,
profilesFolder: cachePath
});
this.target.on("connect", () => formatter.log(`Client connected to target: ${this.target.username}`));
this.client.on("packet", async (data, meta) => {
if (meta.name === "chat" && data.message && data.message.startsWith("/")) {
const handledByJagProx = await this.commands.handle(data.message);
if (handledByJagProx) {
return;
}
if (data.message.toLowerCase().startsWith('/play ')) {
this.lastPlayCommand = data.message;
formatter.log(`Captured last play command: ${this.lastPlayCommand}`);
}
}
if (meta.name === "custom_payload" && data.channel === "MC|Brand") {
data.data = Buffer.from("\x07vanilla");
}
if (this.target && this.target.state === meta.state) {
try {
this.target.write(meta.name, data);
} catch (e) {
formatter.log(`Error writing packet to target: ${e.message}`);
}
}
});
this.target.on("packet", (data, meta) => {
const nicknames = this.config.nicknames || {};
const hasNicknames = Object.keys(nicknames).length > 0;
if (meta.name === 'chat' && data.message) {
let chatObject;
try {
chatObject = JSON.parse(data.message);
if (hasNicknames) {
replaceNamesInComponent(chatObject, nicknames);
}
if (data.position === 0 || data.position === 1) {
console.log(`[JAGPROX_CHAT]${formatter.reconstructLegacyText(chatObject)}`);
}
this.gametrackClientHandler.parseChatMessage(chatObject);
data.message = JSON.stringify(chatObject);
} catch(e) {
this.gametrackClientHandler.parseChatMessage({text: data.message});
formatter.log(`Error parsing chat JSON for gametrack: ${e.message}. Passing raw message.`);
}
} else if (hasNicknames && meta.name === 'player_info' && (data.action === 'add_player' || data.action === 'update_display_name')) {
data.data.forEach(player => {
const nickname = nicknames[player.name];
if (nickname) {
if (player.displayName) {
if (player.displayName.includes(player.name)) {
try {
let component = JSON.parse(player.displayName);
replaceNamesInComponent(component, { [player.name]: nickname });
player.displayName = JSON.stringify(component);
} catch (e) {
player.displayName = player.displayName.replace(new RegExp(player.name, 'g'), nickname);
}
}
} else {
player.displayName = JSON.stringify({ text: nickname });
}
}
});
}
this.queueStats.handlePacket(data, meta);
if (meta.name === 'playerlist_header') {
try {
const q = this.queueStats;
if (q.currentMapName) {
let footerObj;
try {
footerObj = JSON.parse(data.footer);
} catch(e) {
footerObj = { text: data.footer || '' };
}
const myLine = { text: '\n§7You are currently playing on §5' + q.currentMapName };
if (footerObj.extra && Array.isArray(footerObj.extra)) {
footerObj.extra.push(myLine);
} else {
footerObj = { text: '', extra: [footerObj, myLine] };
}
data.footer = JSON.stringify(footerObj);
}
} catch(e) {
formatter.log(`Error injecting into tab footer: ${e.message}`);
}
}
this.entityManager.handlePacket(data, meta);
this.tabManager.handlePacket(data, meta);
this.tabAlerter.handlePacket(data, meta);
this.autoGG.handlePacket(data, meta);
if (meta.name === "custom_payload" && data.channel === "MC|Brand") {
data.data = Buffer.from("\x07vanilla");
}
if (this.client && this.client.state === meta.state) {
try {
this.client.write(meta.name, data);
} catch (e) {
formatter.log(`Error writing packet to client: ${e.message}`);
}
}
});
const onEndOrError = (err) => {
if (err) {
formatter.log(`Connection error: ${err.message || JSON.stringify(err)}`);
}
formatter.log(`Client disconnected: ${this.client ? this.client.username : 'Unknown'}`);
if (this.target) this.target.end();
if (this.client) this.client.end();
this.queueStats.reset();
this.entityManager.reset();
this.tabManager.reset();
this.tabAlerter.reset();
this.autoGG.reset();
this.hypixel.reset();
this.client = null;
this.target = null;
discordRpc.setActivity({
details: 'Idling',
state: 'In Launcher',
largeImageKey: 'icon',
largeImageText: 'JagProx',
instance: false,
startTimestamp: new Date()
});
};
this.client.on("error", onEndOrError);
this.client.on("end", onEndOrError);
this.target.on("error", onEndOrError);
this.target.on("end", onEndOrError);
}
onGameChanged(newGameKey) {
if (this.gametrackClientHandler) {
this.gametrackClientHandler.onGameChanged(newGameKey);
}
this.queueStats.resetTrigger();
this.entityManager.reset();
this.tabManager.reset();
this.tabAlerter.reset();
this.autoGG.reset();
this.hypixel.reset();
}
proxyChat(message, clickEvent) {
if (!this.client) return;
const chatComponent = {
text: `§8[§5jag§dprox§8] §r${message}`
};
if (clickEvent) {
chatComponent.clickEvent = clickEvent;
chatComponent.underlined = true;
chatComponent.color = 'light_purple';
}
this.client.write("chat", {
message: JSON.stringify(chatComponent),
position: 0
});
}
}
module.exports = JagProx;