summaryrefslogtreecommitdiff
path: root/app/src/main/java/se/leap/bitmaskclient/providersetup/connectivity/DnsResolver.java
blob: e8249692266215de39a9dd51b27924639dc7615a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package se.leap.bitmaskclient.providersetup.connectivity;

import static java.net.InetAddress.getByName;

import android.util.Log;

import androidx.annotation.NonNull;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;

import de.blinkt.openvpn.core.VpnStatus;
import okhttp3.Dns;
import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;
import okhttp3.dnsoverhttps.DnsOverHttps;
import se.leap.bitmaskclient.base.models.Provider;
import se.leap.bitmaskclient.base.models.ProviderObservable;
import se.leap.bitmaskclient.base.utils.IPAddress;

public class DnsResolver implements Dns {
    OkHttpClient dohHttpClient;
    boolean preferDoH;

    public DnsResolver(OkHttpClient dohHttpClient, boolean preferDoH) {
        this.dohHttpClient = dohHttpClient;
        this.preferDoH = preferDoH;
    }

    @NonNull
    @Override
    public List<InetAddress> lookup(@NonNull String hostname) throws UnknownHostException {
        Log.d("DNS", "trying to resolve DNS for " + hostname);
        List<InetAddress> list = null;
        if (preferDoH) {
            if ((list = tryLookupDoH(hostname)) == null) {
                list  = tryLookupSystemDNS(hostname);
            }
        } else {
            if ((list = tryLookupSystemDNS(hostname)) == null) {
                list = tryLookupDoH(hostname);
            }
        }

        if (list != null) {
            return list;
        }

        Log.d("DNS", "try hard coded IPs");
        // let's check if there's an hard-coded IP we can use
        ProviderObservable observable = ProviderObservable.getInstance();
        Provider currentProvider;
        if (observable.getProviderForDns() != null) {
            currentProvider = observable.getProviderForDns();
        } else {
            currentProvider = observable.getCurrentProvider();
        }
        String ip = currentProvider.getIpForHostname(hostname);
        if (!ip.isEmpty()) {
            VpnStatus.logWarning("[API] Normal DNS resolution for " + hostname + " seems to be blocked. Circumventing.");
            ArrayList<InetAddress> addresses = new ArrayList<>();
            addresses.add(InetAddress.getByAddress(hostname, IPAddress.asBytes(ip)));
            return addresses;
        } else {
            VpnStatus.logWarning("[API] Could not resolve DNS for " + hostname);
            throw new UnknownHostException("Hostname " + hostname + " not found");
        }
    }

    private List<InetAddress> tryLookupSystemDNS(@NonNull String hostname) throws RuntimeException, UnknownHostException {
        try {
            Log.d("DNS", "trying to resolve " + hostname + "with system DNS");
            return Dns.SYSTEM.lookup(hostname);
        } catch (UnknownHostException e) {
            e.printStackTrace();
            return null;
        }
    }

    private List<InetAddress> tryLookupDoH(@NonNull String hostname) throws UnknownHostException {
        DnsOverHttps njallaDoH = new DnsOverHttps.Builder().client(dohHttpClient)
                .url(HttpUrl.get("https://dns.njal.la/dns-query"))
                .bootstrapDnsHosts(getByName("95.215.19.53"), getByName("2001:67c:2354:2::53"))
                .build();
        try {
            Log.d("DNS", "DoH via dns.njal.la");
            return njallaDoH.lookup(hostname);
        } catch (UnknownHostException e) {
            e.printStackTrace();
            Log.e("DNS", "DoH via dns.njal.la failed");
        }

        DnsOverHttps quad9 = new DnsOverHttps.Builder().client(dohHttpClient)
                .url(HttpUrl.get("https://dns.quad9.net/dns-query"))
                .bootstrapDnsHosts(getByName("9.9.9.9"), getByName("149.112.112.112"), getByName("2620:fe::fe"), getByName("2620:fe::9"))
                .build();
        try {
            Log.d("DNS", "DoH via dns.quad9.net");
            return quad9.lookup(hostname);
        } catch (UnknownHostException e) {
            e.printStackTrace();
            Log.e("DNS", "DoH via dns.quad9.net failed");
        }

        DnsOverHttps cloudFlareDoHClient = new DnsOverHttps.Builder().client(dohHttpClient)
                .url(HttpUrl.get("https://1.1.1.1/dns-query"))
                .bootstrapDnsHosts(getByName("1.1.1.1"), getByName("1.0.0.1"))
                .build();

        try {
            Log.d("DNS", "DoH via cloudflare 1.1.1.1");
            return cloudFlareDoHClient.lookup(hostname);
        } catch (UnknownHostException e) {
            e.printStackTrace();
            Log.e("DNS", "DoH via cloudflare failed");
        }
        return null;
    }
}