mirror of
https://github.com/M66B/FairEmail.git
synced 2024-12-26 17:57:16 +00:00
Use Android's DNS resolver
This commit is contained in:
parent
571ed13f0b
commit
884e5bcef2
1 changed files with 28 additions and 20 deletions
|
@ -29,14 +29,11 @@ import android.os.Build;
|
|||
|
||||
import androidx.annotation.NonNull;
|
||||
|
||||
import org.xbill.DNS.Flags;
|
||||
import org.xbill.DNS.Lookup;
|
||||
import org.xbill.DNS.MXRecord;
|
||||
import org.xbill.DNS.Message;
|
||||
import org.xbill.DNS.OPTRecord;
|
||||
import org.xbill.DNS.Record;
|
||||
import org.xbill.DNS.SRVRecord;
|
||||
import org.xbill.DNS.Section;
|
||||
import org.xbill.DNS.SimpleResolver;
|
||||
import org.xbill.DNS.TextParseException;
|
||||
import org.xbill.DNS.Type;
|
||||
|
@ -46,6 +43,7 @@ import java.net.InetAddress;
|
|||
import java.net.UnknownHostException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.concurrent.Semaphore;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
|
@ -129,60 +127,70 @@ public class DnsHelper {
|
|||
|
||||
@Override
|
||||
public Message send(Message query) throws IOException {
|
||||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q || !BuildConfig.DEBUG)
|
||||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q)
|
||||
return super.send(query);
|
||||
else {
|
||||
Log.i("Using Android DNS resolver");
|
||||
Semaphore sem = new Semaphore(0);
|
||||
DnsResolver resolver = DnsResolver.getInstance();
|
||||
OPTRecord optRecord = new OPTRecord(4096, 0, 0, Flags.DO, null);
|
||||
query.addRecord(optRecord, Section.ADDITIONAL);
|
||||
query.getHeader().setFlag(Flags.AD);
|
||||
//OPTRecord optRecord = new OPTRecord(4096, 0, 0, Flags.DO, null);
|
||||
//query.addRecord(optRecord, Section.ADDITIONAL);
|
||||
//query.getHeader().setFlag(Flags.AD);
|
||||
Log.i("DNS query=" + query.toString());
|
||||
resolver.rawQuery(
|
||||
null,
|
||||
query.toWire(),
|
||||
DnsResolver.FLAG_EMPTY,
|
||||
Helper.getBackgroundExecutor(1, "dns"),
|
||||
new Executor() {
|
||||
@Override
|
||||
public void execute(Runnable command) {
|
||||
command.run();
|
||||
}
|
||||
},
|
||||
null,
|
||||
new DnsResolver.Callback<byte[]>() {
|
||||
@Override
|
||||
public void onAnswer(@NonNull byte[] answer, int rcode) {
|
||||
Log.i("DNS rcode=" + rcode);
|
||||
try {
|
||||
if (rcode == 0)
|
||||
result = new Message(answer);
|
||||
else
|
||||
throw new IOException("rcode=" + rcode);
|
||||
ex = new IOException("rcode=" + rcode);
|
||||
} catch (Throwable e) {
|
||||
ex = new IOException(e.getMessage());
|
||||
} finally {
|
||||
sem.release();
|
||||
}
|
||||
sem.release();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onError(@NonNull DnsResolver.DnsException e) {
|
||||
Log.w(e);
|
||||
ex = new IOException(e.getMessage());
|
||||
sem.release();
|
||||
try {
|
||||
Log.w(e);
|
||||
ex = new IOException(e.getMessage());
|
||||
} finally {
|
||||
sem.release();
|
||||
}
|
||||
}
|
||||
});
|
||||
try {
|
||||
if (!sem.tryAcquire(LOOKUP_TIMEOUT, TimeUnit.SECONDS))
|
||||
throw new IOException("timeout");
|
||||
ex = new IOException("timeout");
|
||||
} catch (InterruptedException e) {
|
||||
ex = new IOException("interrupted");
|
||||
}
|
||||
|
||||
if (ex == null) {
|
||||
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
|
||||
Network active = (cm == null ? null : cm.getActiveNetwork());
|
||||
LinkProperties props = (active == null ? null : cm.getLinkProperties(active));
|
||||
Log.i("DNS private=" + (props == null ? null : props.isPrivateDnsActive()));
|
||||
//ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
|
||||
//Network active = (cm == null ? null : cm.getActiveNetwork());
|
||||
//LinkProperties props = (active == null ? null : cm.getLinkProperties(active));
|
||||
//Log.i("DNS private=" + (props == null ? null : props.isPrivateDnsActive()));
|
||||
Log.i("DNS answer=" + result.toString() + " flags=" + result.getHeader().printFlags());
|
||||
return result;
|
||||
} else
|
||||
} else {
|
||||
Log.w(ex);
|
||||
throw ex;
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue