mirror of
https://github.com/GeyserMC/MCProtocolLib.git
synced 2025-03-13 22:49:51 -04:00
Optimize SRV resolving only when required
- Add an option to disable SRV resolving - Don't resolve SRV records if not a domain (in one instance this shortened loading times by five seconds)
This commit is contained in:
parent
19a9b2be35
commit
312cff5f97
2 changed files with 50 additions and 39 deletions
src/main/java/com/github/steveice10/packetlib
|
@ -13,6 +13,11 @@ public class BuiltinFlags {
|
|||
|
||||
public static final String CLIENT_PROXIED_ADDRESS = "client-proxied-address";
|
||||
|
||||
/**
|
||||
* When set to false, an SRV record resolve is not attempted.
|
||||
*/
|
||||
public static final String ATTEMPT_SRV_RESOLVE = "attempt-srv-resolve";
|
||||
|
||||
private BuiltinFlags() {
|
||||
}
|
||||
}
|
||||
|
|
|
@ -40,6 +40,8 @@ import java.net.InetSocketAddress;
|
|||
import java.net.UnknownHostException;
|
||||
|
||||
public class TcpClientSession extends TcpSession {
|
||||
private static final String IP_REGEX = "\\b\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\b";
|
||||
|
||||
private String bindAddress;
|
||||
private int bindPort;
|
||||
private ProxyInfo proxy;
|
||||
|
@ -140,52 +142,56 @@ public class TcpClientSession extends TcpSession {
|
|||
System.out.println("[PacketLib] Attempting SRV lookup for \"" + name + "\".");
|
||||
}
|
||||
|
||||
DnsNameResolver resolver = null;
|
||||
AddressedEnvelope<DnsResponse, InetSocketAddress> envelope = null;
|
||||
try {
|
||||
resolver = new DnsNameResolverBuilder(this.group.next())
|
||||
.channelType(NioDatagramChannel.class)
|
||||
.build();
|
||||
envelope = resolver.query(new DefaultDnsQuestion(name, DnsRecordType.SRV)).get();
|
||||
if(getFlag(BuiltinFlags.ATTEMPT_SRV_RESOLVE, true) && (!this.host.matches(IP_REGEX) && !this.host.equalsIgnoreCase("localhost"))) {
|
||||
DnsNameResolver resolver = null;
|
||||
AddressedEnvelope<DnsResponse, InetSocketAddress> envelope = null;
|
||||
try {
|
||||
resolver = new DnsNameResolverBuilder(this.group.next())
|
||||
.channelType(NioDatagramChannel.class)
|
||||
.build();
|
||||
envelope = resolver.query(new DefaultDnsQuestion(name, DnsRecordType.SRV)).get();
|
||||
|
||||
DnsResponse response = envelope.content();
|
||||
if(response.count(DnsSection.ANSWER) > 0) {
|
||||
DefaultDnsRawRecord record = response.recordAt(DnsSection.ANSWER, 0);
|
||||
if(record.type() == DnsRecordType.SRV) {
|
||||
ByteBuf buf = record.content();
|
||||
buf.skipBytes(4); // Skip priority and weight.
|
||||
DnsResponse response = envelope.content();
|
||||
if(response.count(DnsSection.ANSWER) > 0) {
|
||||
DefaultDnsRawRecord record = response.recordAt(DnsSection.ANSWER, 0);
|
||||
if(record.type() == DnsRecordType.SRV) {
|
||||
ByteBuf buf = record.content();
|
||||
buf.skipBytes(4); // Skip priority and weight.
|
||||
|
||||
int port = buf.readUnsignedShort();
|
||||
String host = DefaultDnsRecordDecoder.decodeName(buf);
|
||||
if(host.endsWith(".")) {
|
||||
host = host.substring(0, host.length() - 1);
|
||||
int port = buf.readUnsignedShort();
|
||||
String host = DefaultDnsRecordDecoder.decodeName(buf);
|
||||
if(host.endsWith(".")) {
|
||||
host = host.substring(0, host.length() - 1);
|
||||
}
|
||||
|
||||
if(debug) {
|
||||
System.out.println("[PacketLib] Found SRV record containing \"" + host + ":" + port + "\".");
|
||||
}
|
||||
|
||||
this.host = host;
|
||||
this.port = port;
|
||||
} else if(debug) {
|
||||
System.out.println("[PacketLib] Received non-SRV record in response.");
|
||||
}
|
||||
|
||||
if(debug) {
|
||||
System.out.println("[PacketLib] Found SRV record containing \"" + host + ":" + port + "\".");
|
||||
}
|
||||
|
||||
this.host = host;
|
||||
this.port = port;
|
||||
} else if(debug) {
|
||||
System.out.println("[PacketLib] Received non-SRV record in response.");
|
||||
System.out.println("[PacketLib] No SRV record found.");
|
||||
}
|
||||
} catch(Exception e) {
|
||||
if(debug) {
|
||||
System.out.println("[PacketLib] Failed to resolve SRV record.");
|
||||
e.printStackTrace();
|
||||
}
|
||||
} finally {
|
||||
if(envelope != null) {
|
||||
envelope.release();
|
||||
}
|
||||
} else if(debug) {
|
||||
System.out.println("[PacketLib] No SRV record found.");
|
||||
}
|
||||
} catch(Exception e) {
|
||||
if(debug) {
|
||||
System.out.println("[PacketLib] Failed to resolve SRV record.");
|
||||
e.printStackTrace();
|
||||
}
|
||||
} finally {
|
||||
if(envelope != null) {
|
||||
envelope.release();
|
||||
}
|
||||
|
||||
if(resolver != null) {
|
||||
resolver.close();
|
||||
if(resolver != null) {
|
||||
resolver.close();
|
||||
}
|
||||
}
|
||||
} else if(debug) {
|
||||
System.out.println("[PacketLib] Not resolving SRV record for " + this.host);
|
||||
}
|
||||
|
||||
// Resolve host here
|
||||
|
|
Loading…
Reference in a new issue