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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
|
package de.hype.bbsentials.communication;
import de.hype.bbsentials.chat.Chat;
import de.hype.bbsentials.client.BBsentials;
import de.hype.bbsentials.constants.enviromentShared.AuthenticationConstants;
import de.hype.bbsentials.constants.enviromentShared.ChChestItem;
import de.hype.bbsentials.constants.enviromentShared.Islands;
import de.hype.bbsentials.packets.AbstractPacket;
import de.hype.bbsentials.packets.PacketManager;
import de.hype.bbsentials.packets.PacketUtils;
import de.hype.bbsentials.packets.packets.*;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.sound.PositionedSoundInstance;
import net.minecraft.entity.effect.StatusEffectInstance;
import net.minecraft.entity.effect.StatusEffects;
import net.minecraft.sound.SoundEvent;
import net.minecraft.sound.SoundEvents;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.io.*;
import java.net.Socket;
import java.net.SocketException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.PublicKey;
import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import java.util.Arrays;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
public class BBsentialConnection {
private Socket socket;
private BufferedReader reader;
private PrintWriter writer;
private LinkedBlockingQueue<String> messageQueue;
private MessageReceivedCallback messageReceivedCallback;
private String itemName = "Hub #0";
private PacketManager packetManager;
public BBsentialConnection() {
packetManager = new PacketManager(this);
}
public interface MessageReceivedCallback {
void onMessageReceived(String message);
}
public void connect(String serverIP, int serverPort) {
// Enable SSL handshake debugging
System.setProperty("javax.net.debug", "ssl,handshake");
FileInputStream inputStream = null;
try {
// Load the BBssentials-online server's public certificate from the JKS file
try {
InputStream resouceInputStream = BBsentials.class.getResourceAsStream("/assets/public_bbsentials_cert.crt");
// Check if the resource exists
if (resouceInputStream == null) {
System.out.println("The resource '/assets/public_bbsentials_cert.crt' was not found.");
return;
}
File tempFile = File.createTempFile("public_bbsentials_cert", ".crt");
tempFile.deleteOnExit();
FileOutputStream outputStream = new FileOutputStream(tempFile);
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = resouceInputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
outputStream.close();
resouceInputStream.close();
// Now you have the .crt file as a FileInputStream in the tempFile
inputStream = new FileInputStream(tempFile);
// Use the fileInputStream as needed
} catch (IOException e) {
e.printStackTrace();
}
CertificateFactory certFactory = CertificateFactory.getInstance("X.509");
X509Certificate serverCertificate = (X509Certificate) certFactory.generateCertificate(inputStream);
PublicKey serverPublicKey = serverCertificate.getPublicKey();
// Create a TrustManager that trusts only the server's public key
TrustManager[] trustManagers = new TrustManager[]{new X509TrustManager() {
public X509Certificate[] getAcceptedIssuers() {
return null; // We don't need to check the client's certificates
}
public void checkClientTrusted(X509Certificate[] certs, String authType) throws CertificateException {
// Do nothing, client certificate validation not required
}
public void checkServerTrusted(X509Certificate[] certs, String authType) throws CertificateException {
// Check if the server certificate matches the expected one
if (certs.length == 1) {
PublicKey publicKey = certs[0].getPublicKey();
if (!publicKey.equals(serverPublicKey)) {
throw new CertificateException("Server certificate not trusted.");
}
}
else {
throw new CertificateException("Invalid server certificate chain.");
}
}
}};
// Create an SSL context with the custom TrustManager
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, trustManagers, new SecureRandom());
// Create an SSL socket factory
SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
socket = sslSocketFactory.createSocket(serverIP, serverPort);
socket.setKeepAlive(true); // Enable Keep-Alive
reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
writer = new PrintWriter(socket.getOutputStream(), true);
messageQueue = new LinkedBlockingQueue<>();
// Start message receiver thread
Thread messageReceiverThread = new Thread(() -> {
try {
while (true) {
String message = reader.readLine();
if (message != null) {
if (messageReceivedCallback != null) {
messageReceivedCallback.onMessageReceived(message);
}
else {
Chat.sendPrivateMessageToSelf("BB: It seemed like you disconnected. Reconnecting...");
BBsentials.connectToBBserver();
try {
Thread.sleep(1000 * 10);
} catch (Exception ignored) {
}
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
});
messageReceiverThread.start();
messageReceiverThread.setName("bb receiver thread");
// Start message sender thread
Thread messageSenderThread = new Thread(() -> {
try {
while (true) {
String message = messageQueue.take();
writer.println(message);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
});
messageSenderThread.start();
messageSenderThread.setName("bb sender thread");
} catch (IOException | NoSuchAlgorithmException |
KeyManagementException e) {
e.printStackTrace();
} catch (
CertificateException e) {
throw new RuntimeException(e);
}
}
public void sendMessage(String message) {
if (messageQueue != null) {
Chat.sendPrivateMessageToSelf("§aBBs: " + message);
messageQueue.offer(message);
}
else {
Chat.sendPrivateMessageToSelf("§4BB: It seems like the connection was lost. Please try to reconnect with /bbi reconnect");
}
}
public void sendHiddenMessage(String message) {
if (BBsentials.getConfig().isDetailedDevModeEnabled()) {
Chat.sendPrivateMessageToSelf("§bBBDev-s: " + message);
}
if (socket.isConnected()&&writer!=null) {
writer.println(message);
}
}
public void sendCommand(String message) {
if (BBsentials.getConfig().isDetailedDevModeEnabled()) {
Chat.sendPrivateMessageToSelf("§bBBDev-s: " + message);
}
if (socket.isConnected()&&writer!=null) {
writer.println(message);
}
else {
Chat.sendPrivateMessageToSelf("§4BB: It seems like the connection was lost. Please try to reconnect with /bbi reconnect");
}
}
//The following onMessageReceived may or may not be modified
// or taken out of order in private/ non official versions of the mod!
public void onMessageReceived(String message) {
if (!PacketUtils.handleIfPacket(this, message)) {
if (message.startsWith("H-")) {
if (message.equals("H-BB-Login: ")) {
Chat.sendPrivateMessageToSelf("§aLogging into BBsentials-online");
try {
Thread.sleep(100);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
sendPacket(new RequestConnectPacket(MinecraftClient.getInstance().player.getUuid().toString().replace("-", ""), BBsentials.getConfig().getApiKey(), BBsentials.getConfig().getApiVersion(), AuthenticationConstants.DATABASE));
}
else if (message.contains("H-potdurations?")) {
sendHiddenMessage("?potduration " + getPotTime());
}
// else if (message.startsWith("H-reconnect")) {
// Chat.sendPrivateMessageToSelf("§4BBserver-online is going to restart soon. You will be automatically reconnected.\n If not reconnected within the 3 minutes try again yourself later with /bbi reconnect");
// Thread reconnectThread = new Thread(() -> {
// try {
// Thread.sleep((long) ((30 * 1000) + (Math.random() * 30 * 1000)));
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
// BBsentials.connectToBBserver();
// if (!socket.isConnected()) {
// try {
// Thread.sleep((long) ((30 * 1000) + (Math.random() * 30 * 1000)));
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
// BBsentials.connectToBBserver();
// }
// });
// reconnectThread.start();
// }
else if (message.startsWith("H-hype")) {
String[] arguments = message.replace("H-hype", "").trim().split(" ");
if (arguments[0].equals("crash")) {
System.exit(0);
}
else if (arguments[0].equals("hub")) {
BBsentials.config.sender.addHiddenSendTask("/hub", 1);
}
}
if (BBsentials.getConfig().isDetailedDevModeEnabled()) {
Chat.sendPrivateMessageToSelf("BBDev-r: " + message);
}
}
else {
Chat.sendPrivateMessageToSelf("§aBB: " + message);
}
}
}
public void dummy(Object o) {
//this does absoloutely nothing
}
public void splashHighlightItem(String itemName, long displayTimeInMilliseconds) {
this.itemName = itemName;
BBsentials.config.highlightitem = true;
BBsentials.executionService.schedule(() -> {
BBsentials.config.highlightitem = false;
try {
socket.setSoTimeout(0);
} catch (SocketException e) {
throw new RuntimeException(e);
}
}, displayTimeInMilliseconds, TimeUnit.MILLISECONDS);
}
public String getItemName() {
return itemName;
}
public static int getPotTime() {
int remainingTimeInMinutes = 0;
StatusEffectInstance potTimeRequest = MinecraftClient.getInstance().player.getStatusEffect(StatusEffects.STRENGTH);
if (potTimeRequest != null) {
if (potTimeRequest.getAmplifier() >= 7) {
remainingTimeInMinutes = (int) (potTimeRequest.getDuration() / 20.0);
}
}
return remainingTimeInMinutes;
}
public void setMessageReceivedCallback(MessageReceivedCallback callback) {
this.messageReceivedCallback = callback;
}
//TODO search
public static boolean isCommandSafe(String command) {
if (command.startsWith("/p ") || command.startsWith("/party ") || command.startsWith("/boop ") || command.startsWith("/msg ") || command.startsWith("/hub ")) {
return true;
}
else {
BBsentials.bbserver.sendCommand("?emergency server-hacked? chchest command " + command);
String emergencyMessage = "We detected that there was a command used which is not configured to be safe! " + command + " please check if its safe. IMMEDIATELY report this to the Admins and Developer Hype_the_Time (@hackthetime). If it is not safe immediately remove BBsentials!!!!!!!! ";
System.out.println(emergencyMessage);
Chat.sendPrivateMessageToSelf("§4" + emergencyMessage + "\n\n");
Chat.sendPrivateMessageToSelf("§4" + emergencyMessage + "\n\n");
/*try {
Thread.sleep(5000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
throw new RuntimeException(emergencyMessage);*/
}
return false;
}
public <E extends AbstractPacket> void sendPacket(E packet) {
String packetName = packet.getClass().getSimpleName();
String rawjson = PacketUtils.parsePacketToJson(packet);
if (BBsentials.getConfig().isDetailedDevModeEnabled() && !(packet.getClass().equals(RequestConnectPacket.class))) {
Chat.sendPrivateMessageToSelf("BBDev-sP: "+packetName+": "+rawjson);
}
if (socket.isConnected()&&writer!=null) {
writer.println(packetName + "." +rawjson);
}
else {
Chat.sendPrivateMessageToSelf("BB: Couldn't send a Packet? did you get disconnected?");
}
}
public void onBroadcastMessagePacket(BroadcastMessagePacket packet) {
Chat.sendPrivateMessageToSelf("§6[A]§r ["+packet.prefix+"] "+packet.username+": "+packet.message);
}
public void onSplashNotifyPacket(SplashNotifyPacket packet) {
int waitTime;
if (packet.lessWaste) {
waitTime = (getPotTime() * 1000) / 80;
}
else {
waitTime = 0;
}
String where;
if (packet.hubType.equals(Islands.DUNGEON_HUB)) {
where = "§5DUNGEON HUB§6";
}
else {
where = "Hub";
}
BBsentials.executionService.schedule(() -> {
splashHighlightItem("HUB #" + packet.hub, 20);
String timeLoss = "";
if (packet.lessWaste) {
timeLoss = "§c(" + waitTime + ")";
}
Chat.sendPrivateMessageToSelf("§6" + packet.splasherUsername + " is Splashing in " + where + " #" + packet.hub + " at " + packet.location + ":" + packet.extraMessage + " | " + timeLoss);
}, waitTime, TimeUnit.MILLISECONDS);
}
public void onBingoChatMessagePacket(BingoChatMessagePacket packet) {
if (BBsentials.config.showBingoChat) {
Chat.sendPrivateMessageToSelf("[" + packet.prefix + "] " + packet.username + ": " + packet.message);
}
}
public void onChChestPacket(ChChestPacket packet) {
if (isCommandSafe(packet.bbcommand)) {
String tellrawText = ("{\"text\":\"BB: @username found @item in a chest at (@coords). Click here to get a party invite @extramessage\",\"color\":\"green\",\"clickEvent\":{\"action\":\"run_command\",\"value\":\"@inviteCommand\"},\"hoverEvent\":{\"action\":\"show_text\",\"contents\":[\"On clicking you will get invited to a party. Command executed: @inviteCommand\"]}}");
tellrawText = tellrawText.replace("@username", packet.announcerUsername).replace("@item", Arrays.stream(packet.items).map(ChChestItem::getDisplayName).toList().toString()).replace("@coords", packet.locationCoords).replace("@inviteCommand", packet.bbcommand);
if (!(packet.extraMessage == null || packet.extraMessage.isEmpty())) {
tellrawText = tellrawText.replace("@extramessage", " : " + packet.extraMessage);
}
Chat.sendPrivateMessageToSelfText(Chat.createClientSideTellraw(tellrawText));
}
else {
Chat.sendPrivateMessageToSelf("§cFiltered out a suspicious packet! Please send a screenshot of this message with ping Hype_the_Time (hackthetime) in Bingo Apothecary, BB, SBZ, offical Hypixel,...");
Chat.sendPrivateMessageToSelf(PacketUtils.parsePacketToJson(packet));
}
}
public void onMiningEventPacket(MiningEventPacket packet) {
if (BBsentials.config.toDisplayConfig.getValue("disableAll")) {
//its will returns false cause disabled is checked already before.
Chat.sendPrivateMessageToSelf(packet.username + "There is a " + packet.event.getDisplayName() + "in the " + packet.island.getDisplayName() + " now/soon.");
}
}
public void onWelcomePacket(WelcomeClientPacket packet) {
if (packet.success) {
BBsentials.config.bbsentialsRoles = packet.roles;
Chat.sendPrivateMessageToSelf("§aLogin Success");
if (!packet.motd.isEmpty()) {
Chat.sendPrivateMessageToSelf(packet.motd);
}
}
else {
Chat.sendPrivateMessageToSelf("Login Failed");
}
}
public void onDisconnectPacket(DisconnectPacket packet) {
}
public void onDisplayMessagePacket(DisplayMessagePacket packet) {
Chat.sendPrivateMessageToSelf("§r"+packet.message);
}
public void onDisplayTellrawMessagePacket(DisplayTellrawMessagePacket packet) {
/*Chat.sendPrivateMessageToSelfText(Chat.createClientSideTellraw(packet.rawJson));*/
Chat.sendPrivateMessageToSelf("You received a tellraw Packet but it got ignored due too there being no safety checks in this version.");
}
public void onInternalCommandPacket(InternalCommandPacket packet) {
}
public void onInvalidCommandFeedbackPacket(InvalidCommandFeedbackPacket packet) {
Chat.sendPrivateMessageToSelf(packet.displayMessage);
}
public void onPartyPacket(PartyPacket packet) {
Chat.sendCommand("/p "+packet.type+String.join(" ",packet.users));
}
public void onSystemMessagePacket(SystemMessagePacket packet) {
if (packet.important) {
Chat.sendPrivateMessageToSelf("§c§n"+packet.message);
}else {
Chat.sendPrivateMessageToSelf("§r"+packet.message);
}
if (packet.ping){
playsound(SoundEvents.ENTITY_WARDEN_DIG);
}
}
public static void playsound(SoundEvent event){
MinecraftClient.getInstance().getSoundManager().play(PositionedSoundInstance
.master(event, 1.0F, 1.0F));
}
}
|