Skip to content

Commit

Permalink
2.0.1: Fix connection error & make users be able to connect directly
Browse files Browse the repository at this point in the history
  • Loading branch information
metabrixkt committed May 10, 2021
1 parent 40658f2 commit adb6440
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 25 deletions.
45 changes: 42 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@

<groupId>nl.thijsalders</groupId>
<artifactId>spigotproxy</artifactId>
<version>2.0.0</version>
<version>2.0.1</version>
<packaging>jar</packaging>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>

<repositories>
Expand Down Expand Up @@ -41,4 +41,43 @@
<version>20.1.0</version>
</dependency>
</dependencies>

<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.2.0</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.0</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>3.0.0-M1</version>
</plugin>
</plugins>
</pluginManagement>
<resources>
<resource>
<targetPath>.</targetPath>
<filtering>true</filtering>
<directory>${basedir}/src/main/resources/</directory>
<includes>
<include>plugin.yml</include>
</includes>
</resource>
</resources>
</build>
</project>
12 changes: 10 additions & 2 deletions src/main/java/nl/thijsalders/spigotproxy/SpigotProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class SpigotProxy extends JavaPlugin {
private static final String CHANNEL_FIELD_NAME;
private static final String MINECRAFT_PACKAGE;

public final HashMap<UUID, SocketAddress> playerProxies = new HashMap<>();
public final HashMap<Object, SocketAddress> playerProxies = new HashMap<>();

static {
String version = Bukkit.getServer().getClass().getPackage().getName().replace(".", ",")
Expand Down Expand Up @@ -170,7 +170,15 @@ public boolean isConnectedDirectly(@NotNull Player player) {
*/
@Nullable
public SocketAddress getProxyAddress(@NotNull Player player) {
return this.playerProxies.get(player.getUniqueId());
try {
Object entityPlayer = player.getClass().getMethod("getHandle").invoke(player);
Object networkManager = ReflectionUtils.getDeclaredField(ReflectionUtils.getDeclaredField(entityPlayer,
"playerConnection"), "networkManager");
return this.playerProxies.get(networkManager);
} catch (Throwable t) {
t.printStackTrace();
return null;
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@
import io.netty.handler.codec.ByteToMessageDecoder;
import io.netty.handler.codec.LineBasedFrameDecoder;
import io.netty.util.CharsetUtil;
import io.netty.util.ReferenceCountUtil;

import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
* Decodes an HAProxy proxy protocol header
Expand Down Expand Up @@ -161,7 +163,12 @@ private static int findVersion(final ByteBuf buffer) {
}

int idx = buffer.readerIndex();
return match(BINARY_PREFIX, buffer, idx) ? buffer.getByte(idx + BINARY_PREFIX_LENGTH) : 1;
if (match(BINARY_PREFIX, buffer, idx)) {
return 2;
} else if (match(TEXT_PREFIX, buffer, idx)) {
return 1;
} else
return -1;
}

/**
Expand Down Expand Up @@ -211,6 +218,8 @@ public boolean isSingleDecode() {
return true;
}

public static final AtomicInteger i = new AtomicInteger(0);

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
super.channelRead(ctx, msg);
Expand All @@ -223,7 +232,9 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception
protected final void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws HAProxyProtocolException {
// determine the specification version
if (version == -1) {
if ((version = findVersion(in)) == -1) {
if ((version = findVersion(in)) == -1) { // not proxy protocol or user is connecting directly
ctx.pipeline().remove(this);
ctx.fireChannelRead(ReferenceCountUtil.retain(in.resetReaderIndex()));
return;
}
}
Expand All @@ -247,6 +258,9 @@ protected final void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object>
} catch (HAProxyProtocolException e) {
fail(ctx, null, e);
}
} else { // user is connecting directly
ctx.pipeline().remove(this);
ctx.fireChannelRead(ReferenceCountUtil.retain(in.resetReaderIndex()));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,28 +41,28 @@ protected void initChannel(SocketChannel channel) throws Exception {
channel.pipeline().addAfter("haproxy-decoder", "haproxy-handler", new ChannelInboundHandlerAdapter() {
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
if (msg instanceof HAProxyMessage) {
HAProxyMessage message = (HAProxyMessage) msg;
try {
if (msg instanceof HAProxyMessage) {
HAProxyMessage message = (HAProxyMessage) msg;

String realAddress = message.sourceAddress();
int realPort = message.sourcePort();
String realAddress = message.sourceAddress();
int realPort = message.sourcePort();

SocketAddress socketAddress = new InetSocketAddress(realAddress, realPort);
SocketAddress socketAddress = new InetSocketAddress(realAddress, realPort);

ChannelHandler handler = channel.pipeline().get("packet_handler");
ChannelHandler handler = channel.pipeline().get("packet_handler");

SocketAddress oldAddress = (SocketAddress) socketAddressField.get(handler);
if (!socketAddress.equals(oldAddress)) {
Object entityPlayer = handler.getClass().getMethod("getPlayer").invoke(handler);
Player player = (Player) entityPlayer.getClass().getMethod("getBukkitEntity")
.invoke(entityPlayer);
SpigotProxy.getInstance().playerProxies.put(player.getUniqueId(), oldAddress);
}
SocketAddress oldAddress = (SocketAddress) socketAddressField.get(handler);
if (!socketAddress.equals(oldAddress))
SpigotProxy.getInstance().playerProxies.put(handler, oldAddress);

socketAddressField.set(handler, socketAddress);
} else {
super.channelRead(ctx, msg);
}
socketAddressField.set(handler, socketAddress);
} else {
super.channelRead(ctx, msg);
}
} catch (Throwable t) {
t.printStackTrace();
}
}
});
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: SpigotProxy
main: nl.thijsalders.spigotproxy.SpigotProxy
version: ${version}
version: ${project.version}
author: thijs_a
authors: ["l_MrBoom_l"]

Expand Down

0 comments on commit adb6440

Please sign in to comment.