Compare commits

..

No commits in common. "non-api" and "1.20.2" have entirely different histories.

17 changed files with 253 additions and 437 deletions

View File

@ -3,8 +3,7 @@
This is a Velocity plugin that makes it possible to host a modern Forge server behind a Velocity proxy!
Unlike other solutions, this plugin does not require any special modifications to the backend server nor the client. (The player doesn't need to do anything)
## Only for 1.13-1.20.1
Velocity has now added built-in support for newer mc versions and therefore don't need Ambassador. You might still need PCF mod on the server-side, for more info please visit: https://github.com/adde0109/Proxy-Compatible-Forge
## How to get started:
1. Download and install this plugin to your proxy.
2. After starting the server, configure the plugin it to your liking using the config file found in the folder "Ambassador".
@ -14,13 +13,12 @@ Velocity has now added built-in support for newer mc versions and therefore don'
- https://github.com/caunt/BungeeForge (Legacy forwarding)
## Features
* Server switching without any client side mod when the servers are similar. (Mods must match)
* ServerRedirect support for server switching.
* Server switching using Client Reset Packet Mod for instant server switching:
1.16.5: https://github.com/Just-Chaldea/Forge-Client-Reset-Packet
1.18.2+: https://www.curseforge.com/minecraft/mc-mods/forge-client-reset-packet-forward
* Server switching using kick to reset the client with configureble message and switch timeout.
* ServerRedirect support for auto-reconnecting during switch.
* Server switching using client mod for instant server switching:
https://github.com/Just-Chaldea/Forge-Client-Reset-Packet
1.18.2 and 1.19 fork:
https://github.com/adde0109/Forge-Client-Reset-Packet
## Stuck on "Negotiating":
This is an issue with Client Reset Packet Mod being partly incompatible with certain mods on the client. Please remove incompatible mods on the client if you have this issue. (Yes, this also includes client-side only mods.)

View File

@ -5,7 +5,7 @@ plugins {
}
group = "org.adde0109"
version = "1.5.3-beta"
version = "1.4.3-beta"
repositories {
mavenCentral()

View File

@ -19,18 +19,14 @@ import java.util.Map;
import com.velocitypowered.api.proxy.server.RegisteredServer;
import com.velocitypowered.proxy.VelocityServer;
import com.velocitypowered.proxy.connection.MinecraftConnection;
import com.velocitypowered.proxy.connection.client.ConnectedPlayer;
import com.velocitypowered.proxy.network.ConnectionManager;
import com.velocitypowered.proxy.protocol.StateRegistry;
import com.velocitypowered.proxy.protocol.packet.DisconnectPacket;
import com.velocitypowered.proxy.protocol.packet.brigadier.ArgumentIdentifier;
import com.velocitypowered.proxy.protocol.packet.brigadier.ArgumentPropertyRegistry;
import com.velocitypowered.proxy.protocol.packet.brigadier.ArgumentPropertySerializer;
import io.netty.buffer.ByteBuf;
import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.MiniMessage;
import org.adde0109.ambassador.velocity.VelocityBackendChannelInitializer;
import org.adde0109.ambassador.velocity.VelocityServerChannelInitializer;
import org.adde0109.ambassador.velocity.VelocityEventHandler;
@ -46,11 +42,11 @@ import java.util.concurrent.TimeUnit;
import static com.velocitypowered.api.network.ProtocolVersion.MINECRAFT_1_19;
import static com.velocitypowered.proxy.protocol.packet.brigadier.ArgumentIdentifier.mapSet;
@Plugin(id = "ambassador", name = "Ambassador", version = "1.5.3-beta", authors = {"adde0109"})
@Plugin(id = "ambassador", name = "Ambassador", version = "1.4.3-beta", authors = {"adde0109"})
public class Ambassador {
//Don't forget to update checkCompatibleVersion() when changing this value
private static final String minVelocityVersion = "velocity-3.3.0-SNAPSHOT-330";
private static final String minVelocityVersion = "velocity-3.2.0-SNAPSHOT-266";
public ProxyServer server;
public final Logger logger;
@ -79,11 +75,11 @@ public class Ambassador {
boolean checkCompatibleVersion() {
//Update this when changing minVelocityVersion
try {
Class.forName("com.velocitypowered.proxy.protocol.packet.DisconnectPacket");
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
MinecraftConnection.class.getDeclaredMethod("setActiveSessionHandler", StateRegistry.class);
} catch (NoSuchMethodException e) {
return false;
}
return true;
return true;
}
@Subscribe(order = PostOrder.LAST)
@ -156,16 +152,6 @@ public class Ambassador {
return TEMPORARY_FORCED;
}
public void reconnectSwitchPlayer(ConnectedPlayer player) {
TEMPORARY_FORCED.put(player.getUsername(), player.getConnectionInFlight().getServer(),
config.getServerSwitchCancellationTime(), TimeUnit.SECONDS);
MiniMessage mm = MiniMessage.miniMessage();
Component parsed = mm.deserialize(config.getKickReconnectMessageString());
player.disconnect(parsed);
}
private void initMetrics() {
Metrics metrics = metricsFactory.make(this, 15655);
}

View File

@ -14,119 +14,92 @@ import java.nio.file.Path;
public class AmbassadorConfig {
@Expose
private int serverSwitchCancellationTime = 30;
@Expose
private int serverSwitchCancellationTime = 30;
@Expose
private boolean silenceWarnings = false;
@Expose
private boolean bypassRegistryCheck = false;
@Expose
private boolean bypassModCheck = false;
@Expose
private boolean silenceWarnings = false;
@Expose
private boolean debugMode = false;
@Expose
private boolean bypassRegistryCheck = false;
@Expose
private boolean bypassModCheck = false;
@Expose
private boolean enableKickReset = false;
@Expose
private boolean debugMode = false;
@Expose
private String kickReconnectMessageString = "<red>Please reconnect.</red>";
private AmbassadorConfig(boolean silenceWarnings, boolean bypassRegistryCheck, boolean bypassModCheck, boolean debugMode) {
this.silenceWarnings = silenceWarnings;
this.bypassRegistryCheck = bypassRegistryCheck;
this.bypassModCheck = bypassModCheck;
this.debugMode = debugMode;
};
private AmbassadorConfig(boolean silenceWarnings, boolean bypassRegistryCheck, boolean bypassModCheck,
boolean debugMode, boolean enableKickReset, String kickReconnectMessageString) {
this.silenceWarnings = silenceWarnings;
this.bypassRegistryCheck = bypassRegistryCheck;
this.bypassModCheck = bypassModCheck;
this.debugMode = debugMode;
this.enableKickReset = enableKickReset;
this.kickReconnectMessageString = kickReconnectMessageString;
public static AmbassadorConfig read(Path path) throws IOException {
URL defaultConfigLocation = AmbassadorConfig.class.getClassLoader()
.getResource("default-ambassador.toml");
if (defaultConfigLocation == null) {
throw new RuntimeException("Default configuration file does not exist.");
}
public static AmbassadorConfig read(Path path) throws IOException {
URL defaultConfigLocation = AmbassadorConfig.class.getClassLoader()
.getResource("default-ambassador.toml");
if (defaultConfigLocation == null) {
throw new RuntimeException("Default configuration file does not exist.");
}
CommentedFileConfig config = CommentedFileConfig.builder(path)
.defaultData(defaultConfigLocation)
.autosave()
.preserveInsertionOrder()
.sync()
.build();
config.load();
CommentedFileConfig config = CommentedFileConfig.builder(path)
.defaultData(defaultConfigLocation)
.autosave()
.preserveInsertionOrder()
.sync()
.build();
config.load();
double configVersion;
try {
configVersion = Double.parseDouble(config.getOrElse("config-version", "1.0"));
} catch (NumberFormatException e) {
configVersion = 1.0;
}
boolean silenceWarnings = config.getOrElse("silence-warnings", false);
int serverSwitchCancellationTime = config.getOrElse("serverRedirectTimeout", 30);
boolean bypassRegistryCheck = config.getOrElse("bypass-registry-checks", false);
boolean bypassModCheck = config.getOrElse("bypass-mod-checks", false);
boolean debugMode = config.getOrElse("debug-mode", false);
String kickReconnectMessageString = config.getOrElse("disconnect-reset-message",
config.getOrElse("reconnect-message", "<red>Please reconnect.</red>"));
//Upgrade config
if (configVersion <= 2.0) {
Files.delete(path);
config = CommentedFileConfig.builder(path)
.defaultData(defaultConfigLocation)
.autosave()
.preserveInsertionOrder()
.sync()
.build();
config.load();
config.set("silence-warnings", silenceWarnings);
config.set("serverRedirectTimeout", serverSwitchCancellationTime);
config.set("bypass-registry-checks", bypassRegistryCheck);
config.set("bypass-mod-checks", bypassModCheck);
config.set("debug-mode", debugMode);
config.set("reconnect-message", kickReconnectMessageString);
}
boolean enableKickReset = config.getOrElse("enable-kick-reset", false);
return new AmbassadorConfig(silenceWarnings, bypassRegistryCheck, bypassModCheck,
debugMode, enableKickReset, kickReconnectMessageString);
double configVersion;
try {
configVersion = Double.parseDouble(config.getOrElse("config-version", "1.0"));
} catch (NumberFormatException e) {
configVersion = 1.0;
}
public int getServerSwitchCancellationTime() {
return serverSwitchCancellationTime;
boolean silenceWarnings = config.getOrElse("silence-warnings", false);
//Upgrade config
if (configVersion <= 1.2) {
Files.delete(path);
config = CommentedFileConfig.builder(path)
.defaultData(defaultConfigLocation)
.autosave()
.preserveInsertionOrder()
.sync()
.build();
config.load();
config.set("silence-warnings", silenceWarnings);
}
public boolean isSilenceWarnings() {
return silenceWarnings;
}
int serverSwitchCancellationTime = config.getOrElse("serverRedirectTimeout", 30);
public boolean isBypassRegistryCheck() {
return bypassRegistryCheck;
}
boolean bypassRegistryCheck = config.getOrElse("bypass-registry-checks", false);
public boolean isBypassModCheck() {
return bypassModCheck;
}
boolean bypassModCheck = config.getOrElse("bypass-mod-checks", false);
public boolean isDebugMode() {
return debugMode;
}
boolean debugMode = config.getOrElse("debug-mode", false);
public boolean isEnableKickReset() {
return enableKickReset;
}
return new AmbassadorConfig(bypassRegistryCheck, bypassModCheck, silenceWarnings, debugMode);
}
public String getKickReconnectMessageString() {
return kickReconnectMessageString;
}
public int getServerSwitchCancellationTime() {
return serverSwitchCancellationTime;
}
public boolean isSilenceWarnings() {
return silenceWarnings;
}
public boolean isBypassRegistryCheck() {
return bypassRegistryCheck;
}
public boolean isBypassModCheck() {
return bypassModCheck;
}
public boolean isDebugMode() {
return debugMode;
}
}

View File

@ -1,7 +1,5 @@
package org.adde0109.ambassador.forge;
import org.adde0109.ambassador.forge.packet.Context;
import org.adde0109.ambassador.forge.packet.GenericForgeLoginWrapperPacket;
import org.adde0109.ambassador.forge.packet.ModListReplyPacket;
import org.adde0109.ambassador.forge.packet.RegistryPacket;
@ -13,7 +11,6 @@ import java.util.zip.Checksum;
public class ForgeHandshake {
private ModListReplyPacket modListReplyPacket;
private final Map<String, Long> registries = new HashMap<>();
public GenericForgeLoginWrapperPacket<Context.ClientContext> zetaFlagsPacket;
public ForgeHandshake() {
}

View File

@ -6,8 +6,8 @@ import com.google.common.io.ByteStreams;
import com.velocitypowered.proxy.protocol.ProtocolUtils;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.handler.codec.DecoderException;
import org.adde0109.ambassador.forge.packet.Context;
import org.adde0109.ambassador.forge.packet.GenericForgeLoginWrapperPacket;
import org.adde0109.ambassador.forge.packet.IForgeLoginWrapperPacket;
import java.nio.charset.StandardCharsets;
@ -117,52 +117,30 @@ public class ForgeHandshakeUtils {
return stream.toByteArray();
}
public static class ThirdPartyRegistryUtils {
static enum ThirdPartyChannel {
SILENTGEAR_NETWORK {
@Override
public IForgeLoginWrapperPacket<Context.ClientContext> generateResponsePacket(Context.ClientContext context, ForgeHandshake completed) {
return new ACKPacket(context, 3);
}
},
ZETA_MAIN {
@Override
public IForgeLoginWrapperPacket<Context.ClientContext> generateResponsePacket(Context.ClientContext context, ForgeHandshake completed) {
return new GenericForgeLoginWrapperPacket<Context.ClientContext>(completed.zetaFlagsPacket.getContent(), context);
}
};
abstract public IForgeLoginWrapperPacket<Context.ClientContext> generateResponsePacket(Context.ClientContext context, ForgeHandshake completed);
}
static boolean isThirdPartyPacket(GenericForgeLoginWrapperPacket<Context> packet) {
public static class SilentGearUtils {
public static boolean isSilentGearPacket(byte[] data) {
ByteBuf buf = Unpooled.wrappedBuffer(data);
String channel = null;
try {
Enum.valueOf(ThirdPartyChannel.class,
packet.getContext().getChannelName().replace(':', '_').toUpperCase());
return true;
} catch (IllegalArgumentException e) {
return false;
channel = ProtocolUtils.readString(buf);
} catch (DecoderException e) {
} finally {
buf.release();
}
return channel != null && channel.equals("silentgear:network");
}
static ThirdPartyChannel getThirdPartyChannel(GenericForgeLoginWrapperPacket<Context> packet) {
return Enum.valueOf(ThirdPartyChannel.class,
packet.getContext().getChannelName().replace(':', '_').toUpperCase());
}
static class ACKPacket implements IForgeLoginWrapperPacket<Context.ClientContext> {
public static class ACKPacket implements IForgeLoginWrapperPacket<Context.ClientContext> {
private final Context.ClientContext context;
private final int packetID;
ACKPacket(Context.ClientContext context, int packetID) {
this.context = context;
this.packetID = packetID;
}
public ACKPacket(Context.ClientContext context) {
this.context = context;
}
@Override
public ByteBuf encode() {
ByteBuf buf = Unpooled.buffer();
ProtocolUtils.writeVarInt(buf, packetID);
ProtocolUtils.writeVarInt(buf, 3);
return buf;
}

View File

@ -6,8 +6,10 @@ import com.velocitypowered.proxy.connection.backend.VelocityServerConnection;
import com.velocitypowered.proxy.connection.client.ConnectedPlayer;
import com.velocitypowered.proxy.network.Connections;
import com.velocitypowered.proxy.protocol.ProtocolUtils;
import com.velocitypowered.proxy.protocol.StateRegistry;
import com.velocitypowered.proxy.protocol.packet.AvailableCommandsPacket;
import com.velocitypowered.proxy.protocol.packet.PluginMessagePacket;
import net.kyori.adventure.identity.Identity;
import net.kyori.adventure.text.Component;
import org.adde0109.ambassador.Ambassador;
import org.adde0109.ambassador.forge.packet.*;
@ -62,22 +64,37 @@ public enum VelocityForgeBackendConnectionPhase implements BackendConnectionPhas
VelocityForgeBackendConnectionPhase() {
}
public void handle(VelocityServerConnection server, ConnectedPlayer player, IForgeLoginWrapperPacket<Context> message) {
public void handle(VelocityServerConnection server, ConnectedPlayer player, IForgeLoginWrapperPacket message) {
VelocityForgeBackendConnectionPhase newPhase = getNewPhase(server,message);
server.setConnectionPhase(newPhase);
//Forge -> Forge
VelocityForgeClientConnectionPhase clientPhase = (VelocityForgeClientConnectionPhase) player.getPhase();
if (!player.isActive()) {
if (player.getPhase() == VelocityForgeClientConnectionPhase.NOT_STARTED ||
player.getPhase() == VelocityForgeClientConnectionPhase.IN_PROGRESS) {
//Initial Forge
player.getConnection().write(message);
return;
}
if (!clientPhase.consideredComplete()) {
//Initial Forge
//Forge -> Forge
//Reset client if not ready to receive new handshake
VelocityForgeClientConnectionPhase clientPhase = (VelocityForgeClientConnectionPhase) player.getPhase();
if (clientPhase.getResetType() == VelocityForgeClientConnectionPhase.clientResetType.CRP ||
clientPhase.getResetType() == VelocityForgeClientConnectionPhase.clientResetType.SR) {
clientPhase.resetConnectionPhase(player);
player.getConnection().write(message);
return;
}
//STILL WIP
if (!Ambassador.getInstance().config.isDebugMode()) {
server.disconnect();
return;
}
if (clientPhase.getResetType() == VelocityForgeClientConnectionPhase.clientResetType.NONE) {
if (message instanceof ModListPacket modListPacket) {
clientPhase.forgeHandshake = new ForgeHandshake();
}
@ -86,29 +103,6 @@ public enum VelocityForgeBackendConnectionPhase implements BackendConnectionPhas
}
player.getConnection().write(message);
} else {
//Reset client if not ready to receive new handshake
if (clientPhase.getResetType() == VelocityForgeClientConnectionPhase.ClientResetType.CRP ||
clientPhase.getResetType() == VelocityForgeClientConnectionPhase.ClientResetType.SR) {
clientPhase.resetConnectionPhase(player);
player.getConnection().write(message);
return;
}
if (clientPhase.forgeHandshake.getModListReplyPacket() == null) {
//We have nothing to respond with during this handshake. Unable to proceed.
if (Ambassador.getInstance().config.isEnableKickReset()) {
//Kick-reset
Ambassador.getInstance().reconnectSwitchPlayer(player);
} else {
Ambassador.getInstance().logger.error("Unable for {} to switch servers. Vanilla({}) -> Forge({}) switch " +
"without client side mod or kick-reset enabled is not yet supported!",
player.getGameProfile().getName(), player.getConnectedServer().getServerInfo().getName(),
server.getServerInfo().getName());
server.disconnect();
}
return;
}
if (message instanceof ModListPacket modListPacket) {
remainingRegistries = new CountDownLatch(modListPacket.getRegistries().size());
@ -126,17 +120,14 @@ public enum VelocityForgeBackendConnectionPhase implements BackendConnectionPhas
}).thenAcceptAsync((v) -> {
if(Ambassador.getInstance().config.isDebugMode()) {
player.sendMessage(Component.text("Handshake took: " + (System.currentTimeMillis()-time) + " ms"));
player.sendMessage(Component.text("Handshake took: " + (System.currentTimeMillis()-time)/1000 + " seconds"));
player.sendMessage(Component.text("Avg packet time" +
(System.currentTimeMillis()-time)/modListPacket.getRegistries().size() + " ms"));
((System.currentTimeMillis()-time)/1000)/modListPacket.getRegistries().size() + " seconds"));
}
if (Ambassador.getInstance().config.isBypassRegistryCheck() ||
clientPhase.forgeHandshake.isCompatible(handshake)) {
server.ensureConnected().write(clientPhase.forgeHandshake.getModListReplyPacket());
} else if (Ambassador.getInstance().config.isEnableKickReset()) {
//Kick-reset
Ambassador.getInstance().reconnectSwitchPlayer(player);
} else {
Ambassador.getInstance().logger.error("Unable to switch due to the registries of " +
server.getServer().getServerInfo().getName() + " being different from the registries of " +
@ -150,13 +141,10 @@ public enum VelocityForgeBackendConnectionPhase implements BackendConnectionPhas
remainingRegistries.countDown();
} else if (message instanceof ConfigDataPacket) {
server.getConnection().write(new ACKPacket(Context.fromContext(message.getContext(), true)));
} else if (message instanceof GenericForgeLoginWrapperPacket<Context> packet
&& ForgeHandshakeUtils.ThirdPartyRegistryUtils.isThirdPartyPacket(packet)) {
server.getConnection().write(
ForgeHandshakeUtils.ThirdPartyRegistryUtils.getThirdPartyChannel(packet).
generateResponsePacket(
Context.ClientContext.fromContext(packet.getContext(), true),
clientPhase.forgeHandshake));
} else if (message instanceof GenericForgeLoginWrapperPacket<?> packet
&& ForgeHandshakeUtils.SilentGearUtils.isSilentGearPacket(packet.getContent())) {
server.getConnection().write(new ForgeHandshakeUtils.SilentGearUtils.ACKPacket(
Context.fromContext(message.getContext(), true)));
}
}
//Forge server

View File

@ -17,7 +17,6 @@ import io.netty.buffer.Unpooled;
import net.kyori.adventure.text.Component;
import org.adde0109.ambassador.Ambassador;
import org.adde0109.ambassador.forge.packet.Context;
import org.adde0109.ambassador.forge.packet.GenericForgeLoginWrapperPacket;
import org.adde0109.ambassador.forge.packet.IForgeLoginWrapperPacket;
import org.adde0109.ambassador.forge.packet.ModListReplyPacket;
import org.adde0109.ambassador.velocity.client.FML2CRPMResetCompleteDecoder;
@ -25,7 +24,6 @@ import org.adde0109.ambassador.velocity.client.OutboundSuccessHolder;
import org.adde0109.ambassador.velocity.client.ClientPacketQueue;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeUnit;
public enum VelocityForgeClientConnectionPhase implements ClientConnectionPhase {
@ -39,12 +37,17 @@ public enum VelocityForgeClientConnectionPhase implements ClientConnectionPhase
public void complete(ConnectedPlayer player) {
//When no handshake has taken place.
//Test if the client supports CRP.
ClientResetType.CRP.doReset(player);
clientResetType.CRP.doReset(player);
}
@Override
public boolean consideredComplete() {
return true;
}
},
IN_PROGRESS {
},
WAITING_RESET() {
WAITING_RESET {
@Override
void onTransitionToNewPhase(ConnectedPlayer player) {
//We unregister so no plugin sees this client while the client is being reset.
@ -67,7 +70,7 @@ public enum VelocityForgeClientConnectionPhase implements ClientConnectionPhase
if (!(server.getConnection().getType() instanceof ForgeFMLConnectionType)) {
// -> vanilla
complete(player, ((Context.ClientContext) msg.getContext()).success() ? ClientResetType.CRP : null);
complete(player, ((Context.ClientContext) msg.getContext()).success() ? clientResetType.CRP : clientResetType.UNKNOWN);
}
if (player.getConnectionInFlight() != null) {
@ -81,15 +84,8 @@ public enum VelocityForgeClientConnectionPhase implements ClientConnectionPhase
}
},
COMPLETE {
private ClientResetType resetType = ClientResetType.UNKNOWN;
@Override
void onTransitionToNewPhase(ConnectedPlayer player) {
//Send Login Success to client
MinecraftConnection connection = player.getConnection();
((OutboundSuccessHolder) connection.getChannel().pipeline().get(ForgeConstants.SERVER_SUCCESS_LISTENER))
.sendPacket();
connection.setState(StateRegistry.PLAY);
//Plugins may now send packets to client
player.getConnection().getChannel().pipeline().remove(ForgeConstants.PLUGIN_PACKET_QUEUE);
((VelocityServer) Ambassador.getInstance().server).registerConnection(player);
@ -106,34 +102,20 @@ public enum VelocityForgeClientConnectionPhase implements ClientConnectionPhase
}
@Override
public void complete(ConnectedPlayer player) {
public void complete(ConnectedPlayer player, clientResetType resetType) {
if (Ambassador.getInstance().config.isDebugMode()) {
player.sendMessage(Component.text("Not resetting"));
player.sendMessage(Component.text("Forge -> Vanilla - Not resetting"));
}
}
@Override
void setResetType(ConnectedPlayer player, ClientResetType resetType) {
this.resetType = resetType;
if (Ambassador.getInstance().config.isDebugMode()) {
player.sendMessage(Component.text("Reset type: " + this.resetType.toString()));
}
}
@Override
public ClientResetType getResetType() {
return resetType;
}
};
//TODO: Make a new class that's linked to each player with these fields instead of having them in this phase class
public ForgeHandshake forgeHandshake = new ForgeHandshake();
public boolean handle(ConnectedPlayer player, IForgeLoginWrapperPacket<Context.ClientContext> msg, VelocityServerConnection server) {
private clientResetType resetType = clientResetType.UNKNOWN;
if (msg.getContext().getChannelName().equals("zeta:main")) {
forgeHandshake.zetaFlagsPacket = (GenericForgeLoginWrapperPacket<Context.ClientContext>) msg;
}
public boolean handle(ConnectedPlayer player, IForgeLoginWrapperPacket<Context.ClientContext> msg, VelocityServerConnection server) {
if (msg instanceof ModListReplyPacket replyPacket) {
ModInfo modInfo = new ModInfo("FML2", replyPacket.getMods().stream().map(
@ -155,24 +137,47 @@ public enum VelocityForgeClientConnectionPhase implements ClientConnectionPhase
return true;
}
public void complete(ConnectedPlayer player) {
complete(player, null);
complete(player, getResetType(player));
}
public void complete(ConnectedPlayer player, ClientResetType resetType) {
public void complete(ConnectedPlayer player, clientResetType resetType) {
MinecraftConnection connection = player.getConnection();
//Send Login Success to client
((OutboundSuccessHolder) connection.getChannel().pipeline().get(ForgeConstants.SERVER_SUCCESS_LISTENER))
.sendPacket();
connection.setState(StateRegistry.PLAY);
//Change phase to COMPLETE
player.setPhase(COMPLETE);
COMPLETE.resetType = resetType;
COMPLETE.onTransitionToNewPhase(player);
COMPLETE.forgeHandshake = forgeHandshake;
if (resetType != null) {
COMPLETE.setResetType(player, resetType);
}
if (Ambassador.getInstance().config.isDebugMode()) {
player.sendMessage(Component.text("Forge handshake complete"));
player.sendMessage(Component.text("Reset type: " + resetType.toString()));
}
}
private clientResetType getResetType(ConnectedPlayer player) {
if (Ambassador.getInstance().config.isDebugMode()) {
player.sendMessage(Component.text("Scanning modlist for client reset mods"));
}
if (player.getModInfo().isPresent()) {
if (player.getModInfo().get().getMods().stream().anyMatch((mod -> mod.getId().equals("clientresetpacket")))) {
return clientResetType.CRP;
} else if (Ambassador.getInstance().config.getServerSwitchCancellationTime() >= 0 &&
player.getModInfo().get().getMods().stream().anyMatch((mod -> mod.getId().equals("serverredirect")
|| mod.getId().equals("srvredirect:red")))
&& player.getVirtualHost().isPresent()) {
return clientResetType.SR;
}
}
return clientResetType.NONE;
}
void onTransitionToNewPhase(ConnectedPlayer player) {
}
@ -186,35 +191,10 @@ public enum VelocityForgeClientConnectionPhase implements ClientConnectionPhase
return false;
}
public ClientResetType getResetType() {
return COMPLETE.getResetType();
public clientResetType getResetType() {
return resetType;
}
private ClientResetType getResetType(ConnectedPlayer player) {
if (Ambassador.getInstance().config.isDebugMode()) {
player.sendMessage(Component.text("Scanning modlist for client reset mods"));
}
if (player.getModInfo().isPresent()) {
if (player.getModInfo().get().getMods().stream().anyMatch((mod -> mod.getId().equals("clientresetpacket")))) {
return ClientResetType.CRP;
} else if (Ambassador.getInstance().config.getServerSwitchCancellationTime() >= 0 &&
player.getModInfo().get().getMods().stream().anyMatch((mod -> mod.getId().equals("serverredirect")
|| mod.getId().equals("srvredirect:red")))
&& player.getVirtualHost().isPresent()) {
return ClientResetType.SR;
}
}
return ClientResetType.NONE;
}
void setResetType(ConnectedPlayer player, ClientResetType resetType) {
COMPLETE.setResetType(player, resetType);
}
public void updateResetType(ConnectedPlayer player) {
COMPLETE.setResetType(player, getResetType(player));
}
public enum ClientResetType {
enum clientResetType {
UNKNOWN,
NONE,
CRP {
@ -256,8 +236,6 @@ public enum VelocityForgeClientConnectionPhase implements ClientConnectionPhase
buf.writeBytes((player.getVirtualHost().get().getHostName() + ":"
+ player.getVirtualHost().get().getPort()).getBytes(StandardCharsets.UTF_8));
player.getConnection().write(new PluginMessagePacket("srvredirect:red", buf));
Ambassador.getInstance().reconnectSwitchPlayer(player);
}
};

View File

@ -4,38 +4,31 @@ public class Context {
private final int responseID;
private final String channelName;
private Context(int responseID, String channelName) {
private Context(int responseID) {
this.responseID = responseID;
this.channelName = channelName;
}
public static Context createContext(int responseID, String channelName) {
return new Context(responseID, channelName);
public static Context createContext(int responseID) {
return new Context(responseID);
}
public static ClientContext createClientContext(int responseID, boolean clientSuccess, String channelName) {
return new ClientContext(responseID, clientSuccess, channelName);
public static ClientContext createClientContext(int responseID, boolean clientSuccess) {
return new ClientContext(responseID,clientSuccess);
}
public static ClientContext fromContext(Context context, boolean clientSuccess) {
return new ClientContext(context.responseID, clientSuccess, context.channelName);
return new ClientContext(context.responseID,clientSuccess);
}
public int getResponseID() {
return responseID;
}
public String getChannelName() {
return channelName;
}
public static class ClientContext extends Context {
private final boolean clientSuccess;
ClientContext(int responseID, boolean clientSuccess, String channelName) {
super(responseID, channelName);
ClientContext(int responseID, boolean clientSuccess) {
super(responseID);
this.clientSuccess = clientSuccess;
}

View File

@ -8,7 +8,7 @@ public class GenericForgeLoginWrapperPacket<T extends Context> implements IForge
private final byte[] content;
private final T context;
public GenericForgeLoginWrapperPacket(byte[] content, T context) {
GenericForgeLoginWrapperPacket(byte[] content, T context) {
this.content = content;
this.context = context;
}

View File

@ -1,39 +1,10 @@
package org.adde0109.ambassador.forge.packet;
import com.velocitypowered.proxy.protocol.ProtocolUtils;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
public class ModDataPacket implements IForgeLoginWrapperPacket<Context> {
private final byte[] content;
private final Context context;
public class ModDataPacket extends GenericForgeLoginWrapperPacket<Context> {
ModDataPacket(byte[] content, Context context) {
this.content = content;
this.context = context;
}
static public ModDataPacket read(ByteBuf input, Context context) {
byte[] content = new byte[input.readableBytes()];
input.readBytes(content);
return new ModDataPacket(content, context);
}
@Override
public ByteBuf encode() {
ByteBuf buf = Unpooled.buffer();
ProtocolUtils.writeVarInt(buf, 5); //PacketID
buf.writeBytes(content);
return buf;
}
public byte[] getContent() {
return content;
}
@Override
public Context getContext() {
return context;
super(content, context);
}
}

View File

@ -46,7 +46,7 @@ public class ModListPacket implements IForgeLoginWrapperPacket<Context> {
registries.add(ProtocolUtils.readString(input, 32767));
List<String> dataPackRegistries = null;
if (FML3 && input.isReadable()) {
if (FML3) {
dataPackRegistries = new ArrayList<>();
len = ProtocolUtils.readVarInt(input);
for (int x = 0; x < len; x++)

View File

@ -9,137 +9,106 @@ import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.DecoderException;
import io.netty.handler.codec.MessageToMessageCodec;
import io.netty.util.ReferenceCountUtil;
import org.adde0109.ambassador.Ambassador;
import org.adde0109.ambassador.forge.ForgeHandshakeUtils;
import org.adde0109.ambassador.forge.packet.*;
import java.util.HashMap;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class ForgeLoginWrapperCodec extends MessageToMessageCodec<DeferredByteBufHolder, IForgeLoginWrapperPacket<?>> {
private final boolean FML3;
private final Map<Integer, Context> loginWrapperContexts = new HashMap<>();
private final List<Integer> loginWrapperIDs = new ArrayList<>();
public ForgeLoginWrapperCodec(boolean fml3) {
FML3 = fml3;
}
@Override
public boolean acceptInboundMessage(Object msg) throws Exception {
return (msg instanceof LoginPluginMessagePacket
&& ((LoginPluginMessagePacket) msg).getChannel().equals("fml:loginwrapper"))
|| (msg instanceof LoginPluginResponsePacket
&& loginWrapperContexts.containsKey(((LoginPluginResponsePacket) msg).getId()));
}
@Override
protected void decode(ChannelHandlerContext ctx, DeferredByteBufHolder in, List<Object> out) throws Exception {
ByteBuf buf = in.content();
Context context;
if (in instanceof LoginPluginResponsePacket msg) {
//Continue from stored context
context = Context.fromContext(
loginWrapperContexts.remove(((LoginPluginResponsePacket) msg).getId()), msg.isSuccess());
if (!msg.isSuccess()) {
//Nothing to read, just create an empty packet.
out.add(GenericForgeLoginWrapperPacket.read(buf, context));
return;
} else {
String channel = ProtocolUtils.readString(buf); //Read the channel even though we know the channel by context.
Ambassador.getInstance();
}
if (in instanceof LoginPluginMessagePacket msg && msg.getChannel().equals("fml:loginwrapper")) {
context = Context.createContext(msg.getId());
} else if (in instanceof LoginPluginResponsePacket msg && loginWrapperIDs.remove(Integer.valueOf(msg.getId()))) {
context = Context.createClientContext(msg.getId(), msg.isSuccess());
} else {
//New context.
LoginPluginMessagePacket msg = (LoginPluginMessagePacket) in;
String channel = ProtocolUtils.readString(buf);
context = Context.createContext(msg.getId(), channel);
ctx.fireChannelRead(in.retain());
return;
}
//Decoding of data starts here - channel already read
int originalReaderIndex = buf.readerIndex();
if (!context.getChannelName().equals("fml:handshake")) {
out.add(GenericForgeLoginWrapperPacket.read(buf, context));
} else {
int length = ProtocolUtils.readVarInt(buf);
int packetID = ProtocolUtils.readVarInt(buf);
if (context instanceof Context.ClientContext clientContext) {
switch (packetID) {
case 2:
out.add(ModListReplyPacket.read(buf, clientContext));
break;
case 99:
out.add(new ACKPacket(clientContext));
break;
default:
//Undo decoding
buf.readerIndex(originalReaderIndex);
out.add(GenericForgeLoginWrapperPacket.read(buf, context));
if (Ambassador.getInstance().config.isDebugMode()) {
Ambassador.getInstance().logger.warn(
"Unrecognised packet id received from client on fml:handshake: " + packetID);
}
}
try {
String channel = ProtocolUtils.readString(buf);
if (!channel.equals("fml:handshake")) {
throw new DecoderException();
} else {
switch (packetID) {
case 1:
out.add(ModListPacket.read(buf, context, FML3));
break;
case 3:
out.add(RegistryPacket.read(buf, context, FML3));
break;
case 4:
out.add(ConfigDataPacket.read(buf, context, FML3));
break;
case 5:
if (FML3) {
out.add(ModDataPacket.read(buf, context));
int length = ProtocolUtils.readVarInt(buf);
int packetID = ProtocolUtils.readVarInt(buf);
if (context instanceof Context.ClientContext clientContext) {
switch (packetID) {
case 2:
out.add(ModListReplyPacket.read(buf, clientContext));
break;
}
default:
//Undo decoding
buf.readerIndex(originalReaderIndex);
out.add(GenericForgeLoginWrapperPacket.read(buf, context));
if (Ambassador.getInstance().config.isDebugMode()) {
Ambassador.getInstance().logger.warn(
"Unrecognised packet id received from server on fml:handshake: " + packetID);
}
case 99:
out.add(new ACKPacket(clientContext));
break;
default:
throw new DecoderException();
}
} else {
switch (packetID) {
case 1:
out.add(ModListPacket.read(buf, context, FML3));
break;
case 3:
out.add(RegistryPacket.read(buf, context, FML3));
break;
case 4:
out.add(ConfigDataPacket.read(buf, context, FML3));
break;
case 5:
if (FML3) {
buf.readerIndex(originalReaderIndex);
out.add(ModDataPacket.read(buf, context));
break;
}
default:
throw new DecoderException();
}
}
}
} catch (DecoderException e) {
buf.readerIndex(originalReaderIndex);
out.add(GenericForgeLoginWrapperPacket.read(buf, context));
}
}
@Override
protected void encode(ChannelHandlerContext ctx, IForgeLoginWrapperPacket<?> msg, List<Object> out) throws Exception {
ByteBuf wrapped;
boolean data = !(msg.getContext() instanceof Context.ClientContext clientContext && !clientContext.success());
boolean includeLength = !(msg instanceof GenericForgeLoginWrapperPacket);
String channel = msg.getContext().getChannelName();
wrapped = Unpooled.buffer();
if (data) {
if (msg instanceof GenericForgeLoginWrapperPacket<?>) {
wrapped = msg.encode();
} else {
String channel = "fml:handshake";
if (msg instanceof ForgeHandshakeUtils.SilentGearUtils.ACKPacket) {
channel = "silentgear:network";
}
wrapped = Unpooled.buffer();
ByteBuf encoded = msg.encode();
ProtocolUtils.writeString(wrapped, channel);
if (includeLength)
ProtocolUtils.writeVarInt(wrapped, encoded.readableBytes());
ProtocolUtils.writeVarInt(wrapped, encoded.readableBytes());
wrapped.writeBytes(encoded);
encoded.release();
}
if (msg.getContext() instanceof Context.ClientContext clientContext) {
out.add(new LoginPluginResponsePacket(clientContext.getResponseID(), clientContext.success(), wrapped));
} else {
out.add(new LoginPluginMessagePacket(msg.getContext().getResponseID(), "fml:loginwrapper", wrapped));
if (!(msg instanceof ModDataPacket)) { //ModDataPacket doesn't require a response
this.loginWrapperContexts.put(msg.getContext().getResponseID(), msg.getContext());
if (!(msg instanceof ModDataPacket)) {
this.loginWrapperIDs.add(msg.getContext().getResponseID());
}
}
}

View File

@ -71,12 +71,6 @@ public class VelocityEventHandler {
player.setModInfo(new ModInfo("Channels", event.getChannels().stream().map((id) -> {
return new ModInfo.Mod(id.getId(), "");
}).toList()));
VelocityForgeClientConnectionPhase clientPhase = (VelocityForgeClientConnectionPhase) player.getPhase();
//If reset typ is still unknown, set it!
if (clientPhase.getResetType() == VelocityForgeClientConnectionPhase.ClientResetType.UNKNOWN) {
clientPhase.updateResetType(player);
}
}
}

View File

@ -37,8 +37,7 @@ public class ForgeLoginSessionHandler implements MinecraftSessionHandler {
if (serverConnection.getConnection() != null) {
ConnectedPlayer player = serverConnection.getPlayer();
VelocityForgeClientConnectionPhase clientPhase = (VelocityForgeClientConnectionPhase) player.getPhase();
clientPhase.complete(player);
((VelocityForgeClientConnectionPhase) player.getPhase()).complete(player);
}
return true;

View File

@ -28,7 +28,7 @@ public class FML2CRPMResetCompleteDecoder extends ChannelInboundHandlerAdapter {
if (id == 98) {
try {
ctx.fireChannelRead(GenericForgeLoginWrapperPacket.read(
Unpooled.EMPTY_BUFFER, Context.createClientContext(id, success, "fml:handshake")));
Unpooled.EMPTY_BUFFER, Context.createClientContext(id, success)));
} finally {
buf.release();
}

View File

@ -1,5 +1,5 @@
# Do not change this
config-version = "2.1"
config-version = "2.0"
# How much time the player has to reconnect before canceling the server switch. (In seconds)
# Only for players with ServerRedirect mod installed. Set to -1 to disable ServerRedirect mod support.
@ -17,13 +17,5 @@ bypass-registry-checks = false
# Warning: This is a safety measure for when bypassRegistryCheck is true. Setting this to also true can cause crashes.
bypass-mod-checks = false
# Only for debug/troubleshooting
debug-mode = false
# Make the player reconnect when server switching as a last resort if
# the player can't reset or switch servers without resetting.
enable-kick-reset = false
# Message to display when player gets kicked due to server switching according to enable-kick-reset.
# This input is deserialzied as MiniMessage and support formating according to the MiniMessage format.
reconnect-message = "<red>Please reconnect.</red>"
#Only for debug/troubleshooting
debug-mode = false