From 279e9297b9700c6db9cef22d86755f5f8337ad35 Mon Sep 17 00:00:00 2001 From: Mikael Magnusson Date: Mon, 15 Mar 2021 22:39:56 +0000 Subject: WIP: add dhcp information to detail --- .../com/wireguard/android/backend/Backend.java | 2 + .../com/wireguard/android/backend/DhcpInfo.java | 57 ++++++++++++++++++++++ .../com/wireguard/android/backend/GoBackend.java | 11 +++++ .../wireguard/android/backend/WgQuickBackend.java | 6 +++ 4 files changed, 76 insertions(+) create mode 100644 tunnel/src/main/java/com/wireguard/android/backend/DhcpInfo.java (limited to 'tunnel') 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..1d9be593 100644 --- a/tunnel/src/main/java/com/wireguard/android/backend/Backend.java +++ b/tunnel/src/main/java/com/wireguard/android/backend/Backend.java @@ -18,6 +18,8 @@ import androidx.annotation.Nullable; @NonNullForAll public interface Backend { + DhcpInfo getDhcpInfo(Tunnel tunnel) throws Exception; + /** * Enumerate names of currently-running tunnels. * diff --git a/tunnel/src/main/java/com/wireguard/android/backend/DhcpInfo.java b/tunnel/src/main/java/com/wireguard/android/backend/DhcpInfo.java new file mode 100644 index 00000000..35a7dd43 --- /dev/null +++ b/tunnel/src/main/java/com/wireguard/android/backend/DhcpInfo.java @@ -0,0 +1,57 @@ +/* + * Copyright © 2020 WireGuard LLC. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 + */ + +package com.wireguard.android.backend; + +import android.os.SystemClock; +import android.util.Pair; + +import com.wireguard.crypto.Key; +import com.wireguard.util.NonNullForAll; + +import java.util.HashMap; +import java.util.Map; + +/** + * Class representing DHCP info for a {@link Tunnel} instance. + */ +@NonNullForAll +public class DhcpInfo { + private String info; + private long lastTouched = SystemClock.elapsedRealtime(); + + DhcpInfo() { + } + + /** + * Add a peer and its current data usage to the internal map. + * + * @param key A WireGuard public key bound to a particular peer + * @param rx The received traffic for the {@link com.wireguard.config.Peer} referenced by + * the provided {@link Key}. This value is in bytes + * @param tx The transmitted traffic for the {@link com.wireguard.config.Peer} referenced by + * the provided {@link Key}. This value is in bytes. + */ + void set(final String info) { + this.info = info; + lastTouched = SystemClock.elapsedRealtime(); + } + + /** + * Check if the statistics are stale, indicating the need for the {@link Backend} to update them. + * + * @return boolean indicating if the current statistics instance has stale values. + */ + public boolean isStale() { + return SystemClock.elapsedRealtime() - lastTouched > 900; + } + + /** + */ + public String get() { + return info; + } + +} 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 f077189a..fdbf0e9c 100644 --- a/tunnel/src/main/java/com/wireguard/android/backend/GoBackend.java +++ b/tunnel/src/main/java/com/wireguard/android/backend/GoBackend.java @@ -88,6 +88,17 @@ public final class GoBackend implements Backend, EventHandler { private static native String wgVersion(); + @Override + public DhcpInfo getDhcpInfo(final Tunnel tunnel) { + final DhcpInfo info = new DhcpInfo(); + if (tunnel != currentTunnel) { + return info; + } + // TODO update info + info.set("FIXME"); + return info; + } + /** * Method to get the names of running tunnels. * 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..9c9dfbc1 100644 --- a/tunnel/src/main/java/com/wireguard/android/backend/WgQuickBackend.java +++ b/tunnel/src/main/java/com/wireguard/android/backend/WgQuickBackend.java @@ -57,6 +57,12 @@ public final class WgQuickBackend implements Backend { return new File("/sys/module/wireguard").exists(); } + @Override + public DhcpInfo getDhcpInfo(final Tunnel tunnel) { + final DhcpInfo info = new DhcpInfo(); + return info; + } + @Override public Set getRunningTunnelNames() { final List output = new ArrayList<>(); -- cgit v1.2.3