summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorMikael Magnusson <mikma@users.sourceforge.net>2023-05-24 22:48:37 +0200
committerMikael Magnusson <mikma@users.sourceforge.net>2024-01-08 00:14:45 +0100
commite786b19646cc8ffd2c83db8cdb9457610d614ebd (patch)
tree6968b2455c699f7281f1356aa4c2165f23b7574a
parent7de29045e927369b6071bd6103ea46da665ffacb (diff)
tunnel: implement gRPC based wgSetConfig
-rw-r--r--tunnel/src/main/java/com/wireguard/android/backend/GoBackend.java13
-rw-r--r--tunnel/src/main/proto/libwg.proto12
-rw-r--r--tunnel/tools/libwg-go/api-android.go5
-rw-r--r--tunnel/tools/libwg-go/service.go27
4 files changed, 55 insertions, 2 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 3e5c5cc8..9c2afa9f 100644
--- a/tunnel/src/main/java/com/wireguard/android/backend/GoBackend.java
+++ b/tunnel/src/main/java/com/wireguard/android/backend/GoBackend.java
@@ -32,6 +32,8 @@ import com.wireguard.android.backend.Tunnel.State;
import com.wireguard.android.backend.gen.DhcpRequest;
import com.wireguard.android.backend.gen.DhcpResponse;
import com.wireguard.android.backend.gen.GetConnectionOwnerUidResponse;
+import com.wireguard.android.backend.gen.IpcSetRequest;
+import com.wireguard.android.backend.gen.IpcSetResponse;
import com.wireguard.android.backend.gen.Lease;
import com.wireguard.android.backend.gen.LibwgGrpc;
import com.wireguard.android.backend.gen.ReverseRequest;
@@ -40,6 +42,7 @@ import com.wireguard.android.backend.gen.StartHttpProxyRequest;
import com.wireguard.android.backend.gen.StartHttpProxyResponse;
import com.wireguard.android.backend.gen.StopHttpProxyRequest;
import com.wireguard.android.backend.gen.StopHttpProxyResponse;
+import com.wireguard.android.backend.gen.TunnelHandle;
import com.wireguard.android.backend.gen.VersionRequest;
import com.wireguard.android.backend.gen.VersionResponse;
import com.wireguard.android.util.SharedLibraryLoader;
@@ -319,7 +322,10 @@ public final class GoBackend implements Backend {
// TODO removed removeNetworks
Log.w(TAG, "Wg user string: " + goConfig);
- wgSetConfig(currentTunnelHandle, goConfig);
+ LibwgGrpc.LibwgBlockingStub stub = LibwgGrpc.newBlockingStub(channel);
+ TunnelHandle handle = TunnelHandle.newBuilder().setHandle(currentTunnelHandle).build();
+ IpcSetRequest request = IpcSetRequest.newBuilder().setTunnel(handle).setConfig(goConfig).build();
+ IpcSetResponse resp = stub.ipcSet(request);
}
private static String downloadPacFile(Network network, Uri pacFileUrl) {
@@ -871,7 +877,10 @@ public final class GoBackend implements Backend {
final String goConfig = currentConfig.toWgEndpointsUserspaceString(resolver);
Log.w(TAG, "is default network, config:" + goConfig);
- wgSetConfig(currentTunnelHandle, goConfig);
+ LibwgGrpc.LibwgBlockingStub stub = LibwgGrpc.newBlockingStub(channel);
+ TunnelHandle tunnel = TunnelHandle.newBuilder().setHandle(currentTunnelHandle).build();
+ IpcSetRequest request = IpcSetRequest.newBuilder().setTunnel(tunnel).setConfig(goConfig).build();
+ IpcSetResponse resp = stub.ipcSet(request);
}
}
}
diff --git a/tunnel/src/main/proto/libwg.proto b/tunnel/src/main/proto/libwg.proto
index e0975f60..3195690d 100644
--- a/tunnel/src/main/proto/libwg.proto
+++ b/tunnel/src/main/proto/libwg.proto
@@ -16,9 +16,12 @@ service Libwg {
rpc StartHttpProxy(StartHttpProxyRequest) returns (StartHttpProxyResponse);
rpc StopHttpProxy(StopHttpProxyRequest) returns (StopHttpProxyResponse);
rpc Reverse(stream ReverseRequest) returns (stream ReverseResponse);
+ rpc IpcSet(IpcSetRequest) returns (IpcSetResponse);
rpc Dhcp(DhcpRequest) returns (DhcpResponse);
}
+message TunnelHandle { int32 handle = 1; }
+
message Error {
enum Code {
NO_ERROR = 0;
@@ -96,6 +99,15 @@ message GetConnectionOwnerUidResponse {
string package = 2; // context.getPackageManager().getNameForUid()
}
+message IpcSetRequest {
+ TunnelHandle tunnel = 1;
+ string config = 2;
+}
+
+message IpcSetResponse {
+ Error error = 1;
+}
+
message Lease {
InetAddress address = 1;
google.protobuf.Duration preferred_lifetime = 2;
diff --git a/tunnel/tools/libwg-go/api-android.go b/tunnel/tools/libwg-go/api-android.go
index 943ba628..5d9e8b96 100644
--- a/tunnel/tools/libwg-go/api-android.go
+++ b/tunnel/tools/libwg-go/api-android.go
@@ -54,6 +54,11 @@ type TunnelHandle struct {
var tunnelHandles map[int32]TunnelHandle
+func GetTunnel(handle int32) (tunnelHandle TunnelHandle, ok bool) {
+ tunnelHandle, ok = tunnelHandles[handle]
+ return
+}
+
func init() {
tunnelHandles = make(map[int32]TunnelHandle)
signals := make(chan os.Signal)
diff --git a/tunnel/tools/libwg-go/service.go b/tunnel/tools/libwg-go/service.go
index 30fe650b..f5460486 100644
--- a/tunnel/tools/libwg-go/service.go
+++ b/tunnel/tools/libwg-go/service.go
@@ -226,6 +226,33 @@ func (e *LibwgServiceImpl) Reverse(stream gen.Libwg_ReverseServer) error {
return nil
}
+func (e *LibwgServiceImpl) IpcSet(ctx context.Context, req *gen.IpcSetRequest) (*gen.IpcSetResponse, error) {
+ tunnel, ok := GetTunnel(req.GetTunnel().GetHandle())
+ if !ok {
+ r := &gen.IpcSetResponse{
+ Error: &gen.Error{
+ Message: fmt.Sprintf("Invalid tunnel"),
+ },
+ }
+ return r, nil
+ }
+
+ err := tunnel.device.IpcSet(req.GetConfig())
+ if err != nil {
+ r := &gen.IpcSetResponse{
+ Error: &gen.Error{
+ Message: fmt.Sprintf("IpcSet failed: %v", err),
+ },
+ }
+ return r, nil
+ }
+
+ r := &gen.IpcSetResponse{
+ }
+
+ return r, nil
+}
+
func (e *LibwgServiceImpl) Dhcp(ctx context.Context, req *gen.DhcpRequest) (*gen.DhcpResponse, error) {
var relayAddr netip.Addr
var sourceAddr netip.Addr