diff options
author | Mikael Magnusson <mikma@users.sourceforge.net> | 2021-11-20 20:03:12 +0100 |
---|---|---|
committer | Mikael Magnusson <mikma@users.sourceforge.net> | 2023-09-27 23:59:41 +0200 |
commit | 4118c524349a141bf1ee02bbb19cca8d888a9f5a (patch) | |
tree | 3c8b75c17ee71cc5d1dfd427cbc061a3f00c1156 /tunnel/src | |
parent | e61f3f15e282f17337e69c1bb75cf0863e317415 (diff) |
tunnel: add gRPC over unix domain socket to the go backend
With gRPC it will be easier to extend the go backend API.
In this commit the Version function is reimplemented in gRPC.
Gitignore generated protobuf files.
Diffstat (limited to 'tunnel/src')
-rw-r--r-- | tunnel/src/main/java/com/wireguard/android/backend/GoBackend.java | 28 | ||||
-rw-r--r-- | tunnel/src/main/proto/libwg.proto | 27 |
2 files changed, 54 insertions, 1 deletions
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 6af9eb7e..79c362b4 100644 --- a/tunnel/src/main/java/com/wireguard/android/backend/GoBackend.java +++ b/tunnel/src/main/java/com/wireguard/android/backend/GoBackend.java @@ -9,6 +9,7 @@ import android.content.Context; import android.content.Intent; import android.net.ConnectivityManager; import android.net.LinkProperties; +import android.net.LocalSocketAddress; import android.net.Network; import android.net.NetworkCapabilities; import android.net.NetworkRequest; @@ -18,8 +19,15 @@ import android.os.ParcelFileDescriptor; import android.system.OsConstants; import android.util.Log; +import com.google.protobuf.ByteString; +import com.google.protobuf.InvalidProtocolBufferException; +import com.google.protobuf.Empty; + import com.wireguard.android.backend.BackendException.Reason; import com.wireguard.android.backend.Tunnel.State; +import com.wireguard.android.backend.gen.LibwgGrpc; +import com.wireguard.android.backend.gen.VersionRequest; +import com.wireguard.android.backend.gen.VersionResponse; import com.wireguard.android.util.SharedLibraryLoader; import com.wireguard.config.Config; import com.wireguard.config.InetEndpoint; @@ -30,6 +38,12 @@ import com.wireguard.crypto.KeyFormatException; import com.wireguard.util.NonNullForAll; import com.wireguard.util.Resolver; +import io.grpc.ManagedChannel; +import io.grpc.ManagedChannelBuilder; +import io.grpc.android.UdsChannelBuilder; +import io.grpc.okhttp.OkHttpChannelBuilder; + +import java.io.File; import java.net.InetAddress; import java.net.URL; import java.time.Instant; @@ -41,6 +55,8 @@ import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; +import javax.net.SocketFactory; + import androidx.annotation.Nullable; import androidx.collection.ArraySet; @@ -61,6 +77,7 @@ public final class GoBackend implements Backend { private ConnectivityManager connectivityManager; private ConnectivityManager.NetworkCallback myNetworkCallback = new MyNetworkCallback(); @Nullable private Network activeNetwork; + private ManagedChannel channel; /** * Public constructor for GoBackend. @@ -71,6 +88,10 @@ public final class GoBackend implements Backend { SharedLibraryLoader.loadSharedLibrary(context, "wg-go"); this.context = context; connectivityManager = context.getSystemService(ConnectivityManager.class); + File socketFile = new File(context.getCacheDir(), "libwg.sock"); + String socketName = socketFile.getAbsolutePath(); + Log.i(TAG, "wgStartGrpc: " + wgStartGrpc(socketName)); + channel = UdsChannelBuilder.forPath(socketName, LocalSocketAddress.Namespace.FILESYSTEM).build(); } /** @@ -97,6 +118,8 @@ public final class GoBackend implements Backend { private static native String wgVersion(); + private static native int wgStartGrpc(String sockName); + /** * Method to get the names of running tunnels. * @@ -199,7 +222,10 @@ public final class GoBackend implements Backend { */ @Override public String getVersion() { - return wgVersion(); + LibwgGrpc.LibwgBlockingStub stub = LibwgGrpc.newBlockingStub(channel); + VersionRequest request = VersionRequest.newBuilder().build(); + VersionResponse resp = stub.version(request); + return resp.getVersion(); } /** diff --git a/tunnel/src/main/proto/libwg.proto b/tunnel/src/main/proto/libwg.proto new file mode 100644 index 00000000..2d964897 --- /dev/null +++ b/tunnel/src/main/proto/libwg.proto @@ -0,0 +1,27 @@ +syntax = "proto3"; + +option java_multiple_files = true; +option java_package = 'com.wireguard.android.backend.gen'; +option java_outer_classname = "LibwgProto"; +option java_generic_services = true; +option go_package = 'golang.zx2c4.com/wireguard/android/gen'; + +package api; + +service Libwg { + rpc StopGrpc(StopGrpcRequest) returns (StopGrpcResponse); + rpc Version(VersionRequest) returns (VersionResponse); +} + +message StopGrpcRequest { +} + +message StopGrpcResponse { +} + +message VersionRequest { +} + +message VersionResponse { + string version = 1; +} |