13
0
geforkt von Mirrors/Velocity

Better validate addresses in configuration. Fixes #385

Dieser Commit ist enthalten in:
Andrew Steinborn 2020-11-07 20:43:58 -05:00
Ursprung 09de77425d
Commit 44f872eea4

Datei anzeigen

@ -7,6 +7,8 @@ import java.net.InetSocketAddress;
import java.net.URI; import java.net.URI;
public final class AddressUtil { public final class AddressUtil {
private static final int DEFAULT_MINECRAFT_PORT = 25565;
private AddressUtil() { private AddressUtil() {
throw new AssertionError(); throw new AssertionError();
} }
@ -21,11 +23,16 @@ public final class AddressUtil {
public static InetSocketAddress parseAddress(String ip) { public static InetSocketAddress parseAddress(String ip) {
Preconditions.checkNotNull(ip, "ip"); Preconditions.checkNotNull(ip, "ip");
URI uri = URI.create("tcp://" + ip); URI uri = URI.create("tcp://" + ip);
if (uri.getHost() == null) {
throw new IllegalStateException("Invalid hostname/IP " + ip);
}
int port = uri.getPort() == -1 ? DEFAULT_MINECRAFT_PORT : uri.getPort();
try { try {
InetAddress ia = InetAddresses.forUriString(uri.getHost()); InetAddress ia = InetAddresses.forUriString(uri.getHost());
return new InetSocketAddress(ia, uri.getPort()); return new InetSocketAddress(ia, port);
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
return InetSocketAddress.createUnresolved(uri.getHost(), uri.getPort()); return InetSocketAddress.createUnresolved(uri.getHost(), port);
} }
} }
@ -39,6 +46,11 @@ public final class AddressUtil {
public static InetSocketAddress parseAndResolveAddress(String ip) { public static InetSocketAddress parseAndResolveAddress(String ip) {
Preconditions.checkNotNull(ip, "ip"); Preconditions.checkNotNull(ip, "ip");
URI uri = URI.create("tcp://" + ip); URI uri = URI.create("tcp://" + ip);
return new InetSocketAddress(uri.getHost(), uri.getPort()); if (uri.getHost() == null) {
throw new IllegalStateException("Invalid hostname/IP " + ip);
}
int port = uri.getPort() == -1 ? DEFAULT_MINECRAFT_PORT : uri.getPort();
return new InetSocketAddress(uri.getHost(), port);
} }
} }