13
0
geforkt von Mirrors/Velocity

(Implementation) Allow injecting into backend connection

Dieser Commit ist enthalten in:
creeper123123321 2020-04-13 10:35:26 -03:00
Ursprung 4ddc55a5b7
Commit e93a510b0f
5 geänderte Dateien mit 96 neuen und 17 gelöschten Zeilen

Datei anzeigen

@ -44,6 +44,8 @@ import com.velocitypowered.proxy.util.bossbar.VelocityBossBar;
import com.velocitypowered.proxy.util.ratelimit.Ratelimiter; import com.velocitypowered.proxy.util.ratelimit.Ratelimiter;
import com.velocitypowered.proxy.util.ratelimit.Ratelimiters; import com.velocitypowered.proxy.util.ratelimit.Ratelimiters;
import io.netty.bootstrap.Bootstrap; import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup; import io.netty.channel.EventLoopGroup;
import java.io.IOException; import java.io.IOException;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
@ -268,6 +270,10 @@ public class VelocityServer implements ProxyServer {
return this.cm.createWorker(group); return this.cm.createWorker(group);
} }
public ChannelInitializer<Channel> getBackendChannelInitializer() {
return this.cm.backendChannelInitializerHolder.get();
}
public boolean isShutdown() { public boolean isShutdown() {
return shutdown; return shutdown;
} }

Datei anzeigen

