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 1/2] 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 From 1144183da48af082f3c162eb989e02bda93b2252 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 2/2] Update notification icon based on traffic Notification icon would be a good indication for current traffic - going through proxy - connect directly - not going through v2rayNG --- .../com/v2ray/ang/service/V2RayVpnService.kt | 15 ++++++++++++--- .../app/src/main/res/drawable/ic_stat_direct.png | Bin 0 -> 1915 bytes .../app/src/main/res/drawable/ic_stat_proxy.png | Bin 0 -> 3199 bytes .../app/src/main/res/values-zh-rCN/strings.xml | 2 +- .../app/src/main/res/values-zh-rTW/strings.xml | 2 +- V2rayNG/app/src/main/res/values/strings.xml | 3 ++- 6 files changed, 16 insertions(+), 6 deletions(-) create mode 100644 V2rayNG/app/src/main/res/drawable/ic_stat_direct.png create mode 100644 V2rayNG/app/src/main/res/drawable/ic_stat_proxy.png 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 1a1dcebe..7c614210 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 @@ -40,6 +40,7 @@ class V2RayVpnService : VpnService() { const val NOTIFICATION_ID = 1 const val NOTIFICATION_PENDING_INTENT_CONTENT = 0 const val NOTIFICATION_PENDING_INTENT_STOP_V2RAY = 1 + const val NOTIFICATION_ICON_THRESHOLD = 3000 fun startV2Ray(context: Context) { val intent = Intent(context.applicationContext, V2RayVpnService::class.java) @@ -359,8 +360,15 @@ class V2RayVpnService : VpnService() { mSubscription = null } - private fun updateNotification(contentText: String) { + private fun updateNotification(contentText: String, proxyTraffic: Long, directTraffic: Long) { if (mBuilder != null) { + if (proxyTraffic < NOTIFICATION_ICON_THRESHOLD && directTraffic < NOTIFICATION_ICON_THRESHOLD) { + mBuilder?.setSmallIcon(R.drawable.ic_v) + } else if (proxyTraffic > directTraffic) { + mBuilder?.setSmallIcon(R.drawable.ic_stat_proxy) + } else { + mBuilder?.setSmallIcon(R.drawable.ic_stat_direct) + } mBuilder?.setStyle(NotificationCompat.BigTextStyle().bigText(contentText)) getNotificationManager().notify(NOTIFICATION_ID, mBuilder?.build()) } @@ -393,7 +401,8 @@ class V2RayVpnService : VpnService() { updateNotification("proxy\t• ${(proxyUplink / sinceLastQueryInSeconds).toLong().toSpeedString()}↑ " + "${(proxyDownlink / sinceLastQueryInSeconds).toLong().toSpeedString()}↓\n" + "direct\t• ${(directUplink / sinceLastQueryInSeconds).toLong().toSpeedString()}↑ " + - "${(directDownlink / sinceLastQueryInSeconds).toLong().toSpeedString()}↓") + "${(directDownlink / sinceLastQueryInSeconds).toLong().toSpeedString()}↓", + proxyDownlink + proxyUplink, directDownlink + directUplink) } last_zero_speed = zero_speed lastQueryTime = queryTime @@ -408,7 +417,7 @@ class V2RayVpnService : VpnService() { mSubscription = null val cf_name = defaultDPreference.getPrefString(AppConfig.PREF_CURR_CONFIG_NAME, "") - updateNotification(cf_name) + updateNotification(cf_name, 0, 0) } } diff --git a/V2rayNG/app/src/main/res/drawable/ic_stat_direct.png b/V2rayNG/app/src/main/res/drawable/ic_stat_direct.png new file mode 100644 index 0000000000000000000000000000000000000000..406a0c76906cc821cc56ddfa2f6176b32eb5736a GIT binary patch literal 1915 zcmYjSd05if8vgwtpiJ|orVgeOA;nzTL{LZzw*pr*aw|(xP*TAKgUs?r<(QLYI+~`r zC6+o_jd-oAQ<81gxMhnM)5PjJ*`khSi>6(i+cVFc=Q-y)-*>*}eb4*;aZWbRe~Xa; z+5i9mBQD1?NFO5?=TfA8eO@eg1_01U%ny}?`uVb=qzNQJv~;(Sq)15Ap96r)CPktk zDo!ZF>=wp|C9c?s);26g9PNq?q5Dz%5
#
zBsq;FO^zW`nM@{`;y`wAAnH4aDf=Wcfr2PWS-S}Fe+*AyN>s8qQ6`p3FpHRi-O^N<
zD;7&$Bq4ue(aR(MrB9#y<&aRKw