Skip to content

Commit

Permalink
adjust magic link stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
Nathan Martin committed Jul 25, 2024
1 parent 82a645f commit b51d9cf
Show file tree
Hide file tree
Showing 12 changed files with 68 additions and 169 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,6 @@
import java.util.concurrent.Callable;

public class AESCryptor {
private static SecretKey DEFAULT_AES_KEY;
static {
try {
KeyGenerator keyGen = KeyGenerator.getInstance("AES");
keyGen.init(256);
DEFAULT_AES_KEY = keyGen.generateKey();
} catch (Exception e) {
throw new RuntimeException(e);
}
}

private final SecretKey key;

public AESCryptor(SecretKey key) {
Expand All @@ -46,12 +35,15 @@ public String decrypt(String encryptedData) throws Exception {
}


public static byte[] createKey() throws NoSuchAlgorithmException {
KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
keyGenerator.init(256);
SecretKey secretKey = keyGenerator.generateKey();
public static byte[] createKey() {
try {
KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
keyGenerator.init(256);
SecretKey secretKey = keyGenerator.generateKey();

return secretKey.getEncoded();
return secretKey.getEncoded();
} catch (Exception ignore) {}
return "".getBytes();
}

public static AESCryptor from(byte[] key) {
Expand All @@ -63,5 +55,5 @@ public static AESCryptor from(byte[] key) {
/**
* The cryptor returned here is effectively worthless because there's no way to retrieve the AES key used.
*/
public static AESCryptor DEFAULT_CRYPTOR = new AESCryptor(DEFAULT_AES_KEY);
public static AESCryptor DEFAULT_CRYPTOR = AESCryptor.from(AESCryptor.createKey());
}

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package group.aelysium.rustyconnector.mc_loader.events.magic_link;
package group.aelysium.rustyconnector.mc_loader.events;

import group.aelysium.rustyconnector.common.events.Event;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package group.aelysium.rustyconnector.mc_loader.events.magic_link;
package group.aelysium.rustyconnector.mc_loader.events;

import group.aelysium.rustyconnector.common.events.Event;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package group.aelysium.rustyconnector.mc_loader.events.magic_link;
package group.aelysium.rustyconnector.mc_loader.events;

import group.aelysium.rustyconnector.common.events.Event;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,23 @@
import group.aelysium.rustyconnector.common.absolute_redundancy.Particle;
import group.aelysium.rustyconnector.common.cache.MessageCache;
import group.aelysium.rustyconnector.common.magic_link.MagicLinkCore;
import group.aelysium.rustyconnector.common.magic_link.packet.PacketListener;
import group.aelysium.rustyconnector.common.magic_link.packet.PacketParameter;
import group.aelysium.rustyconnector.mc_loader.ServerFlame;
import group.aelysium.rustyconnector.mc_loader.events.magic_link.DisconnectedEvent;
import group.aelysium.rustyconnector.mc_loader.events.DisconnectedEvent;
import group.aelysium.rustyconnector.common.crypt.AESCryptor;
import group.aelysium.rustyconnector.common.magic_link.packet.Packet;
import group.aelysium.rustyconnector.mc_loader.magic_link.handlers.HandshakeFailureListener;
import group.aelysium.rustyconnector.mc_loader.magic_link.handlers.HandshakeStalePingListener;
import group.aelysium.rustyconnector.mc_loader.magic_link.handlers.HandshakeSuccessListener;
import group.aelysium.rustyconnector.proxy.magic_link.packet_handlers.HandshakeDisconnectListener;
import group.aelysium.rustyconnector.proxy.magic_link.packet_handlers.HandshakePingListener;
import org.jetbrains.annotations.NotNull;

import java.util.HashMap;
import java.util.List;
import java.util.UUID;
import java.util.Vector;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -91,38 +99,54 @@ public void close() throws Exception {

public static class Tinder extends Particle.Tinder<MagicLink> {
private final AESCryptor cryptor;
private final MessageCache cache;
private final Packet.Target self;
private final MessageCache cache;
private final String magicConfig;
private final List<PacketListener<? extends Packet>> listeners = new Vector<>();
public Tinder(
@NotNull AESCryptor cryptor,
@NotNull MessageCache cache,
@NotNull Packet.Target self,
@NotNull MessageCache cache,
@NotNull String magicConfig
) {
this.cryptor = cryptor;
this.cache = cache;
this.self = self;
this.cache = cache;
this.magicConfig = magicConfig;
}

public Tinder on(PacketListener<? extends Packet> listener) {
this.listeners.add(listener);
return this;
}

@Override
public @NotNull MagicLink ignite() throws Exception {
return new MagicLink(
MagicLink magicLink = new MagicLink(
this.cryptor,
this.cache,
this.self,
this.magicConfig
);

this.listeners.forEach(magicLink::on);

return magicLink;
}

public static Tinder DEFAULT_CONFIGURATION(UUID serverUUID) {
return new Tinder(
Tinder tinder = new Tinder(
AESCryptor.DEFAULT_CRYPTOR,
new MessageCache(50),
Packet.Target.server(serverUUID),
new MessageCache(50),
"default"
);

tinder.on(new HandshakeFailureListener());
tinder.on(new HandshakeStalePingListener());
tinder.on(new HandshakeSuccessListener());

return tinder;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import group.aelysium.rustyconnector.common.magic_link.packet.PacketListener;
import group.aelysium.rustyconnector.common.magic_link.packet.PacketParameter;
import group.aelysium.rustyconnector.mc_loader.ServerFlame;
import group.aelysium.rustyconnector.mc_loader.events.magic_link.TimeoutEvent;
import group.aelysium.rustyconnector.mc_loader.events.TimeoutEvent;
import group.aelysium.rustyconnector.RC;

public class HandshakeStalePingListener extends PacketListener<MagicLinkCore.Packets.StalePing> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import group.aelysium.rustyconnector.common.magic_link.MagicLinkCore;
import group.aelysium.rustyconnector.common.magic_link.packet.Packet;
import group.aelysium.rustyconnector.common.magic_link.packet.PacketListener;
import group.aelysium.rustyconnector.mc_loader.events.magic_link.ConnectedEvent;
import group.aelysium.rustyconnector.mc_loader.events.ConnectedEvent;
import net.kyori.adventure.text.Component;

public class HandshakeSuccessListener extends PacketListener<MagicLinkCore.Packets.Handshake.Success> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@
import group.aelysium.rustyconnector.common.absolute_redundancy.Particle;
import group.aelysium.rustyconnector.common.magic_link.MagicLinkCore;
import group.aelysium.rustyconnector.common.magic_link.packet.Packet;
import group.aelysium.rustyconnector.common.magic_link.packet.PacketIdentification;
import group.aelysium.rustyconnector.common.magic_link.packet.PacketListener;
import group.aelysium.rustyconnector.proxy.family.Family;
import group.aelysium.rustyconnector.proxy.magic_link.packet_handlers.HandshakeDisconnectListener;
import group.aelysium.rustyconnector.proxy.magic_link.packet_handlers.HandshakePingListener;
import org.jetbrains.annotations.NotNull;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -88,6 +90,7 @@ public static class Tinder extends Particle.Tinder<MagicLink> {
private final Packet.Target self;
private final MessageCache cache;
private final Map<String, MagicLinkServerSettings> magicConfigs;
private final List<PacketListener<? extends Packet>> listeners = new Vector<>();
public Tinder(
@NotNull AESCryptor cryptor,
@NotNull Packet.Target self,
Expand All @@ -100,23 +103,37 @@ public Tinder(
this.magicConfigs = magicConfigs;
}

public Tinder on(PacketListener<? extends Packet> listener) {
this.listeners.add(listener);
return this;
}

@Override
public @NotNull MagicLink ignite() throws Exception {
return new MagicLink(
MagicLink magicLink = new MagicLink(
this.cryptor,
this.cache,
this.self,
this.magicConfigs
);

this.listeners.forEach(magicLink::on);

return magicLink;
}

public static Tinder DEFAULT_CONFIGURATION(UUID proxyUUID) {
return new Tinder(
Tinder tinder = new Tinder(
AESCryptor.DEFAULT_CRYPTOR,
Packet.Target.proxy(proxyUUID),
new MessageCache(50),
new HashMap<>()
);

tinder.on(new HandshakePingListener());
tinder.on(new HandshakeDisconnectListener());

return tinder;
}
}
}

0 comments on commit b51d9cf

Please sign in to comment.