Clean up
This commit is contained in:
parent
dd60b2c1ab
commit
0618777b85
|
|
@ -1,21 +1,13 @@
|
|||
package org.adde0109.ambassador;
|
||||
|
||||
import com.google.common.io.ByteArrayDataInput;
|
||||
import com.google.inject.Inject;
|
||||
import com.velocitypowered.api.event.Continuation;
|
||||
import com.velocitypowered.api.event.Subscribe;
|
||||
import com.velocitypowered.api.event.connection.PreLoginEvent;
|
||||
import com.velocitypowered.api.event.player.ServerLoginPluginMessageEvent;
|
||||
import com.velocitypowered.api.event.proxy.ProxyInitializeEvent;
|
||||
import com.velocitypowered.api.plugin.Plugin;
|
||||
import com.velocitypowered.api.plugin.annotation.DataDirectory;
|
||||
import com.velocitypowered.api.proxy.LoginPhaseConnection;
|
||||
import com.velocitypowered.api.proxy.ProxyServer;
|
||||
import com.velocitypowered.api.proxy.server.RegisteredServer;
|
||||
|
||||
import java.io.EOFException;
|
||||
import java.net.InetSocketAddress;
|
||||
import org.adde0109.ambassador.event.PreSyncEvent;
|
||||
import org.slf4j.Logger;
|
||||
|
||||
import java.nio.file.Path;
|
||||
|
|
@ -30,10 +22,8 @@ public class Ambassador {
|
|||
private Optional<RegisteredServer> forgeServer;
|
||||
private AmbassadorConfig config;
|
||||
|
||||
private static ForgeHandshakeDataHandler forgeHandshakeDataHandler;
|
||||
private ForgeHandshakeHandler forgeHandshakeHandler;
|
||||
|
||||
public Map<RegisteredServer, ForgeServerConnection> forgeServerConnectionMap = new HashMap<RegisteredServer,ForgeServerConnection>();
|
||||
public Map<InetSocketAddress,ForgeConnection> incomingForgeConnections = new HashMap<InetSocketAddress,ForgeConnection>();;
|
||||
|
||||
@Inject
|
||||
public Ambassador(ProxyServer server, Logger logger, @DataDirectory Path dataDirectory) {
|
||||
|
|
@ -46,71 +36,15 @@ public class Ambassador {
|
|||
public void onProxyInitialization(ProxyInitializeEvent event) {
|
||||
config = AmbassadorConfig.readOrCreateConfig(dataDirectory,server,logger);
|
||||
if(config != null) {
|
||||
forgeHandshakeDataHandler = new ForgeHandshakeDataHandler(logger,server);
|
||||
server.getEventManager().register(this, forgeHandshakeDataHandler);
|
||||
forgeHandshakeHandler = new ForgeHandshakeHandler(config, server, logger);
|
||||
server.getEventManager().register(this, forgeHandshakeHandler);
|
||||
}
|
||||
else {
|
||||
logger.warn("Ambassador will be disabled because of errors");
|
||||
}
|
||||
|
||||
}
|
||||
@Subscribe
|
||||
public void onPreLoginEvent(PreLoginEvent event, Continuation continuation) {
|
||||
if (!config.shouldHandle(event.getConnection().getProtocolVersion().getProtocol())) {
|
||||
continuation.resume();
|
||||
return;
|
||||
}
|
||||
RegisteredServer defaultServer = config.getServer(event.getConnection().getProtocolVersion().getProtocol());
|
||||
|
||||
this.server.getEventManager().fire(new PreSyncEvent(event.getUsername(),event.getConnection(), defaultServer))
|
||||
.thenAccept((e) -> {
|
||||
if (e.getResult().getServer().isEmpty()) {
|
||||
//Do not sync
|
||||
return;
|
||||
}
|
||||
RegisteredServer newServer = e.getResult().getServer().get();
|
||||
|
||||
|
||||
|
||||
//If a connection does not already exist, create one.
|
||||
if (!forgeServerConnectionMap.containsKey(newServer)) {
|
||||
forgeServerConnectionMap.put(newServer, new ForgeServerConnection(this,logger,newServer));
|
||||
}
|
||||
|
||||
ForgeServerConnection forgeServerConnection = forgeServerConnectionMap.get(newServer);
|
||||
|
||||
//Syncing - continuation is forwarded to this method
|
||||
ForgeConnection.sync((LoginPhaseConnection) event.getConnection(),forgeServerConnection,continuation).thenAccept(
|
||||
this::onSyncComplete);
|
||||
});
|
||||
}
|
||||
|
||||
public void onSyncComplete(ForgeConnection forgeConnection) {
|
||||
if (forgeConnection != null) {
|
||||
incomingForgeConnections.values().removeIf((c) -> !c.getConnection().isActive());
|
||||
incomingForgeConnections.put(forgeConnection.getConnection().getRemoteAddress(), forgeConnection);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private ForgeConnection getForgeConnection(InetSocketAddress socketAddress) {
|
||||
incomingForgeConnections.values().removeIf((c) -> !c.getConnection().isActive());
|
||||
return incomingForgeConnections.get(socketAddress);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Subscribe
|
||||
public void onServerLoginPluginMessageEvent(ServerLoginPluginMessageEvent event, Continuation continuation) {
|
||||
//Only respond the servers that we can respond to
|
||||
if(!forgeServerConnectionMap.containsKey(event.getConnection().getServer())) {
|
||||
continuation.resume();
|
||||
return;
|
||||
}
|
||||
//Grab the connection responsible for this - no pun intended
|
||||
ForgeServerConnection connection = forgeServerConnectionMap.get(event.getConnection().getServer());
|
||||
connection.handle(event,continuation);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -1,10 +1,8 @@
|
|||
package org.adde0109.ambassador;
|
||||
|
||||
import com.velocitypowered.api.event.Continuation;
|
||||
import com.velocitypowered.api.proxy.InboundConnection;
|
||||
import com.velocitypowered.api.proxy.LoginPhaseConnection;
|
||||
import com.velocitypowered.api.proxy.messages.MinecraftChannelIdentifier;
|
||||
import com.velocitypowered.api.proxy.server.RegisteredServer;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
|
|
@ -14,7 +12,7 @@ public class ForgeConnection {
|
|||
|
||||
private byte[] recivedClientModlist;
|
||||
|
||||
private ForgeHandshakeDataHandler.CachedServerHandshake transmittedHandshake;
|
||||
private ForgeHandshakeUtils.CachedServerHandshake transmittedHandshake;
|
||||
|
||||
|
||||
private ForgeConnection(LoginPhaseConnection connection) {
|
||||
|
|
|
|||
|
|
@ -0,0 +1,90 @@
|
|||
package org.adde0109.ambassador;
|
||||
|
||||
import com.velocitypowered.api.event.Continuation;
|
||||
import com.velocitypowered.api.event.Subscribe;
|
||||
import com.velocitypowered.api.event.connection.PreLoginEvent;
|
||||
import com.velocitypowered.api.event.player.ServerLoginPluginMessageEvent;
|
||||
import com.velocitypowered.api.proxy.LoginPhaseConnection;
|
||||
import com.velocitypowered.api.proxy.ProxyServer;
|
||||
import com.velocitypowered.api.proxy.server.RegisteredServer;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import org.adde0109.ambassador.event.PreSyncEvent;
|
||||
import org.slf4j.Logger;
|
||||
|
||||
public class ForgeHandshakeHandler {
|
||||
|
||||
private final AmbassadorConfig config;
|
||||
private final ProxyServer server;
|
||||
private final Logger logger;
|
||||
|
||||
public Map<RegisteredServer, ForgeServerConnection>
|
||||
forgeServerConnectionMap = new HashMap<RegisteredServer,ForgeServerConnection>();
|
||||
public Map<InetSocketAddress,ForgeConnection> incomingForgeConnections = new HashMap<InetSocketAddress,ForgeConnection>();
|
||||
|
||||
|
||||
ForgeHandshakeHandler(AmbassadorConfig config, ProxyServer server, Logger logger) {
|
||||
this.config = config;
|
||||
this.server = server;
|
||||
this.logger = logger;
|
||||
}
|
||||
|
||||
@Subscribe
|
||||
public void onPreLoginEvent(PreLoginEvent event, Continuation continuation) {
|
||||
if (!config.shouldHandle(event.getConnection().getProtocolVersion().getProtocol())) {
|
||||
continuation.resume();
|
||||
return;
|
||||
}
|
||||
RegisteredServer defaultServer = config.getServer(event.getConnection().getProtocolVersion().getProtocol());
|
||||
|
||||
this.server.getEventManager().fire(new PreSyncEvent(event.getUsername(),event.getConnection(), defaultServer))
|
||||
.thenAccept((e) -> {
|
||||
if (e.getResult().getServer().isEmpty()) {
|
||||
//Do not sync
|
||||
return;
|
||||
}
|
||||
RegisteredServer newServer = e.getResult().getServer().get();
|
||||
|
||||
|
||||
|
||||
//If a connection does not already exist, create one.
|
||||
if (!forgeServerConnectionMap.containsKey(newServer)) {
|
||||
forgeServerConnectionMap.put(newServer, new ForgeServerConnection(newServer,logger));
|
||||
}
|
||||
|
||||
ForgeServerConnection forgeServerConnection = forgeServerConnectionMap.get(newServer);
|
||||
|
||||
//Syncing - continuation is forwarded to this method
|
||||
ForgeConnection.sync((LoginPhaseConnection) event.getConnection(),forgeServerConnection,continuation).thenAccept(
|
||||
this::onSyncComplete);
|
||||
});
|
||||
}
|
||||
|
||||
public void onSyncComplete(ForgeConnection forgeConnection) {
|
||||
if (forgeConnection != null) {
|
||||
incomingForgeConnections.values().removeIf((c) -> !c.getConnection().isActive());
|
||||
incomingForgeConnections.put(forgeConnection.getConnection().getRemoteAddress(), forgeConnection);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private ForgeConnection getForgeConnection(InetSocketAddress socketAddress) {
|
||||
incomingForgeConnections.values().removeIf((c) -> !c.getConnection().isActive());
|
||||
return incomingForgeConnections.get(socketAddress);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Subscribe
|
||||
public void onServerLoginPluginMessageEvent(ServerLoginPluginMessageEvent event, Continuation continuation) {
|
||||
//Only respond the servers that we can respond to
|
||||
if(!forgeServerConnectionMap.containsKey(event.getConnection().getServer())) {
|
||||
continuation.resume();
|
||||
return;
|
||||
}
|
||||
//Grab the connection responsible for this - no pun intended
|
||||
ForgeServerConnection connection = forgeServerConnectionMap.get(event.getConnection().getServer());
|
||||
connection.handle(event,continuation);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,50 +1,16 @@
|
|||
package org.adde0109.ambassador;
|
||||
|
||||
import com.google.common.io.ByteArrayDataInput;
|
||||
import com.velocitypowered.api.event.Continuation;
|
||||
import com.velocitypowered.api.event.Subscribe;
|
||||
import com.velocitypowered.api.event.connection.PreLoginEvent;
|
||||
import com.velocitypowered.api.event.player.ServerLoginPluginMessageEvent;
|
||||
import com.velocitypowered.api.event.player.ServerPostConnectEvent;
|
||||
import com.velocitypowered.api.proxy.ConnectionRequestBuilder;
|
||||
import com.velocitypowered.api.proxy.InboundConnection;
|
||||
import com.velocitypowered.api.proxy.LoginPhaseConnection;
|
||||
import com.velocitypowered.api.proxy.Player;
|
||||
import com.velocitypowered.api.proxy.ProxyServer;
|
||||
import com.velocitypowered.api.proxy.messages.MinecraftChannelIdentifier;
|
||||
import com.velocitypowered.api.proxy.server.RegisteredServer;
|
||||
import com.velocitypowered.api.proxy.server.ServerPing;
|
||||
import com.velocitypowered.api.util.ModInfo;
|
||||
import java.io.EOFException;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import org.slf4j.Logger;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
public class ForgeHandshakeDataHandler {
|
||||
|
||||
public Map<RegisteredServer,CachedServerHandshake> cachedServerHandshake = new HashMap<RegisteredServer,CachedServerHandshake>();
|
||||
public byte[] recivedClientACK;
|
||||
public Map<RegisteredServer,byte[]> recivedClientModlist = new HashMap<RegisteredServer,byte[]>();
|
||||
public LoginPhaseConnection connection;
|
||||
|
||||
|
||||
private Map<InboundConnection,RegisteredServer> syncedConnections = new HashMap<InboundConnection,RegisteredServer>();
|
||||
|
||||
private static final int PACKET_LENGTH_INDEX = 14; //length of "fml:handshake"+1
|
||||
|
||||
private final Logger logger;
|
||||
private final ProxyServer server;
|
||||
|
||||
public ForgeHandshakeDataHandler(Logger logger, ProxyServer server) {
|
||||
this.logger = logger;
|
||||
this.server = server;
|
||||
}
|
||||
|
||||
public class ForgeHandshakeUtils {
|
||||
|
||||
public static int readVarInt(ByteArrayDataInput stream) {
|
||||
int i = 0;
|
||||
|
|
@ -12,10 +12,9 @@ import org.slf4j.Logger;
|
|||
public class ForgeServerConnection {
|
||||
|
||||
private static final int PACKET_LENGTH_INDEX = 14; //length of "fml:handshake"+1
|
||||
private final Ambassador ambassador;
|
||||
private final Logger logger;
|
||||
private final RegisteredServer handshakeServer;
|
||||
private ForgeHandshakeDataHandler.CachedServerHandshake handshake;
|
||||
private ForgeHandshakeUtils.CachedServerHandshake handshake;
|
||||
private byte[] defaultClientModlist;
|
||||
private byte[] defaultClientACK;
|
||||
|
||||
|
|
@ -23,17 +22,16 @@ public class ForgeServerConnection {
|
|||
return handshakeServer;
|
||||
}
|
||||
|
||||
public ForgeServerConnection(Ambassador ambassador, Logger logger, RegisteredServer handshakeServer) {
|
||||
this.ambassador = ambassador;
|
||||
this.logger = logger;
|
||||
public ForgeServerConnection(RegisteredServer handshakeServer, Logger logger) {
|
||||
this.handshakeServer = handshakeServer;
|
||||
this.logger = logger;
|
||||
}
|
||||
|
||||
public CompletableFuture<ForgeHandshakeDataHandler.CachedServerHandshake> getHandshake() {
|
||||
CompletableFuture<ForgeHandshakeDataHandler.CachedServerHandshake> future;
|
||||
public CompletableFuture<ForgeHandshakeUtils.CachedServerHandshake> getHandshake() {
|
||||
CompletableFuture<ForgeHandshakeUtils.CachedServerHandshake> future;
|
||||
if (handshakeServer.getPlayersConnected().isEmpty() || (handshake == null)) {
|
||||
ForgeHandshakeDataHandler.handshakeReceiver
|
||||
receiver = new ForgeHandshakeDataHandler.handshakeReceiver(handshakeServer, logger);
|
||||
ForgeHandshakeUtils.handshakeReceiver
|
||||
receiver = new ForgeHandshakeUtils.handshakeReceiver(handshakeServer, logger);
|
||||
future = receiver.downloadHandshake();
|
||||
future.thenAccept(p -> {
|
||||
handshake = p;
|
||||
|
|
@ -52,8 +50,8 @@ public class ForgeServerConnection {
|
|||
continuation.resumeWithException(new EOFException());
|
||||
return;
|
||||
}
|
||||
ForgeHandshakeDataHandler.readVarInt(data); //Length
|
||||
int packetID = ForgeHandshakeDataHandler.readVarInt(data);
|
||||
ForgeHandshakeUtils.readVarInt(data); //Length
|
||||
int packetID = ForgeHandshakeUtils.readVarInt(data);
|
||||
|
||||
if(packetID == 1) {
|
||||
event.setResult(ServerLoginPluginMessageEvent.ResponseResult.reply(defaultClientModlist));
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user