From 44f872eea436fe9963486ceecebd4b34f0e3e5d0 Mon Sep 17 00:00:00 2001 From: Andrew Steinborn Date: Sat, 7 Nov 2020 20:43:58 -0500 Subject: [PATCH] Better validate addresses in configuration. Fixes #385 --- .../proxy/util/AddressUtil.java | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/proxy/src/main/java/com/velocitypowered/proxy/util/AddressUtil.java b/proxy/src/main/java/com/velocitypowered/proxy/util/AddressUtil.java index b4feb268b..706e80357 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/util/AddressUtil.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/util/AddressUtil.java @@ -7,6 +7,8 @@ import java.net.InetSocketAddress; import java.net.URI; public final class AddressUtil { + private static final int DEFAULT_MINECRAFT_PORT = 25565; + private AddressUtil() { throw new AssertionError(); } @@ -21,11 +23,16 @@ public final class AddressUtil { public static InetSocketAddress parseAddress(String ip) { Preconditions.checkNotNull(ip, "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 { InetAddress ia = InetAddresses.forUriString(uri.getHost()); - return new InetSocketAddress(ia, uri.getPort()); + return new InetSocketAddress(ia, port); } 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) { Preconditions.checkNotNull(ip, "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); } }