android: Import DNS servers
authorTobias Brunner <tobias@strongswan.org>
Tue, 5 Mar 2019 16:44:48 +0000 (17:44 +0100)
committerTobias Brunner <tobias@strongswan.org>
Tue, 5 Mar 2019 16:45:48 +0000 (17:45 +0100)
src/frontends/android/app/src/main/java/org/strongswan/android/ui/VpnProfileImportActivity.java

index 1d8b677..f1c5746 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2016-2018 Tobias Brunner
+ * Copyright (C) 2016-2019 Tobias Brunner
  * HSR Hochschule fuer Technik Rapperswil
  *
  * This program is free software; you can redistribute it and/or modify it
@@ -65,6 +65,7 @@ import java.io.ByteArrayOutputStream;
 import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.io.InputStream;
+import java.net.InetAddress;
 import java.net.URL;
 import java.net.UnknownHostException;
 import java.security.KeyStore;
@@ -74,6 +75,7 @@ import java.security.cert.CertificateException;
 import java.security.cert.CertificateFactory;
 import java.security.cert.X509Certificate;
 import java.util.ArrayList;
+import java.util.Collections;
 import java.util.UUID;
 
 import javax.net.ssl.SSLHandshakeException;
@@ -522,6 +524,7 @@ public class VpnProfileImportActivity extends AppCompatActivity
 
                profile.setIkeProposal(getProposal(obj, "ike-proposal", true));
                profile.setEspProposal(getProposal(obj, "esp-proposal", false));
+               profile.setDnsServers(getAddressList(obj, "dns-servers"));
                profile.setMTU(getInteger(obj, "mtu", Constants.MTU_MIN, Constants.MTU_MAX));
                profile.setNATKeepAlive(getInteger(obj, "nat-keepalive", Constants.NAT_KEEPALIVE_MIN, Constants.NAT_KEEPALIVE_MAX));
                JSONObject split = obj.optJSONObject("split-tunneling");
@@ -605,6 +608,44 @@ public class VpnProfileImportActivity extends AppCompatActivity
                return null;
        }
 
+       private String getAddressList(JSONObject obj, String key) throws JSONException
+       {
+               ArrayList<String> addrs = new ArrayList<>();
+               JSONArray arr = obj.optJSONArray(key);
+               if (arr != null)
+               {
+                       for (int i = 0; i < arr.length(); i++)
+                       {
+                               String addr = arr.getString(i).replace(" ", "");
+                               addrs.add(addr);
+                       }
+               }
+               else
+               {
+                       String value = obj.optString(key, null);
+                       if (!TextUtils.isEmpty(value))
+                       {
+                               Collections.addAll(addrs, value.split("\\s+"));
+                       }
+               }
+               if (addrs.size() > 0)
+               {
+                       for (String addr : addrs)
+                       {
+                               try
+                               {
+                                       InetAddress.getByName(addr);
+                               }
+                               catch (UnknownHostException e)
+                               {
+                                       throw new JSONException(getString(R.string.profile_import_failed_value, key));
+                               }
+                       }
+                       return TextUtils.join(" ", addrs);
+               }
+               return null;
+       }
+
        private String getApps(JSONArray arr) throws JSONException
        {
                ArrayList<String> apps = new ArrayList<>();