BaseBandRewrite/Server/src/main/java/de/com/baseband/server/LoaderHandler.java
TudbuT 38d29a8efd
Some checks failed
/ Build BaseBand DSM & Broadway (push) Successful in 2m1s
/ Build BaseBand Loader (push) Failing after 1m22s
/ Build BaseBand Server (push) Successful in 1m56s
add web services and update notifications
2024-06-13 13:39:43 +02:00

65 lines
2.6 KiB
Java

package de.com.baseband.server;
import de.tudbut.io.TypedInputStream;
import de.tudbut.io.TypedOutputStream;
import de.tudbut.obj.DoubleTypedObject;
import de.tudbut.parsing.TCN;
import de.tudbut.tools.Tools;
import de.tudbut.tools.encryption.Key;
import de.tudbut.tools.encryption.RawKey;
import java.io.IOException;
import java.net.Socket;
import java.util.Map;
public class LoaderHandler {
public static void handle(Socket connection) throws IOException {
TypedInputStream inputStream = new TypedInputStream(connection.getInputStream());
TypedOutputStream outputStream = new TypedOutputStream(connection.getOutputStream());
outputStream.writeString(Main.rsaKey.encodePublicKeyToBase64());
Key key = new Key(Main.rsaKey.rsaDec(inputStream.readString()));
TCN userData = TCN.readMap(Tools.stringToMap(key.decryptString(inputStream.readString())));
DoubleTypedObject<Integer, TCN> response = UserHandler.isValid(userData);
outputStream.writeInt(response.o);
if(response.o == UserHandler.Response.OUTDATED.ordinal()) {
sendClasses(Main.classesLoader, key, outputStream);
}
if(response.o == UserHandler.Response.OK.ordinal()) {
TCN data = new TCN();
data.set("release-branch", "release".equals(response.t.getString("branch")));
data.set("branch", response.t.getString("branch"));
data.set("username", userData.getString("username"));
outputStream.writeString(key.encryptString(Tools.mapToString(data.toMap())));
Map<String, byte[]> classes = null;
if("main".equals(response.t.getString("branch")))
classes = Main.classesDebug;
if("release".equals(response.t.getString("branch")))
classes = Main.classes;
if(classes != null) {
sendClasses(classes, key, outputStream);
}
}
connection.close();
}
private static void sendClasses(Map<String, byte[]> classes, Key key, TypedOutputStream outputStream) throws IOException {
RawKey rk = new RawKey(key.toBytes());
outputStream.writeInt(classes.size());
for (Map.Entry<String, byte[]> entry : classes.entrySet()) {
try {
outputStream.writeString(rk.encryptString(entry.getKey()));
outputStream.writeByteArray(rk.encryptBytes(entry.getValue()));
} catch (Exception e) {
System.err.println("while " + entry.getKey());
e.printStackTrace();
throw e;
}
}
}
}