-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use Netty directly instead of going through the system TCP stack
- Loading branch information
Showing
7 changed files
with
213 additions
and
87 deletions.
There are no files selected for viewing
78 changes: 0 additions & 78 deletions
78
src/main/java/io/github/gaming32/worldhost/ProxyClient.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
src/main/java/io/github/gaming32/worldhost/mixin/ServerConnectionListenerAccessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package io.github.gaming32.worldhost.mixin; | ||
|
||
import io.netty.channel.ChannelFuture; | ||
import net.minecraft.server.network.ServerConnectionListener; | ||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.gen.Accessor; | ||
|
||
import java.util.List; | ||
|
||
@Mixin(ServerConnectionListener.class) | ||
public interface ServerConnectionListenerAccessor { | ||
@Accessor | ||
List<ChannelFuture> getChannels(); | ||
} |
55 changes: 55 additions & 0 deletions
55
src/main/java/io/github/gaming32/worldhost/proxy/ProxyChannels.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package io.github.gaming32.worldhost.proxy; | ||
|
||
import io.github.gaming32.worldhost.mixin.ServerConnectionListenerAccessor; | ||
import io.netty.bootstrap.ServerBootstrap; | ||
import io.netty.channel.Channel; | ||
import io.netty.channel.ChannelFuture; | ||
import io.netty.channel.ChannelInitializer; | ||
import io.netty.channel.ChannelPipeline; | ||
import io.netty.channel.local.LocalAddress; | ||
import io.netty.channel.local.LocalServerChannel; | ||
import io.netty.handler.timeout.ReadTimeoutHandler; | ||
import net.minecraft.network.Connection; | ||
import net.minecraft.network.RateKickingConnection; | ||
import net.minecraft.network.protocol.PacketFlow; | ||
import net.minecraft.server.network.LegacyQueryHandler; | ||
import net.minecraft.server.network.ServerConnectionListener; | ||
import net.minecraft.server.network.ServerHandshakePacketListenerImpl; | ||
|
||
import java.net.SocketAddress; | ||
|
||
public class ProxyChannels { | ||
public static SocketAddress startProxyChannel(ServerConnectionListener listener) { | ||
final ServerConnectionListenerAccessor accessor = (ServerConnectionListenerAccessor)listener; | ||
ChannelFuture channel; | ||
synchronized (accessor.getChannels()) { | ||
channel = new ServerBootstrap() | ||
.channel(LocalServerChannel.class) | ||
.childHandler(new ChannelInitializer<>() { | ||
@Override | ||
protected void initChannel(Channel ch) { | ||
final ChannelPipeline pipeline = ch.pipeline().addLast("timeout", new ReadTimeoutHandler(30)); | ||
if (listener.getServer().repliesToStatus()) { | ||
pipeline.addLast("legacy_query", new LegacyQueryHandler(listener.getServer())); | ||
} | ||
Connection.configureSerialization(pipeline, PacketFlow.SERVERBOUND, false, null); | ||
final int rateLimit = listener.getServer().getRateLimitPacketsPerSecond(); | ||
final Connection connection = rateLimit > 0 | ||
? new RateKickingConnection(rateLimit) | ||
: new Connection(PacketFlow.SERVERBOUND); | ||
listener.getConnections().add(connection); | ||
connection.configurePacketHandler(pipeline); | ||
connection.setListenerForServerboundHandshake( | ||
new ServerHandshakePacketListenerImpl(listener.getServer(), connection) | ||
); | ||
} | ||
}) | ||
.group(ServerConnectionListener.SERVER_EVENT_GROUP.get()) | ||
.localAddress(LocalAddress.ANY) | ||
.bind() | ||
.syncUninterruptibly(); | ||
accessor.getChannels().add(channel); | ||
} | ||
return channel.channel().localAddress(); | ||
} | ||
} |
122 changes: 122 additions & 0 deletions
122
src/main/java/io/github/gaming32/worldhost/proxy/ProxyClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
package io.github.gaming32.worldhost.proxy; | ||
|
||
import io.github.gaming32.worldhost.WorldHost; | ||
import io.github.gaming32.worldhost.protocol.proxy.ProxyPassthrough; | ||
import io.netty.bootstrap.Bootstrap; | ||
import io.netty.buffer.ByteBuf; | ||
import io.netty.buffer.Unpooled; | ||
import io.netty.channel.Channel; | ||
import io.netty.channel.ChannelFutureListener; | ||
import io.netty.channel.ChannelHandlerContext; | ||
import io.netty.channel.ChannelInitializer; | ||
import io.netty.channel.SimpleChannelInboundHandler; | ||
import io.netty.channel.local.LocalChannel; | ||
import net.minecraft.server.network.ServerConnectionListener; | ||
|
||
import java.io.IOException; | ||
import java.net.InetAddress; | ||
import java.util.function.Supplier; | ||
|
||
public final class ProxyClient extends SimpleChannelInboundHandler<ByteBuf> { | ||
private static final int PACKET_SIZE = 0xffff; | ||
|
||
private final InetAddress remoteAddress; | ||
private final long connectionId; | ||
private final Supplier<ProxyPassthrough> proxy; | ||
|
||
private Channel channel; | ||
private boolean closed; | ||
|
||
public ProxyClient( | ||
InetAddress remoteAddress, | ||
long connectionId, | ||
Supplier<ProxyPassthrough> proxy | ||
) throws IOException { | ||
this.remoteAddress = remoteAddress; | ||
this.connectionId = connectionId; | ||
this.proxy = proxy; | ||
if (proxy.get() == null) { | ||
WorldHost.LOGGER.error("ProxyPassthrough for {} ({}) is initially null.", connectionId, remoteAddress); | ||
} | ||
} | ||
|
||
@Override | ||
public void channelActive(ChannelHandlerContext ctx) throws Exception { | ||
super.channelActive(ctx); | ||
channel = ctx.channel(); | ||
WorldHost.LOGGER.info("Started proxy client from {}", remoteAddress); | ||
} | ||
|
||
@Override | ||
public void channelInactive(ChannelHandlerContext ctx) { | ||
WorldHost.CONNECTED_PROXY_CLIENTS.remove(connectionId); | ||
close(); | ||
} | ||
|
||
@Override | ||
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { | ||
WorldHost.LOGGER.error("Proxy client connection for {} had error", remoteAddress, cause); | ||
WorldHost.CONNECTED_PROXY_CLIENTS.remove(connectionId); | ||
close(); | ||
} | ||
|
||
@Override | ||
protected void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) { | ||
final ProxyPassthrough proxy = this.proxy.get(); | ||
if (proxy == null) { | ||
close(); | ||
return; | ||
} | ||
|
||
while (true) { | ||
int len = Math.min(msg.readableBytes(), PACKET_SIZE); | ||
if (len == 0) break; | ||
final byte[] buffer = new byte[len]; | ||
msg.readBytes(buffer); | ||
proxy.proxyS2CPacket(connectionId, buffer); | ||
} | ||
} | ||
|
||
public void start() { | ||
WorldHost.LOGGER.info("Starting proxy client from {}", remoteAddress); | ||
new Bootstrap() | ||
.group(ServerConnectionListener.SERVER_EVENT_GROUP.get()) | ||
.handler(new ChannelInitializer<>() { | ||
@Override | ||
protected void initChannel(Channel ch) { | ||
ch.pipeline().addLast("handler", ProxyClient.this); | ||
} | ||
}) | ||
.channel(LocalChannel.class) | ||
.connect(WorldHost.proxySocketAddress) | ||
.syncUninterruptibly(); | ||
} | ||
|
||
public void close() { | ||
if (closed) return; | ||
closed = true; | ||
try { | ||
channel.close(); | ||
final ProxyPassthrough proxy = this.proxy.get(); | ||
if (proxy != null) { | ||
proxy.proxyDisconnect(connectionId); | ||
} | ||
WorldHost.LOGGER.info("Proxy client connection for {} closed", remoteAddress); | ||
} catch (Exception e) { | ||
WorldHost.LOGGER.error("Proxy client connection for {} failed to close", remoteAddress, e); | ||
} | ||
} | ||
|
||
public void send(byte[] message) { | ||
if (channel.eventLoop().inEventLoop()) { | ||
doSend(message); | ||
} else { | ||
channel.eventLoop().execute(() -> doSend(message)); | ||
} | ||
} | ||
|
||
private void doSend(byte[] message) { | ||
channel.writeAndFlush(Unpooled.wrappedBuffer(message)) | ||
.addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters