From 1d2110f2aa93fef53662caa2e6daf441b3d6bbc9 Mon Sep 17 00:00:00 2001 From: Andrew Steinborn Date: Fri, 27 Jul 2018 20:24:57 -0400 Subject: [PATCH] A bit more --- .../velocitypowered/proxy/VelocityServer.java | 2 +- .../proxy/config/VelocityConfiguration.java | 22 +++++++++++++++++-- .../proxy/connection/MinecraftConnection.java | 2 -- .../proxy/util/AddressUtil.java | 16 ++++++++++++++ 4 files changed, 37 insertions(+), 5 deletions(-) create mode 100644 src/main/java/com/velocitypowered/proxy/util/AddressUtil.java diff --git a/src/main/java/com/velocitypowered/proxy/VelocityServer.java b/src/main/java/com/velocitypowered/proxy/VelocityServer.java index ec8fc430b..09e0f4b0b 100644 --- a/src/main/java/com/velocitypowered/proxy/VelocityServer.java +++ b/src/main/java/com/velocitypowered/proxy/VelocityServer.java @@ -61,7 +61,7 @@ public class VelocityServer { httpClient = new NettyHttpClient(this); - this.cm.bind(new InetSocketAddress(26671)); + this.cm.bind(configuration.getBind()); } public Bootstrap initializeGenericBootstrap() { diff --git a/src/main/java/com/velocitypowered/proxy/config/VelocityConfiguration.java b/src/main/java/com/velocitypowered/proxy/config/VelocityConfiguration.java index f5070d81c..49ef6771a 100644 --- a/src/main/java/com/velocitypowered/proxy/config/VelocityConfiguration.java +++ b/src/main/java/com/velocitypowered/proxy/config/VelocityConfiguration.java @@ -2,6 +2,7 @@ package com.velocitypowered.proxy.config; import com.google.common.collect.ImmutableMap; import com.moandjiezana.toml.Toml; +import com.velocitypowered.proxy.util.AddressUtil; import com.velocitypowered.proxy.util.LegacyChatColorUtils; import net.kyori.text.Component; import net.kyori.text.serializer.ComponentSerializers; @@ -10,6 +11,7 @@ import org.apache.logging.log4j.Logger; import java.io.IOException; import java.io.Reader; +import java.net.InetSocketAddress; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; @@ -50,6 +52,13 @@ public class VelocityConfiguration { valid = false; } + try { + AddressUtil.parseAddress(bind); + } catch (IllegalArgumentException e) { + logger.error("'bind' option does not specify a valid IP address.", e); + valid = false; + } + if (!onlineMode) { logger.info("Proxy is running in offline mode!"); } @@ -72,6 +81,15 @@ public class VelocityConfiguration { valid = false; } + for (Map.Entry entry : servers.entrySet()) { + try { + AddressUtil.parseAddress(entry.getValue()); + } catch (IllegalArgumentException e) { + logger.error("Server {} does not have a valid IP address.", entry.getKey(), e); + valid = false; + } + } + for (String s : attemptConnectionOrder) { if (!servers.containsKey(s)) { logger.error("Fallback server " + s + " doesn't exist!"); @@ -90,8 +108,8 @@ public class VelocityConfiguration { return valid; } - public String getBind() { - return bind; + public InetSocketAddress getBind() { + return AddressUtil.parseAddress(bind); } public String getMotd() { diff --git a/src/main/java/com/velocitypowered/proxy/connection/MinecraftConnection.java b/src/main/java/com/velocitypowered/proxy/connection/MinecraftConnection.java index 7ae89124d..9f59adb1b 100644 --- a/src/main/java/com/velocitypowered/proxy/connection/MinecraftConnection.java +++ b/src/main/java/com/velocitypowered/proxy/connection/MinecraftConnection.java @@ -92,8 +92,6 @@ public class MinecraftConnection extends ChannelInboundHandlerAdapter { @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { if (ctx.channel().isActive()) { - cause.printStackTrace(); - if (sessionHandler != null) { sessionHandler.exception(cause); } diff --git a/src/main/java/com/velocitypowered/proxy/util/AddressUtil.java b/src/main/java/com/velocitypowered/proxy/util/AddressUtil.java new file mode 100644 index 000000000..fdf890db1 --- /dev/null +++ b/src/main/java/com/velocitypowered/proxy/util/AddressUtil.java @@ -0,0 +1,16 @@ +package com.velocitypowered.proxy.util; + +import com.google.common.base.Preconditions; + +import java.net.InetSocketAddress; +import java.net.URI; + +public enum AddressUtil { + ; + + public static InetSocketAddress parseAddress(String ip) { + Preconditions.checkNotNull(ip, "ip"); + URI uri = URI.create("tcp://" + ip); + return new InetSocketAddress(uri.getHost(), uri.getPort()); + } +}