fix typo & add profile reload event
This commit is contained in:
19 files changed
+359
-55
No files matched your search
@@ -118,6 +118,10 @@ abstract class BaseActivity : AppCompatActivity(), IClashEventObserver {
|
||||
|
||||
override fun onProfileChanged(event: ProfileChangedEvent?) =
|
||||
this@BaseActivity.onProfileChanged(event)
|
||||
|
||||
override fun onProfileReloaded(event: ProfileReloadEvent?) {
|
||||
this@BaseActivity.onProfileReloaded(event)
|
||||
}
|
||||
}
|
||||
|
||||
override fun onLogEvent(event: LogEvent?) {}
|
||||
@@ -126,6 +130,7 @@ abstract class BaseActivity : AppCompatActivity(), IClashEventObserver {
|
||||
override fun onProcessEvent(event: ProcessEvent?) {}
|
||||
override fun onSpeedEvent(event: SpeedEvent?) {}
|
||||
override fun onBandwidthEvent(event: BandwidthEvent?) {}
|
||||
override fun onProfileReloaded(event: ProfileReloadEvent?) {}
|
||||
override fun asBinder(): IBinder {
|
||||
return observerBinder
|
||||
}
|
||||
|
||||
@@ -5,6 +5,9 @@ import android.os.Bundle
|
||||
import androidx.recyclerview.widget.LinearLayoutManager
|
||||
import com.github.kr328.clash.adapter.FormAdapter
|
||||
import kotlinx.android.synthetic.main.activity_import_url.*
|
||||
import java.lang.Exception
|
||||
import java.net.HttpURLConnection
|
||||
import java.net.URL
|
||||
|
||||
class ImportUrlActivity : BaseActivity() {
|
||||
private val elements = listOf<FormAdapter.Type>(
|
||||
@@ -40,4 +43,22 @@ class ImportUrlActivity : BaseActivity() {
|
||||
|
||||
super.onActivityResult(requestCode, resultCode, data)
|
||||
}
|
||||
|
||||
private fun checkAndInsert() {
|
||||
try {
|
||||
val name = elements[0] as FormAdapter.TextType
|
||||
val url = elements[1] as FormAdapter.TextType
|
||||
|
||||
if ( name.content.isEmpty() ) {
|
||||
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
URL(url.content).openConnection()
|
||||
}
|
||||
catch (e: Exception) {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -7,10 +7,8 @@ import android.net.VpnService
|
||||
import android.os.Build
|
||||
import android.os.Bundle
|
||||
import android.view.View
|
||||
import com.github.kr328.clash.core.event.BandwidthEvent
|
||||
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.*
|
||||
import com.github.kr328.clash.core.model.GeneralPacket
|
||||
import com.github.kr328.clash.core.utils.ByteFormatter
|
||||
import com.github.kr328.clash.service.ClashService
|
||||
import com.github.kr328.clash.service.TunService
|
||||
@@ -129,6 +127,26 @@ class MainActivity : BaseActivity() {
|
||||
}
|
||||
}
|
||||
|
||||
override fun onProfileReloaded(event: ProfileReloadEvent?) {
|
||||
runClash {
|
||||
val general = it.queryGeneral()
|
||||
|
||||
runOnUiThread {
|
||||
when ( general.mode ) {
|
||||
GeneralPacket.Mode.DIRECT ->
|
||||
activity_main_clash_proxies_summary.text =
|
||||
getText(R.string.clash_proxy_manage_summary_direct)
|
||||
GeneralPacket.Mode.GLOBAL ->
|
||||
activity_main_clash_proxies_summary.text =
|
||||
getText(R.string.clash_proxy_manage_summary_global)
|
||||
GeneralPacket.Mode.RULE ->
|
||||
activity_main_clash_proxies_summary.text =
|
||||
getText(R.string.clash_proxy_manage_summary_rule)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun onBandwidthEvent(event: BandwidthEvent?) {
|
||||
runOnUiThread {
|
||||
if (lastEvent == ProcessEvent.STARTED) {
|
||||
@@ -156,7 +174,8 @@ class MainActivity : BaseActivity() {
|
||||
)
|
||||
}
|
||||
|
||||
loadActiveProfile()
|
||||
onProfileReloaded(ProfileReloadEvent())
|
||||
onProfileChanged(ProfileChangedEvent())
|
||||
}
|
||||
|
||||
override fun onStop() {
|
||||
@@ -184,13 +203,9 @@ class MainActivity : BaseActivity() {
|
||||
|
||||
runOnUiThread {
|
||||
if (profile != null) {
|
||||
activity_main_clash_proxies_summary.text =
|
||||
getString(R.string.clash_proxy_manage_summary, profile.proxies)
|
||||
activity_main_clash_profiles_summary.text =
|
||||
getString(R.string.clash_profiles_summary_selected, profile.name)
|
||||
} else {
|
||||
activity_main_clash_proxies_summary.text =
|
||||
getString(R.string.clash_proxy_manage_summary, 0)
|
||||
activity_main_clash_profiles_summary.text =
|
||||
getString(R.string.clash_profiles_summary_unselected)
|
||||
}
|
||||
|
||||
@@ -40,7 +40,7 @@
|
||||
|
||||
<TextView
|
||||
android:id="@+id/activity_main_clash_proxies_summary"
|
||||
android:text="@string/clash_proxy_manage_summary"
|
||||
android:text="@string/clash_proxy_manage_summary_direct"
|
||||
android:textColor="@android:color/darker_gray"
|
||||
android:textAppearance="@style/TextAppearance.AppCompat.Small"
|
||||
android:layout_marginTop="2dp"
|
||||
|
||||
@@ -7,7 +7,9 @@
|
||||
<string name="clash_status_forwarded_traffic">%s Forwarded</string>
|
||||
|
||||
<string name="clash_proxy_manage">Proxy</string>
|
||||
<string name="clash_proxy_manage_summary">%d Items</string>
|
||||
<string name="clash_proxy_manage_summary_rule">Rule Mode</string>
|
||||
<string name="clash_proxy_manage_summary_direct">Direct Mode</string>
|
||||
<string name="clash_proxy_manage_summary_global">Global Mode</string>
|
||||
|
||||
<string name="clash_profiles">Profiles</string>
|
||||
<string name="clash_profiles_summary_selected">%s Activated</string>
|
||||
|
||||
@@ -10,8 +10,11 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/eapache/queue v1.1.0 h1:YOEu7KNc61ntiQlcEeUIoDTJ2o8mQznoNvUhiigpIqc=
|
||||
github.com/eapache/queue v1.1.0/go.mod h1:6eCeP0CKFpHLu8blIFXhExK/dRa7WDZfr6jVFPTqq+I=
|
||||
github.com/go-chi/chi v4.0.2+incompatible h1:maB6vn6FqCxrpz4FqWdh4+lwpyZIQS7YEAUcHlgXVRs=
|
||||
github.com/go-chi/chi v4.0.2+incompatible/go.mod h1:eB3wogJHnLi3x/kFX2A+IbTBlXxmMeXJVKy9tTv1XzQ=
|
||||
github.com/go-chi/cors v1.0.0 h1:e6x8k7uWbUwYs+aXDoiUzeQFT6l0cygBYyNhD7/1Tg0=
|
||||
github.com/go-chi/cors v1.0.0/go.mod h1:K2Yje0VW/SJzxiyMYu6iPQYa7hMjQX2i/F491VChg1I=
|
||||
github.com/go-chi/render v1.0.1 h1:4/5tis2cKaNdnv9zFLfXzcquC9HbeZgCnxGnKrltBS8=
|
||||
github.com/go-chi/render v1.0.1/go.mod h1:pq4Rr7HbnsdaeHagklXub+p6Wd16Af5l9koip1OvJns=
|
||||
github.com/gofrs/uuid v3.2.0+incompatible h1:y12jRkkFxsd7GpqdSZ+/KCs/fJbqpEXSGd4+jfEaewE=
|
||||
github.com/gofrs/uuid v3.2.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM=
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"net"
|
||||
"time"
|
||||
|
||||
"github.com/Dreamacro/clash/log"
|
||||
"github.com/Dreamacro/clash/tunnel"
|
||||
)
|
||||
|
||||
@@ -46,7 +47,9 @@ func handlePullTrafficEvent(client *net.UnixConn) {
|
||||
Packet.Up = up
|
||||
Packet.Down = down
|
||||
|
||||
json.NewEncoder(buf).Encode(&Packet)
|
||||
if json.NewEncoder(buf).Encode(&Packet) != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if writeCommandPacket(client, buf.Bytes()) != nil {
|
||||
return
|
||||
@@ -67,7 +70,6 @@ func handlePullBandwidthEvent(client *net.UnixConn) {
|
||||
defer ticker.Stop()
|
||||
|
||||
go func() {
|
||||
|
||||
for {
|
||||
var buf [4]byte
|
||||
|
||||
@@ -80,24 +82,93 @@ func handlePullBandwidthEvent(client *net.UnixConn) {
|
||||
}
|
||||
}()
|
||||
|
||||
tick := func() error {
|
||||
buf.Reset()
|
||||
|
||||
var Packet struct {
|
||||
Total int64 `json:"total"`
|
||||
}
|
||||
|
||||
sp := mgr.Snapshot()
|
||||
|
||||
Packet.Total = sp.DownloadTotal + sp.UploadTotal
|
||||
|
||||
json.NewEncoder(buf).Encode(&Packet)
|
||||
|
||||
return writeCommandPacket(client, buf.Bytes())
|
||||
}
|
||||
|
||||
if tick() != nil {
|
||||
return
|
||||
}
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-ticker.C:
|
||||
buf.Reset()
|
||||
if tick() != nil {
|
||||
return
|
||||
}
|
||||
case <-bandWidthExit:
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var Packet struct {
|
||||
Total int64 `json:"total"`
|
||||
func handlePullLogEvent(client *net.UnixConn) {
|
||||
logExit := make(chan int)
|
||||
|
||||
subseribe := log.Subscribe()
|
||||
buf := &bytes.Buffer{}
|
||||
|
||||
go func() {
|
||||
var buf [4]byte
|
||||
|
||||
for {
|
||||
_, err := client.Read(buf[:])
|
||||
|
||||
if err != nil {
|
||||
close(logExit)
|
||||
return
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
for {
|
||||
select {
|
||||
case elm := <-subseribe:
|
||||
buf.Reset()
|
||||
msg := elm.(*log.Event)
|
||||
|
||||
var payload struct {
|
||||
Level int `json:"level"`
|
||||
Messgae string `json:"message"`
|
||||
}
|
||||
|
||||
sp := mgr.Snapshot()
|
||||
switch msg.LogLevel {
|
||||
case log.DEBUG:
|
||||
payload.Level = 1
|
||||
break
|
||||
case log.INFO:
|
||||
payload.Level = 2
|
||||
break
|
||||
case log.WARNING:
|
||||
payload.Level = 3
|
||||
break
|
||||
case log.ERROR:
|
||||
payload.Level = 4
|
||||
}
|
||||
|
||||
Packet.Total = sp.DownloadTotal + sp.UploadTotal
|
||||
payload.Messgae = msg.Payload
|
||||
|
||||
json.NewEncoder(buf).Encode(&Packet)
|
||||
if err := json.NewEncoder(buf).Encode(&payload); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if writeCommandPacket(client, buf.Bytes()) != nil {
|
||||
return
|
||||
}
|
||||
case <-logExit:
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3,13 +3,65 @@ package server
|
||||
import (
|
||||
"encoding/json"
|
||||
"net"
|
||||
"strconv"
|
||||
|
||||
"github.com/Dreamacro/clash/log"
|
||||
"github.com/Dreamacro/clash/proxy"
|
||||
"github.com/Dreamacro/clash/tunnel"
|
||||
|
||||
A "github.com/Dreamacro/clash/adapters/outbound"
|
||||
)
|
||||
|
||||
const (
|
||||
modeDirect = 1
|
||||
modeGlobal = 2
|
||||
modeRule = 3
|
||||
)
|
||||
|
||||
func handleQueryGeneral(client *net.UnixConn) {
|
||||
var payload struct {
|
||||
Ports struct {
|
||||
Http int `json:"http"`
|
||||
Socks int `json:"socks"`
|
||||
Redirect int `json:"redirect"`
|
||||
RandomHttpPort int `json:"randomHttp"`
|
||||
} `json:"ports"`
|
||||
Mode int `json:"mode"`
|
||||
}
|
||||
|
||||
mode := tunnel.Instance().Mode()
|
||||
ports := proxy.GetPorts()
|
||||
|
||||
payload.Ports.Http = ports.Port
|
||||
payload.Ports.Socks = ports.SocksPort
|
||||
payload.Ports.Redirect = ports.RedirPort
|
||||
|
||||
if httpListener != nil {
|
||||
addr := httpListener.Addr().String()
|
||||
|
||||
_, port, _ := net.SplitHostPort(addr)
|
||||
p, _ := strconv.Atoi(port)
|
||||
|
||||
payload.Ports.RandomHttpPort = p
|
||||
}
|
||||
|
||||
switch mode {
|
||||
case tunnel.Direct:
|
||||
payload.Mode = modeDirect
|
||||
break
|
||||
case tunnel.Global:
|
||||
payload.Mode = modeGlobal
|
||||
break
|
||||
case tunnel.Rule:
|
||||
payload.Mode = modeRule
|
||||
break
|
||||
}
|
||||
|
||||
buf, _ := json.Marshal(&payload)
|
||||
|
||||
writeCommandPacket(client, buf)
|
||||
}
|
||||
|
||||
func handleQueryProxies(client *net.UnixConn) {
|
||||
proxies := tunnel.Instance().Proxies()
|
||||
|
||||
|
||||
@@ -18,6 +18,7 @@ const (
|
||||
commandPullLog uint32 = 7
|
||||
commandPullBandwidth uint32 = 8
|
||||
commandSetProxy uint32 = 9
|
||||
commandQueryGeneral uint32 = 10
|
||||
)
|
||||
|
||||
var handlers map[uint32]func(*net.UnixConn) = make(map[uint32]func(*net.UnixConn))
|
||||
@@ -30,13 +31,16 @@ func init() {
|
||||
handlers[commandProfileReload] = handleProfileReload // profile.go
|
||||
handlers[commandQueryProxies] = handleQueryProxies // proxies.go
|
||||
handlers[commandPullTraffic] = handlePullTrafficEvent // event.go
|
||||
handlers[commandPullLog] = nil // event.go
|
||||
handlers[commandPullLog] = handlePullLogEvent // event.go
|
||||
handlers[commandPullBandwidth] = handlePullBandwidthEvent // event.go
|
||||
handlers[commandSetProxy] = handleSetProxy // proxies.go
|
||||
handlers[commandQueryGeneral] = handleQueryGeneral // proxies.go
|
||||
}
|
||||
|
||||
// Start local control server
|
||||
func Start(path string) error {
|
||||
startRandomHttpPort()
|
||||
|
||||
address, _ := net.ResolveUnixAddr("unix", path)
|
||||
|
||||
listener, err := net.ListenUnix("unix", address)
|
||||
|
||||
@@ -6,10 +6,10 @@ import com.github.kr328.clash.core.event.BandwidthEvent
|
||||
import com.github.kr328.clash.core.event.LogEvent
|
||||
import com.github.kr328.clash.core.event.ProcessEvent
|
||||
import com.github.kr328.clash.core.event.SpeedEvent
|
||||
import com.github.kr328.clash.core.model.GeneralPacket
|
||||
import com.github.kr328.clash.core.model.LoadProfilePacket
|
||||
import com.github.kr328.clash.core.model.RawProxyPacket
|
||||
import com.github.kr328.clash.core.model.SetProxyPacket
|
||||
import com.github.kr328.clash.core.utils.Log
|
||||
import kotlinx.serialization.json.Json
|
||||
import kotlinx.serialization.json.JsonConfiguration
|
||||
import java.io.File
|
||||
@@ -32,6 +32,7 @@ class Clash(
|
||||
const val COMMAND_PULL_LOG = 7
|
||||
const val COMMAND_PULL_BANDWIDTH = 8
|
||||
const val COMMAND_SET_PROXY = 9
|
||||
const val COMMAND_QUERY_GENERAL = 10
|
||||
|
||||
const val PING_REPLY = 233
|
||||
}
|
||||
@@ -46,9 +47,13 @@ class Clash(
|
||||
|
||||
fun loadProfile(file: File, selected: Map<String, String>): List<String> {
|
||||
return runControl(COMMAND_PROFILE_RELOAD) { _, input, output ->
|
||||
output.writeString(Json(JsonConfiguration.Stable)
|
||||
.stringify(LoadProfilePacket.Request.serializer(),
|
||||
LoadProfilePacket.Request(file.absolutePath, selected)))
|
||||
output.writeString(
|
||||
Json(JsonConfiguration.Stable)
|
||||
.stringify(
|
||||
LoadProfilePacket.Request.serializer(),
|
||||
LoadProfilePacket.Request(file.absolutePath, selected)
|
||||
)
|
||||
)
|
||||
|
||||
val result = Json(JsonConfiguration.Stable)
|
||||
.parse(LoadProfilePacket.Response.serializer(), input.readString())
|
||||
@@ -61,6 +66,19 @@ class Clash(
|
||||
}
|
||||
}
|
||||
|
||||
fun queryGeneral(): GeneralPacket {
|
||||
return runCatching {
|
||||
runControl(COMMAND_QUERY_GENERAL) { _, input, _ ->
|
||||
Json(JsonConfiguration.Stable).parse(GeneralPacket.serializer(), input.readString())
|
||||
}
|
||||
}.getOrDefault(
|
||||
GeneralPacket(
|
||||
GeneralPacket.Ports(0, 0, 0, 0),
|
||||
GeneralPacket.Mode.DIRECT
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
fun queryProxies(): RawProxyPacket {
|
||||
return runControl(COMMAND_QUERY_PROXIES) { _, input, _ ->
|
||||
val data = input.readString()
|
||||
@@ -71,15 +89,21 @@ class Clash(
|
||||
}
|
||||
|
||||
fun setSelectProxy(key: String, value: String) {
|
||||
runControl(COMMAND_SET_PROXY) {_, input, output ->
|
||||
output.writeString(Json(JsonConfiguration.Stable)
|
||||
.stringify(SetProxyPacket.Request.serializer(),
|
||||
SetProxyPacket.Request(key, value)))
|
||||
runControl(COMMAND_SET_PROXY) { _, input, output ->
|
||||
output.writeString(
|
||||
Json(JsonConfiguration.Stable)
|
||||
.stringify(
|
||||
SetProxyPacket.Request.serializer(),
|
||||
SetProxyPacket.Request(key, value)
|
||||
)
|
||||
)
|
||||
|
||||
val response = Json(JsonConfiguration.Stable).parse(SetProxyPacket.Response.serializer(),
|
||||
input.readString())
|
||||
val response = Json(JsonConfiguration.Stable).parse(
|
||||
SetProxyPacket.Response.serializer(),
|
||||
input.readString()
|
||||
)
|
||||
|
||||
if ( response.error.isNotEmpty() )
|
||||
if (response.error.isNotEmpty())
|
||||
throw IOException(response.error)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
class ProfileReloadEvent : 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<ProfileReloadEvent> {
|
||||
override fun createFromParcel(parcel: Parcel): ProfileReloadEvent {
|
||||
return Parcels.load(serializer(), parcel)
|
||||
}
|
||||
|
||||
override fun newArray(size: Int): Array<ProfileReloadEvent?> {
|
||||
return arrayOfNulls(size)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
package com.github.kr328.clash.core.model
|
||||
|
||||
import android.os.Parcel
|
||||
import android.os.Parcelable
|
||||
import com.github.kr328.clash.core.serialization.Parcels
|
||||
import kotlinx.serialization.*
|
||||
import kotlinx.serialization.internal.StringDescriptor
|
||||
import java.lang.IllegalArgumentException
|
||||
|
||||
@Serializable
|
||||
data class GeneralPacket(val ports: Ports, val mode: Mode) : Parcelable {
|
||||
@Serializable(with = ModeSerializer::class)
|
||||
enum class Mode {
|
||||
DIRECT, GLOBAL, RULE
|
||||
}
|
||||
|
||||
@Serializable
|
||||
data class Ports(val http: Int, val socks: Int, val redirect: Int, val randomHttp: Int)
|
||||
|
||||
class ModeSerializer : KSerializer<Mode> {
|
||||
companion object {
|
||||
const val MODE_DIRECT = 1
|
||||
const val MODE_GLOBAL = 2
|
||||
const val MODE_RULE = 3
|
||||
}
|
||||
|
||||
override val descriptor: SerialDescriptor
|
||||
get() = StringDescriptor
|
||||
|
||||
override fun deserialize(decoder: Decoder): Mode {
|
||||
return when ( decoder.decodeInt() ) {
|
||||
MODE_DIRECT -> Mode.DIRECT
|
||||
MODE_GLOBAL -> Mode.GLOBAL
|
||||
MODE_RULE -> Mode.RULE
|
||||
else -> throw IllegalArgumentException("Invalid mode")
|
||||
}
|
||||
}
|
||||
|
||||
override fun serialize(encoder: Encoder, obj: Mode) {
|
||||
when ( obj ) {
|
||||
Mode.DIRECT -> encoder.encodeInt(MODE_DIRECT)
|
||||
Mode.GLOBAL -> encoder.encodeInt(MODE_GLOBAL)
|
||||
Mode.RULE -> encoder.encodeInt(MODE_RULE)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun writeToParcel(parcel: Parcel, flags: Int) {
|
||||
Parcels.dump(serializer(), this, parcel)
|
||||
}
|
||||
|
||||
override fun describeContents(): Int {
|
||||
return 0
|
||||
}
|
||||
|
||||
companion object CREATOR : Parcelable.Creator<GeneralPacket> {
|
||||
override fun createFromParcel(parcel: Parcel): GeneralPacket {
|
||||
return Parcels.load(serializer(), parcel)
|
||||
}
|
||||
|
||||
override fun newArray(size: Int): Array<GeneralPacket?> {
|
||||
return arrayOfNulls(size)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -6,3 +6,4 @@ parcelable SpeedEvent;
|
||||
parcelable BandwidthEvent;
|
||||
parcelable ErrorEvent;
|
||||
parcelable ProfileChangedEvent;
|
||||
parcelable ProfileReloadEvent;
|
||||
+1
@@ -1,3 +1,4 @@
|
||||
package com.github.kr328.clash.core.model;
|
||||
|
||||
parcelable ProxyPacket;
|
||||
parcelable GeneralPacket;
|
||||
@@ -9,4 +9,5 @@ interface IClashEventObserver {
|
||||
void onSpeedEvent(in SpeedEvent event);
|
||||
void onBandwidthEvent(in BandwidthEvent event);
|
||||
void onProfileChanged(in ProfileChangedEvent event);
|
||||
void onProfileReloaded(in ProfileReloadEvent event);
|
||||
}
|
||||
@@ -5,7 +5,7 @@ import com.github.kr328.clash.service.IClashEventService;
|
||||
import com.github.kr328.clash.service.IClashProfileService;
|
||||
import com.github.kr328.clash.service.IClashSettingService;
|
||||
import com.github.kr328.clash.core.event.Event;
|
||||
import com.github.kr328.clash.core.model.ProxyPacket;
|
||||
import com.github.kr328.clash.core.model.Packet;
|
||||
|
||||
interface IClashService {
|
||||
// Services
|
||||
@@ -25,4 +25,5 @@ interface IClashService {
|
||||
|
||||
// Query
|
||||
ProxyPacket queryAllProxies();
|
||||
GeneralPacket queryGeneral();
|
||||
}
|
||||
@@ -23,9 +23,8 @@ class ClashEventService(private val master: Master) : IClashEventService.Stub()
|
||||
private val handler = Executors.newSingleThreadExecutor()
|
||||
|
||||
private var currentProcessEvent = ProcessEvent.STOPPED
|
||||
private var currentBandwidthEvent = BandwidthEvent(0)
|
||||
|
||||
fun preformProcessEvent(event: ProcessEvent) {
|
||||
fun performProcessEvent(event: ProcessEvent) {
|
||||
handler.submit {
|
||||
currentProcessEvent = event
|
||||
|
||||
@@ -35,7 +34,7 @@ class ClashEventService(private val master: Master) : IClashEventService.Stub()
|
||||
}
|
||||
}
|
||||
|
||||
fun preformLogEvent(event: LogEvent) {
|
||||
fun performLogEvent(event: LogEvent) {
|
||||
handler.submit {
|
||||
observers.values.forEach {
|
||||
if (it.acquiredEvent.contains(Event.EVENT_LOG))
|
||||
@@ -44,7 +43,7 @@ class ClashEventService(private val master: Master) : IClashEventService.Stub()
|
||||
}
|
||||
}
|
||||
|
||||
fun preformSpeedEvent(event: SpeedEvent) {
|
||||
fun performSpeedEvent(event: SpeedEvent) {
|
||||
handler.submit {
|
||||
observers.values.forEach {
|
||||
if (it.acquiredEvent.contains(Event.EVENT_SPEED))
|
||||
@@ -53,10 +52,8 @@ class ClashEventService(private val master: Master) : IClashEventService.Stub()
|
||||
}
|
||||
}
|
||||
|
||||
fun preformBandwidthEvent(event: BandwidthEvent) {
|
||||
fun performBandwidthEvent(event: BandwidthEvent) {
|
||||
handler.submit {
|
||||
currentBandwidthEvent = event
|
||||
|
||||
observers.values.forEach {
|
||||
if ( it.acquiredEvent.contains(Event.EVENT_BANDWIDTH) )
|
||||
it.observer.onBandwidthEvent(event)
|
||||
@@ -64,7 +61,7 @@ class ClashEventService(private val master: Master) : IClashEventService.Stub()
|
||||
}
|
||||
}
|
||||
|
||||
fun preformErrorEvent(event: ErrorEvent) {
|
||||
fun performErrorEvent(event: ErrorEvent) {
|
||||
handler.submit {
|
||||
observers.values.forEach {
|
||||
it.observer.onErrorEvent(event)
|
||||
@@ -72,7 +69,7 @@ class ClashEventService(private val master: Master) : IClashEventService.Stub()
|
||||
}
|
||||
}
|
||||
|
||||
fun preformProfileChangedEvent(event: ProfileChangedEvent) {
|
||||
fun performProfileChangedEvent(event: ProfileChangedEvent) {
|
||||
handler.submit {
|
||||
observers.values.forEach {
|
||||
it.observer.onProfileChanged(event)
|
||||
@@ -80,6 +77,14 @@ class ClashEventService(private val master: Master) : IClashEventService.Stub()
|
||||
}
|
||||
}
|
||||
|
||||
fun performProfileReloadEvent(event: ProfileReloadEvent) {
|
||||
handler.submit {
|
||||
observers.values.forEach {
|
||||
it.observer.onProfileReloaded(event)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun unregisterEventObserver(id: String?) {
|
||||
handler.submit {
|
||||
require(id != null)
|
||||
@@ -110,9 +115,6 @@ class ClashEventService(private val master: Master) : IClashEventService.Stub()
|
||||
|
||||
if (initial) {
|
||||
observer.onProcessEvent(currentProcessEvent)
|
||||
|
||||
if (events.contains(Event.EVENT_BANDWIDTH))
|
||||
observer.onBandwidthEvent(currentBandwidthEvent)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,6 +11,7 @@ import android.os.IInterface
|
||||
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.GeneralPacket
|
||||
import com.github.kr328.clash.core.model.ProxyPacket
|
||||
import com.github.kr328.clash.core.utils.Log
|
||||
import java.io.File
|
||||
@@ -47,17 +48,21 @@ class ClashService : Service(), IClashEventObserver, ClashEventService.Master,
|
||||
catch (e: IOException) {
|
||||
Log.w("Set proxy failure", e)
|
||||
|
||||
this@ClashService.eventService.preformErrorEvent(
|
||||
this@ClashService.eventService.performErrorEvent(
|
||||
ErrorEvent(ErrorEvent.Type.SET_PROXY_SELECTED, e.toString())
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
override fun queryGeneral(): GeneralPacket {
|
||||
return clash.queryGeneral()
|
||||
}
|
||||
|
||||
override fun queryAllProxies(): ProxyPacket {
|
||||
return try {
|
||||
ProxyPacket.fromRawProxy(clash.queryProxies())
|
||||
} catch (e: Exception) {
|
||||
this@ClashService.eventService.preformErrorEvent(
|
||||
this@ClashService.eventService.performErrorEvent(
|
||||
ErrorEvent(ErrorEvent.Type.QUERY_PROXY_FAILURE, e.toString())
|
||||
)
|
||||
ProxyPacket("Unknown", emptyMap())
|
||||
@@ -70,7 +75,7 @@ class ClashService : Service(), IClashEventObserver, ClashEventService.Master,
|
||||
} catch (e: Exception) {
|
||||
Log.e("Start failure", e)
|
||||
|
||||
this@ClashService.eventService.preformErrorEvent(
|
||||
this@ClashService.eventService.performErrorEvent(
|
||||
ErrorEvent(ErrorEvent.Type.START_FAILURE, e.toString())
|
||||
)
|
||||
}
|
||||
@@ -88,7 +93,7 @@ class ClashService : Service(), IClashEventObserver, ClashEventService.Master,
|
||||
} catch (e: Exception) {
|
||||
Log.e("Start tun failure", e)
|
||||
|
||||
this@ClashService.eventService.preformErrorEvent(
|
||||
this@ClashService.eventService.performErrorEvent(
|
||||
ErrorEvent(ErrorEvent.Type.START_FAILURE, e.toString())
|
||||
)
|
||||
}
|
||||
@@ -168,7 +173,7 @@ class ClashService : Service(), IClashEventObserver, ClashEventService.Master,
|
||||
this,
|
||||
filesDir.resolve("clash"),
|
||||
cacheDir.resolve("clash_controller"),
|
||||
eventService::preformProcessEvent
|
||||
eventService::performProcessEvent
|
||||
)
|
||||
|
||||
puller = ClashEventPuller(clash, this)
|
||||
@@ -224,8 +229,8 @@ class ClashService : Service(), IClashEventObserver, ClashEventService.Master,
|
||||
eventService.recastEventRequirement()
|
||||
}
|
||||
ProcessEvent.STOPPED -> {
|
||||
eventService.preformSpeedEvent(SpeedEvent(0, 0))
|
||||
eventService.preformBandwidthEvent(BandwidthEvent(0))
|
||||
eventService.performSpeedEvent(SpeedEvent(0, 0))
|
||||
eventService.performBandwidthEvent(BandwidthEvent(0))
|
||||
|
||||
notification.cancel()
|
||||
|
||||
@@ -252,6 +257,8 @@ class ClashService : Service(), IClashEventObserver, ClashEventService.Master,
|
||||
profileService.removeCurrentProfileProxy(remove)
|
||||
|
||||
notification.setProfile(active.name)
|
||||
|
||||
eventService.performProfileReloadEvent(ProfileReloadEvent())
|
||||
} catch (e: Exception) {
|
||||
clash.process.stop()
|
||||
Log.w("Load profile failure", e)
|
||||
@@ -264,24 +271,25 @@ class ClashService : Service(), IClashEventObserver, ClashEventService.Master,
|
||||
}
|
||||
|
||||
override fun preformProfileChanged() {
|
||||
eventService.preformProfileChangedEvent(ProfileChangedEvent())
|
||||
eventService.performProfileChangedEvent(ProfileChangedEvent())
|
||||
}
|
||||
|
||||
override fun onLogPulled(event: LogEvent) {
|
||||
eventService.preformLogEvent(event)
|
||||
eventService.performLogEvent(event)
|
||||
}
|
||||
|
||||
override fun onSpeedPulled(event: SpeedEvent) {
|
||||
eventService.preformSpeedEvent(event)
|
||||
eventService.performSpeedEvent(event)
|
||||
}
|
||||
|
||||
override fun onBandwidthPulled(event: BandwidthEvent) {
|
||||
eventService.preformBandwidthEvent(event)
|
||||
eventService.performBandwidthEvent(event)
|
||||
}
|
||||
|
||||
override fun onBandwidthEvent(event: BandwidthEvent?) {}
|
||||
override fun onLogEvent(event: LogEvent?) {}
|
||||
override fun onErrorEvent(event: ErrorEvent?) {}
|
||||
override fun onProfileReloaded(event: ProfileReloadEvent?) {}
|
||||
override fun asBinder(): IBinder = object : Binder() {
|
||||
override fun queryLocalInterface(descriptor: String): IInterface? {
|
||||
return this@ClashService
|
||||
|
||||
@@ -194,6 +194,7 @@ class TunService : VpnService(), IClashEventObserver {
|
||||
override fun onLogEvent(event: LogEvent?) {}
|
||||
override fun onErrorEvent(event: ErrorEvent?) {}
|
||||
override fun onProfileChanged(event: ProfileChangedEvent?) {}
|
||||
override fun onProfileReloaded(event: ProfileReloadEvent?) {}
|
||||
override fun asBinder(): IBinder = object : Binder() {
|
||||
override fun queryLocalInterface(descriptor: String): IInterface? {
|
||||
return this@TunService
|
||||
|
||||
Reference in new issue
Block a user