Added BungeeGuard support

This commit is contained in:
Nanit 2021-11-13 11:45:45 +02:00
parent ebd6baf7b5
commit 14fd8f094c
5 changed files with 73 additions and 1 deletions

View File

@ -58,10 +58,13 @@ The server supports player info forwarding from the proxy. There are two type of
* `LEGACY` - The **BungeeCord** IP forwarding. * `LEGACY` - The **BungeeCord** IP forwarding.
* `MODERN` - **Velocity** native info forwarding type. * `MODERN` - **Velocity** native info forwarding type.
* `BUNGEE_GUARD` - **BungeeGuard** forwarding type.
If you use BungeeCord, or Velocity with `LEGACY` forwarding, just set this type in the config. If you use BungeeCord, or Velocity with `LEGACY` forwarding, just set this type in the config.
If you use Velocity with `MODERN` info forwarding, set this type and paste secret key from Velocity If you use Velocity with `MODERN` info forwarding, set this type and paste secret key from Velocity
config into `secret` field. config into `secret` field.
If you installed BungeeGuard on your proxy, then use `BUNGEE_GUARD` forwarding type.
Then add your tokens to `tokens` list.
### Contributing ### Contributing

View File

@ -17,6 +17,7 @@ dependencies {
implementation 'org.spongepowered:configurate-yaml:4.1.2' implementation 'org.spongepowered:configurate-yaml:4.1.2'
implementation 'io.netty:netty-all:4.1.54.Final' implementation 'io.netty:netty-all:4.1.54.Final'
implementation 'net.kyori:adventure-nbt:4.9.2' implementation 'net.kyori:adventure-nbt:4.9.2'
implementation 'com.grack:nanojson:1.7'
} }
jar { jar {

View File

@ -1,5 +1,9 @@
package ru.nanit.limbo.connection; package ru.nanit.limbo.connection;
import com.grack.nanojson.JsonArray;
import com.grack.nanojson.JsonObject;
import com.grack.nanojson.JsonParser;
import com.grack.nanojson.JsonParserException;
import io.netty.buffer.Unpooled; import io.netty.buffer.Unpooled;
import io.netty.channel.Channel; import io.netty.channel.Channel;
import io.netty.channel.ChannelFutureListener; import io.netty.channel.ChannelFutureListener;
@ -29,6 +33,7 @@ import java.net.InetSocketAddress;
import java.net.SocketAddress; import java.net.SocketAddress;
import java.security.InvalidKeyException; import java.security.InvalidKeyException;
import java.security.MessageDigest; import java.security.MessageDigest;
import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.ThreadLocalRandom; import java.util.concurrent.ThreadLocalRandom;
@ -124,6 +129,10 @@ public class ClientConnection extends ChannelInboundHandlerAdapter {
} else { } else {
disconnectLogin("You've enabled player info forwarding. You need to connect with proxy"); disconnectLogin("You've enabled player info forwarding. You need to connect with proxy");
} }
} else if (server.getConfig().getInfoForwarding().isBungeeGuard()) {
if (!checkBungeeGuardHandshake(handshake.getHost())) {
disconnectLogin("Invalid BungeeGuard token or handshake format");
}
} }
return; return;
} }
@ -292,6 +301,43 @@ public class ClientConnection extends ChannelInboundHandlerAdapter {
this.address = new InetSocketAddress(host, ((InetSocketAddress)this.address).getPort()); this.address = new InetSocketAddress(host, ((InetSocketAddress)this.address).getPort());
} }
private boolean checkBungeeGuardHandshake(String handshake) {
String[] split = handshake.split("\00");
if (split.length != 4)
return false;
String socketAddressHostname = split[1];
UUID uuid = UuidUtil.fromString(split[2]);
JsonArray arr;
try {
arr = JsonParser.array().from(split[3]);
} catch (JsonParserException e) {
return false;
}
String token = null;
for (Object obj : arr) {
if (obj instanceof JsonObject) {
JsonObject prop = (JsonObject) obj;
if (prop.getString("name").equals("bungeeguard-token")) {
token = prop.getString("value");
break;
}
}
}
if (!server.getConfig().getInfoForwarding().hasToken(token))
return false;
setAddress(socketAddressHostname);
gameProfile.setUuid(uuid);
return true;
}
private boolean checkVelocityKeyIntegrity(ByteMessage buf) { private boolean checkVelocityKeyIntegrity(ByteMessage buf) {
byte[] signature = new byte[32]; byte[] signature = new byte[32];
buf.readBytes(signature); buf.readBytes(signature);

View File

@ -6,11 +6,13 @@ import org.spongepowered.configurate.serialize.SerializationException;
import org.spongepowered.configurate.serialize.TypeSerializer; import org.spongepowered.configurate.serialize.TypeSerializer;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.List;
public class InfoForwarding { public class InfoForwarding {
private Type type; private Type type;
private byte[] secretKey; private byte[] secretKey;
private List<String> tokens;
public Type getType() { public Type getType() {
return type; return type;
@ -20,6 +22,14 @@ public class InfoForwarding {
return secretKey; return secretKey;
} }
public List<String> getTokens() {
return tokens;
}
public boolean hasToken(String token) {
return tokens != null && token != null && tokens.contains(token);
}
public boolean isNone() { public boolean isNone() {
return type == Type.NONE; return type == Type.NONE;
} }
@ -32,10 +42,15 @@ public class InfoForwarding {
return type == Type.MODERN; return type == Type.MODERN;
} }
public boolean isBungeeGuard() {
return type == Type.BUNGEE_GUARD;
}
public enum Type { public enum Type {
NONE, NONE,
LEGACY, LEGACY,
MODERN MODERN,
BUNGEE_GUARD
} }
public static class Serializer implements TypeSerializer<InfoForwarding> { public static class Serializer implements TypeSerializer<InfoForwarding> {
@ -54,6 +69,10 @@ public class InfoForwarding {
forwarding.secretKey = node.node("secret").getString("").getBytes(StandardCharsets.UTF_8); forwarding.secretKey = node.node("secret").getString("").getBytes(StandardCharsets.UTF_8);
} }
if (forwarding.type == Type.BUNGEE_GUARD) {
forwarding.tokens = node.node("tokens").getList(String.class);
}
return forwarding; return forwarding;
} }

View File

@ -69,10 +69,13 @@ title:
# - NONE # - NONE
# - LEGACY # - LEGACY
# - MODERN # - MODERN
# - BUNGEE_GUARD
# Don't use secret if you not use MODERN type # Don't use secret if you not use MODERN type
infoForwarding: infoForwarding:
type: NONE type: NONE
secret: '<YOUR_SECRET_HERE>' secret: '<YOUR_SECRET_HERE>'
tokens:
- '<BUNGEE_GUARD_TOKEN>'
# Read timeout for connections in milliseconds # Read timeout for connections in milliseconds
readTimeout: 30000 readTimeout: 30000