Support IPv6 addresses (#3753)

This commit is contained in:
Luck committed 2025-11-09 12:36:03 +00:00
1 parent 2a1dfb5112
commit 10378e882b
9 files changed
+41 -24

No files matched your search

@@ -28,6 +28,7 @@ package me.lucko.luckperms.common.messaging.nats;
import com.google.common.io.ByteArrayDataInput;
import com.google.common.io.ByteArrayDataOutput;
import com.google.common.io.ByteStreams;
import com.google.common.net.HostAndPort;
import io.nats.client.Connection;
import io.nats.client.Dispatcher;
import io.nats.client.Message;
@@ -69,9 +70,11 @@ public class NatsMessenger implements Messenger {
}
public void init(String address, String username, String password, boolean ssl) {
String[] addressSplit = address.split(":");
String host = addressSplit[0];
int port = addressSplit.length > 1 ? Integer.parseInt(addressSplit[1]) : Options.DEFAULT_PORT;
HostAndPort hostAndPort = HostAndPort.fromString(address)
.requireBracketsForIPv6()
.withDefaultPort(Options.DEFAULT_PORT);
String host = hostAndPort.getHostText();
int port = hostAndPort.getPort();
this.connection = createConnection(builder -> {
builder.server("nats://" + host + ":" + port)
@@ -28,6 +28,7 @@ package me.lucko.luckperms.common.messaging.rabbitmq;
import com.google.common.io.ByteArrayDataInput;
import com.google.common.io.ByteArrayDataOutput;
import com.google.common.io.ByteStreams;
import com.google.common.net.HostAndPort;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
@@ -70,9 +71,12 @@ public class RabbitMQMessenger implements Messenger {
}
public void init(String address, String virtualHost, String username, String password) {
String[] addressSplit = address.split(":");
String host = addressSplit[0];
int port = addressSplit.length > 1 ? Integer.parseInt(addressSplit[1]) : DEFAULT_PORT;
HostAndPort hostAndPort = HostAndPort.fromString(address)
.requireBracketsForIPv6()
.withDefaultPort(DEFAULT_PORT);
String host = hostAndPort.getHostText();
int port = hostAndPort.getPort();
this.connectionFactory = new ConnectionFactory();
this.connectionFactory.setHost(host);
@@ -86,9 +86,11 @@ public class RedisMessenger implements Messenger {
}
private static HostAndPort parseAddress(String address) {
String[] addressSplit = address.split(":");
String host = addressSplit[0];
int port = addressSplit.length > 1 ? Integer.parseInt(addressSplit[1]) : Protocol.DEFAULT_PORT;
com.google.common.net.HostAndPort hostAndPort = com.google.common.net.HostAndPort.fromString(address)
.requireBracketsForIPv6()
.withDefaultPort(Protocol.DEFAULT_PORT);
String host = hostAndPort.getHostText();
int port = hostAndPort.getPort();
return new HostAndPort(host, port);
}
@@ -27,6 +27,7 @@ package me.lucko.luckperms.common.storage.implementation.mongodb;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Strings;
import com.google.common.net.HostAndPort;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientOptions;
import com.mongodb.MongoClientURI;
@@ -129,9 +130,12 @@ public class MongoStorage implements StorageImplementation {
);
}
String[] addressSplit = this.configuration.getAddress().split(":");
String host = addressSplit[0];
int port = addressSplit.length > 1 ? Integer.parseInt(addressSplit[1]) : 27017;
HostAndPort hostAndPort = HostAndPort.fromString(this.configuration.getAddress())
.requireBracketsForIPv6()
.withDefaultPort(27017);
String host = hostAndPort.getHostText();
int port = hostAndPort.getPort();
ServerAddress address = new ServerAddress(host, port);
if (credential == null) {
@@ -46,7 +46,7 @@ public abstract class DriverBasedHikariConnectionFactory extends HikariConnectio
protected abstract String driverJdbcIdentifier();
@Override
protected void configureDatabase(HikariConfig config, String address, String port, String databaseName, String username, String password) {
protected void configureDatabase(HikariConfig config, String address, int port, String databaseName, String username, String password) {
config.setDriverClassName(driverClassName());
config.setJdbcUrl(String.format("jdbc:%s://%s:%s/%s", driverJdbcIdentifier(), address, port, databaseName));
config.setUsername(username);
@@ -26,6 +26,7 @@
package me.lucko.luckperms.common.storage.implementation.sql.connection.hikari;
import com.google.common.collect.ImmutableList;
import com.google.common.net.HostAndPort;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import me.lucko.luckperms.common.plugin.LuckPermsPlugin;
@@ -58,7 +59,7 @@ public abstract class HikariConnectionFactory implements ConnectionFactory {
*
* @return the default port
*/
protected abstract String defaultPort();
protected abstract int defaultPort();
/**
* Configures the {@link HikariConfig} with the relevant database properties.
@@ -72,7 +73,7 @@ public abstract class HikariConnectionFactory implements ConnectionFactory {
* @param username the database username
* @param password the database password
*/
protected abstract void configureDatabase(HikariConfig config, String address, String port, String databaseName, String username, String password);
protected abstract void configureDatabase(HikariConfig config, String address, int port, String databaseName, String username, String password);
/**
* Allows the connection factory instance to override certain properties before they are set.
@@ -117,9 +118,12 @@ public abstract class HikariConnectionFactory implements ConnectionFactory {
config.setPoolName("luckperms-hikari");
// get the database info/credentials from the config file
String[] addressSplit = this.configuration.getAddress().split(":");
String address = addressSplit[0];
String port = addressSplit.length > 1 ? addressSplit[1] : defaultPort();
HostAndPort hostAndPort = HostAndPort.fromString(this.configuration.getAddress())
.requireBracketsForIPv6()
.withDefaultPort(defaultPort());
String address = hostAndPort.getHostText();
int port = hostAndPort.getPort();
// allow the implementation to configure the HikariConfig appropriately with these values
try {
@@ -39,8 +39,8 @@ public class MariaDbConnectionFactory extends DriverBasedHikariConnectionFactory
}
@Override
protected String defaultPort() {
return "3306";
protected int defaultPort() {
return 3306;
}
@Override
@@ -41,8 +41,8 @@ public class MySqlConnectionFactory extends DriverBasedHikariConnectionFactory {
}
@Override
protected String defaultPort() {
return "3306";
protected int defaultPort() {
return 3306;
}
@Override
@@ -41,8 +41,8 @@ public class PostgresConnectionFactory extends DriverBasedHikariConnectionFactor
}
@Override
protected String defaultPort() {
return "5432";
protected int defaultPort() {
return 5432;
}
@Override