-
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.
Merge pull request #73 from Gaming32/netty-proxy
Use Netty directly instead of going through the system TCP stack
- Loading branch information
Showing
8 changed files
with
222 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
20 changes: 20 additions & 0 deletions
20
src/main/java/io/github/gaming32/worldhost/mixin/MixinServerConnectionListener_1.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,20 @@ | ||
package io.github.gaming32.worldhost.mixin; | ||
|
||
import io.github.gaming32.worldhost.WorldHost; | ||
import io.netty.channel.Channel; | ||
import io.netty.channel.ChannelInitializer; | ||
import net.minecraft.server.network.ServerConnectionListener; | ||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.injection.At; | ||
import org.spongepowered.asm.mixin.injection.Inject; | ||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; | ||
|
||
@Mixin(targets = "net.minecraft.server.network.ServerConnectionListener$1") | ||
public abstract class MixinServerConnectionListener_1 extends ChannelInitializer<Channel> { | ||
@Inject(method = "<init>", at = @At("TAIL")) | ||
private void storeClass(ServerConnectionListener this$0, CallbackInfo ci) throws NoSuchMethodException { | ||
if (WorldHost.channelInitializerConstructor == null) { | ||
WorldHost.channelInitializerConstructor = getClass().getDeclaredConstructor(ServerConnectionListener.class); | ||
} | ||
} | ||
} |
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(); | ||
} |
29 changes: 29 additions & 0 deletions
29
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,29 @@ | ||
package io.github.gaming32.worldhost.proxy; | ||
|
||
import io.github.gaming32.worldhost.WorldHost; | ||
import io.github.gaming32.worldhost.mixin.ServerConnectionListenerAccessor; | ||
import io.netty.bootstrap.ServerBootstrap; | ||
import io.netty.channel.ChannelFuture; | ||
import io.netty.channel.local.LocalAddress; | ||
import io.netty.channel.local.LocalServerChannel; | ||
import net.minecraft.server.network.ServerConnectionListener; | ||
|
||
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(WorldHost.createChannelInitializer(listener)) | ||
.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