]> git.ipfire.org Git - thirdparty/strongswan.git/commitdiff
android: Import proxy server settings
authorTobias Brunner <tobias@strongswan.org>
Thu, 5 Jun 2025 14:10:21 +0000 (16:10 +0200)
committerTobias Brunner <tobias@strongswan.org>
Fri, 27 Jun 2025 06:57:45 +0000 (08:57 +0200)
src/frontends/android/app/src/main/java/org/strongswan/android/ui/VpnProfileImportActivity.java

index 08565d67bb3fd95f0c74b5a998197c8a8b0d37b7..ee56c1e5301e5c1bed00355c1ad5dae627bf37f9 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2016-2020 Tobias Brunner
+ * Copyright (C) 2016-2025 Tobias Brunner
  *
  * Copyright (C) secunet Security Networks AG
  *
@@ -556,6 +556,14 @@ public class VpnProfileImportActivity extends AppCompatActivity
                        flags |= VpnProfile.FLAGS_IPv6_TRANSPORT;
                }
 
+               JSONObject proxy = obj.optJSONObject("proxy");
+               if (proxy != null)
+               {
+                       profile.setProxyHost(proxy.optString("host"));
+                       profile.setProxyPort(getInteger(proxy, "port", 1, 65535));
+                       profile.setProxyExclusions(getFlatStringList(proxy, "exclusions"));
+               }
+
                JSONObject split = obj.optJSONObject("split-tunneling");
                if (split != null)
                {
@@ -569,8 +577,8 @@ public class VpnProfileImportActivity extends AppCompatActivity
                        profile.setSplitTunneling(st == 0 ? null : st);
                }
                /* only one of these can be set, prefer specific apps */
-               String selectedApps = getApps(obj.optJSONArray("apps"));
-               String excludedApps = getApps(obj.optJSONArray("excluded-apps"));
+               String selectedApps = getFlatStringList(obj, "apps");
+               String excludedApps = getFlatStringList(obj, "excluded-apps");
                if (!TextUtils.isEmpty(selectedApps))
                {
                        profile.setSelectedApps(selectedApps);
@@ -606,24 +614,8 @@ public class VpnProfileImportActivity extends AppCompatActivity
 
        private String getSubnets(JSONObject split, String key) throws JSONException
        {
-               ArrayList<String> subnets = new ArrayList<>();
-               JSONArray arr = split.optJSONArray(key);
-               if (arr != null)
-               {
-                       for (int i = 0; i < arr.length(); i++)
-                       {       /* replace all spaces, e.g. in "192.168.1.1 - 192.168.1.10" */
-                               subnets.add(arr.getString(i).replace(" ", ""));
-                       }
-               }
-               else
-               {
-                       String value = split.optString(key, null);
-                       if (!TextUtils.isEmpty(value))
-                       {
-                               subnets.add(value);
-                       }
-               }
-               if (subnets.size() > 0)
+               ArrayList<String> subnets = getStringList(split, key);
+               if (!subnets.isEmpty())
                {
                        String joined = TextUtils.join(" ", subnets);
                        IPRangeSet ranges = IPRangeSet.fromString(joined);
@@ -639,25 +631,8 @@ public class VpnProfileImportActivity extends AppCompatActivity
 
        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)
+               ArrayList<String> addrs = getStringList(obj, key);
+               if (!addrs.isEmpty())
                {
                        for (String addr : addrs)
                        {
@@ -675,17 +650,39 @@ public class VpnProfileImportActivity extends AppCompatActivity
                return null;
        }
 
-       private String getApps(JSONArray arr) throws JSONException
+       private String getFlatStringList(JSONObject obj, String key) throws JSONException
        {
-               ArrayList<String> apps = new ArrayList<>();
+               ArrayList<String> list = getStringList(obj, key);
+               if (!list.isEmpty())
+               {
+                       return TextUtils.join(" ", list);
+               }
+               return null;
+       }
+
+       /**
+        * Return a list of strings, either retrieved from an array or from a space-separated string.
+        */
+       private ArrayList<String> getStringList(JSONObject obj, String key) throws JSONException
+       {
+               ArrayList<String> list = new ArrayList<>();
+               JSONArray arr = obj.optJSONArray(key);
                if (arr != null)
                {
                        for (int i = 0; i < arr.length(); i++)
+                       {       /* replace all spaces, including e.g. in "192.168.1.1 - 192.168.1.10" */
+                               list.add(arr.getString(i).replace(" ", ""));
+                       }
+               }
+               else
+               {
+                       String value = obj.optString(key, null);
+                       if (!TextUtils.isEmpty(value))
                        {
-                               apps.add(arr.getString(i));
+                               Collections.addAll(list, value.split("\\s+"));
                        }
                }
-               return TextUtils.join(" ", apps);
+               return list;
        }
 
        /**