2014-02-28 17:46:01 +00:00
|
|
|
package eu.siacs.conversations.utils;
|
2014-02-07 05:52:09 +00:00
|
|
|
|
2015-07-20 12:26:29 +00:00
|
|
|
import android.os.Bundle;
|
|
|
|
import android.util.Log;
|
2014-04-03 07:42:58 +00:00
|
|
|
|
2014-02-07 05:52:09 +00:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.net.InetAddress;
|
2014-04-03 08:12:50 +00:00
|
|
|
import java.net.SocketTimeoutException;
|
2014-03-25 16:21:56 +00:00
|
|
|
import java.util.ArrayList;
|
2014-04-03 07:42:58 +00:00
|
|
|
import java.util.Collections;
|
2014-02-07 05:52:09 +00:00
|
|
|
import java.util.Random;
|
2014-04-03 07:42:58 +00:00
|
|
|
import java.util.TreeMap;
|
2015-06-19 14:25:08 +00:00
|
|
|
import java.util.regex.Pattern;
|
2014-02-07 05:52:09 +00:00
|
|
|
|
2015-07-20 12:26:29 +00:00
|
|
|
import de.measite.minidns.Client;
|
|
|
|
import de.measite.minidns.DNSMessage;
|
|
|
|
import de.measite.minidns.Record;
|
|
|
|
import de.measite.minidns.Record.CLASS;
|
|
|
|
import de.measite.minidns.Record.TYPE;
|
|
|
|
import de.measite.minidns.record.A;
|
|
|
|
import de.measite.minidns.record.AAAA;
|
|
|
|
import de.measite.minidns.record.Data;
|
|
|
|
import de.measite.minidns.record.SRV;
|
|
|
|
import de.measite.minidns.util.NameUtil;
|
|
|
|
import eu.siacs.conversations.Config;
|
|
|
|
import eu.siacs.conversations.xmpp.jid.Jid;
|
2014-02-07 05:52:09 +00:00
|
|
|
|
|
|
|
public class DNSHelper {
|
2015-06-19 14:25:08 +00:00
|
|
|
|
|
|
|
public static final Pattern PATTERN_IPV4 = Pattern.compile("\\A(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)(\\.(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)){3}\\z");
|
|
|
|
public static final Pattern PATTERN_IPV6_HEX4DECCOMPRESSED = Pattern.compile("\\A((?:[0-9A-Fa-f]{1,4}(?::[0-9A-Fa-f]{1,4})*)?) ::((?:[0-9A-Fa-f]{1,4}:)*)(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)(\\.(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)){3}\\z");
|
|
|
|
public static final Pattern PATTERN_IPV6_6HEX4DEC = Pattern.compile("\\A((?:[0-9A-Fa-f]{1,4}:){6,6})(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)(\\.(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)){3}\\z");
|
|
|
|
public static final Pattern PATTERN_IPV6_HEXCOMPRESSED = Pattern.compile("\\A((?:[0-9A-Fa-f]{1,4}(?::[0-9A-Fa-f]{1,4})*)?)::((?:[0-9A-Fa-f]{1,4}(?::[0-9A-Fa-f]{1,4})*)?)\\z");
|
|
|
|
public static final Pattern PATTERN_IPV6 = Pattern.compile("\\A(?:[0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}\\z");
|
|
|
|
|
2014-04-03 07:42:58 +00:00
|
|
|
protected static Client client = new Client();
|
|
|
|
|
2014-11-05 20:55:47 +00:00
|
|
|
public static Bundle getSRVRecord(final Jid jid) throws IOException {
|
|
|
|
final String host = jid.getDomainpart();
|
2014-04-03 07:42:58 +00:00
|
|
|
String dns[] = client.findDNS();
|
|
|
|
|
|
|
|
if (dns != null) {
|
|
|
|
for (String dnsserver : dns) {
|
|
|
|
InetAddress ip = InetAddress.getByName(dnsserver);
|
|
|
|
Bundle b = queryDNS(host, ip);
|
2014-10-27 20:48:25 +00:00
|
|
|
if (b.containsKey("values")) {
|
2014-04-03 07:42:58 +00:00
|
|
|
return b;
|
2014-03-27 13:31:55 +00:00
|
|
|
}
|
2014-03-25 16:21:56 +00:00
|
|
|
}
|
|
|
|
}
|
2014-04-03 07:42:58 +00:00
|
|
|
return queryDNS(host, InetAddress.getByName("8.8.8.8"));
|
2014-03-27 10:03:10 +00:00
|
|
|
}
|
2014-03-27 13:31:55 +00:00
|
|
|
|
2014-03-27 10:03:10 +00:00
|
|
|
public static Bundle queryDNS(String host, InetAddress dnsServer) {
|
2014-10-27 20:48:25 +00:00
|
|
|
Bundle bundle = new Bundle();
|
2014-03-27 10:03:10 +00:00
|
|
|
try {
|
2014-04-03 09:01:20 +00:00
|
|
|
String qname = "_xmpp-client._tcp." + host;
|
2015-06-04 00:48:07 +00:00
|
|
|
Log.d(Config.LOGTAG, "using dns server: " + dnsServer.getHostAddress() + " to look up " + host);
|
|
|
|
DNSMessage message = client.query(qname, TYPE.SRV, CLASS.IN, dnsServer.getHostAddress());
|
2014-04-03 07:42:58 +00:00
|
|
|
|
2014-11-05 20:55:47 +00:00
|
|
|
TreeMap<Integer, ArrayList<SRV>> priorities = new TreeMap<>();
|
|
|
|
TreeMap<String, ArrayList<String>> ips4 = new TreeMap<>();
|
|
|
|
TreeMap<String, ArrayList<String>> ips6 = new TreeMap<>();
|
2014-04-03 09:01:20 +00:00
|
|
|
|
2015-06-04 00:48:07 +00:00
|
|
|
for (Record[] rrset : new Record[][] { message.getAnswers(), message.getAdditionalResourceRecords() }) {
|
2014-04-03 09:01:20 +00:00
|
|
|
for (Record rr : rrset) {
|
|
|
|
Data d = rr.getPayload();
|
2015-06-04 00:48:07 +00:00
|
|
|
if (d instanceof SRV && NameUtil.idnEquals(qname, rr.getName())) {
|
2014-04-03 09:01:20 +00:00
|
|
|
SRV srv = (SRV) d;
|
|
|
|
if (!priorities.containsKey(srv.getPriority())) {
|
2015-06-04 00:48:07 +00:00
|
|
|
priorities.put(srv.getPriority(),new ArrayList<SRV>());
|
2014-04-03 09:01:20 +00:00
|
|
|
}
|
|
|
|
priorities.get(srv.getPriority()).add(srv);
|
|
|
|
}
|
|
|
|
if (d instanceof A) {
|
2015-06-04 00:48:07 +00:00
|
|
|
A a = (A) d;
|
2014-04-03 09:01:20 +00:00
|
|
|
if (!ips4.containsKey(rr.getName())) {
|
2015-06-04 00:48:07 +00:00
|
|
|
ips4.put(rr.getName(), new ArrayList<String>());
|
2014-04-03 09:01:20 +00:00
|
|
|
}
|
2015-06-04 00:48:07 +00:00
|
|
|
ips4.get(rr.getName()).add(a.toString());
|
2014-04-03 09:01:20 +00:00
|
|
|
}
|
|
|
|
if (d instanceof AAAA) {
|
|
|
|
AAAA aaaa = (AAAA) d;
|
|
|
|
if (!ips6.containsKey(rr.getName())) {
|
2015-06-04 00:48:07 +00:00
|
|
|
ips6.put(rr.getName(), new ArrayList<String>());
|
2014-04-03 09:01:20 +00:00
|
|
|
}
|
|
|
|
ips6.get(rr.getName()).add("[" + aaaa.toString() + "]");
|
2014-04-03 07:42:58 +00:00
|
|
|
}
|
2014-03-27 13:31:55 +00:00
|
|
|
}
|
2014-02-07 05:52:09 +00:00
|
|
|
}
|
2014-04-03 07:42:58 +00:00
|
|
|
|
2015-06-04 00:48:07 +00:00
|
|
|
ArrayList<SRV> result = new ArrayList<>();
|
2014-08-31 14:28:21 +00:00
|
|
|
for (ArrayList<SRV> s : priorities.values()) {
|
2014-04-03 07:42:58 +00:00
|
|
|
result.addAll(s);
|
2014-03-27 13:31:55 +00:00
|
|
|
}
|
2014-04-03 07:42:58 +00:00
|
|
|
|
2015-05-28 15:31:46 +00:00
|
|
|
ArrayList<Bundle> values = new ArrayList<>();
|
2014-04-03 07:42:58 +00:00
|
|
|
if (result.size() == 0) {
|
2015-05-28 15:31:46 +00:00
|
|
|
DNSMessage response;
|
|
|
|
response = client.query(host, TYPE.A, CLASS.IN, dnsServer.getHostAddress());
|
|
|
|
for(int i = 0; i < response.getAnswers().length; ++i) {
|
|
|
|
values.add(createNamePortBundle(host,5222,response.getAnswers()[i].getPayload()));
|
|
|
|
}
|
|
|
|
response = client.query(host, TYPE.AAAA, CLASS.IN, dnsServer.getHostAddress());
|
|
|
|
for(int i = 0; i < response.getAnswers().length; ++i) {
|
|
|
|
values.add(createNamePortBundle(host,5222,response.getAnswers()[i].getPayload()));
|
|
|
|
}
|
2015-07-03 11:07:48 +00:00
|
|
|
values.add(createNamePortBundle(host,5222));
|
2015-05-28 15:31:46 +00:00
|
|
|
bundle.putParcelableArrayList("values", values);
|
2014-10-27 20:48:25 +00:00
|
|
|
return bundle;
|
2014-03-25 17:12:27 +00:00
|
|
|
}
|
2014-04-03 07:42:58 +00:00
|
|
|
for (SRV srv : result) {
|
2014-12-14 17:31:52 +00:00
|
|
|
if (ips6.containsKey(srv.getName())) {
|
2014-12-14 18:02:29 +00:00
|
|
|
values.add(createNamePortBundle(srv.getName(),srv.getPort(),ips6));
|
2015-05-28 14:55:48 +00:00
|
|
|
} else {
|
|
|
|
DNSMessage response = client.query(srv.getName(), TYPE.AAAA, CLASS.IN, dnsServer.getHostAddress());
|
2015-05-28 15:31:46 +00:00
|
|
|
for(int i = 0; i < response.getAnswers().length; ++i) {
|
2015-06-04 14:26:51 +00:00
|
|
|
values.add(createNamePortBundle(srv.getName(),srv.getPort(),response.getAnswers()[i].getPayload()));
|
2015-05-28 14:55:48 +00:00
|
|
|
}
|
2014-12-14 17:31:52 +00:00
|
|
|
}
|
2014-10-27 20:48:25 +00:00
|
|
|
if (ips4.containsKey(srv.getName())) {
|
2014-12-14 18:02:29 +00:00
|
|
|
values.add(createNamePortBundle(srv.getName(),srv.getPort(),ips4));
|
2015-05-28 14:55:48 +00:00
|
|
|
} else {
|
|
|
|
DNSMessage response = client.query(srv.getName(), TYPE.A, CLASS.IN, dnsServer.getHostAddress());
|
2015-05-28 15:31:46 +00:00
|
|
|
for(int i = 0; i < response.getAnswers().length; ++i) {
|
2015-06-04 14:26:51 +00:00
|
|
|
values.add(createNamePortBundle(srv.getName(),srv.getPort(),response.getAnswers()[i].getPayload()));
|
2015-05-28 14:55:48 +00:00
|
|
|
}
|
2014-10-27 20:48:25 +00:00
|
|
|
}
|
2015-05-28 14:55:48 +00:00
|
|
|
values.add(createNamePortBundle(srv.getName(), srv.getPort()));
|
2014-04-03 07:42:58 +00:00
|
|
|
}
|
2014-10-27 20:48:25 +00:00
|
|
|
bundle.putParcelableArrayList("values", values);
|
2014-04-03 08:12:50 +00:00
|
|
|
} catch (SocketTimeoutException e) {
|
2014-10-27 20:48:25 +00:00
|
|
|
bundle.putString("error", "timeout");
|
2014-04-03 08:41:21 +00:00
|
|
|
} catch (Exception e) {
|
2015-07-09 11:40:08 +00:00
|
|
|
e.printStackTrace();
|
2014-10-27 20:48:25 +00:00
|
|
|
bundle.putString("error", "unhandled");
|
2014-03-27 10:03:10 +00:00
|
|
|
}
|
2014-10-27 20:48:25 +00:00
|
|
|
return bundle;
|
2014-02-07 05:52:09 +00:00
|
|
|
}
|
|
|
|
|
2015-05-28 14:55:48 +00:00
|
|
|
private static Bundle createNamePortBundle(String name, int port) {
|
|
|
|
Bundle namePort = new Bundle();
|
|
|
|
namePort.putString("name", name);
|
|
|
|
namePort.putInt("port", port);
|
|
|
|
return namePort;
|
|
|
|
}
|
|
|
|
|
2014-12-14 18:02:29 +00:00
|
|
|
private static Bundle createNamePortBundle(String name, int port, TreeMap<String, ArrayList<String>> ips) {
|
|
|
|
Bundle namePort = new Bundle();
|
|
|
|
namePort.putString("name", name);
|
|
|
|
namePort.putInt("port", port);
|
|
|
|
if (ips!=null) {
|
|
|
|
ArrayList<String> ip = ips.get(name);
|
|
|
|
Collections.shuffle(ip, new Random());
|
|
|
|
namePort.putString("ip", ip.get(0));
|
|
|
|
}
|
|
|
|
return namePort;
|
|
|
|
}
|
|
|
|
|
2015-05-28 14:55:48 +00:00
|
|
|
private static Bundle createNamePortBundle(String name, int port, Data data) {
|
|
|
|
Bundle namePort = new Bundle();
|
|
|
|
namePort.putString("name", name);
|
|
|
|
namePort.putInt("port", port);
|
|
|
|
if (data instanceof A) {
|
|
|
|
namePort.putString("ip", data.toString());
|
|
|
|
} else if (data instanceof AAAA) {
|
|
|
|
namePort.putString("ip","["+data.toString()+"]");
|
|
|
|
}
|
|
|
|
return namePort;
|
|
|
|
}
|
|
|
|
|
2015-06-19 14:25:08 +00:00
|
|
|
public static boolean isIp(final String server) {
|
|
|
|
return PATTERN_IPV4.matcher(server).matches()
|
|
|
|
|| PATTERN_IPV6.matcher(server).matches()
|
|
|
|
|| PATTERN_IPV6_6HEX4DEC.matcher(server).matches()
|
|
|
|
|| PATTERN_IPV6_HEX4DECCOMPRESSED.matcher(server).matches()
|
|
|
|
|| PATTERN_IPV6_HEXCOMPRESSED.matcher(server).matches();
|
2014-02-07 05:52:09 +00:00
|
|
|
}
|
|
|
|
}
|