android: Import proxy server settings

This commit is contained in:
Tobias Brunner
2025-06-27 08:57:45 +02:00
parent c80819c0ad
commit 0f2cd032e1
@@ -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++)
{
apps.add(arr.getString(i));
{ /* replace all spaces, including e.g. in "192.168.1.1 - 192.168.1.10" */
list.add(arr.getString(i).replace(" ", ""));
}
}
return TextUtils.join(" ", apps);
else
{
String value = obj.optString(key, null);
if (!TextUtils.isEmpty(value))
{
Collections.addAll(list, value.split("\\s+"));
}
}
return list;
}
/**