@ -80,22 +80,7 @@ public class VelocityServerConnection implements MinecraftConnectionAssociation,
// Note: we use the event loop for the connection the player is on. This reduces context // Note: we use the event loop for the connection the player is on. This reduces context
// switches. // switches.
server.createBootstrap(proxyPlayer.getConnection().eventLoop()) server.createBootstrap(proxyPlayer.getConnection().eventLoop())
.handler(new ChannelInitializer<Channel>() { .handler(server.getBackendChannelInitializer())
@Override
protected void initChannel(Channel ch) throws Exception {
ch.pipeline()
.addLast(READ_TIMEOUT,
new ReadTimeoutHandler(server.getConfiguration().getReadTimeout(),
TimeUnit.MILLISECONDS))
.addLast(FRAME_DECODER, new MinecraftVarintFrameDecoder())
.addLast(FRAME_ENCODER, MinecraftVarintLengthEncoder.INSTANCE)
.addLast(FLOW_HANDLER, new FlowControlHandler())
.addLast(MINECRAFT_DECODER,
new MinecraftDecoder(ProtocolUtils.Direction.CLIENTBOUND))
.addLast(MINECRAFT_ENCODER,
new MinecraftEncoder(ProtocolUtils.Direction.SERVERBOUND));
}
})
.connect(registeredServer.getServerInfo().getAddress()) .connect(registeredServer.getServerInfo().getAddress())
.addListener((ChannelFutureListener) future -> { .addListener((ChannelFutureListener) future -> {
if (future.isSuccess()) { if (future.isSuccess()) {

Datei anzeigen

@ -0,0 +1,45 @@
package com.velocitypowered.proxy.network;
import static com.velocitypowered.proxy.network.Connections.FLOW_HANDLER;
import static com.velocitypowered.proxy.network.Connections.FRAME_DECODER;
import static com.velocitypowered.proxy.network.Connections.FRAME_ENCODER;
import static com.velocitypowered.proxy.network.Connections.MINECRAFT_DECODER;
import static com.velocitypowered.proxy.network.Connections.MINECRAFT_ENCODER;
import static com.velocitypowered.proxy.network.Connections.READ_TIMEOUT;
import com.velocitypowered.proxy.VelocityServer;
import com.velocitypowered.proxy.protocol.ProtocolUtils;
import com.velocitypowered.proxy.protocol.netty.MinecraftDecoder;
import com.velocitypowered.proxy.protocol.netty.MinecraftEncoder;
import com.velocitypowered.proxy.protocol.netty.MinecraftVarintFrameDecoder;
import com.velocitypowered.proxy.protocol.netty.MinecraftVarintLengthEncoder;
import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer;
import io.netty.handler.flow.FlowControlHandler;
import io.netty.handler.timeout.ReadTimeoutHandler;
import java.util.concurrent.TimeUnit;
@SuppressWarnings("WeakerAccess")
public class BackendChannelInitializer extends ChannelInitializer<Channel> {
private final VelocityServer server;
public BackendChannelInitializer(VelocityServer server) {
this.server = server;
}
@Override
protected void initChannel(Channel ch) throws Exception {
ch.pipeline()
.addLast(READ_TIMEOUT,
new ReadTimeoutHandler(server.getConfiguration().getReadTimeout(),
TimeUnit.MILLISECONDS))
.addLast(FRAME_DECODER, new MinecraftVarintFrameDecoder())
.addLast(FRAME_ENCODER, MinecraftVarintLengthEncoder.INSTANCE)
.addLast(FLOW_HANDLER, new FlowControlHandler())
.addLast(MINECRAFT_DECODER,
new MinecraftDecoder(ProtocolUtils.Direction.CLIENTBOUND))
.addLast(MINECRAFT_ENCODER,
new MinecraftEncoder(ProtocolUtils.Direction.SERVERBOUND));
}
}

Datei anzeigen

@ -0,0 +1,35 @@
package com.velocitypowered.proxy.network;
import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer;
import java.util.function.Supplier;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class BackendChannelInitializerHolder implements Supplier<ChannelInitializer<Channel>> {
private static final Logger LOGGER = LogManager.getLogger(ConnectionManager.class);
private ChannelInitializer<Channel> initializer;
BackendChannelInitializerHolder(final ChannelInitializer<Channel> initializer) {
this.initializer = initializer;
}
@Override
public ChannelInitializer<Channel> get() {
return this.initializer;
}
/**
* Sets the channel initializer.
*
* @param initializer the new initializer to use
* @deprecated Internal implementation detail
*/
@Deprecated
public void set(final ChannelInitializer<Channel> initializer) {
LOGGER.warn("The backend channel initializer has been replaced by {}",
Thread.currentThread().getStackTrace()[2]);
this.initializer = initializer;
}
}

Datei anzeigen

@ -42,10 +42,12 @@ public final class ConnectionManager {
private final EventLoopGroup bossGroup; private final EventLoopGroup bossGroup;
private final EventLoopGroup workerGroup; private final EventLoopGroup workerGroup;
private final VelocityServer server; private final VelocityServer server;
// This is intentionally made public for plugins like ViaVersion, which inject their own // These are intentionally made public for plugins like ViaVersion, which inject their own
// protocol logic into the proxy. // protocol logic into the proxy.
@SuppressWarnings("WeakerAccess") @SuppressWarnings("WeakerAccess")
public final ServerChannelInitializerHolder serverChannelInitializer; public final ServerChannelInitializerHolder serverChannelInitializer;
@SuppressWarnings("WeakerAccess")
public final BackendChannelInitializerHolder backendChannelInitializerHolder;
private final DnsAddressResolverGroup resolverGroup; private final DnsAddressResolverGroup resolverGroup;
private final AsyncHttpClient httpClient; private final AsyncHttpClient httpClient;
@ -62,6 +64,8 @@ public final class ConnectionManager {
this.workerGroup = this.transportType.createEventLoopGroup(TransportType.Type.WORKER); this.workerGroup = this.transportType.createEventLoopGroup(TransportType.Type.WORKER);
this.serverChannelInitializer = new ServerChannelInitializerHolder( this.serverChannelInitializer = new ServerChannelInitializerHolder(
new ServerChannelInitializer(this.server)); new ServerChannelInitializer(this.server));
this.backendChannelInitializerHolder = new BackendChannelInitializerHolder(
new BackendChannelInitializer(this.server));
this.resolverGroup = new DnsAddressResolverGroup(new DnsNameResolverBuilder() this.resolverGroup = new DnsAddressResolverGroup(new DnsNameResolverBuilder()
.channelType(this.transportType.datagramChannelClass) .channelType(this.transportType.datagramChannelClass)
.negativeTtl(15) .negativeTtl(15)
@ -204,4 +208,8 @@ public final class ConnectionManager {
public AsyncHttpClient getHttpClient() { public AsyncHttpClient getHttpClient() {
return httpClient; return httpClient;
} }
public BackendChannelInitializerHolder getBackendChannelInitializerHolder() {
return this.backendChannelInitializerHolder;
}
} }