From f418a3649e8751317c42dfedf8812044fb487747 Mon Sep 17 00:00:00 2001 From: Kr328 <39107975+Kr328@users.noreply.github.com> Date: Sat, 30 Nov 2019 14:43:07 +0800 Subject: [PATCH] split traffic --- .../com/github/kr328/clash/BaseActivity.kt | 15 +-- .../com/github/kr328/clash/MainActivity.kt | 10 +- .../com/github/kr328/clash/ProxyActivity.kt | 33 +++++- .../kr328/clash/adapter/ProxyAdapter.kt | 22 +++- .../kr328/clash/adapter/ProxyGroupAdapter.kt | 26 ++--- .../kr328/clash/model/ListProxyGroup.kt | 9 +- app/src/main/res/layout/activity_proxies.xml | 11 +- app/src/main/res/layout/adapter_proxy.xml | 11 +- .../main/res/layout/adapter_proxy_group.xml | 21 ++-- core/src/main/golang/server/event.go | 59 ++++++++-- core/src/main/golang/server/proxies.go | 2 + core/src/main/golang/server/server.go | 19 +-- .../java/com/github/kr328/clash/core/Clash.kt | 26 ++--- .../{TrafficEvent.kt => BandwidthEvent.kt} | 9 +- .../github/kr328/clash/core/event/Event.kt | 5 +- .../kr328/clash/core/event/SpeedEvent.kt | 27 +++++ .../kr328/clash/core/model/ProxyPacket.kt | 4 +- .../kr328/clash/core/model/RawProxyPacket.kt | 2 +- .../kr328/clash/core/utils/ByteFormatter.kt | 2 +- .../github/kr328/clash/core/event/Event.aidl | 6 +- .../clash/service/IClashEventObserver.aidl | 4 +- .../kr328/clash/service/ClashEventPuller.kt | 108 ++++++++++++------ .../kr328/clash/service/ClashEventService.kt | 22 ++-- .../clash/service/ClashProfileService.kt | 2 +- .../kr328/clash/service/ClashService.kt | 34 +++--- .../github/kr328/clash/service/TunService.kt | 4 +- 26 files changed, 321 insertions(+), 172 deletions(-) rename core/src/main/java/com/github/kr328/clash/core/event/{TrafficEvent.kt => BandwidthEvent.kt} (63%) create mode 100644 core/src/main/java/com/github/kr328/clash/core/event/SpeedEvent.kt diff --git a/app/src/main/java/com/github/kr328/clash/BaseActivity.kt b/app/src/main/java/com/github/kr328/clash/BaseActivity.kt index dbcd892..c8b657e 100644 --- a/app/src/main/java/com/github/kr328/clash/BaseActivity.kt +++ b/app/src/main/java/com/github/kr328/clash/BaseActivity.kt @@ -106,14 +106,15 @@ abstract class BaseActivity : AppCompatActivity(), IClashEventObserver { override fun onProcessEvent(event: ProcessEvent?) = this@BaseActivity.onProcessEvent(event) - override fun onProxyChangedEvent(event: ProxyChangedEvent?) = - this@BaseActivity.onProxyChangedEvent(event) - override fun onErrorEvent(event: ErrorEvent?) = this@BaseActivity.onErrorEvent(event) - override fun onTrafficEvent(event: TrafficEvent?) = - this@BaseActivity.onTrafficEvent(event) + override fun onSpeedEvent(event: SpeedEvent?) = + this@BaseActivity.onSpeedEvent(event) + + override fun onBandwidthEvent(event: BandwidthEvent?) { + this@BaseActivity.onBandwidthEvent(event) + } override fun onProfileChanged(event: ProfileChangedEvent?) = this@BaseActivity.onProfileChanged(event) @@ -123,8 +124,8 @@ abstract class BaseActivity : AppCompatActivity(), IClashEventObserver { override fun onErrorEvent(event: ErrorEvent?) {} override fun onProfileChanged(event: ProfileChangedEvent?) {} override fun onProcessEvent(event: ProcessEvent?) {} - override fun onProxyChangedEvent(event: ProxyChangedEvent?) {} - override fun onTrafficEvent(event: TrafficEvent?) {} + override fun onSpeedEvent(event: SpeedEvent?) {} + override fun onBandwidthEvent(event: BandwidthEvent?) {} override fun asBinder(): IBinder { return observerBinder } 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 a35d968..3608581 100644 --- a/app/src/main/java/com/github/kr328/clash/MainActivity.kt +++ b/app/src/main/java/com/github/kr328/clash/MainActivity.kt @@ -2,14 +2,10 @@ package com.github.kr328.clash import android.app.Activity import android.content.Intent -import android.graphics.Color import android.net.VpnService import android.os.Bundle import android.view.View -import com.github.kr328.clash.core.event.Event -import com.github.kr328.clash.core.event.ProcessEvent -import com.github.kr328.clash.core.event.ProfileChangedEvent -import com.github.kr328.clash.core.event.TrafficEvent +import com.github.kr328.clash.core.event.* import com.github.kr328.clash.core.utils.ByteFormatter import com.github.kr328.clash.service.TunService import kotlinx.android.synthetic.main.activity_main.* @@ -92,7 +88,7 @@ class MainActivity : BaseActivity() { } } - override fun onTrafficEvent(event: TrafficEvent?) { + override fun onBandwidthEvent(event: BandwidthEvent?) { runOnUiThread { if (lastEvent == ProcessEvent.STARTED) { activity_main_clash_status_summary.text = @@ -115,7 +111,7 @@ class MainActivity : BaseActivity() { it.eventService.registerEventObserver( MainActivity::class.java.simpleName, this, - intArrayOf(Event.EVENT_TRAFFIC) + intArrayOf(Event.EVENT_BANDWIDTH) ) } diff --git a/app/src/main/java/com/github/kr328/clash/ProxyActivity.kt b/app/src/main/java/com/github/kr328/clash/ProxyActivity.kt index 6bb2dd6..7e9d595 100644 --- a/app/src/main/java/com/github/kr328/clash/ProxyActivity.kt +++ b/app/src/main/java/com/github/kr328/clash/ProxyActivity.kt @@ -21,13 +21,27 @@ class ProxyActivity : BaseActivity() { it.visibility = View.GONE } + activity_proxies_swipe.setOnRefreshListener { + refreshList() + } + + refreshList() + } + + private fun refreshList() { + if (!activity_proxies_swipe.isRefreshing) + activity_proxies_swipe.isRefreshing = true + runClash { clash -> - val proxies = clash.queryAllProxies().proxies + val packet = clash.queryAllProxies() + val proxies = packet.proxies val adapterProxies = proxies .mapValues { - ListProxyGroup.ListProxy(it.value.name, + ListProxyGroup.ListProxy( + it.value.name, it.value.type.toString(), - it.value.delay) + it.value.delay + ) } val listData = proxies @@ -41,16 +55,24 @@ class ProxyActivity : BaseActivity() { else -> false } } + .filter { + when (packet.mode) { + "Global" -> it.value.name == "GLOBAL" + "Rule" -> it.value.name != "GLOBAL" + else -> false + } + } .sortedWith(compareBy( { it.value.type != ProxyPacket.Type.SELECT }, { it.value.name - })) + }) + ) .map { ListProxyGroup( it.value.name, - it.value.type.toString(), + it.value.type, it.value.all.mapNotNull { hash -> adapterProxies[hash] }, @@ -65,6 +87,7 @@ class ProxyActivity : BaseActivity() { notifyDataSetChanged() } activity_proxies_list.visibility = View.VISIBLE + activity_proxies_swipe.isRefreshing = false } } } diff --git a/app/src/main/java/com/github/kr328/clash/adapter/ProxyAdapter.kt b/app/src/main/java/com/github/kr328/clash/adapter/ProxyAdapter.kt index ac9c133..395f3f0 100644 --- a/app/src/main/java/com/github/kr328/clash/adapter/ProxyAdapter.kt +++ b/app/src/main/java/com/github/kr328/clash/adapter/ProxyAdapter.kt @@ -11,9 +11,10 @@ import com.github.kr328.clash.R import com.github.kr328.clash.model.ListProxyGroup import com.google.android.material.card.MaterialCardView -class ProxyAdapter(private val context: Context): RecyclerView.Adapter() { +class ProxyAdapter(private val context: Context) : RecyclerView.Adapter() { var proxies: List = emptyList() var now: ListProxyGroup.ListProxy? = null + var clickable: Boolean = false class Holder(view: View) : RecyclerView.ViewHolder(view) { val name: TextView = view.findViewById(R.id.adapter_proxy_name) @@ -33,21 +34,32 @@ class ProxyAdapter(private val context: Context): RecyclerView.Adapter 0 ) current.delay.toString() else "" + holder.delay.text = if (current.delay > 0) current.delay.toString() else "" + + if (clickable) { + holder.card.setOnClickListener { + notifyItemChanged(proxies.indexOf(now)) + notifyItemChanged(position) + + now = current + } + } } } \ No newline at end of file diff --git a/app/src/main/java/com/github/kr328/clash/adapter/ProxyGroupAdapter.kt b/app/src/main/java/com/github/kr328/clash/adapter/ProxyGroupAdapter.kt index fcb7b0b..420266f 100644 --- a/app/src/main/java/com/github/kr328/clash/adapter/ProxyGroupAdapter.kt +++ b/app/src/main/java/com/github/kr328/clash/adapter/ProxyGroupAdapter.kt @@ -8,22 +8,25 @@ import android.widget.TextView import androidx.recyclerview.widget.GridLayoutManager import androidx.recyclerview.widget.RecyclerView import com.github.kr328.clash.R -import com.github.kr328.clash.core.utils.Log +import com.github.kr328.clash.core.model.ProxyPacket import com.github.kr328.clash.model.ListProxyGroup -class ProxyGroupAdapter(private val context: Context) : RecyclerView.Adapter() { +class ProxyGroupAdapter(private val context: Context) : + RecyclerView.Adapter() { var data: List = emptyList() class Holder(view: View) : RecyclerView.ViewHolder(view) { val name: TextView = view.findViewById(R.id.adapter_proxy_group_name) val list: RecyclerView = view.findViewById(R.id.adapter_proxy_group_list) - val expend: View = view.findViewById(R.id.adapter_proxy_group_expand) + val test: View = view.findViewById(R.id.adapter_proxy_group_url_test) } override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): Holder { - return Holder(LayoutInflater.from(context) - .inflate(R.layout.adapter_proxy_group, parent, false)).apply { - list.layoutManager = object: GridLayoutManager(context, 2) { + return Holder( + LayoutInflater.from(context) + .inflate(R.layout.adapter_proxy_group, parent, false) + ).apply { + list.layoutManager = object : GridLayoutManager(context, 2) { override fun canScrollHorizontally(): Boolean = false override fun canScrollVertically(): Boolean = false } @@ -38,19 +41,16 @@ class ProxyGroupAdapter(private val context: Context) : RecyclerView.Adapter, var now: ListProxy?, var hide: Boolean = false) { +import com.github.kr328.clash.core.model.ProxyPacket + +data class ListProxyGroup( + val name: String, + val type: ProxyPacket.Type, + val proxies: List, + var now: ListProxy? +) { data class ListProxy(val name: String, val type: String, val delay: Long) } \ No newline at end of file diff --git a/app/src/main/res/layout/activity_proxies.xml b/app/src/main/res/layout/activity_proxies.xml index f0eb146..f9dc8cb 100644 --- a/app/src/main/res/layout/activity_proxies.xml +++ b/app/src/main/res/layout/activity_proxies.xml @@ -15,8 +15,13 @@ android:layout_height="60dp" /> - + android:layout_height="wrap_content"> + + \ No newline at end of file diff --git a/app/src/main/res/layout/adapter_proxy.xml b/app/src/main/res/layout/adapter_proxy.xml index de1bf4e..6c5fcb7 100644 --- a/app/src/main/res/layout/adapter_proxy.xml +++ b/app/src/main/res/layout/adapter_proxy.xml @@ -8,6 +8,7 @@ android:id="@+id/adapter_proxy_card" android:layout_width="match_parent" android:layout_height="wrap_content" + android:foreground="?attr/selectableItemBackground" app:cardUseCompatPadding="true" app:cardElevation="2dp" app:cardCornerRadius="5dp"> @@ -19,10 +20,11 @@ + android:layout_height="wrap_content" + android:layout_toStartOf="@id/adapter_proxy_delay" + android:layout_alignParentStart="true"/> + android:layout_height="wrap_content" /> diff --git a/app/src/main/res/layout/adapter_proxy_group.xml b/app/src/main/res/layout/adapter_proxy_group.xml index 55fd4ce..e7c562f 100644 --- a/app/src/main/res/layout/adapter_proxy_group.xml +++ b/app/src/main/res/layout/adapter_proxy_group.xml @@ -1,12 +1,12 @@ + android:paddingStart="15dp" + android:paddingEnd="15dp" + android:paddingTop="15dp"> - - + android:background="@drawable/ic_url_test" /> val data = input.readString() - Log.d(TAG, data) - Json(JsonConfiguration.Stable.copy(strictMode = false)) .parse(RawProxyPacket.serializer(), data) } } - fun pullTrafficEvent(initial: (LocalSocket) -> Unit, callback: (TrafficEvent) -> Unit) { - runControlNoException(COMMAND_PULL_TRAFFIC) { socket, input, _ -> + fun pullSpeedEvent(initial: (LocalSocket) -> Unit, callback: (SpeedEvent) -> Unit) { + runControlNoException(COMMAND_PULL_SPEED) { socket, input, _ -> initial(socket) while (!Thread.currentThread().isInterrupted) { callback( Json(JsonConfiguration.Stable).parse( - TrafficEvent.serializer(), + SpeedEvent.serializer(), input.readString() ) ) @@ -97,17 +96,14 @@ class Clash( } } - fun pullProxyChangedEvent( - initial: (LocalSocket) -> Unit, - callback: (ProxyChangedEvent) -> Unit - ) { - runControlNoException(COMMAND_PULL_PROXY_CHANGED) { socket, input, _ -> + fun pullBandwidthEvent(initial: (LocalSocket) -> Unit, callback: (BandwidthEvent) -> Unit) { + runControlNoException(COMMAND_PULL_BANDWIDTH) { socket, input, _ -> initial(socket) while (!Thread.currentThread().isInterrupted) { callback( Json(JsonConfiguration.Stable).parse( - ProxyChangedEvent.serializer(), + BandwidthEvent.serializer(), input.readString() ) ) 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/BandwidthEvent.kt similarity index 63% rename from core/src/main/java/com/github/kr328/clash/core/event/TrafficEvent.kt rename to core/src/main/java/com/github/kr328/clash/core/event/BandwidthEvent.kt index 02bc27b..62560ce 100644 --- a/core/src/main/java/com/github/kr328/clash/core/event/TrafficEvent.kt +++ b/core/src/main/java/com/github/kr328/clash/core/event/BandwidthEvent.kt @@ -6,7 +6,7 @@ import com.github.kr328.clash.core.serialization.Parcels import kotlinx.serialization.Serializable @Serializable -data class TrafficEvent(val down: Long, val up: Long, val total: Long) : Event, Parcelable { +data class BandwidthEvent(val total: Long): Parcelable { override fun writeToParcel(parcel: Parcel, flags: Int) { Parcels.dump(serializer(), this, parcel) } @@ -15,13 +15,14 @@ data class TrafficEvent(val down: Long, val up: Long, val total: Long) : Event, return 0 } - companion object CREATOR : Parcelable.Creator { - override fun createFromParcel(parcel: Parcel): TrafficEvent { + companion object CREATOR : Parcelable.Creator { + override fun createFromParcel(parcel: Parcel): BandwidthEvent { return Parcels.load(serializer(), parcel) } - override fun newArray(size: Int): Array { + override fun newArray(size: Int): Array { return arrayOfNulls(size) } } + } \ No newline at end of file diff --git a/core/src/main/java/com/github/kr328/clash/core/event/Event.kt b/core/src/main/java/com/github/kr328/clash/core/event/Event.kt index 3a9c769..fb30b9c 100644 --- a/core/src/main/java/com/github/kr328/clash/core/event/Event.kt +++ b/core/src/main/java/com/github/kr328/clash/core/event/Event.kt @@ -2,9 +2,8 @@ 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 + const val EVENT_SPEED = 3 + const val EVENT_BANDWIDTH = 4 } } \ No newline at end of file diff --git a/core/src/main/java/com/github/kr328/clash/core/event/SpeedEvent.kt b/core/src/main/java/com/github/kr328/clash/core/event/SpeedEvent.kt new file mode 100644 index 0000000..ae0834d --- /dev/null +++ b/core/src/main/java/com/github/kr328/clash/core/event/SpeedEvent.kt @@ -0,0 +1,27 @@ +package com.github.kr328.clash.core.event + +import android.os.Parcel +import android.os.Parcelable +import com.github.kr328.clash.core.serialization.Parcels +import kotlinx.serialization.Serializable + +@Serializable +data class SpeedEvent(val down: Long, val up: Long) : Event, Parcelable { + override fun writeToParcel(parcel: Parcel, flags: Int) { + Parcels.dump(serializer(), this, parcel) + } + + override fun describeContents(): Int { + return 0 + } + + companion object CREATOR : Parcelable.Creator { + override fun createFromParcel(parcel: Parcel): SpeedEvent { + return Parcels.load(serializer(), parcel) + } + + override fun newArray(size: Int): Array { + return arrayOfNulls(size) + } + } +} \ No newline at end of file diff --git a/core/src/main/java/com/github/kr328/clash/core/model/ProxyPacket.kt b/core/src/main/java/com/github/kr328/clash/core/model/ProxyPacket.kt index 703754d..c9fdb2c 100644 --- a/core/src/main/java/com/github/kr328/clash/core/model/ProxyPacket.kt +++ b/core/src/main/java/com/github/kr328/clash/core/model/ProxyPacket.kt @@ -6,7 +6,7 @@ import com.github.kr328.clash.core.serialization.Parcels import kotlinx.serialization.Serializable @Serializable -data class ProxyPacket(val proxies: Map): Parcelable { +data class ProxyPacket(val mode: String, val proxies: Map): Parcelable { @Serializable data class Proxy(val name: String, val type: Type, val now: Int, val all: Set, val delay: Long) @@ -84,7 +84,7 @@ data class ProxyPacket(val proxies: Map): Parcelable { entry.value.first to Proxy(entry.key, type, now, all, delay) } - return ProxyPacket(proxies.toMap()) + return ProxyPacket(rawProxy.mode, proxies.toMap()) } @JvmField diff --git a/core/src/main/java/com/github/kr328/clash/core/model/RawProxyPacket.kt b/core/src/main/java/com/github/kr328/clash/core/model/RawProxyPacket.kt index ab0daa6..ac581ca 100644 --- a/core/src/main/java/com/github/kr328/clash/core/model/RawProxyPacket.kt +++ b/core/src/main/java/com/github/kr328/clash/core/model/RawProxyPacket.kt @@ -3,7 +3,7 @@ package com.github.kr328.clash.core.model import kotlinx.serialization.Serializable @Serializable -data class RawProxyPacket(val proxies: Map) { +data class RawProxyPacket(val mode: String, val proxies: Map) { @Serializable data class RawProxy(val type: String, val all: List = emptyList(), diff --git a/core/src/main/java/com/github/kr328/clash/core/utils/ByteFormatter.kt b/core/src/main/java/com/github/kr328/clash/core/utils/ByteFormatter.kt index ab2dc2e..ec4879f 100644 --- a/core/src/main/java/com/github/kr328/clash/core/utils/ByteFormatter.kt +++ b/core/src/main/java/com/github/kr328/clash/core/utils/ByteFormatter.kt @@ -10,7 +10,7 @@ object ByteFormatter { bytes > 1024 -> String.format("%.2f KB", (bytes.toDouble() / 1024)) else -> - "$bytes Byte" + "$bytes Bytes" } } diff --git a/service/src/main/aidl/com/github/kr328/clash/core/event/Event.aidl b/service/src/main/aidl/com/github/kr328/clash/core/event/Event.aidl index 094c402..f127ee9 100644 --- a/service/src/main/aidl/com/github/kr328/clash/core/event/Event.aidl +++ b/service/src/main/aidl/com/github/kr328/clash/core/event/Event.aidl @@ -2,7 +2,7 @@ package com.github.kr328.clash.core.event; parcelable ProcessEvent; parcelable LogEvent; -parcelable ProxyChangedEvent; -parcelable TrafficEvent; +parcelable SpeedEvent; +parcelable BandwidthEvent; parcelable ErrorEvent; -parcelable ProfileChangedEvent; \ No newline at end of file +parcelable ProfileChangedEvent; diff --git a/service/src/main/aidl/com/github/kr328/clash/service/IClashEventObserver.aidl b/service/src/main/aidl/com/github/kr328/clash/service/IClashEventObserver.aidl index 3347882..0c86848 100644 --- a/service/src/main/aidl/com/github/kr328/clash/service/IClashEventObserver.aidl +++ b/service/src/main/aidl/com/github/kr328/clash/service/IClashEventObserver.aidl @@ -4,9 +4,9 @@ import com.github.kr328.clash.core.event.Event; interface IClashEventObserver { void onProcessEvent(in ProcessEvent event); - void onProxyChangedEvent(in ProxyChangedEvent event); void onLogEvent(in LogEvent event); void onErrorEvent(in ErrorEvent event); - void onTrafficEvent(in TrafficEvent event); + void onSpeedEvent(in SpeedEvent event); + void onBandwidthEvent(in BandwidthEvent event); void onProfileChanged(in ProfileChangedEvent event); } diff --git a/service/src/main/java/com/github/kr328/clash/service/ClashEventPuller.kt b/service/src/main/java/com/github/kr328/clash/service/ClashEventPuller.kt index 3a0bcef..21d02bf 100644 --- a/service/src/main/java/com/github/kr328/clash/service/ClashEventPuller.kt +++ b/service/src/main/java/com/github/kr328/clash/service/ClashEventPuller.kt @@ -2,22 +2,22 @@ package com.github.kr328.clash.service import android.net.LocalSocket import com.github.kr328.clash.core.Clash +import com.github.kr328.clash.core.event.BandwidthEvent import com.github.kr328.clash.core.event.LogEvent -import com.github.kr328.clash.core.event.ProxyChangedEvent -import com.github.kr328.clash.core.event.TrafficEvent +import com.github.kr328.clash.core.event.SpeedEvent import com.github.kr328.clash.core.utils.Log import kotlin.concurrent.thread class ClashEventPuller(private val clash: Clash, private val master: Master) { interface Master { fun onLogPulled(event: LogEvent) - fun onTrafficPulled(event: TrafficEvent) - fun onProxyChangedPulled(event: ProxyChangedEvent) + fun onSpeedPulled(event: SpeedEvent) + fun onBandwidthPulled(event: BandwidthEvent) } private var logSocket: LocalSocket? = null - private var trafficSocket: LocalSocket? = null - private var proxyChangedSocket: LocalSocket? = null + private var speedSocket: LocalSocket? = null + private var bandwidthSocket: LocalSocket? = null fun startLogPuller() { synchronized(LogEvent::class) { @@ -47,45 +47,81 @@ class ClashEventPuller(private val clash: Clash, private val master: Master) { } } + fun startSpeedPull() { + synchronized(SpeedEvent::class) { + if (speedSocket != null) + return + } + + thread { + clash.pullSpeedEvent({ + synchronized(SpeedEvent::class) { + if (speedSocket != null) { + it.close() + return@pullSpeedEvent + } else { + speedSocket = it + Log.i("Speed puller started") + } + + } + }) { + master.onSpeedPulled(it) + } + + synchronized(SpeedEvent::class) { + Log.i("Speed puller stopped") + + speedSocket = null + } + } + } + + fun startBandwidthPull() { + synchronized(BandwidthEvent::class) { + if (bandwidthSocket != null) + return + } + + thread { + clash.pullBandwidthEvent({ + synchronized(BandwidthEvent::class) { + if (bandwidthSocket != null) { + it.close() + return@pullBandwidthEvent + } else { + bandwidthSocket = it + Log.i("Bandwidth puller started") + } + + } + }) { + master.onBandwidthPulled(it) + } + + synchronized(BandwidthEvent::class) { + Log.i("Bandwidth puller stopped") + + bandwidthSocket = null + } + } + } + fun stopLogPull() { synchronized(LogEvent::class) { logSocket.closeSilent() } } - fun startTrafficPull() { - synchronized(TrafficEvent::class) { - if (trafficSocket != null) - return - } - - thread { - clash.pullTrafficEvent({ - synchronized(TrafficEvent::class) { - if (trafficSocket != null) { - it.close() - return@pullTrafficEvent - } else { - trafficSocket = it - Log.i("Traffic puller started") - } - - } - }) { - master.onTrafficPulled(it) - } - - synchronized(TrafficEvent::class) { - Log.i("Traffic puller stopped") - - trafficSocket = null - } + fun stopSpeedPull() { + synchronized(SpeedEvent::class) { + speedSocket.closeSilent() } } - fun stopTrafficPull() { - synchronized(TrafficEvent::class) { - trafficSocket.closeSilent() + fun stopBandwidthPull() { + synchronized(BandwidthEvent::class) { + bandwidthSocket.closeSilent() } } diff --git a/service/src/main/java/com/github/kr328/clash/service/ClashEventService.kt b/service/src/main/java/com/github/kr328/clash/service/ClashEventService.kt index 1ae96d4..2bad13c 100644 --- a/service/src/main/java/com/github/kr328/clash/service/ClashEventService.kt +++ b/service/src/main/java/com/github/kr328/clash/service/ClashEventService.kt @@ -11,7 +11,7 @@ class ClashEventService(private val master: Master) : IClashEventService.Stub() companion object { private val EVENT_SET = - setOf(Event.EVENT_LOG, Event.EVENT_TRAFFIC, Event.EVENT_PROXY_CHANGED) + setOf(Event.EVENT_LOG, Event.EVENT_SPEED, Event.EVENT_BANDWIDTH) } private data class EventObserverRecord( @@ -23,7 +23,7 @@ class ClashEventService(private val master: Master) : IClashEventService.Stub() private val handler = Executors.newSingleThreadExecutor() private var currentProcessEvent = ProcessEvent.STOPPED - private var currentTrafficEvent = TrafficEvent(0, 0, 0) + private var currentBandwidthEvent = BandwidthEvent(0) fun preformProcessEvent(event: ProcessEvent) { handler.submit { @@ -44,22 +44,22 @@ class ClashEventService(private val master: Master) : IClashEventService.Stub() } } - fun preformProxyChangedEvent(event: ProxyChangedEvent) { + fun preformSpeedEvent(event: SpeedEvent) { handler.submit { observers.values.forEach { - if (it.acquiredEvent.contains(Event.EVENT_PROXY_CHANGED)) - it.observer.onProxyChangedEvent(event) + if (it.acquiredEvent.contains(Event.EVENT_SPEED)) + it.observer.onSpeedEvent(event) } } } - fun preformTrafficEvent(event: TrafficEvent) { + fun preformBandwidthEvent(event: BandwidthEvent) { handler.submit { - currentTrafficEvent = event + currentBandwidthEvent = event observers.values.forEach { - if (it.acquiredEvent.contains(Event.EVENT_TRAFFIC)) - it.observer.onTrafficEvent(event) + if ( it.acquiredEvent.contains(Event.EVENT_BANDWIDTH) ) + it.observer.onBandwidthEvent(event) } } } @@ -111,8 +111,8 @@ class ClashEventService(private val master: Master) : IClashEventService.Stub() if (initial) { observer.onProcessEvent(currentProcessEvent) - if (events.contains(Event.EVENT_TRAFFIC)) - observer.onTrafficEvent(currentTrafficEvent) + if (events.contains(Event.EVENT_BANDWIDTH)) + observer.onBandwidthEvent(currentBandwidthEvent) } } } diff --git a/service/src/main/java/com/github/kr328/clash/service/ClashProfileService.kt b/service/src/main/java/com/github/kr328/clash/service/ClashProfileService.kt index 380e9b4..b5b9cf0 100644 --- a/service/src/main/java/com/github/kr328/clash/service/ClashProfileService.kt +++ b/service/src/main/java/com/github/kr328/clash/service/ClashProfileService.kt @@ -10,7 +10,7 @@ class ClashProfileService(context: Context, private val master: Master) : fun preformProfileChanged() } - val database by lazy { + private val database by lazy { ClashDatabase.getInstance(context).openClashProfileDao() } 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 b72072f..067d56d 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 @@ -12,7 +12,6 @@ import android.os.ParcelFileDescriptor import com.github.kr328.clash.core.Clash import com.github.kr328.clash.core.event.* import com.github.kr328.clash.core.model.ProxyPacket -import com.github.kr328.clash.core.model.RawProxyPacket import com.github.kr328.clash.core.utils.Log import com.github.kr328.clash.service.data.ClashDatabase import java.io.File @@ -45,7 +44,7 @@ class ClashService : Service(), IClashEventObserver, ClashEventService.Master, this@ClashService.eventService.preformErrorEvent( ErrorEvent(ErrorEvent.Type.QUERY_PROXY_FAILURE, e.toString()) ) - ProxyPacket(emptyMap()) + ProxyPacket("Unknown", emptyMap()) } } @@ -100,7 +99,7 @@ class ClashService : Service(), IClashEventObserver, ClashEventService.Master, eventService.registerEventObserver( ClashService::class.java.name, this@ClashService, - intArrayOf(Event.EVENT_TRAFFIC) + intArrayOf(Event.EVENT_SPEED) ) Intent.ACTION_SCREEN_OFF -> eventService.registerEventObserver( @@ -117,10 +116,12 @@ class ClashService : Service(), IClashEventObserver, ClashEventService.Master, return when (event) { - Event.EVENT_TRAFFIC -> - puller.startTrafficPull() + Event.EVENT_SPEED -> + puller.startSpeedPull() Event.EVENT_LOG -> puller.startLogPuller() + Event.EVENT_BANDWIDTH -> + puller.startBandwidthPull() } } @@ -129,10 +130,12 @@ class ClashService : Service(), IClashEventObserver, ClashEventService.Master, return when (event) { - Event.EVENT_TRAFFIC -> - puller.stopTrafficPull() + Event.EVENT_SPEED -> + puller.stopSpeedPull() Event.EVENT_LOG -> puller.stopLogPull() + Event.EVENT_BANDWIDTH -> + puller.stopBandwidthPull() } } @@ -155,7 +158,7 @@ class ClashService : Service(), IClashEventObserver, ClashEventService.Master, eventService.registerEventObserver( ClashService::class.java.name, this@ClashService, - intArrayOf(Event.EVENT_TRAFFIC) + intArrayOf(Event.EVENT_SPEED) ) registerReceiver(screenReceiver, IntentFilter().apply { @@ -201,7 +204,8 @@ class ClashService : Service(), IClashEventObserver, ClashEventService.Master, eventService.recastEventRequirement() } ProcessEvent.STOPPED -> { - eventService.preformTrafficEvent(TrafficEvent(0, 0, 0)) + eventService.preformSpeedEvent(SpeedEvent(0, 0)) + eventService.preformBandwidthEvent(BandwidthEvent(0)) notification.cancel() } @@ -230,7 +234,7 @@ class ClashService : Service(), IClashEventObserver, ClashEventService.Master, } } - override fun onTrafficEvent(event: TrafficEvent?) { + override fun onSpeedEvent(event: SpeedEvent?) { notification.setSpeed(event?.up ?: 0, event?.down ?: 0) } @@ -242,15 +246,15 @@ class ClashService : Service(), IClashEventObserver, ClashEventService.Master, eventService.preformLogEvent(event) } - override fun onTrafficPulled(event: TrafficEvent) { - eventService.preformTrafficEvent(event) + override fun onSpeedPulled(event: SpeedEvent) { + eventService.preformSpeedEvent(event) } - override fun onProxyChangedPulled(event: ProxyChangedEvent) { - eventService.preformProxyChangedEvent(event) + override fun onBandwidthPulled(event: BandwidthEvent) { + eventService.preformBandwidthEvent(event) } - override fun onProxyChangedEvent(event: ProxyChangedEvent?) {} + override fun onBandwidthEvent(event: BandwidthEvent?) {} override fun onLogEvent(event: LogEvent?) {} override fun onErrorEvent(event: ErrorEvent?) {} override fun asBinder(): IBinder = object : Binder() { 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 9b1f2f0..475dd70 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 @@ -142,11 +142,11 @@ class TunService : VpnService(), IClashEventObserver { return this } + override fun onSpeedEvent(event: SpeedEvent?) {} + override fun onBandwidthEvent(event: BandwidthEvent?) {} override fun onLogEvent(event: LogEvent?) {} override fun onErrorEvent(event: ErrorEvent?) {} override fun onProfileChanged(event: ProfileChangedEvent?) {} - override fun onProxyChangedEvent(event: ProxyChangedEvent?) {} - override fun onTrafficEvent(event: TrafficEvent?) {} override fun asBinder(): IBinder = object : Binder() { override fun queryLocalInterface(descriptor: String): IInterface? { return this@TunService