create event poll
This commit is contained in:
11 files changed
+58
-16
No files matched your search
@@ -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
|
||||
|
||||
@@ -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<Event>, 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))
|
||||
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
+3
-1
@@ -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
|
||||
}
|
||||
}
|
||||
+8
-3
@@ -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<Level> {
|
||||
+1
-1
@@ -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
|
||||
@@ -0,0 +1,5 @@
|
||||
package com.github.kr328.clash.core.event
|
||||
|
||||
import com.github.kr328.clash.core.event.Event
|
||||
|
||||
class ProxyChangedEvent : Event
|
||||
@@ -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
|
||||
@@ -1,3 +0,0 @@
|
||||
package com.github.kr328.clash.core.model
|
||||
|
||||
class ProxyChangedEvent : Event
|
||||
@@ -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
|
||||
|
||||
@@ -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() {
|
||||
|
||||
Reference in new issue
Block a user