Connect Geyser players directly to the server for plugin versions (#2413)

- Faster loading times and improved latency; Geyser no longer creates a physical TCP connection to join the server
- Less configuration: remote address and port are now irrelevant
- Accurate IP addresses without needing Floodgate.

Co-authored-by: Redned <redned235@gmail.com>
This commit is contained in:
Camotoy 2021-07-31 12:52:49 -04:00 committed by GitHub
parent 1d04a61a46
commit 002be32bb3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
14 changed files with 617 additions and 11 deletions

View file

@ -0,0 +1,82 @@
/*
* Copyright (c) 2019-2021 GeyserMC. http://geysermc.org
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* @author GeyserMC
* @link https://github.com/GeyserMC/Geyser
*/
package org.geysermc.platform.velocity;
import com.github.steveice10.packetlib.io.local.LocalServerChannelWrapper;
import com.velocitypowered.api.proxy.ProxyServer;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
import io.netty.channel.local.LocalAddress;
import org.geysermc.connector.bootstrap.GeyserBootstrap;
import org.geysermc.connector.common.GeyserInjector;
import java.lang.reflect.Field;
import java.util.function.Supplier;
public class GeyserVelocityInjector extends GeyserInjector {
private final ProxyServer proxy;
public GeyserVelocityInjector(ProxyServer proxy) {
this.proxy = proxy;
}
@Override
@SuppressWarnings("unchecked")
protected void initializeLocalChannel0(GeyserBootstrap bootstrap) throws Exception {
Field cm = proxy.getClass().getDeclaredField("cm");
cm.setAccessible(true);
Object connectionManager = cm.get(proxy);
Class<?> connectionManagerClass = connectionManager.getClass();
Supplier<ChannelInitializer<Channel>> serverChannelInitializerHolder = (Supplier<ChannelInitializer<Channel>>) connectionManagerClass
.getMethod("getServerChannelInitializer")
.invoke(connectionManager);
ChannelInitializer<Channel> channelInitializer = serverChannelInitializerHolder.get();
// Is set on Velocity's end for listening to Java connections - required on ours or else the initial world load process won't finish sometimes
Field serverWriteMarkField = connectionManagerClass.getDeclaredField("SERVER_WRITE_MARK");
serverWriteMarkField.setAccessible(true);
WriteBufferWaterMark serverWriteMark = (WriteBufferWaterMark) serverWriteMarkField.get(null);
EventLoopGroup bossGroup = (EventLoopGroup) connectionManagerClass.getMethod("getBossGroup").invoke(connectionManager);
Field workerGroupField = connectionManagerClass.getDeclaredField("workerGroup");
workerGroupField.setAccessible(true);
EventLoopGroup workerGroup = (EventLoopGroup) workerGroupField.get(connectionManager);
ChannelFuture channelFuture = (new ServerBootstrap()
.channel(LocalServerChannelWrapper.class)
.childHandler(channelInitializer)
.group(bossGroup, workerGroup) // Cannot be DefaultEventLoopGroup
.childOption(ChannelOption.WRITE_BUFFER_WATER_MARK, serverWriteMark) // Required or else rare network freezes can occur
.localAddress(LocalAddress.ANY))
.bind()
.syncUninterruptibly();
this.localChannel = channelFuture;
this.serverSocketAddress = channelFuture.channel().localAddress();
}
}

View file

@ -28,6 +28,7 @@ package org.geysermc.platform.velocity;
import com.google.inject.Inject;
import com.velocitypowered.api.command.CommandManager;
import com.velocitypowered.api.event.Subscribe;
import com.velocitypowered.api.event.proxy.ListenerBoundEvent;
import com.velocitypowered.api.event.proxy.ProxyInitializeEvent;
import com.velocitypowered.api.event.proxy.ProxyShutdownEvent;
import com.velocitypowered.api.plugin.Plugin;
@ -45,11 +46,13 @@ import org.geysermc.connector.utils.FileUtils;
import org.geysermc.connector.utils.LanguageUtils;
import org.geysermc.platform.velocity.command.GeyserVelocityCommandExecutor;
import org.geysermc.platform.velocity.command.GeyserVelocityCommandManager;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
import java.io.File;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.UUID;
@ -68,6 +71,7 @@ public class GeyserVelocityPlugin implements GeyserBootstrap {
private GeyserVelocityCommandManager geyserCommandManager;
private GeyserVelocityConfiguration geyserConfig;
private GeyserVelocityInjector geyserInjector;
private GeyserVelocityLogger geyserLogger;
private IGeyserPingPassthrough geyserPingPassthrough;
@ -130,6 +134,9 @@ public class GeyserVelocityPlugin implements GeyserBootstrap {
this.connector = GeyserConnector.start(PlatformType.VELOCITY, this);
this.geyserInjector = new GeyserVelocityInjector(proxyServer);
// Will be initialized after the proxy has been bound
this.geyserCommandManager = new GeyserVelocityCommandManager(connector);
this.commandManager.register("geyser", new GeyserVelocityCommandExecutor(connector));
if (geyserConfig.isLegacyPingPassthrough()) {
@ -141,7 +148,12 @@ public class GeyserVelocityPlugin implements GeyserBootstrap {
@Override
public void onDisable() {
connector.shutdown();
if (connector != null) {
connector.shutdown();
}
if (geyserInjector != null) {
geyserInjector.shutdown();
}
}
@Override
@ -174,8 +186,20 @@ public class GeyserVelocityPlugin implements GeyserBootstrap {
onDisable();
}
@Subscribe
public void onProxyBound(ListenerBoundEvent event) {
// After this bound, we know that the channel initializer cannot change without it being ineffective for Velocity, too
geyserInjector.initializeLocalChannel(this);
}
@Override
public BootstrapDumpInfo getDumpInfo() {
return new GeyserVelocityDumpInfo(proxyServer);
}
@Nullable
@Override
public SocketAddress getSocketAddress() {
return this.geyserInjector.getServerSocketAddress();
}
}