Skip to content

Commit

Permalink
Configurable username prefix (GeyserMC#11)
Browse files Browse the repository at this point in the history
* Add configurable username prefix
  • Loading branch information
williamjohnstone authored Mar 20, 2020
1 parent 2904612 commit 95e1a81
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws E
}

static {
handshakeHandler = new HandshakeHandler(plugin.getConfiguration().getPrivateKey(), false);
handshakeHandler = new HandshakeHandler(plugin.getConfiguration().getPrivateKey(), false, plugin.getConfiguration().getUsernamePrefix());

networkManagerClass = getPrefixedClass("NetworkManager");
loginStartPacketClass = getPrefixedClass("PacketLoginInStart");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public void onLoad() {
getDataFolder().mkdir();
}
config = FloodgateConfig.load(getLogger(), getDataFolder().toPath().resolve("config.yml"), BungeeFloodgateConfig.class);
handshakeHandler = new HandshakeHandler(config.getPrivateKey(), true);
handshakeHandler = new HandshakeHandler(config.getPrivateKey(), true, config.getUsernamePrefix());
}

@Override
Expand Down
5 changes: 5 additions & 0 deletions bungee/src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
# The public key should be used for the Geyser(s) and the private key for the Floodgate(s)
key-file-name: key.pem

# Floodgate appends a prefix to bedrock usernames to avoid conflicts
# However, certain conflicts can cause issues with some plugins so this prefix is configurable using the property below
# It is recommended to use a prefix that does not contain alphanumerical to avoid the possibility of duplicate usernames.
username-prefix: "*"

# Should Bungeecord send the bedrock player data to the servers it is connecting to?
# This requires Floodgate to be installed on the servers.
# You'll get kicked if you don't use the plugin. The default value is false because of it
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public class FloodgateConfig {
private String keyFileName;
@JsonProperty(value = "disconnect")
private DisconnectMessages messages;
@JsonProperty(value = "username-prefix")
private String usernamePrefix;

@JsonProperty
private boolean debug;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,11 @@ public class FloodgatePlayer {
*/
private UUID javaUniqueId;

FloodgatePlayer(BedrockData data) {
FloodgatePlayer(BedrockData data, String prefix) {
version = data.getVersion();
username = data.getUsername();
javaUsername = "*" + data.getUsername().substring(0, Math.min(data.getUsername().length(), 15));
if (prefix.length() < 1) prefix = "*";
javaUsername = prefix + data.getUsername().substring(0, Math.min(data.getUsername().length(), 16 - prefix.length()));
xuid = data.getXuid();
deviceOS = DeviceOS.getById(data.getDeviceId());
languageCode = data.getLanguageCode();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@
public class HandshakeHandler {
private PrivateKey privateKey;
private boolean bungee;
private String usernamePrefix;

public HandshakeHandler(@NonNull PrivateKey privateKey, boolean bungee) {
public HandshakeHandler(@NonNull PrivateKey privateKey, boolean bungee, String usernamePrefix) {
this.privateKey = privateKey;
this.bungee = bungee;
this.usernamePrefix = usernamePrefix;
}

public HandshakeResult handle(@NonNull String handshakeData) {
Expand All @@ -40,7 +42,7 @@ public HandshakeResult handle(@NonNull String handshakeData) {
return ResultType.INVALID_DATA_LENGTH.getCachedResult();
}

FloodgatePlayer player = new FloodgatePlayer(bedrockData);
FloodgatePlayer player = new FloodgatePlayer(bedrockData, usernamePrefix);
AbstractFloodgateAPI.players.put(player.getJavaUniqueId(), player);
return new HandshakeResult(ResultType.SUCCESS, data, bedrockData, player);
} catch (NoSuchPaddingException | NoSuchAlgorithmException | InvalidKeyException | IllegalBlockSizeException | BadPaddingException e) {
Expand Down
5 changes: 5 additions & 0 deletions common/src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
# The public key should be used for the Geyser(s) and the private key for the Floodgate(s)
key-file-name: key.pem

# Floodgate appends a prefix to bedrock usernames to avoid conflicts
# However, certain conflicts can cause issues with some plugins so this prefix is configurable using the property below
# It is recommended to use a prefix that does not contain alphanumerical to avoid the possibility of duplicate usernames.
username-prefix: "*"

disconnect:
# The disconnect message Geyser users should get when connecting
# to the server with an invalid key
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public void onProxyInitialization(ProxyInitializeEvent event) {
}

config = FloodgateConfig.load(logger, dataFolder.toPath().resolve("config.yml"), VelocityFloodgateConfig.class);
handshakeHandler = new HandshakeHandler(config.getPrivateKey(), true);
handshakeHandler = new HandshakeHandler(config.getPrivateKey(), true, config.getUsernamePrefix());
}

@Subscribe
Expand Down

0 comments on commit 95e1a81

Please sign in to comment.