1
0
Fork 0
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:
M66B 2020-09-16 20:57:13 +02:00
parent 571ed13f0b
commit 884e5bcef2

View file

@ -29,14 +29,11 @@ import android.os.Build;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import org.xbill.DNS.Flags;
import org.xbill.DNS.Lookup; import org.xbill.DNS.Lookup;
import org.xbill.DNS.MXRecord; import org.xbill.DNS.MXRecord;
import org.xbill.DNS.Message; import org.xbill.DNS.Message;
import org.xbill.DNS.OPTRecord;
import org.xbill.DNS.Record; import org.xbill.DNS.Record;
import org.xbill.DNS.SRVRecord; import org.xbill.DNS.SRVRecord;
import org.xbill.DNS.Section;
import org.xbill.DNS.SimpleResolver; import org.xbill.DNS.SimpleResolver;
import org.xbill.DNS.TextParseException; import org.xbill.DNS.TextParseException;
import org.xbill.DNS.Type; import org.xbill.DNS.Type;
@ -46,6 +43,7 @@ import java.net.InetAddress;
import java.net.UnknownHostException; import java.net.UnknownHostException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.concurrent.Executor;
import java.util.concurrent.Semaphore; import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@ -129,60 +127,70 @@ public class DnsHelper {
@Override @Override
public Message send(Message query) throws IOException { 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); return super.send(query);
else { else {
Log.i("Using Android DNS resolver"); Log.i("Using Android DNS resolver");
Semaphore sem = new Semaphore(0); Semaphore sem = new Semaphore(0);
DnsResolver resolver = DnsResolver.getInstance(); DnsResolver resolver = DnsResolver.getInstance();
OPTRecord optRecord = new OPTRecord(4096, 0, 0, Flags.DO, null); //OPTRecord optRecord = new OPTRecord(4096, 0, 0, Flags.DO, null);
query.addRecord(optRecord, Section.ADDITIONAL); //query.addRecord(optRecord, Section.ADDITIONAL);
query.getHeader().setFlag(Flags.AD); //query.getHeader().setFlag(Flags.AD);
Log.i("DNS query=" + query.toString()); Log.i("DNS query=" + query.toString());
resolver.rawQuery( resolver.rawQuery(
null, null,
query.toWire(), query.toWire(),
DnsResolver.FLAG_EMPTY, DnsResolver.FLAG_EMPTY,
Helper.getBackgroundExecutor(1, "dns"), new Executor() {
@Override
public void execute(Runnable command) {
command.run();
}
},
null, null,
new DnsResolver.Callback<byte[]>() { new DnsResolver.Callback<byte[]>() {
@Override @Override
public void onAnswer(@NonNull byte[] answer, int rcode) { public void onAnswer(@NonNull byte[] answer, int rcode) {
Log.i("DNS rcode=" + rcode);
try { try {
if (rcode == 0) if (rcode == 0)
result = new Message(answer); result = new Message(answer);
else else
throw new IOException("rcode=" + rcode); ex = new IOException("rcode=" + rcode);
} catch (Throwable e) { } catch (Throwable e) {
ex = new IOException(e.getMessage()); ex = new IOException(e.getMessage());
} finally {
sem.release();
} }
sem.release();
} }
@Override @Override
public void onError(@NonNull DnsResolver.DnsException e) { public void onError(@NonNull DnsResolver.DnsException e) {
Log.w(e); try {
ex = new IOException(e.getMessage()); Log.w(e);
sem.release(); ex = new IOException(e.getMessage());
} finally {
sem.release();
}
} }
}); });
try { try {
if (!sem.tryAcquire(LOOKUP_TIMEOUT, TimeUnit.SECONDS)) if (!sem.tryAcquire(LOOKUP_TIMEOUT, TimeUnit.SECONDS))
throw new IOException("timeout"); ex = new IOException("timeout");
} catch (InterruptedException e) { } catch (InterruptedException e) {
ex = new IOException("interrupted"); ex = new IOException("interrupted");
} }
if (ex == null) { if (ex == null) {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); //ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
Network active = (cm == null ? null : cm.getActiveNetwork()); //Network active = (cm == null ? null : cm.getActiveNetwork());
LinkProperties props = (active == null ? null : cm.getLinkProperties(active)); //LinkProperties props = (active == null ? null : cm.getLinkProperties(active));
Log.i("DNS private=" + (props == null ? null : props.isPrivateDnsActive())); //Log.i("DNS private=" + (props == null ? null : props.isPrivateDnsActive()));
Log.i("DNS answer=" + result.toString() + " flags=" + result.getHeader().printFlags()); Log.i("DNS answer=" + result.toString() + " flags=" + result.getHeader().printFlags());
return result; return result;
} else } else {
Log.w(ex);
throw ex; throw ex;
}
} }
} }
}; };