-
Notifications
You must be signed in to change notification settings - Fork 2
/
ClientHandler.js
212 lines (198 loc) · 6.95 KB
/
ClientHandler.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
import EventEmitter from "events"
import { createClient, states } from "minecraft-protocol"
import { CustomCommands } from "./internalModules/CustomCommands.js"
import { AutoQueue } from "./internalModules/AutoQueue.js"
import { StateHandler } from "./internalModules/StateHandler.js"
import { PartyCommands } from "./internalModules/PartyCommands.js"
import { PartyChatThrottle } from "./internalModules/PartyChatThrottle.js"
import { TimeDetail } from "./internalModules/TimeDetail.js"
import { BetterGameInfo } from "./internalModules/BetterGameInfo.js"
import { ConsoleLogger } from "./internalModules/ConsoleLogger.js"
import { TickCounter } from "./internalModules/TickCounter.js"
import { WorldTracker } from "./internalModules/WorldTracker.js"
import { ServerAgeTracker } from "./internalModules/ServerAgeTracker.js"
import { CustomModules } from "./internalModules/CustomModules.js"
import { ChunkPreloader } from "./internalModules/ChunkPreloader.js"
import { TabListHandler } from "./internalModules/TabListHandler.js"
import { random64BitBigInt } from "./utils/utils.js"
export class ClientHandler extends EventEmitter {
constructor(userClient, proxy, id) {
super()
this.userClient = userClient
this.proxy = proxy
this.id = id
this.proxyClient = createClient({
host: "hypixel.net",
username: userClient.username,
keepAlive: false,
version: userClient.protocolVersion,
auth: "microsoft",
hideErrors: true
})
//add trimmed UUIDs
this.userClient.trimmedUUID = this.userClient.uuid.replaceAll("-", "")
this.proxyClient.trimmedUUID = this.userClient.trimmedUUID
this.destroyed = false
this.outgoingModifiers = []
this.incomingModifiers = []
//due to issues with chunk parsing on 1.18, this does not currently support tick counting on 1.18.
this.disableTickCounter = userClient.protocolVersion >= 757
if (!this.disableTickCounter) this.worldTracker = new WorldTracker(this)
this.stateHandler = new StateHandler(this)
if (!this.disableTickCounter) {
this.tickCounter = new TickCounter(this)
this.stateHandler.bindTickCounter()
}
//previously used just for party chat, now it throttles every party command
this.partyChatThrottle = new PartyChatThrottle(this)
this.customCommands = new CustomCommands(this)
this.autoQueue = new AutoQueue(this)
this.partyCommands = new PartyCommands(this)
this.timeDetail = new TimeDetail(this)
this.betterGameInfo = new BetterGameInfo(this)
this.consoleLogger = new ConsoleLogger(this)
this.serverAgeTracker = new ServerAgeTracker(this)
this.chunkPreloader = new ChunkPreloader(this)
this.customModules = new CustomModules(this)
this.tabListHandler = new TabListHandler(this)
this.bindEventListeners()
}
destroy() {
if (this.destroyed) return
this.destroyed = true
this.proxy.removeClientHandler(this.id)
this.emit("destroy")
}
bindEventListeners() {
let userClient = this.userClient
let proxyClient = this.proxyClient
userClient.on("packet", (data, meta, buffer) => {
let replaced = false
for (let modifier of this.outgoingModifiers) {
let result = modifier(data, meta)
if (result) {
let type = result.type
if (type === "cancel") {
return
} else if (type === "replace") {
data = result.data
meta = result.meta
replaced = true
}
}
}
if (replaced) {
proxyClient.write(meta.name, data, meta)
} else {
proxyClient.writeRaw(buffer)
}
})
proxyClient.on("packet", (data, meta, buffer) => {
let replaced = false
for (let modifier of this.incomingModifiers) {
let result = modifier(data, meta)
if (result) {
let type = result.type
if (type === "cancel") {
return
} else if (type === "replace") {
data = result.data
meta = result.meta
replaced = true
}
}
}
if (meta.state !== states.PLAY) return
if (replaced) {
userClient.write(meta.name, data)
} else {
userClient.writeRaw(buffer)
}
})
userClient.on("end", (reason) => {
proxyClient.end()
this.destroy()
})
proxyClient.on("end", (reason) => {
userClient.end(`§cProxy lost connection to Hypixel: §r${reason}`)
})
userClient.on("error", () => {})
proxyClient.on("error", () => {})
//if the proxy client gets kicked while logging in, kick the user client
proxyClient.once("disconnect", data => {
userClient.write("kick_disconnect", data)
})
}
sendClientMessage(content) {
if (this.userClient.protocolVersion < 759) {
this.userClient.write("chat", {
position: 1,
message: JSON.stringify(content),
sender: "00000000-0000-0000-0000-000000000000"
})
} else if (this.userClient.protocolVersion < 760) {
this.userClient.write("system_chat", {
content: JSON.stringify(content),
type: 1
})
} else {
this.userClient.write("system_chat", {
content: JSON.stringify(content),
isActionBar: false
})
}
}
sendClientActionBar(content) {
if (this.userClient.protocolVersion < 759) {
this.userClient.write("chat", {
position: 2,
message: JSON.stringify(content),
sender: "00000000-0000-0000-0000-000000000000"
})
} else if (this.userClient.protocolVersion < 760) {
this.userClient.write("system_chat", {
content: JSON.stringify(content),
type: 2
})
} else {
this.userClient.write("system_chat", {
content: JSON.stringify(content),
isActionBar: true
})
}
}
sendServerCommand(content) {
if (this.userClient.protocolVersion < 759) {
this.proxyClient.write("chat", {
message: "/" + content
})
} else if (this.userClient.protocolVersion < 760) {
this.proxyClient.write("chat_command", {
command: content,
timestamp: BigInt(Date.now()),
salt: 0n,
argumentSignatures: [],
signedPreview: false
})
} else if (this.userClient.protocolVersion < 761) {
this.proxyClient.write("chat_command", {
command: content,
timestamp: BigInt(Date.now()),
salt: random64BitBigInt(),
argumentSignatures: [],
signedPreview: false,
previousMessages: [],
lastRejectedMessage: undefined
})
} else {
this.proxyClient.write("chat_command", {
command: content,
timestamp: BigInt(Date.now()),
salt: random64BitBigInt(),
argumentSignatures: [],
messageCount: 0,
acknowledged: Buffer.alloc(3)
})
}
}
}