-
Notifications
You must be signed in to change notification settings - Fork 390
Add pre/post up/down support for rooted GoBackend #23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
adamirr
wants to merge
1
commit into
WireGuard:master
Choose a base branch
from
adamirr:up-down-actions
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+283
−3
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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) { | ||
|
||
} | ||
} |
76 changes: 76 additions & 0 deletions
76
tunnel/src/main/java/com/wireguard/android/backend/RootTunnelActionHandler.java
This file contains hidden or 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,76 @@ | ||
/* | ||
* 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()) return; | ||
Log.d(TAG, "Running PreDown scripts"); | ||
runTunnelScripts(scripts); | ||
} | ||
|
||
@Override | ||
public void runPostDown(final Collection<String> scripts) { | ||
if (scripts.isEmpty()) return; | ||
Log.d(TAG, "Running PostDown scripts"); | ||
runTunnelScripts(scripts); | ||
} | ||
|
||
@Override | ||
public void runPreUp(final Collection<String> scripts) { | ||
if (scripts.isEmpty()) return; | ||
Log.d(TAG, "Running PreUp scripts"); | ||
runTunnelScripts(scripts); | ||
} | ||
|
||
@Override | ||
public void runPostUp(final Collection<String> scripts) { | ||
if (scripts.isEmpty()) return; | ||
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 hidden or 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 hidden or 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 |
---|---|---|
|
@@ -14,6 +14,7 @@ | |
import com.wireguard.util.NonNullForAll; | ||
|
||
import java.net.InetAddress; | ||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.LinkedHashSet; | ||
|
@@ -45,6 +46,10 @@ public final class Interface { | |
private final KeyPair keyPair; | ||
private final Optional<Integer> listenPort; | ||
private final Optional<Integer> mtu; | ||
private final List<String> preUp; | ||
private final List<String> postUp; | ||
private final List<String> preDown; | ||
private final List<String> postDown; | ||
|
||
private Interface(final Builder builder) { | ||
// Defensively copy to ensure immutability even if the Builder is reused. | ||
|
@@ -55,6 +60,10 @@ private Interface(final Builder builder) { | |
keyPair = Objects.requireNonNull(builder.keyPair, "Interfaces must have a private key"); | ||
listenPort = builder.listenPort; | ||
mtu = builder.mtu; | ||
preUp = Collections.unmodifiableList(new ArrayList<>(builder.preUp)); | ||
postUp = Collections.unmodifiableList(new ArrayList<>(builder.postUp)); | ||
preDown = Collections.unmodifiableList(new ArrayList<>(builder.preDown)); | ||
postDown = Collections.unmodifiableList(new ArrayList<>(builder.postDown)); | ||
} | ||
|
||
/** | ||
|
@@ -93,6 +102,18 @@ public static Interface parse(final Iterable<? extends CharSequence> lines) | |
case "privatekey": | ||
builder.parsePrivateKey(attribute.getValue()); | ||
break; | ||
case "preup": | ||
builder.parsePreUp(attribute.getValue()); | ||
break; | ||
case "postup": | ||
builder.parsePostUp(attribute.getValue()); | ||
break; | ||
case "predown": | ||
builder.parsePreDown(attribute.getValue()); | ||
break; | ||
case "postdown": | ||
builder.parsePostDown(attribute.getValue()); | ||
break; | ||
default: | ||
throw new BadConfigException(Section.INTERFACE, Location.TOP_LEVEL, | ||
Reason.UNKNOWN_ATTRIBUTE, attribute.getKey()); | ||
|
@@ -112,7 +133,12 @@ public boolean equals(final Object obj) { | |
&& includedApplications.equals(other.includedApplications) | ||
&& keyPair.equals(other.keyPair) | ||
&& listenPort.equals(other.listenPort) | ||
&& mtu.equals(other.mtu); | ||
&& mtu.equals(other.mtu) | ||
&& preUp.equals(other.preUp) | ||
&& postUp.equals(other.postUp) | ||
&& preDown.equals(other.preDown) | ||
&& postDown.equals(other.postDown); | ||
|
||
} | ||
|
||
/** | ||
|
@@ -182,6 +208,22 @@ public Optional<Integer> getMtu() { | |
return mtu; | ||
} | ||
|
||
public List<String> getPreUp() { | ||
return preUp; | ||
} | ||
|
||
public List<String> getPostUp() { | ||
return postUp; | ||
} | ||
|
||
public List<String> getPreDown() { | ||
return preDown; | ||
} | ||
|
||
public List<String> getPostDown() { | ||
return postDown; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
int hash = 1; | ||
|
@@ -192,6 +234,10 @@ public int hashCode() { | |
hash = 31 * hash + keyPair.hashCode(); | ||
hash = 31 * hash + listenPort.hashCode(); | ||
hash = 31 * hash + mtu.hashCode(); | ||
hash = 31 * hash + preUp.hashCode(); | ||
hash = 31 * hash + postUp.hashCode(); | ||
hash = 31 * hash + preDown.hashCode(); | ||
hash = 31 * hash + postDown.hashCode(); | ||
return hash; | ||
} | ||
|
||
|
@@ -231,6 +277,14 @@ public String toWgQuickString() { | |
listenPort.ifPresent(lp -> sb.append("ListenPort = ").append(lp).append('\n')); | ||
mtu.ifPresent(m -> sb.append("MTU = ").append(m).append('\n')); | ||
sb.append("PrivateKey = ").append(keyPair.getPrivateKey().toBase64()).append('\n'); | ||
for (final String script : preUp) | ||
sb.append("PreUp = ").append(script).append('\n'); | ||
for (final String script : postUp) | ||
sb.append("PostUp = ").append(script).append('\n'); | ||
for (final String script : preDown) | ||
sb.append("PreDown = ").append(script).append('\n'); | ||
for (final String script : postDown) | ||
sb.append("PostDown = ").append(script).append('\n'); | ||
Comment on lines
+280
to
+287
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This fails since Android's |
||
return sb.toString(); | ||
} | ||
|
||
|
@@ -263,6 +317,14 @@ public static final class Builder { | |
private Optional<Integer> listenPort = Optional.empty(); | ||
// Defaults to not present. | ||
private Optional<Integer> mtu = Optional.empty(); | ||
// Defaults to empty list | ||
private List<String> preUp = new ArrayList<>(); | ||
// Defaults to empty list | ||
private List<String> postUp = new ArrayList<>(); | ||
// Defaults to empty list | ||
private List<String> preDown = new ArrayList<>(); | ||
// Defaults to empty list | ||
private List<String> postDown = new ArrayList<>(); | ||
|
||
public Builder addAddress(final InetNetwork address) { | ||
addresses.add(address); | ||
|
@@ -366,6 +428,26 @@ public Builder parsePrivateKey(final String privateKey) throws BadConfigExceptio | |
} | ||
} | ||
|
||
public Builder parsePreUp(final String script) { | ||
preUp.add(script); | ||
return this; | ||
} | ||
|
||
public Builder parsePostUp(final String script) { | ||
postUp.add(script); | ||
return this; | ||
} | ||
|
||
public Builder parsePreDown(final String script) { | ||
preDown.add(script); | ||
return this; | ||
} | ||
|
||
public Builder parsePostDown(final String script) { | ||
postDown.add(script); | ||
return this; | ||
} | ||
|
||
public Builder setKeyPair(final KeyPair keyPair) { | ||
this.keyPair = keyPair; | ||
return this; | ||
|
This file contains hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.