package de.thedevstack.conversationsplus.utils; import android.annotation.TargetApi; import android.content.Context; import android.net.ConnectivityManager; import android.net.LinkProperties; import android.net.Network; import android.net.RouteInfo; import android.os.Build; import java.io.IOException; import java.net.InetAddress; import java.net.UnknownHostException; import java.util.ArrayList; import java.util.List; import java.util.TreeSet; import java.util.regex.Pattern; 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.Data; import de.measite.minidns.record.SRV; import de.measite.minidns.util.NameUtil; import de.thedevstack.android.logcat.Logging; import de.thedevstack.conversationsplus.ConversationsPlusApplication; import de.thedevstack.conversationsplus.dto.SrvRecord; import de.thedevstack.conversationsplus.Config; import de.thedevstack.conversationsplus.xmpp.jid.Jid; public class DNSHelper { private static final String CLIENT_SRV_PREFIX = "_xmpp-client._tcp."; private static final String SECURE_CLIENT_SRV_PREFIX = "_xmpps-client._tcp."; private 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"); private 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"); private 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"); private 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"); private static final Pattern PATTERN_IPV6 = Pattern.compile("\\A(?:[0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}\\z"); protected static Client client = new Client(); static { client.setTimeout(Config.PING_TIMEOUT * 1000); } /** * Queries the SRV record for the server JID. * This method uses all available Domain Name Servers. * @param jid the server JID * @return TreeSet with SrvRecords. If no SRV record is found for JID an empty TreeSet is returned. */ public static final TreeSet querySrvRecord(Jid jid) { String host = jid.getDomainpart(); TreeSet result = new TreeSet<>(); final List dnsServers = Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP ? getDnsServers() : getDnsServersPreLollipop(); if (dnsServers != null) { for (InetAddress dnsServer : dnsServers) { result = querySrvRecord(host, dnsServer); if (!result.isEmpty()) { break; } } } return result; } @TargetApi(21) private static List getDnsServers() { List servers = new ArrayList<>(); ConnectivityManager connectivityManager = (ConnectivityManager) ConversationsPlusApplication.getInstance().getSystemService(Context.CONNECTIVITY_SERVICE); Network[] networks = connectivityManager == null ? null : connectivityManager.getAllNetworks(); if (networks == null) { return getDnsServersPreLollipop(); } for(int i = 0; i < networks.length; ++i) { LinkProperties linkProperties = connectivityManager.getLinkProperties(networks[i]); if (linkProperties != null) { if (hasDefaultRoute(linkProperties)) { servers.addAll(0, linkProperties.getDnsServers()); } else { servers.addAll(linkProperties.getDnsServers()); } } } if (servers.size() > 0) { Logging.d("dns", "used lollipop variant to discover dns servers in " + networks.length + " networks"); } return servers.size() > 0 ? servers : getDnsServersPreLollipop(); } @TargetApi(Build.VERSION_CODES.LOLLIPOP) private static boolean hasDefaultRoute(LinkProperties linkProperties) { for(RouteInfo route: linkProperties.getRoutes()) { if (route.isDefaultRoute()) { return true; } } return false; } private static List getDnsServersPreLollipop() { List servers = new ArrayList<>(); String[] dns = client.findDNS(); for(int i = 0; i < dns.length; ++i) { try { servers.add(InetAddress.getByName(dns[i])); } catch (UnknownHostException e) { //ignore } } return servers; } /** * Queries the SRV record for an host from the given Domain Name Server. * @param host the host to query for * @param dnsServerAddress the DNS to query on * @return TreeSet with SrvRecords. */ private static final TreeSet querySrvRecord(String host, InetAddress dnsServerAddress) { TreeSet result = new TreeSet<>(); querySrvRecord(host, dnsServerAddress, false, result); querySrvRecord(host, dnsServerAddress, true, result); return result; } private static final void querySrvRecord(String host, InetAddress dnsServerAddress, boolean tlsSrvRecord, TreeSet result) { String qname = (tlsSrvRecord ? SECURE_CLIENT_SRV_PREFIX : CLIENT_SRV_PREFIX) + host; String dnsServerHostAddress = dnsServerAddress.getHostAddress(); Logging.d("dns", "using dns server: " + dnsServerHostAddress + " to look up " + qname); try { DNSMessage message = client.query(qname, TYPE.SRV, CLASS.IN, dnsServerHostAddress); if (null != message) { Record[] rrset = message.getAnswers(); for (Record rr : rrset) { Data d = rr.getPayload(); if (d instanceof SRV && NameUtil.idnEquals(qname, rr.getName())) { SRV srv = (SRV) d; SrvRecord srvRecord = new SrvRecord(srv.getPriority(), srv.getName(), srv.getPort(), tlsSrvRecord); result.add(srvRecord); } } } else { Logging.e("dns", "No valid DNS message retrieved."); } } catch (IOException e) { Logging.d("dns", "Error while retrieving SRV record '" + qname + "' for '" + host + "' from DNS '" + dnsServerHostAddress + "': " + e.getMessage()); } } public static boolean isIp(final String server) { return server != null && ( 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()); } }