MCPcopy Create free account
hub / github.com/M66B/FairEmail / DnsHelper

Class DnsHelper

app/src/main/java/eu/faircode/email/DnsHelper.java:93–746  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

91import javax.net.ssl.SSLSocketFactory;
92
93public class DnsHelper {
94 // https://dns.watch/
95 private static final String DEFAULT_DNS4 = "84.200.69.80";
96 private static final String DEFAULT_DNS6 = "2001:1608:10:25::1c04:b12f";
97 private static final int CHECK_TIMEOUT = 5; // seconds
98 private static final int LOOKUP_TIMEOUT = 15; // seconds
99
100 static void init(Context context) {
101 DnsClient.addDnsServerLookupMechanism(
102 new AbstractDnsServerLookupMechanism("FairEmail", 1) {
103 @Override
104 public boolean isAvailable() {
105 return true;
106 }
107
108 @Override
109 public List<String> getDnsServerAddresses() {
110 List<String> servers = getDnsServers(context);
111 Log.i("DNS servers=" + TextUtils.join(",", servers));
112 return servers;
113 }
114 });
115 }
116
117 static void checkMx(Context context, Address[] addresses) throws UnknownHostException {
118 if (addresses == null)
119 return;
120
121 for (Address address : addresses) {
122 String email = ((InternetAddress) address).getAddress();
123 String domain = UriHelper.getEmailDomain(email);
124 if (domain == null)
125 continue;
126 DnsRecord[] records = _lookup(context, domain, "mx", CHECK_TIMEOUT);
127 if (records.length == 0)
128 throw new UnknownHostException(domain);
129 }
130 }
131
132 @NonNull
133 static DnsRecord[] lookup(Context context, String name, String type) {
134 return _lookup(context, name, type, LOOKUP_TIMEOUT);
135 }
136
137 @NonNull
138 private static DnsRecord[] _lookup(Context context, String name, String type, int timeout) {
139 try {
140 return _lookup(context, name, type, timeout, false);
141 } catch (Throwable ex) {
142 if (ex instanceof MultipleIoException ||
143 ex instanceof ResolutionUnsuccessfulException ||
144 ex instanceof DnssecValidationFailedException)
145 Log.i(ex);
146 else
147 Log.e(ex);
148 return new DnsRecord[0];
149 }
150 }

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected