aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/de/hype/bbsentials/communication/EncryptedBBsentialsConnection.java
blob: d8dfb1324e9bcb125edce2e9a67c90dc7d8a077a (plain)
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
package de.hype.bbsentials.communication;

import de.hype.bbsentials.chat.Chat;
import de.hype.bbsentials.client.BBsentials;
import net.minecraft.client.MinecraftClient;
import net.minecraft.entity.effect.StatusEffectInstance;
import net.minecraft.entity.effect.StatusEffects;
import net.minecraft.text.Text;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.SocketException;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
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 ScheduledExecutorService executorService;
    private String itemName = "Hub #0";
    private boolean highlightItem = false;

    public interface MessageReceivedCallback {
        void onMessageReceived(String message);
    }

    public void connect(String serverIP, int serverPort) {
        try {
            // Erstellen Sie eine SSL-Verbindung
            SSLContext sslContext = SSLContext.getInstance("TLS");

            // Erstellen Sie einen SSL-Socket-Factory und einen SSL-Socket
            SSLSocketFactory sslSocketFactory;
            if (System.getProperty("os.name").toLowerCase().contains("win")) {
                // Windows-Betriebssystem
                sslSocketFactory = (SSLSocketFactory) SSLSocketFactory.getDefault();
            } else {
                // Linux und andere Betriebssysteme
                TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
                tmf.init((KeyStore) null); // Lade die Standard-Rot-Zertifikate
                TrustManager[] trustManagers = tmf.getTrustManagers();

                sslContext.init(null, trustManagers, new SecureRandom());
                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<>();

            executorService = new ScheduledThreadPoolExecutor(2); // Adjust the pool size as needed

            // Start message receiver thread
            Thread messageReceiverThread = new Thread(() -> {
                try {
                    while (true) {
                        String message = reader.readLine();
                        if (message != null) {
                            if (messageReceivedCallback != null) {
                                messageReceivedCallback.onMessageReceived(message);
                            }
                        }
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            });
            messageReceiverThread.start();

            // 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();

            // Example: Sending a message to the server
            // sendMessage("Hello from client!");
            // More logic here...

        } catch (IOException | NoSuchAlgorithmException | KeyManagementException e) {
            e.printStackTrace();
        }
    }

    public void setMessageReceivedCallback(MessageReceivedCallback callback) {
        this.messageReceivedCallback = callback;
    }

    public void sendMessage(String message) {
        messageQueue.offer(message);
    }

    public void sendHiddenMessage(String message) {
        if (BBsentials.getConfig().isDetailedDevModeEnabled()){
            Chat.sendPrivateMessageToSelf("BBDev-s: "+message);
        }
        writer.println(message);
    }

    public void onMessageReceived(String message) {
        if (message.startsWith("H-")) {
            if (message.equals("H-BB-Login: ")) {
                sendHiddenMessage(MinecraftClient.getInstance().player.getUuid().toString());
                sendHiddenMessage(BBsentials.getConfig().getApiKey());
                sendHiddenMessage("?getperms");
            }
            else if (message.contains("H-PotDurations?")) {
                System.out.println("worked");
                int remainingTimeInMinutes = 0;
                StatusEffectInstance potTimeRequest = MinecraftClient.getInstance().player.getStatusEffect(StatusEffects.JUMP_BOOST);
                if (potTimeRequest != null) {
                    Chat.sendPrivateMessageToSelf(String.valueOf(potTimeRequest.getAmplifier()));
                    if (true) {
                        remainingTimeInMinutes = (int) (potTimeRequest.getDuration() / 20.0);
                    }
                }
                sendHiddenMessage("?potduration " + remainingTimeInMinutes);
            }
            else if (message.startsWith("H-?splash")) {
                String[] arguments = message.split(" ", 4);
                Chat.sendPrivateMessageToSelf(arguments[2] + "is splashing in Hub #" + arguments[1] + " soon.");
                splashHighlightItem("Hub #" + arguments[1], 30000);
            }
            else if (message.startsWith("H-Roles")) {
                BBsentials.getConfig().bbsentialsRoles = message.replace("H-Roles ", "");
                BBsentials.refreshCommands();
            }
            else if (message.startsWith("H-chchest")) {
                String[] arguments = message.replace("H-chchest","").trim().split(" ", 5); // Split with limit of 5
                String item = arguments[0];
                int x = Integer.parseInt(arguments[1]);
                int y = Integer.parseInt(arguments[2]);
                int z = Integer.parseInt(arguments[3]);
                String inviteCommand = arguments[4];
                Chat.createClientSideTellraw("");
            }
            if (BBsentials.getConfig().isDetailedDevModeEnabled()) {
                Chat.sendPrivateMessageToSelf("BBDev-r: " + message);
            }
        }
        else {
            Chat.sendPrivateMessageToSelf("§aBB: " + message);
        }
    }

    public void splashHighlightItem(String itemName, long displayTimeInMilliseconds) {
        this.itemName = itemName;
        highlightItem = true;
        executorService.schedule(() -> {
            highlightItem = false;
            try {
                socket.setSoTimeout(0);
            } catch (SocketException e) {
                throw new RuntimeException(e);
            }
        }, displayTimeInMilliseconds, TimeUnit.MILLISECONDS);
    }

    public void sleep(int time) {
        try {
            Thread.sleep(time);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }

    public String getItemName() {
        return itemName;
    }

    public boolean highlightItem() {
        return highlightItem;
    }

    //TODO socket verschlüsseln
    //TODO dyndns eintrag?
    //TODO search
    //[11:17:21] [Render thread/INFO] (Minecraft) [STDOUT]: {AttributeModifiers:[],display:{Lore:['{"italic":false,"extra":[{"color":"gray","text":"Players: 5/80"}],"text":""}','{"italic":false,"extra":[{"color":"dark_gray","text":"Server: mega38A"}],"text":""}','{"italic":false,"text":""}','{"italic":false,"extra":[{"color":"yellow","text":"Click to connect!"}],"text":""}'],Name:'{"text":"§r§6Splash Hub"}'},overrideMeta:1b}
}