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