From ecf03507e6fe87570aec0841b98c514f9aa7789e Mon Sep 17 00:00:00 2001 From: Steve Johnson Date: Sun, 29 Oct 2023 21:38:36 +0800 Subject: [PATCH] Squashed commit of the following: commit 574fba87ab733332efa17733a6602a1649e62379 Author: Steve Johnson Date: Sun Oct 29 21:31:23 2023 +0800 support importing local geofile commit ec410293f3abe29835645233349d026d3a55acc0 Author: Steve Johnson Date: Sun Oct 29 17:18:52 2023 +0800 release assets at runtime commit 2dfb95bab98ba661a28efe255e2965c35c6580c4 Author: Steve Johnson Date: Sun Oct 29 16:43:41 2023 +0800 remove embedded country.mmdb commit fb245ed4a3c257284685f3b1bee5d9f7333833ce Author: Steve Johnson Date: Sun Oct 29 16:35:14 2023 +0800 simplity gradle commit 2fb75c87a13dea7e5c8f8f4126cc53d2d6926b99 Author: Steve Johnson Date: Sun Oct 29 16:06:17 2023 +0800 add geofiles download --- .gitignore | 3 + app/build.gradle.kts | 41 ++++++++ .../com/github/kr328/clash/MainApplication.kt | 23 +++++ .../clash/MetaFeatureSettingsActivity.kt | 94 +++++++++++++++++++ .../clash/design/MetaFeatureSettingsDesign.kt | 32 ++++++- design/src/main/res/values-ja-rJP/strings.xml | 5 + design/src/main/res/values-ko-rKR/strings.xml | 5 + design/src/main/res/values-ru/strings.xml | 5 + design/src/main/res/values-zh-rHK/strings.xml | 5 + design/src/main/res/values-zh-rTW/strings.xml | 5 + design/src/main/res/values-zh/strings.xml | 5 + design/src/main/res/values/strings.xml | 5 + 12 files changed, 227 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 14ce7480..a169a396 100644 --- a/.gitignore +++ b/.gitignore @@ -27,6 +27,9 @@ gradle-app.setting /core/src/premium/golang/.idea/* !/core/src/premium/golang/.idea/codeStyles +# Ignore builtin geofiles +app/src/main/assets + # KeyStore signing.properties *.keystore diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 432bc4cb..20f61121 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -1,3 +1,7 @@ +import java.net.URL +import java.nio.file.Files +import java.nio.file.StandardCopyOption + plugins { kotlin("android") kotlin("kapt") @@ -25,3 +29,40 @@ dependencies { tasks.getByName("clean", type = Delete::class) { delete(file("release")) } + +val geoFilesDownloadDir = "src/main/assets" + +task("downloadGeoFiles") { + + val geoFilesUrls = mapOf( + "https://github.com/MetaCubeX/meta-rules-dat/releases/download/latest/geoip.metadb" to "geoip.metadb", + "https://github.com/MetaCubeX/meta-rules-dat/releases/download/latest/geosite.dat" to "geosite.dat", + // "https://github.com/MetaCubeX/meta-rules-dat/releases/download/latest/country.mmdb" to "country.mmdb", + ) + + doLast { + geoFilesUrls.forEach { (downloadUrl, outputFileName) -> + val url = URL(downloadUrl) + val outputPath = file("$geoFilesDownloadDir/$outputFileName") + outputPath.parentFile.mkdirs() + url.openStream().use { input -> + Files.copy(input, outputPath.toPath(), StandardCopyOption.REPLACE_EXISTING) + println("$outputFileName downloaded to $outputPath") + } + } + } +} + +afterEvaluate { + val downloadGeoFilesTask = tasks["downloadGeoFiles"] + + tasks.forEach { + if (it.name.startsWith("assemble")) { + it.dependsOn(downloadGeoFilesTask) + } + } +} + +tasks.getByName("clean", type = Delete::class) { + delete(file(geoFilesDownloadDir)) +} \ No newline at end of file diff --git a/app/src/main/java/com/github/kr328/clash/MainApplication.kt b/app/src/main/java/com/github/kr328/clash/MainApplication.kt index d885d232..153a2116 100644 --- a/app/src/main/java/com/github/kr328/clash/MainApplication.kt +++ b/app/src/main/java/com/github/kr328/clash/MainApplication.kt @@ -7,6 +7,12 @@ import com.github.kr328.clash.common.compat.currentProcessName import com.github.kr328.clash.common.log.Log import com.github.kr328.clash.remote.Remote import com.github.kr328.clash.service.util.sendServiceRecreated +import java.io.File +import java.io.FileOutputStream +import java.io.IOException +import java.io.InputStream +import java.io.OutputStream + @Suppress("unused") class MainApplication : Application() { @@ -20,6 +26,7 @@ class MainApplication : Application() { super.onCreate() val processName = currentProcessName + extractGeoFiles() Log.d("Process $processName started") @@ -30,6 +37,22 @@ class MainApplication : Application() { } } + private fun extractGeoFiles() { + val geoipFile = File(filesDir, "clash/geoip.metadb") + if(!geoipFile.exists()) { + FileOutputStream(geoipFile).use { + assets.open("geoip.metadb").copyTo(it); + } + } + + val geositeFile = File(filesDir, "clash/geosite.dat") + if(!geositeFile.exists()) { + FileOutputStream(geositeFile).use { + assets.open("geosite.dat").copyTo(it); + } + } + } + fun finalize() { Global.destroy() } diff --git a/app/src/main/java/com/github/kr328/clash/MetaFeatureSettingsActivity.kt b/app/src/main/java/com/github/kr328/clash/MetaFeatureSettingsActivity.kt index fd1f0164..0bc7684a 100644 --- a/app/src/main/java/com/github/kr328/clash/MetaFeatureSettingsActivity.kt +++ b/app/src/main/java/com/github/kr328/clash/MetaFeatureSettingsActivity.kt @@ -1,10 +1,23 @@ package com.github.kr328.clash +import android.R +import android.annotation.SuppressLint +import android.content.Context +import android.content.DialogInterface +import android.content.Intent +import android.database.Cursor +import android.provider.OpenableColumns +import android.widget.Toast import com.github.kr328.clash.core.Clash import com.github.kr328.clash.design.MetaFeatureSettingsDesign import com.github.kr328.clash.util.withClash +import com.google.android.material.dialog.MaterialAlertDialogBuilder import kotlinx.coroutines.isActive import kotlinx.coroutines.selects.select +import java.io.File +import java.io.FileOutputStream +import kotlin.coroutines.resume + class MetaFeatureSettingsActivity : BaseActivity() { override suspend fun main() { @@ -41,9 +54,90 @@ class MetaFeatureSettingsActivity : BaseActivity() { finish() } } + MetaFeatureSettingsDesign.Request.ImportGeoIp -> { + val intent = Intent(Intent.ACTION_GET_CONTENT) + intent.type = "*/*" + intent.addCategory(Intent.CATEGORY_OPENABLE) + startActivityForResult( + intent, + MetaFeatureSettingsDesign.Request.ImportGeoIp.ordinal + ) + } + MetaFeatureSettingsDesign.Request.ImportGeoSite -> { + val intent = Intent(Intent.ACTION_GET_CONTENT) + intent.type = "*/*" + intent.addCategory(Intent.CATEGORY_OPENABLE) + startActivityForResult( + intent, + MetaFeatureSettingsDesign.Request.ImportGeoSite.ordinal + ) + } + MetaFeatureSettingsDesign.Request.ImportCountry -> { + val intent = Intent(Intent.ACTION_GET_CONTENT) + intent.type = "*/*" + intent.addCategory(Intent.CATEGORY_OPENABLE) + startActivityForResult( + intent, + MetaFeatureSettingsDesign.Request.ImportCountry.ordinal + ) + } } } } } } + + public val validDatabaseExtensions = listOf( + ".metadb", ".db", ".dat", ".mmdb" + ) + @SuppressLint("Range") + override fun onActivityResult(requestCode: Int, resultCode: Int, resultData: Intent?) { + super.onActivityResult(requestCode, resultCode, resultData) + if(resultCode == RESULT_OK) { + val uri = resultData?.data + val cursor: Cursor? = uri?.let { + contentResolver.query(it, null, null, null, null, null) + } + cursor?.use { + if (it.moveToFirst()) { + val displayName: String = + it.getString(it.getColumnIndex(OpenableColumns.DISPLAY_NAME)) + val ext = "." + displayName.substringAfterLast(".") + if(!validDatabaseExtensions.contains(ext)) + { + val dialog = MaterialAlertDialogBuilder(this) + .setTitle("Unknown Database Format") + .setMessage("Only ${validDatabaseExtensions.joinToString("/")} are supported") + .setPositiveButton("OK"){ _, _ -> } + .show() + return + } + val outputFileName = when (requestCode) { + MetaFeatureSettingsDesign.Request.ImportGeoIp.ordinal -> + "geoip$ext" + MetaFeatureSettingsDesign.Request.ImportGeoSite.ordinal -> + "geosite$ext" + MetaFeatureSettingsDesign.Request.ImportCountry.ordinal -> + "country$ext" + else -> "" + } + if(outputFileName.isEmpty()) + { + Toast.makeText(this, "Bad request", Toast.LENGTH_LONG).show() + return + } + + val outputFile = File(File(filesDir, "clash"), outputFileName); + contentResolver.openInputStream(uri).use { ins-> + FileOutputStream(outputFile).use { outs-> + ins?.copyTo(outs) + } + } + Toast.makeText(this, "$displayName imported", Toast.LENGTH_LONG).show() + return + } + } + } + Toast.makeText(this, "Import failed", Toast.LENGTH_LONG).show() + } } \ No newline at end of file diff --git a/design/src/main/java/com/github/kr328/clash/design/MetaFeatureSettingsDesign.kt b/design/src/main/java/com/github/kr328/clash/design/MetaFeatureSettingsDesign.kt index de928ded..099bf9a2 100644 --- a/design/src/main/java/com/github/kr328/clash/design/MetaFeatureSettingsDesign.kt +++ b/design/src/main/java/com/github/kr328/clash/design/MetaFeatureSettingsDesign.kt @@ -15,7 +15,7 @@ class MetaFeatureSettingsDesign( configuration: ConfigurationOverride ) : Design(context) { enum class Request { - ResetOverride + ResetOverride, ImportGeoIp, ImportGeoSite, ImportCountry } private val binding = DesignSettingsMetaFeatureBinding @@ -198,6 +198,36 @@ class MetaFeatureSettingsDesign( ) sniffer.listener?.onChanged() + + category(R.string.geox_files) + + clickable ( + title = R.string.import_geoip_file, + summary = R.string.press_to_import, + ){ + clicked { + requests.trySend(Request.ImportGeoIp) + } + } + + clickable ( + title = R.string.import_geosite_file, + summary = R.string.press_to_import, + ){ + clicked { + requests.trySend(Request.ImportGeoSite) + } + } + + clickable ( + title = R.string.import_country_file, + summary = R.string.press_to_import, + ){ + clicked { + requests.trySend(Request.ImportCountry) + } + } + /* category(R.string.geox_url_setting) diff --git a/design/src/main/res/values-ja-rJP/strings.xml b/design/src/main/res/values-ja-rJP/strings.xml index ff349bd4..efbb34f0 100644 --- a/design/src/main/res/values-ja-rJP/strings.xml +++ b/design/src/main/res/values-ja-rJP/strings.xml @@ -234,4 +234,9 @@ GeoIP URL MMDB URL Geosite URL + Geo Files + Import GeoIP Database + Press to import... + Import GeoSite Database + Import Country Database \ No newline at end of file diff --git a/design/src/main/res/values-ko-rKR/strings.xml b/design/src/main/res/values-ko-rKR/strings.xml index b5381f6f..3290535a 100644 --- a/design/src/main/res/values-ko-rKR/strings.xml +++ b/design/src/main/res/values-ko-rKR/strings.xml @@ -234,4 +234,9 @@ GeoIP Url MMDB Url Geosite Url + Geo Files + Import GeoIP Database + Press to import... + Import GeoSite Database + Import Country Database \ No newline at end of file diff --git a/design/src/main/res/values-ru/strings.xml b/design/src/main/res/values-ru/strings.xml index e81ba0c4..069fee33 100644 --- a/design/src/main/res/values-ru/strings.xml +++ b/design/src/main/res/values-ru/strings.xml @@ -299,4 +299,9 @@ https://raw.githubusercontent.com/Loyalsoldier/v2ray-rules-dat/release/geoip.dat https://raw.githubusercontent.com/Loyalsoldier/geoip/release/Country.mmdb https://raw.githubusercontent.com/Loyalsoldier/v2ray-rules-dat/release/geosite.dat + Geo Files + Import GeoIP Database + Press to import... + Import GeoSite Database + Import Country Database diff --git a/design/src/main/res/values-zh-rHK/strings.xml b/design/src/main/res/values-zh-rHK/strings.xml index 3ac6d9ee..de2a3080 100644 --- a/design/src/main/res/values-zh-rHK/strings.xml +++ b/design/src/main/res/values-zh-rHK/strings.xml @@ -231,4 +231,9 @@ Geosite Url Prefer h3 Port Whitelist + Geo Files + Import GeoIP Database + Press to import... + Import GeoSite Database + Import Country Database \ No newline at end of file diff --git a/design/src/main/res/values-zh-rTW/strings.xml b/design/src/main/res/values-zh-rTW/strings.xml index dee57003..fcefb208 100644 --- a/design/src/main/res/values-zh-rTW/strings.xml +++ b/design/src/main/res/values-zh-rTW/strings.xml @@ -231,4 +231,9 @@ Geosite Url Prefer h3 Port Whitelist + Geo Files + Import GeoIP Database + Press to import... + Import GeoSite Database + Import Country Database diff --git a/design/src/main/res/values-zh/strings.xml b/design/src/main/res/values-zh/strings.xml index 8fc03d6d..98e27c6b 100644 --- a/design/src/main/res/values-zh/strings.xml +++ b/design/src/main/res/values-zh/strings.xml @@ -234,4 +234,9 @@ Geosite Url Prefer h3 Port Whitelist + Geo Files + 导入 GeoIP 数据库 + Press to import... + 导入 GeoSite 数据库 + 导入 Country 数据库 \ No newline at end of file diff --git a/design/src/main/res/values/strings.xml b/design/src/main/res/values/strings.xml index ba76f44b..9a9232c0 100644 --- a/design/src/main/res/values/strings.xml +++ b/design/src/main/res/values/strings.xml @@ -299,4 +299,9 @@ https://raw.githubusercontent.com/MetaCubeX/meta-rules-dat/release/geoip.dat https://raw.githubusercontent.com/MetaCubeX/meta-rules-dat/release/country.mmdb https://raw.githubusercontent.com/MetaCubeX/meta-rules-dat/release/geosite.dat + Geo Files + Import GeoIP Database + Press to import... + Import GeoSite Database + Import Country Database