Start the tunnel automatically after the settings change

This commit is contained in:
deep
2022-08-10 12:03:31 -04:00
parent 1d21f4744f
commit 23373a5a75
7 changed files with 64 additions and 15 deletions
@@ -208,6 +208,12 @@ public class BraveVpnPreferences extends BravePreferenceFragment implements Brav
BraveVpnUtils.showProgressDialog(
getActivity(), getResources().getString(R.string.vpn_connect_text));
verifyPurchase(false);
} else if (BraveVpnUtils.mUpdateProfileAfterSplitTunnel) {
BraveVpnUtils.mUpdateProfileAfterSplitTunnel = false;
BraveVpnUtils.showProgressDialog(
getActivity(), getResources().getString(R.string.updating_vpn_profile));
BraveVpnUtils.updateProfileConfiguration(getActivity());
new Handler().post(() -> updateSummaries());
} else {
BraveVpnUtils.dismissProgressDialog();
}
@@ -119,11 +119,7 @@ public class SplitTunnelActivity extends AsyncInitializationActivity
if (mRecyclerViewAdapterExcludedApps != null) {
BraveVpnPrefUtils.setExcludedPackages(
mRecyclerViewAdapterExcludedApps.getApplicationPackages());
BraveVpnNativeWorker.getInstance().invalidateCredentials(
BraveVpnPrefUtils.getHostname(), BraveVpnPrefUtils.getClientId(),
BraveVpnPrefUtils.getSubscriberCredential(),
BraveVpnPrefUtils.getApiAuthToken());
BraveVpnUtils.resetProfileConfiguration(SplitTunnelActivity.this);
BraveVpnUtils.mUpdateProfileAfterSplitTunnel = true;
finish();
return true;
}
@@ -17,6 +17,8 @@ import android.util.Pair;
import androidx.fragment.app.FragmentActivity;
import com.wireguard.config.Config;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
@@ -49,6 +51,7 @@ public class BraveVpnUtils {
public static final String VERIFY_CREDENTIALS_FAILED = "verify_credentials_failed";
public static boolean mIsServerLocationChanged;
public static boolean mUpdateProfileAfterSplitTunnel;
public static String selectedServerRegion;
private static ProgressDialog mProgressDialog;
@@ -224,6 +227,21 @@ public class BraveVpnUtils {
dismissProgressDialog();
}
public static void updateProfileConfiguration(Activity activity) {
try {
Config existingConfig = WireguardConfigUtils.loadConfig(activity);
WireguardConfigUtils.deleteConfig(activity);
WireguardConfigUtils.createConfig(activity, existingConfig);
} catch (Exception ex) {
Log.e(TAG, "updateProfileConfiguration : " + ex.getMessage());
}
if (BraveVpnProfileUtils.getInstance().isBraveVPNConnected(activity)) {
BraveVpnProfileUtils.getInstance().stopVpn(activity);
BraveVpnProfileUtils.getInstance().startVpn(activity);
}
dismissProgressDialog();
}
public static void showVpnAlwaysOnErrorDialog(Activity activity) {
BraveVpnAlwaysOnErrorDialogFragment mBraveVpnAlwaysOnErrorDialogFragment =
new BraveVpnAlwaysOnErrorDialogFragment();
@@ -39,6 +39,21 @@ public class WireguardConfigUtils {
return config;
}
public static Config createConfig(Context context, Config existingConfig)
throws IOException, BadConfigException {
File file = fileForConfig(context);
if (!file.createNewFile()) {
throw new IOException("Configuration file already exists");
}
FileOutputStream fileOutputStream = new FileOutputStream(file, false);
Config config = new Config.Builder()
.setInterface(getInterface(existingConfig.getInterface()))
.addPeers(existingConfig.getPeers())
.build();
fileOutputStream.write(config.toWgQuickString().getBytes(StandardCharsets.UTF_8));
return config;
}
public static void deleteConfig(Context context) throws Exception {
File file = fileForConfig(context);
if (!file.delete()) {
@@ -33,6 +33,16 @@ public class WireguardUtils {
return builder.build();
}
public static Interface getInterface(Interface existingInterface) throws BadConfigException {
Interface.Builder builder = new Interface.Builder();
builder.addAddresses(existingInterface.getAddresses());
builder.parseDnsServers("1.1.1.1, 1.0.0.1");
builder.parseListenPort("51821");
builder.parsePrivateKey(existingInterface.getKeyPair().getPrivateKey().toBase64());
builder.excludeApplications(BraveVpnPrefUtils.getExcludedPackages());
return builder.build();
}
public static List<Peer> getPeers(String host, String serverPublicKey)
throws BadConfigException {
List<Peer> peers = new ArrayList<>();
@@ -1,10 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:ignore="MergeRootFrame"
android:layout_width="match_parent"
android:layout_height="match_parent">
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/BraveVpnToolbar"
android:minHeight="?attr/actionBarSize"/>
<ProgressBar
android:id="@+id/progressbar"
@@ -26,13 +34,6 @@
android:orientation="vertical"
tools:context=".SplitTunnelActivity">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/BraveVpnToolbar"
android:minHeight="?attr/actionBarSize"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
@@ -100,4 +101,4 @@
</androidx.core.widget.NestedScrollView>
</FrameLayout>
</LinearLayout>