android: Use configured custom DNS servers
authorTobias Brunner <tobias@strongswan.org>
Tue, 5 Mar 2019 16:26:26 +0000 (17:26 +0100)
committerTobias Brunner <tobias@strongswan.org>
Tue, 5 Mar 2019 16:36:09 +0000 (17:36 +0100)
src/frontends/android/app/src/main/java/org/strongswan/android/logic/CharonVpnService.java

index ac9ff49..a43095f 100644 (file)
@@ -838,8 +838,7 @@ public class CharonVpnService extends VpnService implements Runnable, VpnStateSe
                {
                        try
                        {
-                               mBuilder.addDnsServer(address);
-                               mCache.recordAddressFamily(address);
+                               mCache.addDnsServer(address);
                        }
                        catch (IllegalArgumentException ex)
                        {
@@ -1073,8 +1072,9 @@ public class CharonVpnService extends VpnService implements Runnable, VpnStateSe
                private final int mSplitTunneling;
                private final SelectedAppsHandling mAppHandling;
                private final SortedSet<String> mSelectedApps;
+               private final List<InetAddress> mDnsServers = new ArrayList<>();
                private int mMtu;
-               private boolean mIPv4Seen, mIPv6Seen;
+               private boolean mIPv4Seen, mIPv6Seen, mDnsServersConfigured;
 
                public BuilderCache(VpnProfile profile)
                {
@@ -1111,6 +1111,23 @@ public class CharonVpnService extends VpnService implements Runnable, VpnStateSe
                        }
                        mAppHandling = appHandling;
 
+                       if (profile.getDnsServers() != null)
+                       {
+                               for (String server : profile.getDnsServers().split("\\s+"))
+                               {
+                                       try
+                                       {
+                                               mDnsServers.add(InetAddress.getByName(server));
+                                               recordAddressFamily(server);
+                                               mDnsServersConfigured = true;
+                                       }
+                                       catch (UnknownHostException e)
+                                       {
+                                               e.printStackTrace();
+                                       }
+                               }
+                       }
+
                        /* set a default MTU, will be set by the daemon for regular interfaces */
                        Integer mtu = profile.getMTU();
                        mMtu = mtu == null ? Constants.MTU_MAX : mtu;
@@ -1129,6 +1146,25 @@ public class CharonVpnService extends VpnService implements Runnable, VpnStateSe
                        }
                }
 
+               public void addDnsServer(String address)
+               {
+                       /* ignore received DNS servers if any were configured */
+                       if (mDnsServersConfigured)
+                       {
+                               return;
+                       }
+
+                       try
+                       {
+                               mDnsServers.add(InetAddress.getByName(address));
+                               recordAddressFamily(address);
+                       }
+                       catch (UnknownHostException e)
+                       {
+                               e.printStackTrace();
+                       }
+               }
+
                public void addRoute(String address, int prefixLength)
                {
                        try
@@ -1179,6 +1215,10 @@ public class CharonVpnService extends VpnService implements Runnable, VpnStateSe
                        {
                                builder.addAddress(address.getFrom(), address.getPrefix());
                        }
+                       for (InetAddress server : mDnsServers)
+                       {
+                               builder.addDnsServer(server);
+                       }
                        /* add routes depending on whether split tunneling is allowed or not,
                         * that is, whether we have to handle and block non-VPN traffic */
                        if ((mSplitTunneling & VpnProfile.SPLIT_TUNNELING_BLOCK_IPV4) == 0)