diff options
author | Mikael Magnusson <mikma@users.sourceforge.net> | 2019-04-13 20:37:03 +0200 |
---|---|---|
committer | Mikael Magnusson <mikma@users.sourceforge.net> | 2022-03-18 23:35:32 +0100 |
commit | 4536f837dd6fb7371026c32c6186989a5b31d435 (patch) | |
tree | ae883703ba086af2397f23e648684cbd29819670 | |
parent | a04105032137325ba108f219e194ba64842ff9f2 (diff) |
Implement get and set tunnel config actions
Add three tunnel actions:
com.wireguard.android.action.GET_TUNNEL_CONFIG
com.wireguard.android.action.SET_TUNNEL_CONFIG
com.wireguard.android.action.SET_TUNNEL_USERSPACE_CONFIG
-rw-r--r-- | tunnel/src/main/java/com/wireguard/android/backend/Backend.java | 11 | ||||
-rw-r--r-- | tunnel/src/main/java/com/wireguard/android/backend/GoBackend.java | 18 | ||||
-rw-r--r-- | tunnel/src/main/java/com/wireguard/android/backend/WgQuickBackend.java | 5 | ||||
-rw-r--r-- | tunnel/tools/libwg-go/api-android.go | 42 | ||||
-rw-r--r-- | tunnel/tools/libwg-go/jni.c | 25 | ||||
-rw-r--r-- | ui/src/main/AndroidManifest.xml | 3 |
6 files changed, 104 insertions, 0 deletions
diff --git a/tunnel/src/main/java/com/wireguard/android/backend/Backend.java b/tunnel/src/main/java/com/wireguard/android/backend/Backend.java index 5aaad826..45cd73e5 100644 --- a/tunnel/src/main/java/com/wireguard/android/backend/Backend.java +++ b/tunnel/src/main/java/com/wireguard/android/backend/Backend.java @@ -19,6 +19,17 @@ import androidx.annotation.Nullable; @NonNullForAll public interface Backend { /** + * Update the volatile configuration of a running tunnel and return the resulting configuration. + * If the tunnel is not up, return the configuration that would result (if known), or else + * simply return the given configuration. + * + * @param tunnel The tunnel to apply the configuration to. + * @param config The new configuration for this tunnel. + * @return The updated configuration of the tunnel. + */ + Config applyUserspaceConfig(ObservableTunnel tunnel, Config config) throws Exception; + + /** * Enumerate names of currently-running tunnels. * * @return The set of running tunnel names. diff --git a/tunnel/src/main/java/com/wireguard/android/backend/GoBackend.java b/tunnel/src/main/java/com/wireguard/android/backend/GoBackend.java index 3d0886cf..21c449b9 100644 --- a/tunnel/src/main/java/com/wireguard/android/backend/GoBackend.java +++ b/tunnel/src/main/java/com/wireguard/android/backend/GoBackend.java @@ -80,6 +80,10 @@ public final class GoBackend implements Backend { private static native int wgTurnOn(String ifName, int tunFd, String settings); + private static native String wgGetOperation(int handle); + + private static native int wgSetOperation(int handle, String settings); + private static native String wgVersion(); /** @@ -88,6 +92,19 @@ public final class GoBackend implements Backend { * @return A set of string values denoting names of running tunnels. */ @Override + public Config applyUserspaceConfig(final ObservableTunnel tunnel, final Config config) throws Exception { + if (currentTunnelHandle != -1) { + // Build config + final String goConfig = config.toWgUserspaceString(); + + Log.d(TAG, "Go backend v" + wgVersion()); + wgSetOperation(currentTunnelHandle, goConfig); + Log.d(TAG, "Settings " + wgGetOperation(currentTunnelHandle)); + } + return config; + } + + @Override public Set<String> getRunningTunnelNames() { if (currentTunnel != null) { final Set<String> runningTunnels = new ArraySet<>(); @@ -305,6 +322,7 @@ public final class GoBackend implements Backend { throw new BackendException(Reason.TUN_CREATION_ERROR); Log.d(TAG, "Go backend " + wgVersion()); currentTunnelHandle = wgTurnOn(tunnel.getName(), tun.detachFd(), goConfig); + Log.d(TAG, "Settings " + wgGetOperation(currentTunnelHandle)); } if (currentTunnelHandle < 0) throw new BackendException(Reason.GO_ACTIVATION_ERROR_CODE, currentTunnelHandle); diff --git a/tunnel/src/main/java/com/wireguard/android/backend/WgQuickBackend.java b/tunnel/src/main/java/com/wireguard/android/backend/WgQuickBackend.java index 3121c996..f07a662c 100644 --- a/tunnel/src/main/java/com/wireguard/android/backend/WgQuickBackend.java +++ b/tunnel/src/main/java/com/wireguard/android/backend/WgQuickBackend.java @@ -58,6 +58,11 @@ public final class WgQuickBackend implements Backend { } @Override + public Config applyUserspaceConfig(final ObservableTunnel tunnel, final Config config) throws Exception { + throw new RuntimeException("Not implemented"); + } + + @Override public Set<String> getRunningTunnelNames() { final List<String> output = new ArrayList<>(); // Don't throw an exception here or nothing will show up in the UI. diff --git a/tunnel/tools/libwg-go/api-android.go b/tunnel/tools/libwg-go/api-android.go index 94d07c39..6c65ae10 100644 --- a/tunnel/tools/libwg-go/api-android.go +++ b/tunnel/tools/libwg-go/api-android.go @@ -10,7 +10,15 @@ package main import "C" import ( + "bufio" + "bytes" "fmt" + "golang.org/x/sys/unix" + "golang.zx2c4.com/wireguard/device" + "golang.zx2c4.com/wireguard/ipc" + "golang.zx2c4.com/wireguard/tun" + "bytes" + "log" "math" "net" "os" @@ -159,6 +167,40 @@ func wgTurnOff(tunnelHandle int32) { handle.device.Close() } +//export wgGetOperation +func wgGetOperation(tunnelHandle int32) *C.char { + handle, ok := tunnelHandles[tunnelHandle] + if !ok { + return C.CString("(bad handle)") + } + + var buf bytes.Buffer + writer := bufio.NewWriter(&buf) + + getError := handle.device.IpcGetOperation(writer) + if getError != nil { + return C.CString(getError.Error()) + } + + writer.Flush() + return C.CString(buf.String()) +} + +//export wgSetOperation +func wgSetOperation(tunnelHandle int32, settings string) int32 { + handle, ok := tunnelHandles[tunnelHandle] + if !ok { + return -1 + } + + setError := handle.device.IpcSetOperation(bufio.NewReader(strings.NewReader(settings))) + if setError != nil { + return -1 + } + + return 0 +} + //export wgGetSocketV4 func wgGetSocketV4(tunnelHandle int32) int32 { handle, ok := tunnelHandles[tunnelHandle] diff --git a/tunnel/tools/libwg-go/jni.c b/tunnel/tools/libwg-go/jni.c index 7ad94d35..6444c235 100644 --- a/tunnel/tools/libwg-go/jni.c +++ b/tunnel/tools/libwg-go/jni.c @@ -10,6 +10,8 @@ struct go_string { const char *str; long n; }; extern int wgTurnOn(struct go_string ifname, int tun_fd, struct go_string settings); extern void wgTurnOff(int handle); +extern char *wgGetOperation(int handle); +extern int wgSetOperation(int handle, struct go_string settings); extern int wgGetSocketV4(int handle); extern int wgGetSocketV6(int handle); extern char *wgGetConfig(int handle); @@ -38,6 +40,29 @@ JNIEXPORT void JNICALL Java_com_wireguard_android_backend_GoBackend_wgTurnOff(JN wgTurnOff(handle); } +JNIEXPORT jstring JNICALL Java_com_wireguard_android_backend_GoBackend_wgGetOperation(JNIEnv *env, jclass c, jint handle) +{ + jstring ret; + char *settings = wgGetOperation(handle); + if (!settings) + return NULL; + ret = (*env)->NewStringUTF(env, settings); + free(settings); + return ret; +} + +JNIEXPORT jint JNICALL Java_com_wireguard_android_backend_GoBackend_wgSetOperation(JNIEnv *env, jclass c, jint handle, jstring settings) +{ + const char *settings_str = (*env)->GetStringUTFChars(env, settings, 0); + size_t settings_len = (*env)->GetStringUTFLength(env, settings); + int ret = wgSetOperation(handle, (struct go_string){ + .str = settings_str, + .n = settings_len + }); + (*env)->ReleaseStringUTFChars(env, settings, settings_str); + return ret; +} + JNIEXPORT jint JNICALL Java_com_wireguard_android_backend_GoBackend_wgGetSocketV4(JNIEnv *env, jclass c, jint handle) { return wgGetSocketV4(handle); diff --git a/ui/src/main/AndroidManifest.xml b/ui/src/main/AndroidManifest.xml index 4dd38cb2..744e451f 100644 --- a/ui/src/main/AndroidManifest.xml +++ b/ui/src/main/AndroidManifest.xml @@ -108,6 +108,9 @@ <action android:name="com.wireguard.android.action.REFRESH_TUNNEL_STATES" /> <action android:name="com.wireguard.android.action.SET_TUNNEL_UP" /> <action android:name="com.wireguard.android.action.SET_TUNNEL_DOWN" /> + <action android:name="com.wireguard.android.action.GET_TUNNEL_CONFIG" /> + <action android:name="com.wireguard.android.action.SET_TUNNEL_CONFIG" /> + <action android:name="com.wireguard.android.action.SET_TUNNEL_USERSPACE_CONFIG" /> </intent-filter> </receiver> |