From f882375cfa9bd2a3d92f64299dc860ce2c3f3cc6 Mon Sep 17 00:00:00 2001 From: Kr328 <39107975+Kr328@users.noreply.github.com> Date: Sun, 24 Nov 2019 17:25:46 +0800 Subject: [PATCH] create event poll --- .../com/github/kr328/clash/MainActivity.kt | 2 +- .../java/com/github/kr328/clash/core/Clash.kt | 29 ++++++++++++++++++- .../github/kr328/clash/core/ClashProcess.kt | 8 ++--- .../clash/core/{model => event}/Event.kt | 4 ++- .../clash/core/{model => event}/LogEvent.kt | 11 +++++-- .../core/{model => event}/ProcessEvent.kt | 2 +- .../clash/core/event/ProxyChangedEvent.kt | 5 ++++ .../kr328/clash/core/event/TrafficEvent.kt | 6 ++++ .../clash/core/model/ProxyChangedEvent.kt | 3 -- .../kr328/clash/service/ClashService.kt | 2 +- .../github/kr328/clash/service/TunService.kt | 2 +- 11 files changed, 58 insertions(+), 16 deletions(-) rename core/src/main/java/com/github/kr328/clash/core/{model => event}/Event.kt (52%) rename core/src/main/java/com/github/kr328/clash/core/{model => event}/LogEvent.kt (83%) rename core/src/main/java/com/github/kr328/clash/core/{model => event}/ProcessEvent.kt (94%) create mode 100644 core/src/main/java/com/github/kr328/clash/core/event/ProxyChangedEvent.kt create mode 100644 core/src/main/java/com/github/kr328/clash/core/event/TrafficEvent.kt delete mode 100644 core/src/main/java/com/github/kr328/clash/core/model/ProxyChangedEvent.kt diff --git a/app/src/main/java/com/github/kr328/clash/MainActivity.kt b/app/src/main/java/com/github/kr328/clash/MainActivity.kt index 59c61d4..dd86a73 100644 --- a/app/src/main/java/com/github/kr328/clash/MainActivity.kt +++ b/app/src/main/java/com/github/kr328/clash/MainActivity.kt @@ -14,7 +14,7 @@ import android.view.View import androidx.appcompat.app.AppCompatActivity import androidx.lifecycle.MutableLiveData import androidx.lifecycle.observe -import com.github.kr328.clash.core.model.ProcessEvent +import com.github.kr328.clash.core.event.ProcessEvent import com.github.kr328.clash.service.ClashService import com.github.kr328.clash.service.IClashObserver import com.github.kr328.clash.service.IClashService diff --git a/core/src/main/java/com/github/kr328/clash/core/Clash.kt b/core/src/main/java/com/github/kr328/clash/core/Clash.kt index 3bc8664..70bfef4 100644 --- a/core/src/main/java/com/github/kr328/clash/core/Clash.kt +++ b/core/src/main/java/com/github/kr328/clash/core/Clash.kt @@ -3,6 +3,7 @@ package com.github.kr328.clash.core import android.content.Context import android.util.Log import com.github.kr328.clash.core.Constants.TAG +import com.github.kr328.clash.core.event.* import com.github.kr328.clash.core.model.* import kotlinx.serialization.json.Json import kotlinx.serialization.json.JsonConfiguration @@ -10,6 +11,8 @@ import org.json.JSONObject import java.io.File import java.io.FileDescriptor import java.io.IOException +import java.lang.IllegalArgumentException +import kotlin.reflect.KClass class Clash( context: Context, @@ -24,6 +27,7 @@ class Clash( const val COMMAND_PROFILE_RELOAD = 4 const val COMMAND_QUERY_PROXIES = 5 const val COMMAND_POLL_EVENT = 6 + const val COMMAND_SET_EVENT_ENABLED = 7 const val PING_REPLY = 233 } @@ -62,20 +66,43 @@ class Clash( fun pollEvent(listener: (Event) -> Unit) { runControl(COMMAND_POLL_EVENT) { _, input, _ -> - while ( !Thread.currentThread().isInterrupted ) { + loop@while ( process.isRunning() ) { when (val type = input.readInt()) { + Event.EVENT_CLOSE -> { + break@loop + } Event.EVENT_LOG -> { listener(Json(JsonConfiguration.Stable).parse(LogEvent.serializer(), input.readString())) } Event.EVENT_PROXY_CHANGED -> { listener(ProxyChangedEvent()) } + Event.EVENT_TRAFFIC -> { + listener(Json(JsonConfiguration.Stable).parse(TrafficEvent.serializer(), input.readString())) + } else -> Log.w(TAG, "Invalid event type $type") } } } } + fun setEventEnabled(type: KClass, enabled: Boolean) { + runControl(COMMAND_SET_EVENT_ENABLED) { _, _, output -> + output.writeInt( + when ( type ) { + LogEvent::class -> + Event.EVENT_LOG + ProxyChangedEvent::class -> + Event.EVENT_PROXY_CHANGED + TrafficEvent::class -> + Event.EVENT_TRAFFIC + else -> throw IllegalArgumentException("Invalid event type $type") + } + ) + output.writeInt(if ( enabled ) 1 else 0) + } + } + fun startTunDevice(fd: FileDescriptor, mtu: Int) { runControl(COMMAND_TUN_START) { socket, _, output -> socket.setFileDescriptorsForSend(arrayOf(fd)) diff --git a/core/src/main/java/com/github/kr328/clash/core/ClashProcess.kt b/core/src/main/java/com/github/kr328/clash/core/ClashProcess.kt index ca8ee9c..3dd8286 100644 --- a/core/src/main/java/com/github/kr328/clash/core/ClashProcess.kt +++ b/core/src/main/java/com/github/kr328/clash/core/ClashProcess.kt @@ -3,7 +3,7 @@ package com.github.kr328.clash.core import android.content.Context import android.util.Log import com.github.kr328.clash.core.Constants.TAG -import com.github.kr328.clash.core.model.ProcessEvent +import com.github.kr328.clash.core.event.ProcessEvent import java.io.File import java.io.FileOutputStream import java.io.IOException @@ -73,7 +73,7 @@ class ClashProcess(private val context: Context, process = p pid = currentPid - listener(ProcessEvent.STATUS_STARTED) + listener(ProcessEvent.STARTED) Log.i(TAG, "Clash started pid = $pid") @@ -89,11 +89,11 @@ class ClashProcess(private val context: Context, pid = -1 } - listener(ProcessEvent.STATUS_STOPPED) + listener(ProcessEvent.STOPPED) } } catch (e: Exception) { - listener(ProcessEvent.STATUS_STOPPED) + listener(ProcessEvent.STOPPED) throw e } } diff --git a/core/src/main/java/com/github/kr328/clash/core/model/Event.kt b/core/src/main/java/com/github/kr328/clash/core/event/Event.kt similarity index 52% rename from core/src/main/java/com/github/kr328/clash/core/model/Event.kt rename to core/src/main/java/com/github/kr328/clash/core/event/Event.kt index 2f8f3d1..3a9c769 100644 --- a/core/src/main/java/com/github/kr328/clash/core/model/Event.kt +++ b/core/src/main/java/com/github/kr328/clash/core/event/Event.kt @@ -1,8 +1,10 @@ -package com.github.kr328.clash.core.model +package com.github.kr328.clash.core.event interface Event { companion object { + const val EVENT_CLOSE = 0 const val EVENT_LOG = 1 const val EVENT_PROXY_CHANGED = 2 + const val EVENT_TRAFFIC = 3 } } \ No newline at end of file diff --git a/core/src/main/java/com/github/kr328/clash/core/model/LogEvent.kt b/core/src/main/java/com/github/kr328/clash/core/event/LogEvent.kt similarity index 83% rename from core/src/main/java/com/github/kr328/clash/core/model/LogEvent.kt rename to core/src/main/java/com/github/kr328/clash/core/event/LogEvent.kt index 47cf40c..b71c0bd 100644 --- a/core/src/main/java/com/github/kr328/clash/core/model/LogEvent.kt +++ b/core/src/main/java/com/github/kr328/clash/core/event/LogEvent.kt @@ -1,4 +1,4 @@ -package com.github.kr328.clash.core.model +package com.github.kr328.clash.core.event import kotlinx.serialization.Decoder import kotlinx.serialization.Encoder @@ -9,7 +9,8 @@ import kotlinx.serialization.internal.StringDescriptor import java.lang.IllegalArgumentException @Serializable -data class LogEvent(val level: Level, val message: String): Event { +data class LogEvent(val level: Level, val message: String): + Event { companion object { const val DEBUG_VALUE = 1 const val INFO_VALUE = 2 @@ -19,7 +20,11 @@ data class LogEvent(val level: Level, val message: String): Event { @Serializable(LevelSerializer::class) enum class Level(val value: Int) { - DEBUG(DEBUG_VALUE), INFO(INFO_VALUE), WARN(WARN_VALUE), ERROR(ERROR_VALUE) + DEBUG(DEBUG_VALUE), INFO( + INFO_VALUE + ), WARN(WARN_VALUE), ERROR( + ERROR_VALUE + ) } class LevelSerializer : KSerializer { diff --git a/core/src/main/java/com/github/kr328/clash/core/model/ProcessEvent.kt b/core/src/main/java/com/github/kr328/clash/core/event/ProcessEvent.kt similarity index 94% rename from core/src/main/java/com/github/kr328/clash/core/model/ProcessEvent.kt rename to core/src/main/java/com/github/kr328/clash/core/event/ProcessEvent.kt index 9a26e88..f73c534 100644 --- a/core/src/main/java/com/github/kr328/clash/core/model/ProcessEvent.kt +++ b/core/src/main/java/com/github/kr328/clash/core/event/ProcessEvent.kt @@ -1,4 +1,4 @@ -package com.github.kr328.clash.core.model +package com.github.kr328.clash.core.event import android.os.Parcel import android.os.Parcelable diff --git a/core/src/main/java/com/github/kr328/clash/core/event/ProxyChangedEvent.kt b/core/src/main/java/com/github/kr328/clash/core/event/ProxyChangedEvent.kt new file mode 100644 index 0000000..46175c2 --- /dev/null +++ b/core/src/main/java/com/github/kr328/clash/core/event/ProxyChangedEvent.kt @@ -0,0 +1,5 @@ +package com.github.kr328.clash.core.event + +import com.github.kr328.clash.core.event.Event + +class ProxyChangedEvent : Event \ No newline at end of file diff --git a/core/src/main/java/com/github/kr328/clash/core/event/TrafficEvent.kt b/core/src/main/java/com/github/kr328/clash/core/event/TrafficEvent.kt new file mode 100644 index 0000000..1342546 --- /dev/null +++ b/core/src/main/java/com/github/kr328/clash/core/event/TrafficEvent.kt @@ -0,0 +1,6 @@ +package com.github.kr328.clash.core.event + +import kotlinx.serialization.Serializable + +@Serializable +data class TrafficEvent(val down: Long, val up: Long, val total: Long): Event \ No newline at end of file diff --git a/core/src/main/java/com/github/kr328/clash/core/model/ProxyChangedEvent.kt b/core/src/main/java/com/github/kr328/clash/core/model/ProxyChangedEvent.kt deleted file mode 100644 index bfca74c..0000000 --- a/core/src/main/java/com/github/kr328/clash/core/model/ProxyChangedEvent.kt +++ /dev/null @@ -1,3 +0,0 @@ -package com.github.kr328.clash.core.model - -class ProxyChangedEvent : Event \ No newline at end of file diff --git a/service/src/main/java/com/github/kr328/clash/service/ClashService.kt b/service/src/main/java/com/github/kr328/clash/service/ClashService.kt index 23c4e42..265f111 100644 --- a/service/src/main/java/com/github/kr328/clash/service/ClashService.kt +++ b/service/src/main/java/com/github/kr328/clash/service/ClashService.kt @@ -13,7 +13,7 @@ import androidx.core.app.NotificationManagerCompat import androidx.lifecycle.LiveData import androidx.lifecycle.Observer import com.github.kr328.clash.core.Clash -import com.github.kr328.clash.core.model.ProcessEvent +import com.github.kr328.clash.core.event.ProcessEvent import com.github.kr328.clash.core.Constants import com.github.kr328.clash.service.data.ClashDatabase import com.github.kr328.clash.service.data.ClashProfileEntity diff --git a/service/src/main/java/com/github/kr328/clash/service/TunService.kt b/service/src/main/java/com/github/kr328/clash/service/TunService.kt index 1858637..46890cd 100644 --- a/service/src/main/java/com/github/kr328/clash/service/TunService.kt +++ b/service/src/main/java/com/github/kr328/clash/service/TunService.kt @@ -7,7 +7,7 @@ import android.os.Build import android.os.IBinder import android.os.ParcelFileDescriptor import android.util.Log -import com.github.kr328.clash.core.model.ProcessEvent +import com.github.kr328.clash.core.event.ProcessEvent import com.github.kr328.clash.service.net.DefaultNetworkObserver class TunService : VpnService() {