From 5bf2fda990d3136b30e9aa2ac19ddf75d3c6e029 Mon Sep 17 00:00:00 2001 From: yuhan6665 <1588741+yuhan6665@users.noreply.github.com> Date: Sat, 18 Jul 2020 13:09:53 -0400 Subject: [PATCH] Measure traffic from outbounds With v2ray-core 4.26.0, traffic can be measured from outbounds. Stats is shown separately for proxy and direct traffic. In the future, it is possible to add stats for each server node and even historic usage graph. --- AndroidLibV2rayLite/interact.go | 2 +- V2rayNG/app/src/main/assets/v2ray_config.json | 4 ++-- .../com/v2ray/ang/service/V2RayVpnService.kt | 17 +++++++++++------ 3 files changed, 14 insertions(+), 9 deletions(-) diff --git a/AndroidLibV2rayLite/interact.go b/AndroidLibV2rayLite/interact.go index 6b49ed3c..e05c00ea 100644 --- a/AndroidLibV2rayLite/interact.go +++ b/AndroidLibV2rayLite/interact.go @@ -119,7 +119,7 @@ func (v V2RayPoint) QueryStats(tag string, direct string) int64 { if v.statsManager == nil { return 0 } - counter := v.statsManager.GetCounter(fmt.Sprintf("inbound>>>%s>>>traffic>>>%s", tag, direct)) + counter := v.statsManager.GetCounter(fmt.Sprintf("outbound>>>%s>>>traffic>>>%s", tag, direct)) if counter == nil { return 0 } diff --git a/V2rayNG/app/src/main/assets/v2ray_config.json b/V2rayNG/app/src/main/assets/v2ray_config.json index c88ca53c..2783ec74 100644 --- a/V2rayNG/app/src/main/assets/v2ray_config.json +++ b/V2rayNG/app/src/main/assets/v2ray_config.json @@ -13,8 +13,8 @@ } }, "system": { - "statsInboundUplink": true, - "statsInboundDownlink": true + "statsOutboundUplink": true, + "statsOutboundDownlink": true } }, "inbounds": [{ diff --git a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/service/V2RayVpnService.kt b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/service/V2RayVpnService.kt index a07941af..1a1dcebe 100644 --- a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/service/V2RayVpnService.kt +++ b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/service/V2RayVpnService.kt @@ -361,7 +361,7 @@ class V2RayVpnService : VpnService() { private fun updateNotification(contentText: String) { if (mBuilder != null) { - mBuilder?.setContentTitle(contentText) + mBuilder?.setStyle(NotificationCompat.BigTextStyle().bigText(contentText)) getNotificationManager().notify(NOTIFICATION_ID, mBuilder?.build()) } } @@ -382,13 +382,18 @@ class V2RayVpnService : VpnService() { mSubscription = Observable.interval(3, java.util.concurrent.TimeUnit.SECONDS) .subscribe { - val uplink = v2rayPoint.queryStats("socks", "uplink") - val downlink = v2rayPoint.queryStats("socks", "downlink") - val zero_speed = (uplink == 0L && downlink == 0L) + val proxyUplink = v2rayPoint.queryStats("proxy", "uplink") + val proxyDownlink = v2rayPoint.queryStats("proxy", "downlink") + val directUplink = v2rayPoint.queryStats("direct", "uplink") + val directDownlink = v2rayPoint.queryStats("direct", "downlink") + val zero_speed = (proxyUplink == 0L && proxyDownlink == 0L && directUplink == 0L && directDownlink == 0L) val queryTime = System.currentTimeMillis() if (!zero_speed || !last_zero_speed) { - updateNotification("${cf_name} • ${(uplink * 1000 / (queryTime - lastQueryTime)).toSpeedString()}↑" + - " ${(downlink * 1000 / (queryTime - lastQueryTime)).toSpeedString()}↓") + val sinceLastQueryInSeconds = (queryTime - lastQueryTime) / 1000.0 + updateNotification("proxy\t• ${(proxyUplink / sinceLastQueryInSeconds).toLong().toSpeedString()}↑ " + + "${(proxyDownlink / sinceLastQueryInSeconds).toLong().toSpeedString()}↓\n" + + "direct\t• ${(directUplink / sinceLastQueryInSeconds).toLong().toSpeedString()}↑ " + + "${(directDownlink / sinceLastQueryInSeconds).toLong().toSpeedString()}↓") } last_zero_speed = zero_speed lastQueryTime = queryTime