Add sending statistics
This commit is contained in:
parent
ef530780bd
commit
20f3c0388a
2 changed files with 34 additions and 11 deletions
|
@ -23,6 +23,7 @@ import kotlinx.coroutines.Job
|
||||||
import kotlinx.coroutines.SupervisorJob
|
import kotlinx.coroutines.SupervisorJob
|
||||||
import kotlinx.coroutines.cancel
|
import kotlinx.coroutines.cancel
|
||||||
import kotlinx.coroutines.cancelAndJoin
|
import kotlinx.coroutines.cancelAndJoin
|
||||||
|
import kotlinx.coroutines.delay
|
||||||
import kotlinx.coroutines.flow.MutableStateFlow
|
import kotlinx.coroutines.flow.MutableStateFlow
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import org.amnezia.vpn.protocol.BadConfigException
|
import org.amnezia.vpn.protocol.BadConfigException
|
||||||
|
@ -54,6 +55,7 @@ const val ERROR_MSG = "ERROR_MSG"
|
||||||
const val AFTER_PERMISSION_CHECK = "AFTER_PERMISSION_CHECK"
|
const val AFTER_PERMISSION_CHECK = "AFTER_PERMISSION_CHECK"
|
||||||
private const val PREFS_CONFIG_KEY = "LAST_CONF"
|
private const val PREFS_CONFIG_KEY = "LAST_CONF"
|
||||||
private const val NOTIFICATION_ID = 1337
|
private const val NOTIFICATION_ID = 1337
|
||||||
|
private const val STATISTICS_SENDING_TIMEOUT = 1000L
|
||||||
|
|
||||||
class AmneziaVpnService : VpnService() {
|
class AmneziaVpnService : VpnService() {
|
||||||
|
|
||||||
|
@ -75,6 +77,7 @@ class AmneziaVpnService : VpnService() {
|
||||||
|
|
||||||
private var connectionJob: Job? = null
|
private var connectionJob: Job? = null
|
||||||
private var disconnectionJob: Job? = null
|
private var disconnectionJob: Job? = null
|
||||||
|
private var statisticsSendingJob: Job? = null
|
||||||
private lateinit var clientMessenger: IpcMessenger
|
private lateinit var clientMessenger: IpcMessenger
|
||||||
|
|
||||||
private val connectionExceptionHandler = CoroutineExceptionHandler { _, e ->
|
private val connectionExceptionHandler = CoroutineExceptionHandler { _, e ->
|
||||||
|
@ -123,14 +126,6 @@ class AmneziaVpnService : VpnService() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Action.REQUEST_STATISTICS -> {
|
|
||||||
clientMessenger.send {
|
|
||||||
ServiceEvent.STATISTICS_UPDATE.packToMessage {
|
|
||||||
putStatistics(protocol?.statistics ?: Statistics.EMPTY_STATISTICS)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -207,6 +202,7 @@ class AmneziaVpnService : VpnService() {
|
||||||
Log.d(TAG, "onBind by $intent")
|
Log.d(TAG, "onBind by $intent")
|
||||||
if (intent?.action == "android.net.VpnService") return super.onBind(intent)
|
if (intent?.action == "android.net.VpnService") return super.onBind(intent)
|
||||||
isServiceBound = true
|
isServiceBound = true
|
||||||
|
if (isConnected) launchSendingStatistics()
|
||||||
return vpnServiceMessenger.binder
|
return vpnServiceMessenger.binder
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -214,6 +210,7 @@ class AmneziaVpnService : VpnService() {
|
||||||
Log.d(TAG, "onUnbind by $intent")
|
Log.d(TAG, "onUnbind by $intent")
|
||||||
if (intent?.action != "android.net.VpnService") {
|
if (intent?.action != "android.net.VpnService") {
|
||||||
isServiceBound = false
|
isServiceBound = false
|
||||||
|
stopSendingStatistics()
|
||||||
clientMessenger.reset()
|
clientMessenger.reset()
|
||||||
if (isUnknown || isDisconnected) stopSelf()
|
if (isUnknown || isDisconnected) stopSelf()
|
||||||
}
|
}
|
||||||
|
@ -247,19 +244,46 @@ class AmneziaVpnService : VpnService() {
|
||||||
when (protocolState) {
|
when (protocolState) {
|
||||||
CONNECTED -> {
|
CONNECTED -> {
|
||||||
clientMessenger.send(ServiceEvent.CONNECTED)
|
clientMessenger.send(ServiceEvent.CONNECTED)
|
||||||
|
if (isServiceBound) launchSendingStatistics()
|
||||||
}
|
}
|
||||||
|
|
||||||
DISCONNECTED -> {
|
DISCONNECTED -> {
|
||||||
clientMessenger.send(ServiceEvent.DISCONNECTED)
|
clientMessenger.send(ServiceEvent.DISCONNECTED)
|
||||||
|
stopSendingStatistics()
|
||||||
if (!isServiceBound) stopSelf()
|
if (!isServiceBound) stopSelf()
|
||||||
}
|
}
|
||||||
|
|
||||||
CONNECTING, DISCONNECTING, UNKNOWN -> {}
|
DISCONNECTING -> {
|
||||||
|
stopSendingStatistics()
|
||||||
|
}
|
||||||
|
|
||||||
|
CONNECTING, UNKNOWN -> {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@MainThread
|
||||||
|
private fun launchSendingStatistics() {
|
||||||
|
if (isServiceBound && isConnected) {
|
||||||
|
statisticsSendingJob = mainScope.launch {
|
||||||
|
while (true) {
|
||||||
|
clientMessenger.send {
|
||||||
|
ServiceEvent.STATISTICS_UPDATE.packToMessage {
|
||||||
|
putStatistics(protocol?.statistics ?: Statistics.EMPTY_STATISTICS)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
delay(STATISTICS_SENDING_TIMEOUT)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@MainThread
|
||||||
|
private fun stopSendingStatistics() {
|
||||||
|
statisticsSendingJob?.cancel()
|
||||||
|
}
|
||||||
|
|
||||||
@MainThread
|
@MainThread
|
||||||
private fun connect(vpnConfig: String?) {
|
private fun connect(vpnConfig: String?) {
|
||||||
Log.v(TAG, "Start VPN connection")
|
Log.v(TAG, "Start VPN connection")
|
||||||
|
|
|
@ -31,8 +31,7 @@ enum class Action : IpcMessage {
|
||||||
REGISTER_CLIENT,
|
REGISTER_CLIENT,
|
||||||
CONNECT,
|
CONNECT,
|
||||||
DISCONNECT,
|
DISCONNECT,
|
||||||
REQUEST_STATUS,
|
REQUEST_STATUS
|
||||||
REQUEST_STATISTICS
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun <T> T.packToMessage(): Message
|
fun <T> T.packToMessage(): Message
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue