fix bot
Some checks failed
/ Build BaseBand Server (push) Has been cancelled

This commit is contained in:
Daniella / Tove 2024-06-10 17:31:19 +02:00
parent 07cc11c13b
commit 9c7fe36608
Signed by: TudbuT
GPG key ID: B3CF345217F202D3
3 changed files with 169 additions and 58 deletions

View file

@ -3,6 +3,8 @@ package com.baseband.bot;
//TIP To <b>Run</b> code, press <shortcut actionId="Run"/> or
// click the <icon src="AllIcons.Actions.Execute"/> icon in the gutter.
import de.tudbut.io.TypedInputStream;
import de.tudbut.io.TypedOutputStream;
import de.tudbut.net.ws.Client;
import de.tudbut.parsing.TCN;
import de.tudbut.tools.Hasher;
@ -19,6 +21,7 @@ import net.dv8tion.jda.api.requests.GatewayIntent;
import net.dv8tion.jda.api.requests.restaction.CommandListUpdateAction;
import java.io.IOException;
import java.net.Socket;
import static de.tudbut.net.http.HTTPContentType.TCN;
import static net.dv8tion.jda.api.interactions.commands.OptionType.STRING;
@ -59,6 +62,13 @@ public class Main extends ListenerAdapter {
.setDefaultPermissions(DefaultMemberPermissions.DISABLED)
);
commands.addCommands(
Commands.slash("password", "Set your password.")
.addOptions(new OptionData(STRING, "password", "Your new password.")
.setRequired(true)).setGuildOnly(true)
.setDefaultPermissions(DefaultMemberPermissions.ENABLED)
);
commands.queue();
}
@ -67,36 +77,48 @@ public class Main extends ListenerAdapter {
public void onSlashCommandInteraction(SlashCommandInteractionEvent event) {
if (event.getGuild() == null)
return;
TypedInputStream tis;
TypedOutputStream tos;
try {
Socket connection = new Socket("baseband.lol", 40001);
tis = new TypedInputStream(connection.getInputStream());
tos = new TypedOutputStream(connection.getOutputStream());
} catch (IOException e) {
throw new RuntimeException(e);
}
switch (event.getName()) {
case "reset" : {
case "reset": {
User user = event.getOption("user").getAsUser();
String password = event.getOption("password").getAsString();
try {
Client client = new Client("127.0.0.1", 40001);
TCN tcn = new TCN();
tcn.set("action", "reset");
tcn.set("authorisation", Hasher.sha512hex(password));
tcn.set("discord-id", user.getIdLong());
client.send(tcn.toString());
tos.writeString(tcn.toString());
switch (client.receive()) {
case "200": {
event.reply("`Reset the HWID of "+user.getName()+".`").queue();
switch (tis.readInt()) {
case 201: {
event.reply("Reset the HWID of **`" + user.getName() + "`**.").queue();
break;
}
case "403": {
event.reply("`Incorrect Admin Password.`").setEphemeral(true).queue();
}
case "503": {
event.reply("`Error! TCN Exception.`").setEphemeral(true).queue();
case 403: {
event.reply("Incorrect Admin Password.").setEphemeral(true).queue();
break;
}
case "404": {
event.reply("`User Not Found, Do they have BaseBand?`").setEphemeral(true).queue();
case 400: {
event.reply("Error! I sent malformed data somehow.").setEphemeral(true).queue();
break;
}
case 404: {
event.reply("User Not Found, Do they have BaseBand?").setEphemeral(true).queue();
break;
}
@ -111,35 +133,36 @@ public class Main extends ListenerAdapter {
break;
}
case "add" : {
case "add": {
User user = event.getOption("user").getAsUser();
String password = event.getOption("password").getAsString();
String username = event.getOption("username").getAsString();
try {
Client client = new Client("127.0.0.1", 40001);
TCN tcn = new TCN();
tcn.set("action", "create");
tcn.set("authorisation", Hasher.sha512hex(password));
tcn.set("discord-id", user.getIdLong());
client.send(tcn.toString());
tos.writeString(tcn.toString());
switch (client.receive()) {
case "200": {
event.reply("`Created user with username "+username+". ("+ user.getName() +")`").queue();
switch (tis.readInt()) {
case 201: {
event.reply("Created user with username **`" + username + "`**. (`" + user.getName() + "`)").queue();
break;
}
case "403": {
event.reply("`Incorrect Admin Password.`").setEphemeral(true).queue();
case 403: {
event.reply("Incorrect Admin Password.").setEphemeral(true).queue();
break;
}
case "400": {
event.reply("`User already exists!`").setEphemeral(true).queue();
case 409: {
event.reply("User already exists!").setEphemeral(true).queue();
break;
}
case "503": {
event.reply("`Error! TCN Exception.`").setEphemeral(true).queue();
case 400: {
event.reply("Error! I sent malformed data somehow.").setEphemeral(true).queue();
break;
}
@ -151,6 +174,52 @@ public class Main extends ListenerAdapter {
e.printStackTrace();
event.reply("Exception occurred pushing data to server.").setEphemeral(true).queue();
}
break;
}
case "password": {
User user = event.getUser();
String password = event.getOption("password").getAsString();
try {
TCN tcn = new TCN();
tcn.set("action", "password");
tcn.set("authorisation", "1ee7a143df6e0d2f3d2b86b3e5c098c06a07d7c8eb01d629f51712b6bba3a468dc96ef9729d586007ca71383b1c203f6f996bdce3972772d0e5351364eba0d1e");
tcn.set("password", password);
tcn.set("discord-id", user.getIdLong());
tos.writeString(tcn.toString());
switch (tis.readInt()) {
case 201: {
event.reply("Your password was changed.").queue();
break;
}
case 403: {
event.reply("Error! __Please ask a developer to update my communication key.__");
break;
}
case 404: {
event.reply("You do not have BaseBand.").setEphemeral(true).queue();
break;
}
case 400: {
event.reply("Error! I sent malformed data somehow.").setEphemeral(true).queue();
break;
}
default: {
break;
}
}
} catch (IOException e) {
e.printStackTrace();
event.reply("Exception occurred pushing data to server.").setEphemeral(true).queue();
}
break;
}
default:
event.reply("I can't handle that command right now :(").setEphemeral(true).queue();

View file

@ -1,34 +1,49 @@
package com.baseband.server;
import at.favre.lib.crypto.bcrypt.BCrypt;
import de.tudbut.net.ws.Connection;
import de.tudbut.net.ws.ConnectionHandler;
import de.tudbut.io.TypedInputStream;
import de.tudbut.io.TypedOutputStream;
import de.tudbut.parsing.TCN;
import de.tudbut.tools.Hasher;
import de.tudbut.tools.StringTools;
import de.tudbut.tools.encryption.Key;
import java.io.IOException;
import java.net.Socket;
import java.security.SecureRandom;
public class BotServiceHandler implements ConnectionHandler {
public class BotServiceHandler {
@Override
public void run(Connection connection) throws IOException {
public static void handle(Socket connection) throws IOException {
TypedInputStream tis = new TypedInputStream(connection.getInputStream());
TypedOutputStream tos = new TypedOutputStream(connection.getOutputStream());
try {
TCN tcn = TCN.read(connection.receive());
TCN tcn = TCN.read(tis.readString());
String password = "1ee7a143df6e0d2f3d2b86b3e5c098c06a07d7c8eb01d629f51712b6bba3a468dc96ef9729d586007ca71383b1c203f6f996bdce3972772d0e5351364eba0d1e";
if(!tcn.getString("authorisation").equals(password)) {
connection.send("403");
tos.writeInt(403);
return;
}
String action = tcn.getString("action");
switch (action) {
case "reset" : {
case "password": {
TCN user = (TCN) UserHandler.users.stream()
.filter(
a -> ((TCN) a).getLong("discord-id").equals(tcn.getLong("discord-id"))
).findFirst().orElse(null);
if(user != null) {
user.set("password", BCrypt.withDefaults().hashToString(4, tcn.getString("password").toCharArray()));
tos.writeInt(201);
} else {
tos.writeInt(404);
}
break;
}
case "reset": {
TCN user = (TCN) UserHandler.users.stream()
.filter(
a -> ((TCN) a).getLong("discord-id").equals(tcn.getLong("discord-id"))
@ -36,14 +51,14 @@ public class BotServiceHandler implements ConnectionHandler {
if(user != null) {
user.set("hardware-id-reset", true);
connection.send("200");
tos.writeInt(201);
} else {
connection.send("404");
tos.writeInt(404);
}
break;
}
case "add" : {
case "add": {
long discord_id = tcn.getLong("discord-id");
String username = tcn.getString("username");
@ -64,17 +79,17 @@ public class BotServiceHandler implements ConnectionHandler {
if(user == null) {
UserHandler.users.add(userToAdd);
connection.send("200");
tos.writeInt(201);
} else {
connection.send("400");
tos.writeInt(409);
}
break;
}
}
Main.save();
} catch (TCN.TCNException e) {
connection.send("503");
tos.writeInt(400);
}
}
}

View file

@ -14,11 +14,11 @@ import de.tudbut.parsing.JSON;
import de.tudbut.parsing.TCN;
import de.tudbut.parsing.TCNArray;
import de.tudbut.tools.Hasher;
import de.tudbut.net.ws.Server;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.file.Files;
@ -29,12 +29,28 @@ import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
public class Main {
private static final File dbTmp = new File("baseband.db.tmp");
private static final File db = new File("baseband.db");
public static RSAKey rsaKey;
public static boolean denyAll = false;
public static Map<String, byte[]> classes;
public static void save() {
try {
String s = JSON.writeReadable(UserHandler.users.toTCN(), 4);
try (FileOutputStream fileOutputStream = new FileOutputStream(dbTmp)) {
fileOutputStream.write(s.getBytes());
fileOutputStream.flush();
}
dbTmp.renameTo(db);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws Exception {
File key = new File("baseband.key");
if(key.exists()) {
@ -54,7 +70,6 @@ public class Main {
stream.getStream().close();
}
//Loader
File db = new File("baseband.db");
if(db.exists()) {
UserHandler.users = TCNArray.fromTCN(JSON.read(new StreamReader(Files.newInputStream(db.toPath())).readAllAsString()));
} else {
@ -72,16 +87,7 @@ public class Main {
}
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();
}
}));
Runtime.getRuntime().addShutdownHook(new Thread(Main::save));
System.out.println("Indexing Jar...");
@ -97,9 +103,14 @@ public class Main {
System.out.println("Enabling Service Handler for Remote Actioning...");
Server botServiceServer = new Server(40001);
botServiceServer.addHandler(new BotServiceHandler());
botServiceServer.run();
new Thread(() -> {
try {
remoteActionHandler();
} catch (IOException e) {
throw new RuntimeException(e);
}
}).start();
System.out.println("Listening!");
ServerSocket server = new ServerSocket(40000);
@ -120,5 +131,21 @@ public class Main {
}
private static void remoteActionHandler() throws IOException {
ServerSocket server = new ServerSocket(40001);
Socket client;
while ((client = server.accept()) != null) {
client.setSoTimeout(1000);
Socket thisClient = client;
new Thread(() -> {
try {
BotServiceHandler.handle(thisClient);
} catch (Exception e) {
e.printStackTrace();
}
}).start();
}
}
}