Config file works almost
This commit is contained in:
parent
435199e3d7
commit
31933ee718
|
|
@ -31,6 +31,7 @@ public class Ambassador {
|
||||||
private final Logger logger;
|
private final Logger logger;
|
||||||
private final Path dataDirectory;
|
private final Path dataDirectory;
|
||||||
private Optional<RegisteredServer> forgeServer;
|
private Optional<RegisteredServer> forgeServer;
|
||||||
|
private AmbassadorConfig config;
|
||||||
|
|
||||||
private static ForgeHandshakeDataHandler forgeHandshakeDataHandler;
|
private static ForgeHandshakeDataHandler forgeHandshakeDataHandler;
|
||||||
|
|
||||||
|
|
@ -43,97 +44,16 @@ public class Ambassador {
|
||||||
|
|
||||||
@Subscribe
|
@Subscribe
|
||||||
public void onProxyInitialization(ProxyInitializeEvent event) {
|
public void onProxyInitialization(ProxyInitializeEvent event) {
|
||||||
if(readOrCreateConfig()) {
|
config = AmbassadorConfig.readOrCreateConfig(dataDirectory,server,logger);
|
||||||
forgeHandshakeDataHandler = new ForgeHandshakeDataHandler(forgeServer.get(),logger);
|
if(config != null) {
|
||||||
|
forgeHandshakeDataHandler = new ForgeHandshakeDataHandler(config,logger);
|
||||||
server.getEventManager().register(this, forgeHandshakeDataHandler);
|
server.getEventManager().register(this, forgeHandshakeDataHandler);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
logger.info("Ambassador will be disabled because of errors");
|
logger.warn("Ambassador will be disabled because of errors");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean readOrCreateConfig() {
|
|
||||||
try {
|
|
||||||
Files.createDirectories(dataDirectory);
|
|
||||||
Files.createFile(dataDirectory.resolve("forgeServer.toml"));
|
|
||||||
|
|
||||||
}
|
|
||||||
catch (FileAlreadyExistsException ignored) {
|
|
||||||
|
|
||||||
}
|
|
||||||
catch (IOException e) {
|
|
||||||
logger.error("Config related error: " + e.toString());
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
CommentedFileConfig config = CommentedFileConfig.builder(dataDirectory.resolve("forgeServer.toml"))
|
|
||||||
.defaultData(Ambassador.class.getClassLoader().getResource("default-ambassador.toml"))
|
|
||||||
.autosave()
|
|
||||||
.preserveInsertionOrder()
|
|
||||||
.sync()
|
|
||||||
.build();
|
|
||||||
config.load();
|
|
||||||
|
|
||||||
CommentedConfig settingsConfig = config.get("Differentiators");
|
|
||||||
|
|
||||||
|
|
||||||
Differentiators settings = new Differentiators(settingsConfig);
|
|
||||||
|
|
||||||
logger.info(settings.differentiators.get("758").handshakeServer);
|
|
||||||
|
|
||||||
config.save();
|
|
||||||
}
|
|
||||||
catch (ParsingException e) {
|
|
||||||
logger.error("Config related error: " + e.toString());
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//758 - 1.18.2
|
|
||||||
|
|
||||||
//754 - 1.16.5
|
|
||||||
|
|
||||||
forgeServer = server.getServer("lobby");
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static class Differentiators {
|
|
||||||
private Map<String,DifferentiatorSettings> differentiators = ImmutableMap.of(
|
|
||||||
"758", new DifferentiatorSettings(),
|
|
||||||
"754", new DifferentiatorSettings()
|
|
||||||
);
|
|
||||||
private Differentiators(){
|
|
||||||
}
|
|
||||||
|
|
||||||
private Differentiators(CommentedConfig config) {
|
|
||||||
if (config != null) {
|
|
||||||
Map<String,DifferentiatorSettings> differentiators = new HashMap<>();
|
|
||||||
for (UnmodifiableConfig.Entry entry : config.entrySet()) {
|
|
||||||
if (entry.getValue() instanceof CommentedConfig) {
|
|
||||||
differentiators.put(entry.getKey(),new DifferentiatorSettings(entry.getValue()));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
this.differentiators = ImmutableMap.copyOf(differentiators);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static class DifferentiatorSettings {
|
|
||||||
private String handshakeServer = "";
|
|
||||||
private boolean forced = false;
|
|
||||||
|
|
||||||
private DifferentiatorSettings(){
|
|
||||||
}
|
|
||||||
|
|
||||||
private DifferentiatorSettings(CommentedConfig config) {
|
|
||||||
if (config != null) {
|
|
||||||
this.handshakeServer = config.getOrElse("forge-server", handshakeServer);
|
|
||||||
this.forced = config.getOrElse("forced",forced);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
124
src/main/java/org/adde0109/ambassador/AmbassadorConfig.java
Normal file
124
src/main/java/org/adde0109/ambassador/AmbassadorConfig.java
Normal file
|
|
@ -0,0 +1,124 @@
|
||||||
|
package org.adde0109.ambassador;
|
||||||
|
|
||||||
|
import com.electronwill.nightconfig.core.CommentedConfig;
|
||||||
|
import com.electronwill.nightconfig.core.UnmodifiableConfig;
|
||||||
|
import com.electronwill.nightconfig.core.file.CommentedFileConfig;
|
||||||
|
import com.electronwill.nightconfig.core.io.ParsingException;
|
||||||
|
import com.google.common.collect.ImmutableMap;
|
||||||
|
import com.velocitypowered.api.proxy.ProxyServer;
|
||||||
|
import com.velocitypowered.api.proxy.server.RegisteredServer;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.nio.file.FileAlreadyExistsException;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.rmi.NoSuchObjectException;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Objects;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
public class AmbassadorConfig {
|
||||||
|
|
||||||
|
|
||||||
|
private final ProxyServer server;
|
||||||
|
private final Logger logger;
|
||||||
|
private Differentiators settings;
|
||||||
|
|
||||||
|
|
||||||
|
private AmbassadorConfig(ProxyServer server,Logger logger) {
|
||||||
|
this.server = server;
|
||||||
|
this.logger = logger;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public RegisteredServer getServer(int protocolVersion) {
|
||||||
|
return settings.differentiators.get(protocolVersion).handshakeServer;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean shouldHandle(int protocolVersion) {
|
||||||
|
return settings.differentiators.containsKey(protocolVersion);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public static AmbassadorConfig readOrCreateConfig(Path dataDirectory,ProxyServer server, Logger logger) {
|
||||||
|
AmbassadorConfig ambassadorConfig = new AmbassadorConfig(server,logger);
|
||||||
|
try {
|
||||||
|
Files.createDirectories(dataDirectory);
|
||||||
|
|
||||||
|
}
|
||||||
|
catch (FileAlreadyExistsException ignored) {
|
||||||
|
|
||||||
|
}
|
||||||
|
catch (IOException e) {
|
||||||
|
logger.error("Config related error: " + e.toString());
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
CommentedFileConfig config = CommentedFileConfig.builder(dataDirectory.resolve("ambassador.toml"))
|
||||||
|
.defaultData(Ambassador.class.getClassLoader().getResource("default-ambassador.toml"))
|
||||||
|
.autosave()
|
||||||
|
.preserveInsertionOrder()
|
||||||
|
.sync()
|
||||||
|
.build();
|
||||||
|
config.load();
|
||||||
|
|
||||||
|
CommentedConfig settingsConfig = config.get("Differentiators");
|
||||||
|
|
||||||
|
|
||||||
|
ambassadorConfig.settings = ambassadorConfig.new Differentiators(settingsConfig);
|
||||||
|
|
||||||
|
|
||||||
|
config.save();
|
||||||
|
return ambassadorConfig;
|
||||||
|
}
|
||||||
|
catch (Exception e) {
|
||||||
|
logger.error("Config related error: " + e.toString());
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private class Differentiators {
|
||||||
|
private Map<Integer,DifferentiatorSettings> differentiators = ImmutableMap.of(
|
||||||
|
758, new DifferentiatorSettings(),
|
||||||
|
754, new DifferentiatorSettings()
|
||||||
|
);
|
||||||
|
private Differentiators(){
|
||||||
|
}
|
||||||
|
|
||||||
|
private Differentiators(CommentedConfig config) throws Exception {
|
||||||
|
if (config != null) {
|
||||||
|
Map<Integer,DifferentiatorSettings> differentiators = new HashMap<>();
|
||||||
|
for (UnmodifiableConfig.Entry entry : config.entrySet()) {
|
||||||
|
if (entry.getValue() instanceof CommentedConfig) {
|
||||||
|
differentiators.put(Integer.decode(entry.getKey()),new DifferentiatorSettings(entry.getValue()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
this.differentiators = ImmutableMap.copyOf(differentiators);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private class DifferentiatorSettings {
|
||||||
|
private RegisteredServer handshakeServer = null;
|
||||||
|
private boolean forced = false;
|
||||||
|
|
||||||
|
private DifferentiatorSettings(){
|
||||||
|
}
|
||||||
|
|
||||||
|
private DifferentiatorSettings(CommentedConfig config) throws Exception {
|
||||||
|
if (config != null) {
|
||||||
|
String serverName = config.getOrElse("forge-server", "");
|
||||||
|
if (!Objects.equals(serverName, ""))
|
||||||
|
this.handshakeServer = server.getServer(serverName)
|
||||||
|
.orElseThrow(() -> new Exception(serverName + "is not a registered server!"));
|
||||||
|
this.forced = config.getOrElse("forced",forced);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,20 +0,0 @@
|
||||||
package org.adde0109.ambassador;
|
|
||||||
|
|
||||||
import com.velocitypowered.api.event.Continuation;
|
|
||||||
import com.velocitypowered.api.event.connection.PreLoginEvent;
|
|
||||||
import com.velocitypowered.api.network.ProtocolVersion;
|
|
||||||
|
|
||||||
public class ForgeConnectionHandler {
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public void onPreLogin(PreLoginEvent event, Continuation continuation) {
|
|
||||||
if(event.getConnection().getProtocolVersion() == ProtocolVersion.MINECRAFT_1_16_4) {
|
|
||||||
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
continuation.resume();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
@ -5,60 +5,59 @@ import com.velocitypowered.api.event.Continuation;
|
||||||
import com.velocitypowered.api.event.PostOrder;
|
import com.velocitypowered.api.event.PostOrder;
|
||||||
import com.velocitypowered.api.event.Subscribe;
|
import com.velocitypowered.api.event.Subscribe;
|
||||||
import com.velocitypowered.api.event.connection.PreLoginEvent;
|
import com.velocitypowered.api.event.connection.PreLoginEvent;
|
||||||
|
import com.velocitypowered.api.event.permission.PermissionsSetupEvent;
|
||||||
import com.velocitypowered.api.event.player.ServerLoginPluginMessageEvent;
|
import com.velocitypowered.api.event.player.ServerLoginPluginMessageEvent;
|
||||||
|
import com.velocitypowered.api.proxy.InboundConnection;
|
||||||
import com.velocitypowered.api.proxy.LoginPhaseConnection;
|
import com.velocitypowered.api.proxy.LoginPhaseConnection;
|
||||||
|
import com.velocitypowered.api.proxy.Player;
|
||||||
import com.velocitypowered.api.proxy.messages.MinecraftChannelIdentifier;
|
import com.velocitypowered.api.proxy.messages.MinecraftChannelIdentifier;
|
||||||
import com.velocitypowered.api.proxy.server.RegisteredServer;
|
import com.velocitypowered.api.proxy.server.RegisteredServer;
|
||||||
import com.velocitypowered.api.proxy.server.ServerPing;
|
import com.velocitypowered.api.proxy.server.ServerPing;
|
||||||
import com.velocitypowered.api.util.ModInfo;
|
import com.velocitypowered.api.util.ModInfo;
|
||||||
import java.io.EOFException;
|
import java.io.EOFException;
|
||||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
import java.util.*;
|
||||||
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
|
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Objects;
|
|
||||||
import java.util.concurrent.CompletableFuture;
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
|
||||||
public class ForgeHandshakeDataHandler {
|
public class ForgeHandshakeDataHandler {
|
||||||
|
|
||||||
public handshakeReceiver.CachedServerHandshake cachedServerHandshake;
|
public Map<RegisteredServer, handshakeReceiver.CachedServerHandshake> cachedServerHandshake = new HashMap<RegisteredServer, handshakeReceiver.CachedServerHandshake>();
|
||||||
public byte[] recivedClientACK;
|
public byte[] recivedClientACK;
|
||||||
public byte[] recivedClientModlist;
|
public Map<RegisteredServer,byte[]> recivedClientModlist = new HashMap<RegisteredServer,byte[]>();
|
||||||
|
public LoginPhaseConnection connection;
|
||||||
|
|
||||||
|
private Collection<InboundConnection> pendingConnections = new HashSet<InboundConnection>();
|
||||||
|
|
||||||
private static final int PACKET_LENGTH_INDEX = 14; //length of "fml:handshake"+1
|
private static final int PACKET_LENGTH_INDEX = 14; //length of "fml:handshake"+1
|
||||||
|
|
||||||
|
private final AmbassadorConfig config;
|
||||||
private final Logger logger;
|
private final Logger logger;
|
||||||
private RegisteredServer handshakeServer;
|
|
||||||
|
|
||||||
public ForgeHandshakeDataHandler(RegisteredServer handshakeServer,Logger logger) {
|
public ForgeHandshakeDataHandler(AmbassadorConfig config,Logger logger) {
|
||||||
this.handshakeServer = handshakeServer;
|
this.config = config;
|
||||||
this.logger = logger;
|
this.logger = logger;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Subscribe(order = PostOrder.EARLY)
|
@Subscribe
|
||||||
public void onPreLogin(PreLoginEvent event, Continuation continuation) {
|
public void onPreLoginEvent(PreLoginEvent event, Continuation continuation) {
|
||||||
if(handshakeServer.getPlayersConnected().isEmpty()) {
|
if (!config.shouldHandle(event.getConnection().getProtocolVersion().getProtocol())) {
|
||||||
handshakeReceiver receiver = new handshakeReceiver(handshakeServer, logger);
|
|
||||||
receiver.downloadHandshake().thenAccept((p) -> {
|
|
||||||
cachedServerHandshake = p;
|
|
||||||
sendModlist(p.modListPacket,(LoginPhaseConnection) event.getConnection());
|
|
||||||
sendOther(p.otherPackets,(LoginPhaseConnection) event.getConnection());
|
|
||||||
continuation.resume();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
else if(cachedServerHandshake != null) {
|
|
||||||
sendModlist(cachedServerHandshake.modListPacket, (LoginPhaseConnection) event.getConnection());
|
|
||||||
sendOther(cachedServerHandshake.otherPackets,(LoginPhaseConnection) event.getConnection());
|
|
||||||
continuation.resume();
|
continuation.resume();
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
RegisteredServer server = config.getServer(event.getConnection().getProtocolVersion().getProtocol());
|
||||||
|
getHandshake(server).thenAccept(p -> {
|
||||||
|
sendModlist(p.modListPacket,(LoginPhaseConnection) event.getConnection(),server);
|
||||||
|
sendOther(p.otherPackets,(LoginPhaseConnection) event.getConnection());
|
||||||
|
continuation.resume();
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@Subscribe
|
@Subscribe
|
||||||
public void onServerLoginPluginMessageEvent(ServerLoginPluginMessageEvent event, Continuation continuation) {
|
public void onServerLoginPluginMessageEvent(ServerLoginPluginMessageEvent event, Continuation continuation) {
|
||||||
if((recivedClientModlist == null) || (recivedClientACK == null) || (!Objects.equals(event.getIdentifier().getId(), "fml:loginwrapper"))) {
|
if((!recivedClientModlist.containsKey(event.getConnection().getServer())) || (recivedClientACK == null)) {
|
||||||
continuation.resume();
|
continuation.resume();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
@ -71,7 +70,7 @@ public class ForgeHandshakeDataHandler {
|
||||||
int packetID = readVarInt(data);
|
int packetID = readVarInt(data);
|
||||||
|
|
||||||
if(packetID == 1) {
|
if(packetID == 1) {
|
||||||
event.setResult(ServerLoginPluginMessageEvent.ResponseResult.reply(recivedClientModlist));
|
event.setResult(ServerLoginPluginMessageEvent.ResponseResult.reply(recivedClientModlist.get(event.getConnection().getServer())));
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
event.setResult(ServerLoginPluginMessageEvent.ResponseResult.reply(recivedClientACK));
|
event.setResult(ServerLoginPluginMessageEvent.ResponseResult.reply(recivedClientACK));
|
||||||
|
|
@ -81,15 +80,34 @@ public class ForgeHandshakeDataHandler {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public CompletableFuture<handshakeReceiver.CachedServerHandshake> getHandshake(RegisteredServer handshakeServer) {
|
||||||
|
CompletableFuture<handshakeReceiver.CachedServerHandshake> future;
|
||||||
|
if((handshakeServer.getPlayersConnected().isEmpty()) || (!cachedServerHandshake.containsKey(handshakeServer))) {
|
||||||
|
handshakeReceiver receiver = new handshakeReceiver(handshakeServer, logger);
|
||||||
|
future = receiver.downloadHandshake();
|
||||||
|
future.thenAccept(p -> {
|
||||||
|
cachedServerHandshake.put(handshakeServer,p);
|
||||||
|
});
|
||||||
|
return future;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
future = new CompletableFuture<>();
|
||||||
|
future.complete(cachedServerHandshake.get(handshakeServer));
|
||||||
|
return future;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void sendModlist(byte[] modListPacket, LoginPhaseConnection connection) {
|
private void sendModlist(byte[] modListPacket, LoginPhaseConnection connection, RegisteredServer server) {
|
||||||
connection.sendLoginPluginMessage(MinecraftChannelIdentifier.create("fml","loginwrapper"), modListPacket, responseBody -> recivedClientModlist = responseBody);
|
connection.sendLoginPluginMessage(MinecraftChannelIdentifier.create("fml","loginwrapper"), modListPacket, responseBody -> recivedClientModlist.put(server,responseBody));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void sendOther(List<byte[]> otherPackets, LoginPhaseConnection connection) {
|
private void sendOther(List<byte[]> otherPackets, LoginPhaseConnection connection) {
|
||||||
otherPackets.forEach((packet) -> {
|
for (int i = 0;i<otherPackets.size();i++) {
|
||||||
connection.sendLoginPluginMessage(MinecraftChannelIdentifier.create("fml","loginwrapper"), packet, responseBody -> recivedClientACK = responseBody);
|
connection.sendLoginPluginMessage(MinecraftChannelIdentifier.create("fml","loginwrapper"), otherPackets.get(i),
|
||||||
});
|
(i<(otherPackets.size()-1)) ? responseBody -> recivedClientACK = responseBody : responseBody -> {
|
||||||
|
pendingConnections.add(connection);
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static int readVarInt(ByteArrayDataInput stream) {
|
public static int readVarInt(ByteArrayDataInput stream) {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user