the big serverside update
All checks were successful
/ Build BaseBand DSM & Broadway (push) Successful in 8m25s

This commit is contained in:
Jess H 2024-06-08 19:03:07 +01:00
parent 8fe0752a5b
commit 38d1f25234
7 changed files with 151 additions and 7 deletions

Binary file not shown.

BIN
Server/lib/bytes-1.6.1.jar Normal file

Binary file not shown.

View file

@ -0,0 +1,19 @@
package dev.baseband.server;
import java.util.Arrays;
import java.util.Objects;
import java.util.stream.StreamSupport;
public class CheckNull {
public static boolean isNull(Object... objects) {
return Arrays.stream(objects).anyMatch(Objects::isNull);
}
public static boolean isNull(Iterable<?>... iterables) {
return Arrays.stream(iterables)
.anyMatch(iterable -> iterable == null ||
StreamSupport.stream(iterable.spliterator(), false)
.anyMatch(o -> (o instanceof Iterable<?>) ? isNull((Iterable<?>) o) : isNull(o)
));
}
}

View file

@ -18,10 +18,21 @@ public class LoaderHandler implements ConnectionHandler {
Key key = new Key(Main.rsaKey.rsaDec(connection.receive()));
TCN userData = key.decryptObject(connection.receive());
Object userData = key.decryptObject(connection.receive());
if(!(userData instanceof TCN)) {
connection.send(String.valueOf(UserHandler.RESPONSE.SERVER_ERROR.getValue()));
return;
}
int response = UserHandler.isValid((TCN) userData);
connection.send(String.valueOf(response));
if(response == 0) {
Map<String, byte[]> classes = new HashMap<>();
connection.send(key.encryptObject(classes));
}
}
}

View file

@ -6,22 +6,61 @@
package dev.baseband.server;
import at.favre.lib.crypto.bcrypt.BCrypt;
import de.tudbut.io.StreamReader;
import de.tudbut.net.ws.Server;
import de.tudbut.parsing.JSON;
import de.tudbut.parsing.TCN;
import de.tudbut.parsing.TCNArray;
import de.tudbut.tools.Hasher;
import java.io.File;
import java.io.FileOutputStream;
import java.nio.file.Files;
public class Main {
public static final RSAKey rsaKey = new RSAKey();
public static boolean denyAll = false;
public static void main(String[] args) throws Exception {
//Loader
File db = new File("baseband.db");
if(db.exists()) {
UserHandler.users = TCNArray.fromTCN(JSON.read(new StreamReader(Files.newInputStream(db.toPath())).readAllAsString()));
} else {
db.createNewFile();
TCN tcn = new TCN();
tcn.set("username", "root");
tcn.set("password", BCrypt.withDefaults().hashToString(4, "test".toCharArray()));
tcn.set("hardware-id", Hasher.sha512hex("hardware-id"));
tcn.set("hardware-id-reset", false);
tcn.set("disabled", false);
UserHandler.users.add(tcn);
}
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
try {
try (FileOutputStream fileOutputStream = new FileOutputStream(db)) {
fileOutputStream.write(JSON.writeReadable(UserHandler.users.toTCN(), 4).getBytes());
fileOutputStream.flush();
}
} catch (Exception e) {
e.printStackTrace();
}
}));
Server loaderServer = new Server(40000);
loaderServer.addHandler(new LoaderHandler());
loaderServer.run();
//WebServices
Server webServiceServer = new Server(40001);
webServiceServer.addHandler(new WebServiceHandler());
webServiceServer.run();
//Server webServiceServer = new Server(40001);
//webServiceServer.addHandler(new WebServiceHandler());
//webServiceServer.run();
}

View file

@ -1,5 +1,80 @@
package dev.baseband.server;
import at.favre.lib.crypto.bcrypt.BCrypt;
import de.tudbut.parsing.TCN;
import de.tudbut.parsing.TCNArray;
public class UserHandler {
public static TCNArray users = new TCNArray();
public static int isValid(TCN remoteTCN) {
if(CheckNull.isNull(remoteTCN.get("username"))) {
return RESPONSE.FORBIDDEN.getValue();
}
try {
TCN localTCN = (TCN) users.stream()
.filter(user -> ((TCN) user).getString("username").equals(remoteTCN.getString("username")))
.findFirst().orElse(null);
if (localTCN != null) {
String localPassword = localTCN.getString("password");
String localHardwareID = localTCN.getString("hardware-id");
boolean isReset = localTCN.getBoolean("hardware-id-reset");
boolean isDisabled = localTCN.getBoolean("disabled");
if(isDisabled) { //if they're banned we're not even looking at their TCN
return RESPONSE.BANNED.getValue();
}
String remotePassword = remoteTCN.getString("password");
String remoteHardwareID = remoteTCN.getString("hardware-id");
if(CheckNull.isNull(remotePassword, remoteHardwareID)) {
return RESPONSE.SERVER_ERROR.getValue();
}
if (isReset) {
localTCN.set("hardware-id-reset", false);
localTCN.set("hardware-id", remoteHardwareID);
return RESPONSE.RESET.getValue();
}
if(!localHardwareID.equals(remoteTCN.getString("hardware-id"))) {
return RESPONSE.HWID_INVALID.getValue();
}
if(BCrypt.verifyer().verify(remotePassword.toCharArray(), localPassword.toCharArray()).verified) {
return RESPONSE.OK.getValue();
}
}
//LOL
return RESPONSE.FORBIDDEN.getValue();
} catch(Exception e) {
return RESPONSE.SERVER_ERROR.getValue();
}
}
public enum RESPONSE {
OK(0),
FORBIDDEN(1),
OUTDATED(2),
BANNED(3),
RESET(4),
SERVER_ERROR(5),
SERVER_DOWN(6),
HWID_INVALID(7),
LOGIN_LOCKOUT(8);
private final int value;
RESPONSE(final int newValue) {
value = newValue;
}
public int getValue() { return value; }
}
}

View file

@ -1,4 +1,4 @@
/*
;/*
* Copyright (c) 2023 Jess H & Daniella H. All Rights Reserved.
* Unauthorized copying of this file via any medium is Strictly Prohibited.
*/