add url test

This commit is contained in:
Kr328 committed 2019-12-07 01:30:35 +08:00
1 parent a72c2d8abb
commit bda542d2e5
16 files changed
+270 -66

No files matched your search

@@ -0,0 +1,5 @@
package com.github.kr328.clash.callback;
interface IUrlTestCallback {
void onResult(String proxy, long delay);
}
@@ -4,6 +4,7 @@ import com.github.kr328.clash.service.IClashEventObserver;
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.callback.IUrlTestCallback;
import com.github.kr328.clash.core.event.Event;
import com.github.kr328.clash.core.model.Packet;
@@ -26,4 +27,5 @@ interface IClashService {
// Query
ProxyPacket queryAllProxies();
GeneralPacket queryGeneral();
void startUrlTest(in String[] proxies, IUrlTestCallback callback);
}
@@ -5,7 +5,7 @@ import java.util.concurrent.Executors
class ClashEventService(private val master: Master) : IClashEventService.Stub() {
interface Master {
fun requireEvent(event: Int)
fun acquireEvent(event: Int)
fun releaseEvent(event: Int)
}
@@ -130,7 +130,7 @@ class ClashEventService(private val master: Master) : IClashEventService.Stub()
}.toSet()
val rel = EVENT_SET - req
req.forEach(master::requireEvent)
req.forEach(master::acquireEvent)
rel.forEach(master::releaseEvent)
}
}
@@ -6,6 +6,7 @@ import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.os.*
import com.github.kr328.clash.callback.IUrlTestCallback
import com.github.kr328.clash.core.Clash
import com.github.kr328.clash.core.ClashProcess
import com.github.kr328.clash.core.event.*
@@ -15,6 +16,7 @@ import com.github.kr328.clash.core.utils.Log
import java.io.File
import java.io.IOException
import java.util.concurrent.Executors
import kotlin.concurrent.thread
class ClashService : Service(), IClashEventObserver, ClashEventService.Master,
ClashProfileService.Master, ClashEventPuller.Master {
@@ -66,6 +68,24 @@ class ClashService : Service(), IClashEventObserver, ClashEventService.Master,
}
}
override fun startUrlTest(proxies: Array<out String>?, callback: IUrlTestCallback?) {
require(proxies != null && callback != null)
thread {
try {
clash.startUrlTest(proxies.toList()) { name, delay ->
callback.onResult(name, delay)
}
}
catch (e: Exception) {
Log.w("Url test failure", e)
}
callback.onResult(null, -1)
// Ignore exceptions
}
}
override fun start() {
try {
clash.process.start()
@@ -134,7 +154,7 @@ class ClashService : Service(), IClashEventObserver, ClashEventService.Master,
}
}
override fun requireEvent(event: Int) {
override fun acquireEvent(event: Int) {
if (clash.process.getProcessStatus() == ProcessEvent.STOPPED)
return
@@ -146,7 +166,6 @@ class ClashService : Service(), IClashEventObserver, ClashEventService.Master,
Event.EVENT_BANDWIDTH ->
puller.startBandwidthPull()
}
}
override fun releaseEvent(event: Int) {