Resolve remote address eagerly

This commit is contained in:
Mark Vainomaa 2020-12-19 05:05:34 +02:00
parent 7b9af65ed0
commit b94345387b
No known key found for this signature in database
GPG key ID: 1B3F9523B542D315

View file

@ -28,7 +28,9 @@ import io.netty.handler.proxy.Socks5ProxyHandler;
import io.netty.resolver.dns.DnsNameResolver; import io.netty.resolver.dns.DnsNameResolver;
import io.netty.resolver.dns.DnsNameResolverBuilder; import io.netty.resolver.dns.DnsNameResolverBuilder;
import java.net.InetAddress;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.net.UnknownHostException;
public class TcpClientSession extends TcpSession { public class TcpClientSession extends TcpSession {
private Client client; private Client client;
@ -110,8 +112,8 @@ public class TcpClientSession extends TcpSession {
@Override @Override
public void run() { public void run() {
try { try {
resolveAddress(); InetSocketAddress remoteAddress = resolveAddress();
bootstrap.remoteAddress(getHost(), getPort()); bootstrap.remoteAddress(remoteAddress);
ChannelFuture future = bootstrap.connect().sync(); ChannelFuture future = bootstrap.connect().sync();
if(future.isSuccess()) { if(future.isSuccess()) {
@ -138,7 +140,7 @@ public class TcpClientSession extends TcpSession {
} }
} }
private void resolveAddress() { private InetSocketAddress resolveAddress() {
boolean debug = getFlag(BuiltinFlags.PRINT_DEBUG, false); boolean debug = getFlag(BuiltinFlags.PRINT_DEBUG, false);
String name = this.getPacketProtocol().getSRVRecordPrefix() + "._tcp." + this.getHost(); String name = this.getPacketProtocol().getSRVRecordPrefix() + "._tcp." + this.getHost();
@ -193,6 +195,21 @@ public class TcpClientSession extends TcpSession {
resolver.close(); resolver.close();
} }
} }
// Resolve host here
try {
InetAddress resolved = InetAddress.getByName(getHost());
if (debug) {
System.out.printf("[PacketLib] Resolved %s -> %s%n", getHost(), resolved.getHostAddress());
}
return new InetSocketAddress(resolved, getPort());
} catch (UnknownHostException e) {
if (debug) {
System.out.println("[PacketLib] Failed to resolve host, letting Netty do it instead.");
e.printStackTrace();
}
return InetSocketAddress.createUnresolved(getHost(), getPort());
}
} }
@Override @Override