-
Notifications
You must be signed in to change notification settings - Fork 362
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add pre/post up/down support for rooted GoBackend
When using the GoBackend on a rooted device allow for pre/post up/down actions to be executed when the tunnel state changes. On non-rooted devices the scripts are not executed but will still be parsed from the configuration file. %i syntax is not supported. If any script fails to execute the remaining scripts in that step are skipped Signed-off-by: Adam Irr <[email protected]>
- Loading branch information
Showing
8 changed files
with
277 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
tunnel/src/main/java/com/wireguard/android/backend/NoopTunnelActionHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
* Copyright © 2020 WireGuard LLC. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package com.wireguard.android.backend; | ||
|
||
import java.util.Collection; | ||
|
||
/** | ||
* A {@link TunnelActionHandler} implementation that does not execute any scripts. | ||
*/ | ||
public final class NoopTunnelActionHandler implements TunnelActionHandler { | ||
|
||
@Override | ||
public void runPreUp(final Collection<String> scripts) { | ||
|
||
} | ||
|
||
@Override | ||
public void runPostUp(final Collection<String> scripts) { | ||
|
||
} | ||
|
||
@Override | ||
public void runPreDown(final Collection<String> scripts) { | ||
|
||
} | ||
|
||
@Override | ||
public void runPostDown(final Collection<String> scripts) { | ||
|
||
} | ||
} |
80 changes: 80 additions & 0 deletions
80
tunnel/src/main/java/com/wireguard/android/backend/RootTunnelActionHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* | ||
* Copyright © 2020 WireGuard LLC. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package com.wireguard.android.backend; | ||
|
||
import android.util.Log; | ||
|
||
import com.wireguard.android.util.RootShell; | ||
import com.wireguard.android.util.RootShell.RootShellException; | ||
import com.wireguard.util.NonNullForAll; | ||
|
||
import java.io.IOException; | ||
import java.util.Collection; | ||
|
||
/** | ||
* A {@link TunnelActionHandler} implementation that executes scripts using a root shell. | ||
* Scripts are executed sequentially. If there is an error executing a script for a given step | ||
* the remaining scripts in that step are skipped. | ||
*/ | ||
@NonNullForAll | ||
public final class RootTunnelActionHandler implements TunnelActionHandler { | ||
|
||
private static final String TAG = "WireGuard/TunnelAction"; | ||
private final RootShell rootShell; | ||
|
||
public RootTunnelActionHandler(final RootShell rootShell) { | ||
this.rootShell = rootShell; | ||
} | ||
|
||
@Override | ||
public void runPreDown(final Collection<String> scripts) { | ||
if (!scripts.isEmpty()) { | ||
Log.d(TAG, "Running PreDown scripts"); | ||
runTunnelScripts(scripts); | ||
} | ||
} | ||
|
||
@Override | ||
public void runPostDown(final Collection<String> scripts) { | ||
if (!scripts.isEmpty()) { | ||
Log.d(TAG, "Running PostDown scripts"); | ||
runTunnelScripts(scripts); | ||
} | ||
} | ||
|
||
@Override | ||
public void runPreUp(final Collection<String> scripts) { | ||
if (!scripts.isEmpty()) { | ||
Log.d(TAG, "Running PreUp scripts"); | ||
runTunnelScripts(scripts); | ||
} | ||
} | ||
|
||
@Override | ||
public void runPostUp(final Collection<String> scripts) { | ||
if (!scripts.isEmpty()) { | ||
Log.d(TAG, "Running PostUp scripts"); | ||
runTunnelScripts(scripts); | ||
} | ||
} | ||
|
||
private void runTunnelScripts(final Iterable<String> scripts) { | ||
for (final String script : scripts) { | ||
if (script.contains("%i")) { | ||
Log.e(TAG, "'%i' syntax is not supported with the GoBackend. Aborting"); | ||
return; | ||
} | ||
|
||
try { | ||
rootShell.run(null, script); | ||
} catch (final IOException | RootShellException e) { | ||
Log.e(TAG, "Failed to execute script.", e); | ||
return; | ||
} | ||
} | ||
|
||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
tunnel/src/main/java/com/wireguard/android/backend/TunnelActionHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* Copyright © 2020 WireGuard LLC. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package com.wireguard.android.backend; | ||
|
||
import java.util.Collection; | ||
|
||
/** | ||
* Handles executing Pre/Post Up/Down scripts when the state of the WireGuard tunnel changes | ||
*/ | ||
public interface TunnelActionHandler { | ||
|
||
/** | ||
* Execute scripts before bringing up the tunnel | ||
* | ||
* @param scripts Collection of scripts to execute | ||
*/ | ||
void runPreUp(Collection<String> scripts); | ||
|
||
/** | ||
* Execute scripts after bringing up the tunnel | ||
* | ||
* @param scripts Collection of scripts to execute | ||
*/ | ||
void runPostUp(Collection<String> scripts); | ||
|
||
/** | ||
* Execute scripts before bringing down the tunnel | ||
* | ||
* @param scripts Collection of scripts to execute | ||
*/ | ||
void runPreDown(Collection<String> scripts); | ||
|
||
/** | ||
* Execute scripts after bringing down the tunnel | ||
* | ||
* @param scripts Collection of scripts to execute | ||
*/ | ||
void runPostDown(Collection<String> scripts); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.