mirror of
https://github.com/MetaCubeX/ClashMetaForAndroid.git
synced 2024-11-25 06:46:09 +03:00
remove geoip embed and sideload
we had geoip.metadb and geosite.dat in asserts
This commit is contained in:
parent
a1d838a98a
commit
262a6b563c
7
.github/workflows/update-dependencies.yaml
vendored
7
.github/workflows/update-dependencies.yaml
vendored
@ -21,9 +21,6 @@ jobs:
|
||||
distribution: 'zulu'
|
||||
java-version: 17
|
||||
|
||||
- name: Setup Gradle
|
||||
uses: gradle/actions/setup-gradle@v4
|
||||
|
||||
- name: Setup Go
|
||||
uses: actions/setup-go@v5
|
||||
with:
|
||||
@ -37,10 +34,6 @@ jobs:
|
||||
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
|
||||
restore-keys: |
|
||||
${{ runner.os }}-go-
|
||||
|
||||
- name: Download GeoIP Database
|
||||
run: |
|
||||
./gradlew :core:downloadGeoipDatabase
|
||||
|
||||
- name: Install update-go-mod-replace
|
||||
run: |
|
||||
|
@ -45,45 +45,14 @@ class OverrideSettingsActivity : BaseActivity<OverrideSettingsDesign>() {
|
||||
withClash {
|
||||
clearOverride(Clash.OverrideSlot.Persist)
|
||||
}
|
||||
|
||||
service.sideloadGeoip = ""
|
||||
}
|
||||
|
||||
finish()
|
||||
}
|
||||
}
|
||||
OverrideSettingsDesign.Request.EditSideloadGeoip -> {
|
||||
withContext(Dispatchers.IO) {
|
||||
val list = querySideloadProviders()
|
||||
val initial = service.sideloadGeoip
|
||||
val exist = list.any { info -> info.packageName == initial }
|
||||
|
||||
service.sideloadGeoip =
|
||||
design.requestSelectSideload(if (exist) initial else "", list)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun querySideloadProviders(): List<AppInfo> {
|
||||
val apps = packageManager.getInstalledPackages(PackageManager.GET_META_DATA)
|
||||
.filter {
|
||||
it.applicationInfo.metaData?.containsKey(Metadata.GEOIP_FILE_NAME)
|
||||
?: false
|
||||
}
|
||||
.map { it.toAppInfo(packageManager) }
|
||||
|
||||
return listOf(
|
||||
AppInfo(
|
||||
packageName = "",
|
||||
label = getString(R.string.use_built_in),
|
||||
icon = getDrawableCompat(R.drawable.ic_baseline_work)!!,
|
||||
installTime = 0,
|
||||
updateDate = 0,
|
||||
)
|
||||
) + apps
|
||||
}
|
||||
}
|
@ -11,10 +11,6 @@ plugins {
|
||||
id("golang-android")
|
||||
}
|
||||
|
||||
val geoipDatabaseUrl =
|
||||
"https://raw.githubusercontent.com/Loyalsoldier/geoip/release/Country.mmdb"
|
||||
val geoipInvalidate = Duration.ofDays(7)!!
|
||||
val geoipOutput = buildDir.resolve("intermediates/golang_blob")
|
||||
val golangSource = file("src/main/golang/native")
|
||||
|
||||
golang {
|
||||
@ -66,52 +62,4 @@ afterEvaluate {
|
||||
tasks.withType(GolangBuildTask::class.java).forEach {
|
||||
it.inputs.dir(golangSource)
|
||||
}
|
||||
}
|
||||
|
||||
task("downloadGeoipDatabase") {
|
||||
val databaseFile = geoipOutput.resolve("Country.mmdb")
|
||||
val moduleFile = geoipOutput.resolve("go.mod")
|
||||
val sourceFile = geoipOutput.resolve("blob.go")
|
||||
|
||||
val moduleContent = """
|
||||
module "cfa/blob"
|
||||
""".trimIndent()
|
||||
|
||||
val sourceContent = """
|
||||
package blob
|
||||
|
||||
import _ "embed"
|
||||
|
||||
//go:embed Country.mmdb
|
||||
var GeoipDatabase []byte
|
||||
""".trimIndent()
|
||||
|
||||
outputs.dir(geoipOutput)
|
||||
|
||||
onlyIf {
|
||||
System.currentTimeMillis() - databaseFile.lastModified() > geoipInvalidate.toMillis()
|
||||
}
|
||||
|
||||
doLast {
|
||||
geoipOutput.mkdirs()
|
||||
|
||||
moduleFile.writeText(moduleContent)
|
||||
sourceFile.writeText(sourceContent)
|
||||
|
||||
URL(geoipDatabaseUrl).openConnection().getInputStream().use { input ->
|
||||
FileOutputStream(databaseFile).use { output ->
|
||||
input.copyTo(output)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
afterEvaluate {
|
||||
val downloadTask = tasks["downloadGeoipDatabase"]
|
||||
|
||||
tasks.forEach {
|
||||
if (it.name.startsWith("externalGolangBuild")) {
|
||||
it.dependsOn(downloadTask)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -4,8 +4,6 @@ go 1.20
|
||||
|
||||
require cfa v0.0.0
|
||||
|
||||
require cfa/blob v0.0.0-00010101000000-000000000000 // indirect
|
||||
|
||||
require (
|
||||
github.com/3andne/restls-client-go v0.1.6 // indirect
|
||||
github.com/RyuaNerin/go-krypto v1.2.4 // indirect
|
||||
@ -115,5 +113,3 @@ replace github.com/sagernet/sing => github.com/metacubex/sing v0.0.0-20240724044
|
||||
replace cfa => ../../main/golang
|
||||
|
||||
replace github.com/metacubex/mihomo => ./clash
|
||||
|
||||
replace cfa/blob => ../../../build/intermediates/golang_blob
|
||||
|
@ -287,33 +287,6 @@ Java_com_github_kr328_clash_core_bridge_Bridge_nativeClearOverride(JNIEnv *env,
|
||||
clearOverride(slot);
|
||||
}
|
||||
|
||||
JNIEXPORT void JNICALL
|
||||
Java_com_github_kr328_clash_core_bridge_Bridge_nativeInstallSideloadGeoip(JNIEnv *env, jobject thiz,
|
||||
jbyteArray data) {
|
||||
TRACE_METHOD();
|
||||
|
||||
if (data == NULL) {
|
||||
installSideloadGeoip(NULL, 0);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
jbyte *bytes = (*env)->GetByteArrayElements(env, data, NULL);
|
||||
int size = (*env)->GetArrayLength(env, data);
|
||||
|
||||
scoped_string err = installSideloadGeoip(bytes, size);
|
||||
|
||||
(*env)->ReleaseByteArrayElements(env, data, bytes, JNI_ABORT);
|
||||
|
||||
if (err != NULL) {
|
||||
(*env)->ThrowNew(
|
||||
env,
|
||||
find_class("com/github/kr328/clash/core/bridge/ClashException"),
|
||||
err
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
JNIEXPORT jstring JNICALL
|
||||
Java_com_github_kr328_clash_core_bridge_Bridge_nativeQueryConfiguration(JNIEnv *env, jobject thiz) {
|
||||
TRACE_METHOD();
|
||||
|
@ -5,7 +5,6 @@
|
||||
<option name="MOVE_ALL_IMPORTS_IN_ONE_DECLARATION" value="true" />
|
||||
<option name="MOVE_ALL_STDLIB_IMPORTS_IN_ONE_GROUP" value="true" />
|
||||
<option name="GROUP_STDLIB_IMPORTS" value="true" />
|
||||
<option name="GROUP_CURRENT_PROJECT_IMPORTS" value="true" />
|
||||
</GoCodeStyleSettings>
|
||||
</code_scheme>
|
||||
</component>
|
@ -5,7 +5,6 @@ go 1.20
|
||||
require (
|
||||
github.com/dlclark/regexp2 v1.11.4
|
||||
github.com/metacubex/mihomo v1.7.0
|
||||
github.com/oschwald/maxminddb-golang v1.12.0
|
||||
golang.org/x/sync v0.8.0
|
||||
gopkg.in/yaml.v3 v3.0.1
|
||||
)
|
||||
@ -71,6 +70,7 @@ require (
|
||||
github.com/oasisprotocol/deoxysii v0.0.0-20220228165953-2091330c22b7 // indirect
|
||||
github.com/onsi/ginkgo/v2 v2.9.5 // indirect
|
||||
github.com/openacid/low v0.1.21 // indirect
|
||||
github.com/oschwald/maxminddb-golang v1.12.0 // indirect
|
||||
github.com/pierrec/lz4/v4 v4.1.14 // indirect
|
||||
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect
|
||||
github.com/puzpuzpuz/xsync/v3 v3.4.0 // indirect
|
||||
|
@ -4,8 +4,6 @@ import (
|
||||
"errors"
|
||||
"syscall"
|
||||
|
||||
"cfa/blob"
|
||||
|
||||
"github.com/metacubex/mihomo/component/process"
|
||||
"github.com/metacubex/mihomo/log"
|
||||
|
||||
@ -13,14 +11,12 @@ import (
|
||||
"cfa/native/platform"
|
||||
|
||||
"github.com/metacubex/mihomo/component/dialer"
|
||||
"github.com/metacubex/mihomo/component/mmdb"
|
||||
"github.com/metacubex/mihomo/constant"
|
||||
)
|
||||
|
||||
var errBlocked = errors.New("blocked")
|
||||
|
||||
func Init(home, versionName string, platformVersion int) {
|
||||
mmdb.LoadFromBytes(blob.GeoipDatabase)
|
||||
constant.SetHomeDir(home)
|
||||
app.ApplyVersionName(versionName)
|
||||
app.ApplyPlatformVersion(platformVersion)
|
||||
|
@ -109,16 +109,3 @@ func updateProvider(completable unsafe.Pointer, pType C.c_string, name C.c_strin
|
||||
func suspend(suspended C.int) {
|
||||
tunnel.Suspend(suspended != 0)
|
||||
}
|
||||
|
||||
//export installSideloadGeoip
|
||||
func installSideloadGeoip(block unsafe.Pointer, blockSize C.int) *C.char {
|
||||
if block == nil {
|
||||
_ = tunnel.InstallSideloadGeoip(nil)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
bytes := C.GoBytes(block, blockSize)
|
||||
|
||||
return marshalString(tunnel.InstallSideloadGeoip(bytes))
|
||||
}
|
||||
|
@ -1,24 +0,0 @@
|
||||
package tunnel
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/metacubex/mihomo/component/mmdb"
|
||||
"github.com/oschwald/maxminddb-golang"
|
||||
)
|
||||
|
||||
func InstallSideloadGeoip(block []byte) error {
|
||||
if block == nil {
|
||||
mmdb.InstallOverride(nil)
|
||||
return nil
|
||||
}
|
||||
|
||||
db, err := maxminddb.FromBytes(block)
|
||||
if err != nil {
|
||||
return fmt.Errorf("load sideload geoip mmdb: %s", err.Error())
|
||||
}
|
||||
|
||||
mmdb.InstallOverride(db)
|
||||
|
||||
return nil
|
||||
}
|
@ -208,10 +208,6 @@ object Clash {
|
||||
Bridge.nativeClearOverride(slot.ordinal)
|
||||
}
|
||||
|
||||
fun installSideloadGeoip(data: ByteArray?) {
|
||||
Bridge.nativeInstallSideloadGeoip(data)
|
||||
}
|
||||
|
||||
fun queryConfiguration(): UiConfiguration {
|
||||
return Json.Default.decodeFromString(
|
||||
UiConfiguration.serializer(),
|
||||
|
@ -46,7 +46,6 @@ object Bridge {
|
||||
external fun nativeReadOverride(slot: Int): String
|
||||
external fun nativeWriteOverride(slot: Int, content: String)
|
||||
external fun nativeClearOverride(slot: Int)
|
||||
external fun nativeInstallSideloadGeoip(data: ByteArray?)
|
||||
external fun nativeQueryConfiguration(): String
|
||||
external fun nativeSubscribeLogcat(callback: LogcatInterface)
|
||||
external fun nativeCoreVersion(): String
|
||||
|
@ -5,7 +5,6 @@ import android.view.View
|
||||
import com.github.kr328.clash.core.model.ConfigurationOverride
|
||||
import com.github.kr328.clash.core.model.LogMessage
|
||||
import com.github.kr328.clash.core.model.TunnelState
|
||||
import com.github.kr328.clash.design.adapter.SideloadProviderAdapter
|
||||
import com.github.kr328.clash.design.databinding.DesignSettingsOverideBinding
|
||||
import com.github.kr328.clash.design.databinding.DialogPreferenceListBinding
|
||||
import com.github.kr328.clash.design.dialog.FullScreenDialog
|
||||
@ -23,7 +22,7 @@ class OverrideSettingsDesign(
|
||||
configuration: ConfigurationOverride
|
||||
) : Design<OverrideSettingsDesign.Request>(context) {
|
||||
enum class Request {
|
||||
ResetOverride, EditSideloadGeoip
|
||||
ResetOverride
|
||||
}
|
||||
|
||||
private val binding = DesignSettingsOverideBinding
|
||||
@ -52,49 +51,6 @@ class OverrideSettingsDesign(
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun requestSelectSideload(initial: String, apps: List<AppInfo>): String =
|
||||
withContext(Dispatchers.Main) {
|
||||
suspendCancellableCoroutine { ctx ->
|
||||
val binding = DialogPreferenceListBinding
|
||||
.inflate(context.layoutInflater, context.root, false)
|
||||
val adapter = SideloadProviderAdapter(context, apps, initial)
|
||||
val dialog = FullScreenDialog(context)
|
||||
|
||||
dialog.setContentView(binding.root)
|
||||
|
||||
binding.surface = dialog.surface
|
||||
|
||||
binding.titleView.text = context.getString(R.string.sideload_geoip)
|
||||
|
||||
binding.newView.visibility = View.INVISIBLE
|
||||
|
||||
binding.mainList.applyLinearAdapter(context, adapter)
|
||||
|
||||
binding.resetView.setOnClickListener {
|
||||
ctx.resume("")
|
||||
|
||||
dialog.dismiss()
|
||||
}
|
||||
|
||||
binding.cancelView.setOnClickListener {
|
||||
dialog.dismiss()
|
||||
}
|
||||
|
||||
binding.okView.setOnClickListener {
|
||||
ctx.resume(adapter.selectedPackageName)
|
||||
|
||||
dialog.dismiss()
|
||||
}
|
||||
|
||||
dialog.setOnDismissListener {
|
||||
if (!ctx.isCompleted)
|
||||
ctx.resume(initial)
|
||||
}
|
||||
|
||||
dialog.show()
|
||||
}
|
||||
}
|
||||
|
||||
init {
|
||||
binding.self = this
|
||||
|
||||
@ -247,15 +203,6 @@ class OverrideSettingsDesign(
|
||||
placeholder = R.string.dont_modify,
|
||||
)
|
||||
|
||||
clickable(
|
||||
title = R.string.sideload_geoip,
|
||||
summary = R.string.sideload_geoip_summary
|
||||
) {
|
||||
clicked {
|
||||
requests.trySend(Request.EditSideloadGeoip)
|
||||
}
|
||||
}
|
||||
|
||||
category(R.string.dns)
|
||||
|
||||
val dnsDependencies: MutableList<Preference> = mutableListOf()
|
||||
|
@ -1,48 +0,0 @@
|
||||
package com.github.kr328.clash.design.adapter
|
||||
|
||||
import android.content.Context
|
||||
import android.view.ViewGroup
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import com.github.kr328.clash.design.databinding.AdapterSideloadProviderBinding
|
||||
import com.github.kr328.clash.design.model.AppInfo
|
||||
import com.github.kr328.clash.design.util.layoutInflater
|
||||
import com.github.kr328.clash.design.util.root
|
||||
|
||||
class SideloadProviderAdapter(
|
||||
private val context: Context,
|
||||
private val apps: List<AppInfo>,
|
||||
var selectedPackageName: String
|
||||
) : RecyclerView.Adapter<SideloadProviderAdapter.Holder>() {
|
||||
class Holder(val binding: AdapterSideloadProviderBinding) :
|
||||
RecyclerView.ViewHolder(binding.root)
|
||||
|
||||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): Holder {
|
||||
return Holder(
|
||||
AdapterSideloadProviderBinding
|
||||
.inflate(context.layoutInflater, context.root, false)
|
||||
)
|
||||
}
|
||||
|
||||
override fun onBindViewHolder(holder: Holder, position: Int) {
|
||||
val current = apps[position]
|
||||
|
||||
holder.binding.appInfo = current
|
||||
|
||||
holder.binding.selected = selectedPackageName == current.packageName
|
||||
|
||||
holder.binding.root.setOnClickListener {
|
||||
val index = apps.indexOfFirst { it.packageName == selectedPackageName }
|
||||
|
||||
selectedPackageName = current.packageName
|
||||
|
||||
if (index >= 0)
|
||||
notifyItemChanged(index)
|
||||
|
||||
notifyItemChanged(position)
|
||||
}
|
||||
}
|
||||
|
||||
override fun getItemCount(): Int {
|
||||
return apps.size
|
||||
}
|
||||
}
|
@ -119,8 +119,6 @@
|
||||
<string name="log_level">ログレベル</string>
|
||||
<string name="ipv6">IPv6</string>
|
||||
<string name="hosts">ホスト</string>
|
||||
<string name="sideload_geoip">GeoIPを読み込み中</string>
|
||||
<string name="sideload_geoip_summary">外部のGeoIPデータベース</string>
|
||||
<string name="_new">新規</string>
|
||||
<string name="value">値</string>
|
||||
<string name="strategy">ポリシー</string>
|
||||
|
@ -119,8 +119,6 @@
|
||||
<string name="log_level">로그 레벨</string>
|
||||
<string name="ipv6">IPv6</string>
|
||||
<string name="hosts">호스트</string>
|
||||
<string name="sideload_geoip">Sideload GEOIP</string>
|
||||
<string name="sideload_geoip_summary">외부 GEOIP 데이터베이스</string>
|
||||
<string name="_new">신규</string>
|
||||
<string name="value">값</string>
|
||||
<string name="strategy">정책</string>
|
||||
|
@ -145,8 +145,6 @@
|
||||
<string name="log_level">Уровень логов</string>
|
||||
<string name="ipv6">IPv6</string>
|
||||
<string name="hosts">Hosts</string>
|
||||
<string name="sideload_geoip">Загрузить GeoIP</string>
|
||||
<string name="sideload_geoip_summary">Внешняя база GeoIP</string>
|
||||
<string name="_new">Новый</string>
|
||||
<string name="value">Значение</string>
|
||||
|
||||
|
@ -175,8 +175,6 @@
|
||||
<string name="format_fetching_provider">正在下載外部資源 %s</string>
|
||||
<string name="initializing">正在初始化</string>
|
||||
<string name="verifying">正在校驗</string>
|
||||
<string name="sideload_geoip">旁加載 GEOIP</string>
|
||||
<string name="sideload_geoip_summary">外部 GEOIP 數據庫</string>
|
||||
<string name="force_enable">強制啓用</string>
|
||||
<string name="document">文檔</string>
|
||||
<string name="clash_wiki">Clash Wiki</string>
|
||||
|
@ -175,8 +175,6 @@
|
||||
<string name="format_fetching_provider">正在下載外部資源 %s</string>
|
||||
<string name="initializing">正在初始化</string>
|
||||
<string name="verifying">正在校驗</string>
|
||||
<string name="sideload_geoip">旁載入 GEOIP</string>
|
||||
<string name="sideload_geoip_summary">外部 GEOIP 資料庫</string>
|
||||
<string name="force_enable">強制啟用</string>
|
||||
<string name="document">文件</string>
|
||||
<string name="clash_wiki">Clash Wiki</string>
|
||||
|
@ -176,8 +176,6 @@
|
||||
<string name="format_fetching_provider">正在下载外部资源 %s</string>
|
||||
<string name="initializing">正在初始化</string>
|
||||
<string name="verifying">正在校验</string>
|
||||
<string name="sideload_geoip">旁加载 GEOIP</string>
|
||||
<string name="sideload_geoip_summary">外部 GEOIP 数据库</string>
|
||||
<string name="force_enable">强制启用</string>
|
||||
<string name="document">文档</string>
|
||||
<string name="clash_wiki">Clash Wiki</string>
|
||||
|
@ -149,8 +149,6 @@
|
||||
<string name="external_controller">External Controller</string>
|
||||
<string name="secret">Secret</string>
|
||||
<string name="hosts">Hosts</string>
|
||||
<string name="sideload_geoip">Sideload GEOIP</string>
|
||||
<string name="sideload_geoip_summary">External GEOIP database</string>
|
||||
<string name="_new">New</string>
|
||||
<string name="value">Value</string>
|
||||
|
||||
|
@ -27,7 +27,6 @@ class ClashService : BaseService() {
|
||||
val close = install(CloseModule(self))
|
||||
val config = install(ConfigurationModule(self))
|
||||
val network = install(NetworkObserveModule(self))
|
||||
val sideload = install(SideloadDatabaseModule(self))
|
||||
|
||||
if (store.dynamicNotification)
|
||||
install(DynamicNotificationModule(self))
|
||||
@ -49,11 +48,6 @@ class ClashService : BaseService() {
|
||||
|
||||
true
|
||||
}
|
||||
sideload.onEvent {
|
||||
reason = it.message
|
||||
|
||||
true
|
||||
}
|
||||
network.onEvent {
|
||||
false
|
||||
}
|
||||
|
@ -33,7 +33,6 @@ class TunService : VpnService(), CoroutineScope by CoroutineScope(Dispatchers.De
|
||||
val tun = install(TunModule(self))
|
||||
val config = install(ConfigurationModule(self))
|
||||
val network = install(NetworkObserveModule(self))
|
||||
val sideload = install(SideloadDatabaseModule(self))
|
||||
|
||||
if (store.dynamicNotification)
|
||||
install(DynamicNotificationModule(self))
|
||||
@ -57,11 +56,6 @@ class TunService : VpnService(), CoroutineScope by CoroutineScope(Dispatchers.De
|
||||
|
||||
true
|
||||
}
|
||||
sideload.onEvent {
|
||||
reason = it.message
|
||||
|
||||
true
|
||||
}
|
||||
network.onEvent { n ->
|
||||
if (Build.VERSION.SDK_INT in 22..28) @TargetApi(22) {
|
||||
setUnderlyingNetworks(n?.let { arrayOf(it) })
|
||||
|
@ -1,87 +0,0 @@
|
||||
package com.github.kr328.clash.service.clash.module
|
||||
|
||||
import android.app.Service
|
||||
import android.content.Intent
|
||||
import com.github.kr328.clash.common.constants.Intents
|
||||
import com.github.kr328.clash.common.log.Log
|
||||
import com.github.kr328.clash.core.Clash
|
||||
import com.github.kr328.clash.service.sideload.readGeoipDatabaseFrom
|
||||
import com.github.kr328.clash.service.store.ServiceStore
|
||||
import com.github.kr328.clash.service.util.packageName
|
||||
import kotlinx.coroutines.channels.Channel
|
||||
import kotlinx.coroutines.selects.select
|
||||
import java.io.FileNotFoundException
|
||||
import java.io.IOException
|
||||
|
||||
class SideloadDatabaseModule(service: Service) :
|
||||
Module<SideloadDatabaseModule.LoadException>(service) {
|
||||
data class LoadException(val message: String)
|
||||
|
||||
private val store = ServiceStore(service)
|
||||
|
||||
private var current: String = ""
|
||||
|
||||
override suspend fun run() {
|
||||
val packagesChanged = receiveBroadcast(false) {
|
||||
addAction(Intent.ACTION_PACKAGE_ADDED)
|
||||
addAction(Intent.ACTION_PACKAGE_REPLACED)
|
||||
addAction(Intent.ACTION_PACKAGE_FULLY_REMOVED)
|
||||
addDataScheme("package")
|
||||
}
|
||||
val profileChanged = receiveBroadcast(capacity = Channel.CONFLATED) {
|
||||
addAction(Intents.ACTION_PROFILE_CHANGED)
|
||||
}
|
||||
val initial = Channel<Unit>(1).apply { send(Unit) }
|
||||
|
||||
while (true) {
|
||||
val (reload, force) = select<Pair<Boolean, Boolean>> {
|
||||
packagesChanged.onReceive {
|
||||
when (it.action) {
|
||||
Intent.ACTION_PACKAGE_ADDED ->
|
||||
(it.packageName == store.sideloadGeoip) to true
|
||||
Intent.ACTION_PACKAGE_REPLACED ->
|
||||
(it.packageName == current) to true
|
||||
Intent.ACTION_PACKAGE_FULLY_REMOVED ->
|
||||
(it.packageName == current) to true
|
||||
else -> false to false
|
||||
}
|
||||
}
|
||||
profileChanged.onReceive {
|
||||
true to false
|
||||
}
|
||||
initial.onReceive {
|
||||
true to true
|
||||
}
|
||||
}
|
||||
|
||||
if (!reload) continue
|
||||
|
||||
val pkg = store.sideloadGeoip
|
||||
|
||||
try {
|
||||
if (!force && pkg == current)
|
||||
continue
|
||||
|
||||
current = pkg
|
||||
|
||||
if (pkg.isNotBlank()) {
|
||||
val data = service.readGeoipDatabaseFrom(pkg)
|
||||
|
||||
Clash.installSideloadGeoip(data)
|
||||
|
||||
if (data != null) {
|
||||
Log.d("Sideload geoip loaded, pkg = $pkg")
|
||||
} else {
|
||||
Log.d("Sideload geoip not found")
|
||||
}
|
||||
}
|
||||
} catch (e: FileNotFoundException) {
|
||||
return enqueueEvent(LoadException("file $pkg/assets/${e.message} not found"))
|
||||
} catch (e: IOException) {
|
||||
return enqueueEvent(LoadException("read data from $pkg: ${e.message}"))
|
||||
} catch (e: Exception) {
|
||||
return enqueueEvent(LoadException(e.toString()))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -1,21 +0,0 @@
|
||||
package com.github.kr328.clash.service.sideload
|
||||
|
||||
import android.content.Context
|
||||
import android.content.pm.PackageManager
|
||||
import com.github.kr328.clash.common.constants.Metadata
|
||||
import com.github.kr328.clash.common.log.Log
|
||||
import java.io.InputStream
|
||||
|
||||
fun Context.readGeoipDatabaseFrom(packageName: String): ByteArray? {
|
||||
return try {
|
||||
val appInfo = packageManager.getApplicationInfo(packageName, PackageManager.GET_META_DATA)
|
||||
val path = appInfo.metaData.getString(Metadata.GEOIP_FILE_NAME) ?: return null
|
||||
|
||||
createPackageContext(packageName, 0)
|
||||
.resources.assets.open(path).use(InputStream::readBytes)
|
||||
} catch (e: PackageManager.NameNotFoundException) {
|
||||
Log.w("Sideload geoip: $packageName not found", e)
|
||||
|
||||
null
|
||||
}
|
||||
}
|
@ -60,9 +60,4 @@ class ServiceStore(context: Context) {
|
||||
key = "dynamic_notification",
|
||||
defaultValue = true
|
||||
)
|
||||
|
||||
var sideloadGeoip by store.string(
|
||||
key = "sideload_geoip",
|
||||
defaultValue = ""
|
||||
)
|
||||
}
|
Loading…
Reference in New Issue
Block a user