add more useless code which doesn't work at all

This commit is contained in:
Chayapak 2023-06-13 21:22:33 +07:00
parent 7f84f94c24
commit ca830ed9bf
3 changed files with 1212 additions and 5 deletions

View file

@ -5,8 +5,17 @@ import com.github.steveice10.mc.protocol.packet.ingame.clientbound.ClientboundLo
import com.github.steveice10.mc.protocol.packet.ingame.serverbound.ServerboundCustomPayloadPacket;
import com.github.steveice10.packetlib.Session;
import com.github.steveice10.packetlib.packet.Packet;
import io.netty.buffer.Unpooled;
import land.chipmunk.chayapak.chomens_bot.Bot;
import land.chipmunk.chayapak.chomens_bot.util.AES;
import land.chipmunk.chayapak.chomens_bot.util.FriendlyByteBuf;
import lombok.AllArgsConstructor;
import lombok.Getter;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketAddress;
import java.net.SocketException;
import java.util.Arrays;
// exists for some reason, still wip and will be finished in the next 69 years
@ -14,10 +23,20 @@ import java.util.Arrays;
// at least when you do `/voicechat test (bot username)` it will show `Client not connected`
// instead of `(bot username) does not have Simple Voice Chat installed`
public class VoiceChatPlugin extends Bot.Listener {
public static final byte MAGIC_BYTE = (byte) 0b11111111;
private final Bot bot;
private final ClientVoiceChatSocket socket;
public VoiceChatPlugin(Bot bot) {
this.bot = bot;
this.socket = new ClientVoiceChatSocket();
try {
socket.open();
} catch (Exception e) {
e.printStackTrace();
}
bot.addListener(this);
}
@ -41,12 +60,89 @@ public class VoiceChatPlugin extends Bot.Listener {
));
}
public void packetReceived(ClientboundCustomPayloadPacket packet) {
public void packetReceived(ClientboundCustomPayloadPacket _packet) {
// sus
/*
System.out.println(packet.getChannel());
System.out.println(Arrays.toString(packet.getData()));
System.out.println(new String(packet.getData()));
*/
System.out.println(_packet.getChannel());
System.out.println(Arrays.toString(_packet.getData()));
System.out.println(new String(_packet.getData()));
*/
// for some reason this entire part is not running
if (_packet.getChannel().equals("voicechat:secret")) {
bot.executorService().submit(() -> {
while (true) {
final RawUdpPacket packet = socket.read();
if (packet == null) return;
byte[] data = packet.data();
FriendlyByteBuf buf = new FriendlyByteBuf(Unpooled.wrappedBuffer(data));
if (buf.readByte() != MAGIC_BYTE) return;
// AES.decrypt(secret, payload) ?
}
});
}
}
private static class VoiceChatSocketBase {
private final byte[] BUFFER = new byte[4096];
public RawUdpPacket read (DatagramSocket socket) {
try {
DatagramPacket packet = new DatagramPacket(BUFFER, BUFFER.length);
socket.receive(packet);
// Setting the timestamp after receiving the packet
long timestamp = System.currentTimeMillis();
byte[] data = new byte[packet.getLength()];
System.arraycopy(packet.getData(), packet.getOffset(), data, 0, packet.getLength());
return new RawUdpPacket(data, packet.getSocketAddress(), timestamp);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
private static class ClientVoiceChatSocket extends VoiceChatSocketBase {
private DatagramSocket socket;
public void open() throws SocketException {
this.socket = new DatagramSocket();
}
public RawUdpPacket read() {
if (socket == null) {
throw new IllegalStateException("Socket not opened yet");
}
return read(socket);
}
public void send(byte[] data, SocketAddress address) throws Exception {
if (socket == null) {
return; // Ignoring packet sending when socket isn't open yet
}
socket.send(new DatagramPacket(data, data.length, address));
}
public void close() {
if (socket != null) {
socket.close();
}
}
public boolean isClosed() {
return socket == null;
}
}
@AllArgsConstructor
private static class RawUdpPacket {
@Getter private final byte[] data;
@Getter private final SocketAddress socketAddress;
@Getter private final long timestamp;
}
}

View file

@ -0,0 +1,68 @@
package land.chipmunk.chayapak.chomens_bot.util;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.nio.ByteBuffer;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.UUID;
public class AES {
private static final SecureRandom RANDOM = new SecureRandom();
private static final String CIPHER = "AES/CBC/PKCS5Padding";
public static byte[] getBytesFromUUID(UUID uuid) {
ByteBuffer buffer = ByteBuffer.wrap(new byte[16]);
buffer.putLong(uuid.getMostSignificantBits());
buffer.putLong(uuid.getLeastSignificantBits());
return buffer.array();
}
public static UUID getUUIDFromBytes(byte[] bytes) {
ByteBuffer byteBuffer = ByteBuffer.wrap(bytes);
long most = byteBuffer.getLong();
long least = byteBuffer.getLong();
return new UUID(most, least);
}
private static byte[] generateIV() {
byte[] iv = new byte[16];
RANDOM.nextBytes(iv);
return iv;
}
private static SecretKeySpec createKeySpec(UUID secret) {
return new SecretKeySpec(getBytesFromUUID(secret), "AES");
}
public static byte[] encrypt(UUID secret, byte[] data) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
byte[] iv = generateIV();
IvParameterSpec ivSpec = new IvParameterSpec(iv);
Cipher cipher = Cipher.getInstance(CIPHER);
cipher.init(Cipher.ENCRYPT_MODE, createKeySpec(secret), ivSpec);
byte[] enc = cipher.doFinal(data);
byte[] payload = new byte[iv.length + enc.length];
System.arraycopy(iv, 0, payload, 0, iv.length);
System.arraycopy(enc, 0, payload, iv.length, enc.length);
return payload;
}
public static byte[] decrypt(UUID secret, byte[] payload) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
byte[] iv = new byte[16];
System.arraycopy(payload, 0, iv, 0, iv.length);
byte[] data = new byte[payload.length - iv.length];
System.arraycopy(payload, iv.length, data, 0, data.length);
IvParameterSpec ivSpec = new IvParameterSpec(iv);
Cipher cipher = Cipher.getInstance(CIPHER);
cipher.init(Cipher.DECRYPT_MODE, createKeySpec(secret), ivSpec);
return cipher.doFinal(data);
}
}

File diff suppressed because it is too large Load diff