X-Git-Url: https://git.nmode.ca/signal-cli/blobdiff_plain/81a11dc9776672e3468ee9a8eed556889fb2e070..1cc21834e2429a5efa27b1766dcff9d6b3151ddf:/src/main/java/org/asamk/signal/util/IOUtils.java diff --git a/src/main/java/org/asamk/signal/util/IOUtils.java b/src/main/java/org/asamk/signal/util/IOUtils.java index b4c4c6dd..2d4ffd8e 100644 --- a/src/main/java/org/asamk/signal/util/IOUtils.java +++ b/src/main/java/org/asamk/signal/util/IOUtils.java @@ -39,6 +39,11 @@ public class IOUtils { private IOUtils() { } + public static Charset getConsoleCharset() { + final var console = System.console(); + return console == null ? Charset.defaultCharset() : console.charset(); + } + public static String readAll(InputStream in, Charset charset) throws IOException { var output = new StringWriter(); var buffer = new byte[4096]; @@ -98,23 +103,29 @@ public class IOUtils { public static InetSocketAddress parseInetSocketAddress(final String tcpAddress) throws UserErrorException { final var colonIndex = tcpAddress.lastIndexOf(':'); if (colonIndex < 0) { - throw new UserErrorException("Invalid tcp bind address: " + tcpAddress); + throw new UserErrorException("Invalid tcp bind address (expected host:port): " + tcpAddress); } - final String host = tcpAddress.substring(0, colonIndex); + final var host = tcpAddress.substring(0, colonIndex); + final var portString = tcpAddress.substring(colonIndex + 1); + final int port; try { - port = Integer.parseInt(tcpAddress.substring(colonIndex + 1)); + port = Integer.parseInt(portString); } catch (NumberFormatException e) { - throw new UserErrorException("Invalid tcp bind address: " + tcpAddress, e); + throw new UserErrorException("Invalid tcp port: " + portString, e); + } + final var socketAddress = new InetSocketAddress(host, port); + if (socketAddress.isUnresolved()) { + throw new UserErrorException("Invalid tcp bind address, invalid host: " + host); } - return new InetSocketAddress(host, port); + return socketAddress; } public static UnixDomainPrincipal getUnixDomainPrincipal(final SocketChannel channel) throws IOException { UnixDomainPrincipal principal = null; try { principal = channel.getOption(ExtendedSocketOptions.SO_PEERCRED); - } catch (UnsupportedOperationException ignored) { + } catch (UnsupportedOperationException | NoClassDefFoundError ignored) { } return principal; } @@ -130,7 +141,7 @@ public class IOUtils { logger.info("Listening on socket: " + address); postBind(address); } catch (IOException e) { - throw new IOErrorException("Failed to bind socket: " + e.getMessage(), e); + throw new IOErrorException("Failed to bind socket " + address + ": " + e.getMessage(), e); } return serverChannel; }