Change SRV lookup code to use dnsjava.

This commit is contained in:
Steveice10 2020-04-21 12:01:26 -07:00
parent 29f960a24c
commit b98e525016
2 changed files with 22 additions and 13 deletions

11
pom.xml
View file

@ -52,6 +52,17 @@
<version>4.1.45.Final</version> <version>4.1.45.Final</version>
<scope>compile</scope> <scope>compile</scope>
</dependency> </dependency>
<dependency>
<groupId>dnsjava</groupId>
<artifactId>dnsjava</artifactId>
<version>3.0.2</version>
</dependency>
<!-- Silence dnsjava slf4j warnings -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-nop</artifactId>
<version>1.7.30</version>
</dependency>
</dependencies> </dependencies>
<build> <build>

View file

@ -15,10 +15,11 @@ import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.proxy.HttpProxyHandler; import io.netty.handler.proxy.HttpProxyHandler;
import io.netty.handler.proxy.Socks4ProxyHandler; import io.netty.handler.proxy.Socks4ProxyHandler;
import io.netty.handler.proxy.Socks5ProxyHandler; import io.netty.handler.proxy.Socks5ProxyHandler;
import io.netty.util.concurrent.Future; import org.xbill.DNS.Lookup;
import org.xbill.DNS.Record;
import javax.naming.directory.InitialDirContext; import org.xbill.DNS.SRVRecord;
import java.util.Hashtable; import org.xbill.DNS.TextParseException;
import org.xbill.DNS.Type;
public class TcpClientSession extends TcpSession { public class TcpClientSession extends TcpSession {
private Client client; private Client client;
@ -104,17 +105,14 @@ public class TcpClientSession extends TcpSession {
int port = getPort(); int port = getPort();
try { try {
Hashtable<String, String> environment = new Hashtable<String, String>(); Record[] records = new Lookup(getPacketProtocol().getSRVRecordPrefix() + "._tcp." + host, Type.SRV).run();
environment.put("java.naming.factory.initial", "com.sun.jndi.dns.DnsContextFactory"); if(records.length > 0) {
environment.put("java.naming.provider.url", "dns:"); SRVRecord srv = (SRVRecord) records[0];
String[] result = new InitialDirContext(environment).getAttributes(getPacketProtocol().getSRVRecordPrefix() + "._tcp." + host, new String[] {"SRV"}).get("srv").get().toString().split(" ", 4); host = srv.getTarget().toString().replaceFirst("\\.$", "");
host = result[3]; port = srv.getPort();
port = Integer.parseInt(result[2]);
if(host.endsWith(".")) {
host = host.substring(0, host.length() - 1);
} }
} catch(Throwable t) { } catch(TextParseException e) {
} }
bootstrap.remoteAddress(host, port); bootstrap.remoteAddress(host, port);