aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorRoger Chuang <lchuang@mozilla.com>2019-11-26 16:09:42 +0800
committerRoger Chuang <lchuang@mozilla.com>2019-11-27 10:11:59 +0800
commit4aab6bf50136e278f48133c9e056651d3f951394 (patch)
tree370ed3fae7525a489057d323b53f9de85e560ad0
parentAdd simplified TunnelManager (diff)
downloadwireguard-android-4aab6bf50136e278f48133c9e056651d3f951394.tar.xz
wireguard-android-4aab6bf50136e278f48133c9e056651d3f951394.zip
Add statistic interface to TunnelManager.kt
Signed-off-by: Roger Chuang <lchuang@mozilla.com>
-rw-r--r--libwg/build.gradle1
-rw-r--r--libwg/src/main/java/com/wireguard/android/backend/TunnelManager.kt94
-rw-r--r--libwg/src/main/java/com/wireguard/android/backend/VpnServiceBackend.kt4
3 files changed, 99 insertions, 0 deletions
diff --git a/libwg/build.gradle b/libwg/build.gradle
index bf4e6f7b..9c187499 100644
--- a/libwg/build.gradle
+++ b/libwg/build.gradle
@@ -32,6 +32,7 @@ android {
dependencies {
implementation project(":config")
+ implementation project(":crypto")
implementation "android.arch.lifecycle:extensions:1.1.1"
implementation "androidx.annotation:annotation:$annotationsVersion"
diff --git a/libwg/src/main/java/com/wireguard/android/backend/TunnelManager.kt b/libwg/src/main/java/com/wireguard/android/backend/TunnelManager.kt
index 54fdc8c8..3bc20e9e 100644
--- a/libwg/src/main/java/com/wireguard/android/backend/TunnelManager.kt
+++ b/libwg/src/main/java/com/wireguard/android/backend/TunnelManager.kt
@@ -9,10 +9,15 @@ import android.content.Context
import android.content.Intent
import android.net.VpnService
import android.net.VpnService.prepare
+import android.os.SystemClock
+import android.util.Pair
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Observer
import androidx.lifecycle.ProcessLifecycleOwner
import com.wireguard.config.Config
+import com.wireguard.crypto.Key
+import com.wireguard.crypto.KeyFormatException
+import java.util.HashMap
class TunnelManager {
@@ -78,6 +83,54 @@ class TunnelManager {
return currentTunnel?.state == Tunnel.State.Up
}
+ fun getConfig(tunnel: Tunnel): String? {
+ return backend.getConfig(tunnel)
+ }
+
+ fun getStatistics(tunnel: Tunnel): Statistics {
+ val stats = Statistics()
+ if (tunnel !== currentTunnel) {
+ return stats
+ }
+ val config = backend.getConfig(tunnel) ?: return stats
+ var key: Key? = null
+ var rx: Long = 0
+ var tx: Long = 0
+ for (line in config.split("\\n".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()) {
+ if (line.startsWith("public_key=")) {
+ key?.let { stats.add(it, rx, tx) }
+ rx = 0
+ tx = 0
+ key = try {
+ Key.fromHex(line.substring(11))
+ } catch (ignored: KeyFormatException) {
+ null
+ }
+
+ } else if (line.startsWith("rx_bytes=")) {
+ if (key == null)
+ continue
+ rx = try {
+ java.lang.Long.parseLong(line.substring(9))
+ } catch (ignored: NumberFormatException) {
+ 0
+ }
+
+ } else if (line.startsWith("tx_bytes=")) {
+ if (key == null)
+ continue
+ tx = try {
+ java.lang.Long.parseLong(line.substring(9))
+ } catch (ignored: NumberFormatException) {
+ 0
+ }
+
+ }
+ }
+ key?.let { stats.add(it, rx, tx) }
+ return stats
+ }
+
public class VpnService : android.net.VpnService() {
val builder: Builder
get() {
@@ -118,4 +171,45 @@ fun VpnService.Builder.applyConfig(config: Config){
setMtu(config.getInterface().mtu.orElse(1280))
setBlocking(true)
+}
+
+class Statistics {
+ private var lastTouched = SystemClock.elapsedRealtime()
+ private val peerBytes = HashMap<Key, Pair<Long, Long>>()
+
+ private val isStale: Boolean
+ get() = SystemClock.elapsedRealtime() - lastTouched > 900
+
+ fun add(key: Key, rx: Long, tx: Long) {
+ peerBytes[key] = Pair.create(rx, tx)
+ lastTouched = SystemClock.elapsedRealtime()
+ }
+
+ fun peers(): Array<Key> {
+ return peerBytes.keys.toTypedArray()
+ }
+
+ fun peerRx(peer: Key): Long {
+ return if (!peerBytes.containsKey(peer)) 0 else peerBytes[peer]?.first ?: 0
+ }
+
+ fun peerTx(peer: Key): Long {
+ return if (!peerBytes.containsKey(peer)) 0 else peerBytes[peer]?.second ?: 0
+ }
+
+ fun totalRx(): Long {
+ var rx: Long = 0
+ for (`val` in peerBytes.values) {
+ rx += `val`.first
+ }
+ return rx
+ }
+
+ fun totalTx(): Long {
+ var tx: Long = 0
+ for (`val` in peerBytes.values) {
+ tx += `val`.second
+ }
+ return tx
+ }
} \ No newline at end of file
diff --git a/libwg/src/main/java/com/wireguard/android/backend/VpnServiceBackend.kt b/libwg/src/main/java/com/wireguard/android/backend/VpnServiceBackend.kt
index 6e03a9d3..542cdfe6 100644
--- a/libwg/src/main/java/com/wireguard/android/backend/VpnServiceBackend.kt
+++ b/libwg/src/main/java/com/wireguard/android/backend/VpnServiceBackend.kt
@@ -37,6 +37,10 @@ class VpnServiceBackend : BackendNative {
return wgVersion()
}
+ fun getConfig(tunnel: Tunnel): String? {
+ return tunnel.tunnelHandle?.let { wgGetConfig(it) }
+ }
+
interface VpnServiceDelegate {
fun protect(socket: Int): Boolean
}