-
-
Notifications
You must be signed in to change notification settings - Fork 6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d6000af
commit 711715c
Showing
15 changed files
with
207 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
app/src/main/java/org/thoughtcrime/securesms/net/CustomDns.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package org.thoughtcrime.securesms.net; | ||
|
||
import androidx.annotation.NonNull; | ||
|
||
import com.annimon.stream.Stream; | ||
|
||
import org.xbill.DNS.ARecord; | ||
import org.xbill.DNS.Lookup; | ||
import org.xbill.DNS.Record; | ||
import org.xbill.DNS.Resolver; | ||
import org.xbill.DNS.SimpleResolver; | ||
import org.xbill.DNS.TextParseException; | ||
import org.xbill.DNS.Type; | ||
|
||
import java.net.InetAddress; | ||
import java.net.UnknownHostException; | ||
import java.util.List; | ||
|
||
import okhttp3.Dns; | ||
|
||
/** | ||
* A {@link Dns} implementation that specifies the hostname of a specific DNS. | ||
*/ | ||
public class CustomDns implements Dns { | ||
|
||
private final String dnsHostname; | ||
|
||
public CustomDns(@NonNull String dnsHostname) { | ||
this.dnsHostname = dnsHostname; | ||
} | ||
|
||
@Override | ||
public @NonNull List<InetAddress> lookup(@NonNull String hostname) throws UnknownHostException { | ||
Resolver resolver = new SimpleResolver(dnsHostname); | ||
Lookup lookup = doLookup(hostname); | ||
|
||
lookup.setResolver(resolver); | ||
|
||
Record[] records = lookup.run(); | ||
|
||
if (records != null) { | ||
List<InetAddress> ipv4Addresses = Stream.of(records) | ||
.filter(r -> r.getType() == Type.A) | ||
.map(r -> (ARecord) r) | ||
.map(ARecord::getAddress) | ||
.toList(); | ||
if (ipv4Addresses.size() > 0) { | ||
return ipv4Addresses; | ||
} | ||
} | ||
|
||
throw new UnknownHostException(hostname); | ||
} | ||
|
||
private static @NonNull Lookup doLookup(@NonNull String hostname) throws UnknownHostException { | ||
try { | ||
return new Lookup(hostname); | ||
} catch (TextParseException e) { | ||
throw new UnknownHostException(); | ||
} | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
app/src/main/java/org/thoughtcrime/securesms/net/SequentialDns.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package org.thoughtcrime.securesms.net; | ||
|
||
import androidx.annotation.NonNull; | ||
|
||
import org.thoughtcrime.securesms.logging.Log; | ||
|
||
import java.net.InetAddress; | ||
import java.net.UnknownHostException; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Locale; | ||
|
||
import okhttp3.Dns; | ||
|
||
/** | ||
* Iterates through an ordered list of {@link Dns}, trying each one in sequence. | ||
*/ | ||
public class SequentialDns implements Dns { | ||
|
||
private static final String TAG = Log.tag(SequentialDns.class); | ||
|
||
private List<Dns> dnsList; | ||
|
||
public SequentialDns(Dns... dns) { | ||
this.dnsList = Arrays.asList(dns); | ||
} | ||
|
||
@Override | ||
public @NonNull List<InetAddress> lookup(@NonNull String hostname) throws UnknownHostException { | ||
for (Dns dns : dnsList) { | ||
try { | ||
List<InetAddress> addresses = dns.lookup(hostname); | ||
if (addresses.size() > 0) { | ||
return addresses; | ||
} else { | ||
Log.w(TAG, String.format(Locale.ENGLISH, "Didn't find any addresses for %s using %s. Continuing.", hostname, dns.getClass().getSimpleName())); | ||
} | ||
} catch (UnknownHostException e) { | ||
Log.w(TAG, String.format(Locale.ENGLISH, "Failed to resolve %s using %s. Continuing.", hostname, dns.getClass().getSimpleName())); | ||
} | ||
} | ||
Log.w(TAG, "Failed to resolve using any DNS."); | ||
throw new UnknownHostException(hostname); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
app/src/main/java/org/thoughtcrime/securesms/net/StaticDns.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package org.thoughtcrime.securesms.net; | ||
|
||
import androidx.annotation.NonNull; | ||
|
||
import java.net.InetAddress; | ||
import java.net.UnknownHostException; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import okhttp3.Dns; | ||
|
||
/** | ||
* A super simple {@link Dns} implementation that maps hostnames to a static IP addresses. | ||
*/ | ||
public class StaticDns implements Dns { | ||
|
||
private final Map<String, String> hostnameMap; | ||
|
||
public StaticDns(@NonNull Map<String, String> hostnameMap) { | ||
this.hostnameMap = hostnameMap; | ||
} | ||
|
||
@Override | ||
public @NonNull List<InetAddress> lookup(@NonNull String hostname) throws UnknownHostException { | ||
String ip = hostnameMap.get(hostname); | ||
|
||
if (ip != null) { | ||
return Collections.singletonList(InetAddress.getByName(ip)); | ||
} else { | ||
throw new UnknownHostException(hostname); | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.