summaryrefslogtreecommitdiffhomepage
path: root/tunnel/src/main/java/com/wireguard/android
diff options
context:
space:
mode:
authorJason A. Donenfeld <Jason@zx2c4.com>2023-04-24 18:06:57 +0200
committerJason A. Donenfeld <Jason@zx2c4.com>2023-04-24 18:06:57 +0200
commit20480992c4514325b4233c7ae8efd3e806b46081 (patch)
tree98c0d26dee2b4719aa2a57c250a5f07b621c194c /tunnel/src/main/java/com/wireguard/android
parent9df90b259a075c505d471c5079ae6f64829550d6 (diff)
tunnel: export latest handshake stat
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Diffstat (limited to 'tunnel/src/main/java/com/wireguard/android')
-rw-r--r--tunnel/src/main/java/com/wireguard/android/backend/GoBackend.java23
-rw-r--r--tunnel/src/main/java/com/wireguard/android/backend/Statistics.java86
-rw-r--r--tunnel/src/main/java/com/wireguard/android/backend/WgQuickBackend.java7
3 files changed, 74 insertions, 42 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 670f04a1..429cb1f1 100644
--- a/tunnel/src/main/java/com/wireguard/android/backend/GoBackend.java
+++ b/tunnel/src/main/java/com/wireguard/android/backend/GoBackend.java
@@ -24,6 +24,7 @@ import com.wireguard.crypto.KeyFormatException;
import com.wireguard.util.NonNullForAll;
import java.net.InetAddress;
+import java.time.Instant;
import java.util.Collections;
import java.util.Set;
import java.util.concurrent.ExecutionException;
@@ -125,12 +126,14 @@ public final class GoBackend implements Backend {
Key key = null;
long rx = 0;
long tx = 0;
+ long latestHandshakeMSec = 0;
for (final String line : config.split("\\n")) {
if (line.startsWith("public_key=")) {
if (key != null)
- stats.add(key, rx, tx);
+ stats.add(key, rx, tx, latestHandshakeMSec);
rx = 0;
tx = 0;
+ latestHandshakeMSec = 0;
try {
key = Key.fromHex(line.substring(11));
} catch (final KeyFormatException ignored) {
@@ -152,10 +155,26 @@ public final class GoBackend implements Backend {
} catch (final NumberFormatException ignored) {
tx = 0;
}
+ } else if (line.startsWith("last_handshake_time_sec=")) {
+ if (key == null)
+ continue;
+ try {
+ latestHandshakeMSec += Long.parseLong(line.substring(24)) * 1000;
+ } catch (final NumberFormatException ignored) {
+ latestHandshakeMSec = 0;
+ }
+ } else if (line.startsWith("last_handshake_time_nsec=")) {
+ if (key == null)
+ continue;
+ try {
+ latestHandshakeMSec += Long.parseLong(line.substring(25)) / 1000000;
+ } catch (final NumberFormatException ignored) {
+ latestHandshakeMSec = 0;
+ }
}
}
if (key != null)
- stats.add(key, rx, tx);
+ stats.add(key, rx, tx, latestHandshakeMSec);
return stats;
}
diff --git a/tunnel/src/main/java/com/wireguard/android/backend/Statistics.java b/tunnel/src/main/java/com/wireguard/android/backend/Statistics.java
index 35a472bd..9fc92c53 100644
--- a/tunnel/src/main/java/com/wireguard/android/backend/Statistics.java
+++ b/tunnel/src/main/java/com/wireguard/android/backend/Statistics.java
@@ -6,36 +6,65 @@
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;
+import java.util.Objects;
+
+import androidx.annotation.Nullable;
/**
* Class representing transfer statistics for a {@link Tunnel} instance.
*/
@NonNullForAll
public class Statistics {
- private final Map<Key, Pair<Long, Long>> peerBytes = new HashMap<>();
+
+ // TODO: switch to Java Record class once R8 supports desugaring those.
+ public final class PeerStats {
+ public final long rxBytes, txBytes, latestHandshakeEpochMillis;
+
+ PeerStats(final long rxBytes, final long txBytes, final long latestHandshakeEpochMillis) {
+ this.rxBytes = rxBytes;
+ this.txBytes = txBytes;
+ this.latestHandshakeEpochMillis = latestHandshakeEpochMillis;
+ }
+
+ @Override public boolean equals(final Object o) {
+ if (this == o)
+ return true;
+ if (o == null || getClass() != o.getClass())
+ return false;
+ final PeerStats stats = (PeerStats) o;
+ return rxBytes == stats.rxBytes && txBytes == stats.txBytes && latestHandshakeEpochMillis == stats.latestHandshakeEpochMillis;
+ }
+
+ @Override public int hashCode() {
+ return Objects.hash(rxBytes, txBytes, latestHandshakeEpochMillis);
+ }
+ }
+
+ private final Map<Key, PeerStats> stats = new HashMap<>();
private long lastTouched = SystemClock.elapsedRealtime();
Statistics() {
}
/**
- * Add a peer and its current data usage to the internal map.
+ * Add a peer and its current stats 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.
+ * @param key A WireGuard public key bound to a particular peer
+ * @param rxBytes The received traffic for the {@link com.wireguard.config.Peer} referenced by
+ * the provided {@link Key}. This value is in bytes
+ * @param txBytes The transmitted traffic for the {@link com.wireguard.config.Peer} referenced by
+ * the provided {@link Key}. This value is in bytes.
+ * @param latestHandshake The timestamp of the latest handshake for the {@link com.wireguard.config.Peer}
+ * referenced by the provided {@link Key}. The value is in epoch milliseconds.
*/
- void add(final Key key, final long rx, final long tx) {
- peerBytes.put(key, Pair.create(rx, tx));
+ void add(final Key key, final long rxBytes, final long txBytes, final long latestHandshake) {
+ stats.put(key, new PeerStats(rxBytes, txBytes, latestHandshake));
lastTouched = SystemClock.elapsedRealtime();
}
@@ -49,31 +78,14 @@ public class Statistics {
}
/**
- * Get the received traffic (in bytes) for the {@link com.wireguard.config.Peer} referenced by
- * the provided {@link Key}
- *
- * @param peer A {@link Key} representing a {@link com.wireguard.config.Peer}.
- * @return a long representing the number of bytes received by this peer.
- */
- public long peerRx(final Key peer) {
- final Pair<Long, Long> rxTx = peerBytes.get(peer);
- if (rxTx == null)
- return 0;
- return rxTx.first;
- }
-
- /**
- * Get the transmitted traffic (in bytes) for the {@link com.wireguard.config.Peer} referenced by
- * the provided {@link Key}
+ * Get the statistics for the {@link com.wireguard.config.Peer} referenced by the provided {@link Key}
*
* @param peer A {@link Key} representing a {@link com.wireguard.config.Peer}.
- * @return a long representing the number of bytes transmitted by this peer.
+ * @return a {@link PeerStats} representing various statistics about this peer.
*/
- public long peerTx(final Key peer) {
- final Pair<Long, Long> rxTx = peerBytes.get(peer);
- if (rxTx == null)
- return 0;
- return rxTx.second;
+ @Nullable
+ public PeerStats peer(final Key peer) {
+ return this.stats.get(peer);
}
/**
@@ -83,7 +95,7 @@ public class Statistics {
* {@link com.wireguard.config.Peer}s
*/
public Key[] peers() {
- return peerBytes.keySet().toArray(new Key[0]);
+ return stats.keySet().toArray(new Key[0]);
}
/**
@@ -93,8 +105,8 @@ public class Statistics {
*/
public long totalRx() {
long rx = 0;
- for (final Pair<Long, Long> val : peerBytes.values()) {
- rx += val.first;
+ for (final PeerStats val : stats.values()) {
+ rx += val.rxBytes;
}
return rx;
}
@@ -106,8 +118,8 @@ public class Statistics {
*/
public long totalTx() {
long tx = 0;
- for (final Pair<Long, Long> val : peerBytes.values()) {
- tx += val.second;
+ for (final PeerStats val : stats.values()) {
+ tx += val.txBytes;
}
return tx;
}
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 143a3ab4..023743a8 100644
--- a/tunnel/src/main/java/com/wireguard/android/backend/WgQuickBackend.java
+++ b/tunnel/src/main/java/com/wireguard/android/backend/WgQuickBackend.java
@@ -20,6 +20,7 @@ import com.wireguard.util.NonNullForAll;
import java.io.File;
import java.io.FileOutputStream;
import java.nio.charset.StandardCharsets;
+import java.time.Instant;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
@@ -83,17 +84,17 @@ public final class WgQuickBackend implements Backend {
final Statistics stats = new Statistics();
final Collection<String> output = new ArrayList<>();
try {
- if (rootShell.run(output, String.format("wg show '%s' transfer", tunnel.getName())) != 0)
+ if (rootShell.run(output, String.format("wg show '%s' dump", tunnel.getName())) != 0)
return stats;
} catch (final Exception ignored) {
return stats;
}
for (final String line : output) {
final String[] parts = line.split("\\t");
- if (parts.length != 3)
+ if (parts.length != 8)
continue;
try {
- stats.add(Key.fromBase64(parts[0]), Long.parseLong(parts[1]), Long.parseLong(parts[2]));
+ stats.add(Key.fromBase64(parts[0]), Long.parseLong(parts[5]), Long.parseLong(parts[6]), Long.parseLong(parts[4]) * 1000);
} catch (final Exception ignored) {
}
}