mirror of
https://github.com/AsamK/signal-cli
synced 2025-08-29 18:40:39 +00:00
Improve error handling for tcp address parsing
This commit is contained in:
parent
6bdfa11b66
commit
1c9d16e199
1 changed files with 11 additions and 5 deletions
|
@ -98,16 +98,22 @@ public class IOUtils {
|
||||||
public static InetSocketAddress parseInetSocketAddress(final String tcpAddress) throws UserErrorException {
|
public static InetSocketAddress parseInetSocketAddress(final String tcpAddress) throws UserErrorException {
|
||||||
final var colonIndex = tcpAddress.lastIndexOf(':');
|
final var colonIndex = tcpAddress.lastIndexOf(':');
|
||||||
if (colonIndex < 0) {
|
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;
|
final int port;
|
||||||
try {
|
try {
|
||||||
port = Integer.parseInt(tcpAddress.substring(colonIndex + 1));
|
port = Integer.parseInt(portString);
|
||||||
} catch (NumberFormatException e) {
|
} catch (NumberFormatException e) {
|
||||||
throw new UserErrorException("Invalid tcp bind address: " + tcpAddress, e);
|
throw new UserErrorException("Invalid tcp port: " + portString, e);
|
||||||
}
|
}
|
||||||
return new InetSocketAddress(host, port);
|
final var socketAddress = new InetSocketAddress(host, port);
|
||||||
|
if (socketAddress.isUnresolved()) {
|
||||||
|
throw new UserErrorException("Invalid tcp bind address, invalid host: " + host);
|
||||||
|
}
|
||||||
|
return socketAddress;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static UnixDomainPrincipal getUnixDomainPrincipal(final SocketChannel channel) throws IOException {
|
public static UnixDomainPrincipal getUnixDomainPrincipal(final SocketChannel channel) throws IOException {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue