Skip to content

Commit a5f05c6

Browse files
authored
Merge branch 'TheMeinerLP:master' into master
2 parents c9bc415 + 61ce546 commit a5f05c6

10 files changed

Lines changed: 122 additions & 24 deletions

File tree

common/src/main/java/me/lucko/luckperms/common/messaging/nats/NatsMessenger.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import io.nats.client.Options;
3737
import io.nats.client.Options.Builder;
3838
import me.lucko.luckperms.common.plugin.LuckPermsPlugin;
39+
import me.lucko.luckperms.common.util.HostAndPort;
3940
import me.lucko.luckperms.common.util.Throwing;
4041
import net.luckperms.api.messenger.IncomingMessageConsumer;
4142
import net.luckperms.api.messenger.Messenger;
@@ -69,9 +70,11 @@ public void sendOutgoingMessage(@NonNull OutgoingMessage outgoingMessage) {
6970
}
7071

7172
public void init(String address, String username, String password, boolean ssl) {
72-
String[] addressSplit = address.split(":");
73-
String host = addressSplit[0];
74-
int port = addressSplit.length > 1 ? Integer.parseInt(addressSplit[1]) : Options.DEFAULT_PORT;
73+
HostAndPort hostAndPort = new HostAndPort(address)
74+
.requireBracketsForIPv6()
75+
.withDefaultPort(Options.DEFAULT_PORT);
76+
String host = hostAndPort.getHost();
77+
int port = hostAndPort.getPort();
7578

7679
this.connection = createConnection(builder -> {
7780
builder.server("nats://" + host + ":" + port)

common/src/main/java/me/lucko/luckperms/common/messaging/rabbitmq/RabbitMQMessenger.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import com.rabbitmq.client.Delivery;
3838
import me.lucko.luckperms.common.plugin.LuckPermsPlugin;
3939
import me.lucko.luckperms.common.plugin.scheduler.SchedulerTask;
40+
import me.lucko.luckperms.common.util.HostAndPort;
4041
import net.luckperms.api.messenger.IncomingMessageConsumer;
4142
import net.luckperms.api.messenger.Messenger;
4243
import net.luckperms.api.messenger.message.OutgoingMessage;
@@ -70,9 +71,12 @@ public RabbitMQMessenger(LuckPermsPlugin plugin, IncomingMessageConsumer consume
7071
}
7172

7273
public void init(String address, String virtualHost, String username, String password) {
73-
String[] addressSplit = address.split(":");
74-
String host = addressSplit[0];
75-
int port = addressSplit.length > 1 ? Integer.parseInt(addressSplit[1]) : DEFAULT_PORT;
74+
HostAndPort hostAndPort = new HostAndPort(address)
75+
.requireBracketsForIPv6()
76+
.withDefaultPort(DEFAULT_PORT);
77+
78+
String host = hostAndPort.getHost();
79+
int port = hostAndPort.getPort();
7680

7781
this.connectionFactory = new ConnectionFactory();
7882
this.connectionFactory.setHost(host);

common/src/main/java/me/lucko/luckperms/common/messaging/redis/RedisMessenger.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,11 @@ private static JedisClientConfig jedisConfig(String username, String password, b
8686
}
8787

8888
private static HostAndPort parseAddress(String address) {
89-
String[] addressSplit = address.split(":");
90-
String host = addressSplit[0];
91-
int port = addressSplit.length > 1 ? Integer.parseInt(addressSplit[1]) : Protocol.DEFAULT_PORT;
89+
me.lucko.luckperms.common.util.HostAndPort hostAndPort = new me.lucko.luckperms.common.util.HostAndPort(address)
90+
.requireBracketsForIPv6()
91+
.withDefaultPort(Protocol.DEFAULT_PORT);
92+
String host = hostAndPort.getHost();
93+
int port = hostAndPort.getPort();
9294
return new HostAndPort(host, port);
9395
}
9496

common/src/main/java/me/lucko/luckperms/common/storage/implementation/mongodb/MongoStorage.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
import me.lucko.luckperms.common.storage.misc.NodeEntry;
6060
import me.lucko.luckperms.common.storage.misc.PlayerSaveResultImpl;
6161
import me.lucko.luckperms.common.storage.misc.StorageCredentials;
62+
import me.lucko.luckperms.common.util.HostAndPort;
6263
import me.lucko.luckperms.common.util.Iterators;
6364
import net.luckperms.api.actionlog.Action;
6465
import net.luckperms.api.context.Context;
@@ -129,9 +130,12 @@ public void init() {
129130
);
130131
}
131132

132-
String[] addressSplit = this.configuration.getAddress().split(":");
133-
String host = addressSplit[0];
134-
int port = addressSplit.length > 1 ? Integer.parseInt(addressSplit[1]) : 27017;
133+
HostAndPort hostAndPort = new HostAndPort(this.configuration.getAddress())
134+
.requireBracketsForIPv6()
135+
.withDefaultPort(27017);
136+
137+
String host = hostAndPort.getHost();
138+
int port = hostAndPort.getPort();
135139
ServerAddress address = new ServerAddress(host, port);
136140

137141
if (credential == null) {

common/src/main/java/me/lucko/luckperms/common/storage/implementation/sql/connection/hikari/DriverBasedHikariConnectionFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ protected DriverBasedHikariConnectionFactory(StorageCredentials configuration) {
4646
protected abstract String driverJdbcIdentifier();
4747

4848
@Override
49-
protected void configureDatabase(HikariConfig config, String address, String port, String databaseName, String username, String password) {
49+
protected void configureDatabase(HikariConfig config, String address, int port, String databaseName, String username, String password) {
5050
config.setDriverClassName(driverClassName());
5151
config.setJdbcUrl(String.format("jdbc:%s://%s:%s/%s", driverJdbcIdentifier(), address, port, databaseName));
5252
config.setUsername(username);

common/src/main/java/me/lucko/luckperms/common/storage/implementation/sql/connection/hikari/HikariConnectionFactory.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import me.lucko.luckperms.common.storage.StorageMetadata;
3434
import me.lucko.luckperms.common.storage.implementation.sql.connection.ConnectionFactory;
3535
import me.lucko.luckperms.common.storage.misc.StorageCredentials;
36+
import me.lucko.luckperms.common.util.HostAndPort;
3637

3738
import java.sql.Connection;
3839
import java.sql.SQLException;
@@ -58,7 +59,7 @@ public HikariConnectionFactory(StorageCredentials configuration) {
5859
*
5960
* @return the default port
6061
*/
61-
protected abstract String defaultPort();
62+
protected abstract int defaultPort();
6263

6364
/**
6465
* Configures the {@link HikariConfig} with the relevant database properties.
@@ -72,7 +73,7 @@ public HikariConnectionFactory(StorageCredentials configuration) {
7273
* @param username the database username
7374
* @param password the database password
7475
*/
75-
protected abstract void configureDatabase(HikariConfig config, String address, String port, String databaseName, String username, String password);
76+
protected abstract void configureDatabase(HikariConfig config, String address, int port, String databaseName, String username, String password);
7677

7778
/**
7879
* Allows the connection factory instance to override certain properties before they are set.
@@ -117,9 +118,12 @@ public void init(LuckPermsPlugin plugin) {
117118
config.setPoolName("luckperms-hikari");
118119

119120
// get the database info/credentials from the config file
120-
String[] addressSplit = this.configuration.getAddress().split(":");
121-
String address = addressSplit[0];
122-
String port = addressSplit.length > 1 ? addressSplit[1] : defaultPort();
121+
HostAndPort hostAndPort = new HostAndPort(this.configuration.getAddress())
122+
.requireBracketsForIPv6()
123+
.withDefaultPort(defaultPort());
124+
125+
String address = hostAndPort.getHost();
126+
int port = hostAndPort.getPort();
123127

124128
// allow the implementation to configure the HikariConfig appropriately with these values
125129
try {

common/src/main/java/me/lucko/luckperms/common/storage/implementation/sql/connection/hikari/MariaDbConnectionFactory.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ public String getImplementationName() {
3939
}
4040

4141
@Override
42-
protected String defaultPort() {
43-
return "3306";
42+
protected int defaultPort() {
43+
return 3306;
4444
}
4545

4646
@Override

common/src/main/java/me/lucko/luckperms/common/storage/implementation/sql/connection/hikari/MySqlConnectionFactory.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ public String getImplementationName() {
4141
}
4242

4343
@Override
44-
protected String defaultPort() {
45-
return "3306";
44+
protected int defaultPort() {
45+
return 3306;
4646
}
4747

4848
@Override

common/src/main/java/me/lucko/luckperms/common/storage/implementation/sql/connection/hikari/PostgresConnectionFactory.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ public String getImplementationName() {
4141
}
4242

4343
@Override
44-
protected String defaultPort() {
45-
return "5432";
44+
protected int defaultPort() {
45+
return 5432;
4646
}
4747

4848
@Override
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* This file is part of LuckPerms, licensed under the MIT License.
3+
*
4+
* Copyright (c) lucko (Luck) <luck@lucko.me>
5+
* Copyright (c) contributors
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in all
15+
* copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23+
* SOFTWARE.
24+
*/
25+
26+
package me.lucko.luckperms.common.util;
27+
28+
import java.lang.reflect.Method;
29+
import java.util.Objects;
30+
31+
/**
32+
* A wrapper around Guava's HostAndPort to account for the method name change of getHostText().
33+
*/
34+
@SuppressWarnings("UnstableApiUsage")
35+
public class HostAndPort {
36+
private static final Method GET_HOST_METHOD;
37+
38+
static {
39+
Method getHostMethod = null;
40+
try {
41+
getHostMethod = com.google.common.net.HostAndPort.class.getMethod("getHostText");
42+
} catch (NoSuchMethodException e) {
43+
try {
44+
getHostMethod = com.google.common.net.HostAndPort.class.getMethod("getHost");
45+
} catch (NoSuchMethodException ex) {
46+
// ignore
47+
}
48+
}
49+
Objects.requireNonNull(getHostMethod);
50+
GET_HOST_METHOD = getHostMethod;
51+
}
52+
53+
private com.google.common.net.HostAndPort delegate;
54+
55+
public HostAndPort(String hostAndPort) {
56+
this.delegate = com.google.common.net.HostAndPort.fromString(hostAndPort);
57+
}
58+
59+
public HostAndPort withDefaultPort(int defaultPort) {
60+
this.delegate = this.delegate.withDefaultPort(defaultPort);
61+
return this;
62+
}
63+
64+
public HostAndPort requireBracketsForIPv6() {
65+
this.delegate.requireBracketsForIPv6();
66+
return this;
67+
}
68+
69+
public String getHost() {
70+
try {
71+
return (String) GET_HOST_METHOD.invoke(this.delegate);
72+
} catch (ReflectiveOperationException e) {
73+
throw new RuntimeException(e);
74+
}
75+
}
76+
77+
public int getPort() {
78+
return this.delegate.getPort();
79+
}
80+
81+
}

0 commit comments

Comments
 (0)