| 1 |
/* |
| 2 |
* Copyright 2015-2020 the original author or authors |
| 3 |
* |
| 4 |
* This software is licensed under the Apache License, Version 2.0, |
| 5 |
* the GNU Lesser General Public License version 2 or later ("LGPL") |
| 6 |
* and the WTFPL. |
| 7 |
* You may choose either license to govern your use of this software only |
| 8 |
* upon the condition that you accept all of the terms of either |
| 9 |
* the Apache License 2.0, the LGPL 2.1+ or the WTFPL. |
| 10 |
*/ |
| 11 |
package org.minidns.dnsserverlookup; |
| 12 |
|
| 13 |
import org.minidns.util.PlatformDetection; |
| 14 |
|
| 15 |
import java.lang.reflect.Method; |
| 16 |
import java.net.InetAddress; |
| 17 |
import java.util.ArrayList; |
| 18 |
import java.util.List; |
| 19 |
import java.util.logging.Level; |
| 20 |
|
| 21 |
/** |
| 22 |
* Try to retrieve the list of DNS server by calling SystemProperties. |
| 23 |
*/ |
| 24 |
public class AndroidUsingReflection extends AbstractDnsServerLookupMechanism { |
| 25 |
|
| 26 |
public static final DnsServerLookupMechanism INSTANCE = new AndroidUsingReflection(); |
| 27 |
public static final int PRIORITY = 1000; |
| 28 |
|
| 29 |
protected AndroidUsingReflection() { |
| 30 |
super(AndroidUsingReflection.class.getSimpleName(), PRIORITY); |
| 31 |
} |
| 32 |
|
| 33 |
@Override |
| 34 |
public List<String> getDnsServerAddresses() { |
| 35 |
try { |
| 36 |
Class<?> SystemProperties = |
| 37 |
Class.forName("android.os.SystemProperties"); |
| 38 |
Method method = SystemProperties.getMethod("get", |
| 39 |
new Class<?>[] { String.class }); |
| 40 |
|
| 41 |
ArrayList<String> servers = new ArrayList<String>(5); |
| 42 |
|
| 43 |
for (String propKey : new String[] { |
| 44 |
"net.dns1", "net.dns2", "net.dns3", "net.dns4"}) { |
| 45 |
|
| 46 |
String value = (String) method.invoke(null, propKey); |
| 47 |
|
| 48 |
if (value == null) continue; |
| 49 |
if (value.length() == 0) continue; |
| 50 |
if (servers.contains(value)) continue; |
| 51 |
|
| 52 |
InetAddress ip = InetAddress.getByName(value); |
| 53 |
|
| 54 |
if (ip == null) continue; |
| 55 |
|
| 56 |
value = ip.getHostAddress(); |
| 57 |
|
| 58 |
if (value == null) continue; |
| 59 |
if (value.length() == 0) continue; |
| 60 |
if (servers.contains(value)) continue; |
| 61 |
|
| 62 |
servers.add(value); |
| 63 |
} |
| 64 |
|
| 65 |
if (servers.size() > 0) { |
| 66 |
return servers; |
| 67 |
} |
| 68 |
} catch (Exception e) { |
| 69 |
// we might trigger some problems this way |
| 70 |
LOGGER.log(Level.WARNING, "Exception in findDNSByReflection", e); |
| 71 |
} |
| 72 |
return null; |
| 73 |
} |
| 74 |
|
| 75 |
@Override |
| 76 |
public boolean isAvailable() { |
| 77 |
return PlatformDetection.isAndroid(); |
| 78 |
} |
| 79 |
|
| 80 |
} |