summaryrefslogtreecommitdiffhomepage
path: root/tunnel/src/main/java/com
diff options
context:
space:
mode:
authorMikael Magnusson <mikma@users.sourceforge.net>2023-07-05 22:48:28 +0200
committerMikael Magnusson <mikma@users.sourceforge.net>2023-10-26 23:11:22 +0200
commit67d63b28ea2e692c857b9882b6e6b26201d4c65f (patch)
tree409d882d214d80911d2cf0df18f3de4eabc0c433 /tunnel/src/main/java/com
parent7f58506d3eaff8c26fad336a6fc3caa97e287dd3 (diff)
WIP: add cap to grpc
Diffstat (limited to 'tunnel/src/main/java/com')
-rw-r--r--tunnel/src/main/java/com/wireguard/android/backend/GoBackend.java31
1 files changed, 31 insertions, 0 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 79b724e8..2af43fb2 100644
--- a/tunnel/src/main/java/com/wireguard/android/backend/GoBackend.java
+++ b/tunnel/src/main/java/com/wireguard/android/backend/GoBackend.java
@@ -34,6 +34,8 @@ 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.CapabilitiesChangedRequest;
+import com.wireguard.android.backend.gen.CapabilitiesChangedResponse;
import com.wireguard.android.backend.gen.DhcpRequest;
import com.wireguard.android.backend.gen.DhcpResponse;
import com.wireguard.android.backend.gen.GetConnectionOwnerUidResponse;
@@ -110,6 +112,7 @@ public final class GoBackend implements Backend {
private static final String TAG = "WireGuard/GoBackend";
private static final int STATS_TAG = 2;
private static final int MSG_DHCP_EXPIRE = 1;
+ private static final int MSG_CAPABILITIES_CHANGED = 2;
@Nullable private static AlwaysOnCallback alwaysOnCallback;
private static GhettoCompletableFuture<VpnService> vpnService = new GhettoCompletableFuture<>();
private final Context context;
@@ -125,6 +128,7 @@ public final class GoBackend implements Backend {
@Nullable private Bgp bgp;
private HandlerThread thread;
private Handler handler;
+ private NetworkCapabilities activeNetworkCapabilities;
/**
* Public constructor for GoBackend.
@@ -363,6 +367,18 @@ public final class GoBackend implements Backend {
return buf.toString();
}
+ private void capabilitiesChanged(NetworkCapabilities capabilities) {
+ LibwgGrpc.LibwgBlockingStub stub = LibwgGrpc.newBlockingStub(channel);
+ CapabilitiesChangedRequest.Builder reqBuilder = CapabilitiesChangedRequest.newBuilder();
+
+ if (capabilities != null && capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_NOT_METERED)) {
+ reqBuilder.addCapabilities(CapabilitiesChangedRequest.Capability.NOT_METERED);
+ }
+
+ CapabilitiesChangedResponse resp = stub.capabilitiesChanged(reqBuilder.build());
+ Log.i(TAG, "Capabilities change: " + resp.getError().getMessage());
+ }
+
private int startHttpProxy(String pacFile) {
LibwgGrpc.LibwgStub asyncStub = LibwgGrpc.newStub(channel);
LibwgGrpc.LibwgBlockingStub stub = LibwgGrpc.newBlockingStub(channel);
@@ -386,6 +402,7 @@ public final class GoBackend implements Backend {
StartHttpProxyRequest req = reqBuilder.build();
StartHttpProxyResponse resp = stub.startHttpProxy(req);
Log.i(TAG, "Start http proxy listen_port:" + resp.getListenPort() + ", error:" + resp.getError().getMessage());
+ capabilitiesChanged(activeNetworkCapabilities);
streamer.start();
return resp.getListenPort();
}
@@ -755,6 +772,10 @@ public final class GoBackend implements Backend {
Log.e(TAG, "DHCP failed: " + ex);
}
break;
+ case MSG_CAPABILITIES_CHANGED:
+ Log.w(TAG, "Msg: capabilities changed: " + activeNetworkCapabilities);
+ capabilitiesChanged(activeNetworkCapabilities);
+ break;
default:
Log.w(TAG, "Unknown message: " + msg.what);
break;
@@ -972,6 +993,16 @@ public final class GoBackend implements Backend {
}
}
}
+
+ @Override
+ public void onCapabilitiesChanged (Network network, NetworkCapabilities networkCapabilities) {
+ boolean hasCapNotMetered = networkCapabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_NOT_METERED);
+ Log.w(TAG, "onCapabilitiesChanged: " + network + " is not metered:" + (hasCapNotMetered));
+ if (network.equals(activeNetwork) && handler != null) {
+ activeNetworkCapabilities = networkCapabilities;
+ handler.sendEmptyMessage(MSG_CAPABILITIES_CHANGED);
+ }
+ }
}
private AlarmManager.OnAlarmListener alarmListener = new AlarmManager.OnAlarmListener() {