Debian Codegraph @

minidns-client/src/main/java/org/minidns/dnsserverlookup/AndroidUsingExec.java

raw
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.io.BufferedReader;
16 import java.io.IOException;
17 import java.io.InputStream;
18 import java.io.InputStreamReader;
19 import java.io.LineNumberReader;
20 import java.net.InetAddress;
21 import java.net.UnknownHostException;
22 import java.nio.charset.StandardCharsets;
23 import java.util.ArrayList;
24 import java.util.HashSet;
25 import java.util.List;
26 import java.util.Set;
27 import java.util.logging.Level;
28
29 /**
30 * Try to retrieve the list of DNS server by executing getprop.
31 */
32 public final class AndroidUsingExec extends AbstractDnsServerLookupMechanism {
33
34 public static final DnsServerLookupMechanism INSTANCE = new AndroidUsingExec();
35 public static final int PRIORITY = AndroidUsingReflection.PRIORITY - 1;
36
37 private AndroidUsingExec() {
38 super(AndroidUsingExec.class.getSimpleName(), PRIORITY);
39 }
40
41 @Override
42 public List<String> getDnsServerAddresses() {
43 try {
44 Process process = Runtime.getRuntime().exec("getprop");
45 InputStream inputStream = process.getInputStream();
46 LineNumberReader lnr = new LineNumberReader(
47 new InputStreamReader(inputStream, StandardCharsets.UTF_8));
48 Set<String> server = parseProps(lnr, true);
49 if (server.size() > 0) {
50 List<String> res = new ArrayList<>(server.size());
51 res.addAll(server);
52 return res;
53 }
54 } catch (IOException e) {
55 LOGGER.log(Level.WARNING, "Exception in findDNSByExec", e);
56 }
57 return null;
58 }
59
60 @Override
61 public boolean isAvailable() {
62 return PlatformDetection.isAndroid();
63 }
64
65 private static final String PROP_DELIM = "]: [";
66 protected static Set<String> parseProps(BufferedReader lnr, boolean logWarning) throws UnknownHostException, IOException {
67 String line = null;
68 Set<String> server = new HashSet<String>(6);
69
70 while ((line = lnr.readLine()) != null) {
71 int split = line.indexOf(PROP_DELIM);
72 if (split == -1) {
73 continue;
74 }
75 String property = line.substring(1, split);
76
77 int valueStart = split + PROP_DELIM.length();
78 int valueEnd = line.length() - 1;
79 if (valueEnd < valueStart) {
80 // This can happen if a newline sneaks in as the first character of the property value. For example
81 // "[propName]: [\n…]".
82 if (logWarning) {
83 LOGGER.warning("Malformed property detected: \"" + line + '"');
84 }
85 continue;
86 }
87
88 String value = line.substring(valueStart, valueEnd);
89
90 if (value.isEmpty()) {
91 continue;
92 }
93
94 if (property.endsWith(".dns") || property.endsWith(".dns1") ||
95 property.endsWith(".dns2") || property.endsWith(".dns3") ||
96 property.endsWith(".dns4")) {
97
98 // normalize the address
99
100 InetAddress ip = InetAddress.getByName(value);
101
102 if (ip == null) continue;
103
104 value = ip.getHostAddress();
105
106 if (value == null) continue;
107 if (value.length() == 0) continue;
108
109 server.add(value);
110 }
111 }
112
113 return server;
114 }
115 }