Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
75de6ce041 | ||
|
|
4417a85ef7 | ||
|
|
9e0b67c04a | ||
|
|
8cd203e7b0 | ||
|
|
211160f7f8 | ||
|
|
2afc7262eb | ||
|
|
94a0282c70 | ||
|
|
96b78829ab | ||
|
|
35d5a90b2a | ||
|
|
50d73ad498 | ||
|
|
d1f8bc3062 | ||
|
|
d3353b1aa5 | ||
|
|
cd48fb4e08 | ||
|
|
f70738ecf5 | ||
|
|
2d63466597 | ||
|
|
ecf67443fb | ||
|
|
43987ad111 | ||
|
|
5fbd6c6196 | ||
|
|
1ec7f1f7e9 | ||
|
|
e958e7f675 | ||
|
|
68113e694e |
No files matched your search
@@ -31,7 +31,7 @@ if applicable, add logs to help detect problem
|
||||
- Device: [e.g. Pixel 4]
|
||||
- ROM: [e.g: AOSP]
|
||||
- ROM Version:
|
||||
- Android Version [e.g. Oreo]
|
||||
- Android Version [e.g. 10]
|
||||
|
||||
**Application Info (please complete the following information):**
|
||||
|
||||
@@ -40,4 +40,4 @@ if applicable, add logs to help detect problem
|
||||
- Distribution Channel: [e.g. Google Play]
|
||||
|
||||
**Additional context**
|
||||
Add any other context about the problem here.
|
||||
Add any other context about the problem here.
|
||||
@@ -24,13 +24,12 @@ assignees: ''
|
||||
如果适用, 上传屏幕截图以帮助描述错误
|
||||
|
||||
**日志**
|
||||
|
||||
如果适用, 上传日志以帮助侦测错误
|
||||
|
||||
**设备信息 (请完成一下信息):**
|
||||
**设备信息 (请完成以下信息):**
|
||||
- 机型: [例如: Pixel 4]
|
||||
- 系统/ROM: [例如: MIUI 11]
|
||||
- Android 版本 [例如: Oreo]
|
||||
- Android 版本 [例如: 10]
|
||||
- ROM版本 [例如: 20.3.19]
|
||||
|
||||
**应用信息**
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
blank_issues_enabled: false
|
||||
@@ -42,7 +42,7 @@ class MainApplication : Application() {
|
||||
if (!report.stackTrace.contains("DeadObjectException"))
|
||||
return mutableListOf()
|
||||
|
||||
val logcat = LogcatDumper.dump().joinToString(separator = "\n")
|
||||
val logcat = LogcatDumper.dump()
|
||||
|
||||
return mutableListOf(
|
||||
ErrorAttachmentLog.attachmentWithText(logcat, "logcat.txt")
|
||||
|
||||
@@ -1,34 +1,20 @@
|
||||
package com.github.kr328.clash.dump
|
||||
|
||||
object LogcatDumper {
|
||||
fun dump(): List<String> {
|
||||
fun dump(): String {
|
||||
return try {
|
||||
val process =
|
||||
Runtime.getRuntime().exec(arrayOf("logcat", "-d", "-s", "-v", "raw", "Go"))
|
||||
Runtime.getRuntime().exec(arrayOf("logcat", "-d", "-s", "-v", "raw", "Go", "AndroidRuntime", "DEBUG"))
|
||||
|
||||
val result = process.inputStream.bufferedReader().useLines {
|
||||
var list = mutableListOf<String>()
|
||||
var capture = false
|
||||
|
||||
it.forEach { line ->
|
||||
if (line.startsWith("panic")) {
|
||||
capture = true
|
||||
|
||||
list = mutableListOf()
|
||||
}
|
||||
|
||||
if (capture)
|
||||
list.add(line)
|
||||
}
|
||||
|
||||
list
|
||||
val result = process.inputStream.use {
|
||||
it.reader().readText()
|
||||
}
|
||||
|
||||
process.waitFor()
|
||||
|
||||
result
|
||||
} catch (e: Exception) {
|
||||
emptyList()
|
||||
""
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -114,12 +114,14 @@ class ProfileEditFragment(
|
||||
|
||||
if (s.isBlank()) {
|
||||
content = ""
|
||||
interval = 0
|
||||
return@onTextChanged
|
||||
}
|
||||
|
||||
val value = s.toIntOrNull()
|
||||
if (value == null || value < 15) {
|
||||
content = ""
|
||||
interval = 0
|
||||
Snackbar.make(view, R.string.invalid_interval, Snackbar.LENGTH_LONG)
|
||||
.show()
|
||||
return@onTextChanged
|
||||
|
||||
+3
-3
@@ -1,7 +1,7 @@
|
||||
// Top-level build file where you can add configuration options common to all sub-projects/modules.
|
||||
|
||||
buildscript {
|
||||
val kotlinVersion = "1.3.71"
|
||||
val kotlinVersion = "1.3.72"
|
||||
|
||||
rootProject.extra.apply {
|
||||
this["gBuildToolsVersion"] = "29.0.3"
|
||||
@@ -10,8 +10,8 @@ buildscript {
|
||||
this["gMinSdkVersion"] = 24
|
||||
this["gTargetSdkVersion"] = 29
|
||||
|
||||
this["gVersionCode"] = 10206
|
||||
this["gVersionName"] = "1.2.6"
|
||||
this["gVersionCode"] = 10209
|
||||
this["gVersionName"] = "1.2.9"
|
||||
|
||||
this["gKotlinVersion"] = kotlinVersion
|
||||
this["gKotlinCoroutineVersion"] = "1.3.5"
|
||||
|
||||
@@ -20,12 +20,14 @@ func init() {
|
||||
dialer.ListenConfigHook = onNewListenConfig
|
||||
}
|
||||
|
||||
func onNewDialer(dialer *net.Dialer) {
|
||||
func onNewDialer(dialer *net.Dialer) error {
|
||||
dialer.Control = onNewSocket
|
||||
return nil
|
||||
}
|
||||
|
||||
func onNewListenConfig(listen *net.ListenConfig) {
|
||||
func onNewListenConfig(listen *net.ListenConfig) error {
|
||||
listen.Control = onNewSocket
|
||||
return nil
|
||||
}
|
||||
|
||||
func onNewSocket(_, _ string, c syscall.RawConn) error {
|
||||
|
||||
Submodule core/src/main/golang/clash updated: a3850e54ae...7eabb6651e.
@@ -4,7 +4,7 @@ go 1.13
|
||||
|
||||
require (
|
||||
github.com/Dreamacro/clash v0.0.0 // local
|
||||
github.com/kr328/tun2socket v0.0.0-20200415021819-256b721ac9a4
|
||||
github.com/kr328/tun2socket v0.0.0-20200421133501-c8848c95a7ca
|
||||
github.com/miekg/dns v1.1.29
|
||||
)
|
||||
|
||||
|
||||
@@ -8,7 +8,7 @@ github.com/go-chi/render v1.0.1/go.mod h1:pq4Rr7HbnsdaeHagklXub+p6Wd16Af5l9koip1
|
||||
github.com/gofrs/uuid v3.2.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM=
|
||||
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
|
||||
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
|
||||
github.com/kr328/tun2socket v0.0.0-20200415021819-256b721ac9a4/go.mod h1:FWfSixjrLgtK+dHkDoN6lHMNhvER24gnjUZd/wt8Z9o=
|
||||
github.com/kr328/tun2socket v0.0.0-20200421133501-c8848c95a7ca/go.mod h1:FWfSixjrLgtK+dHkDoN6lHMNhvER24gnjUZd/wt8Z9o=
|
||||
github.com/miekg/dns v1.1.29/go.mod h1:KNUDUusw/aVsxyTYZM1oqvCicbwhgbNgztCETuNZ7xM=
|
||||
github.com/oschwald/geoip2-golang v1.4.0/go.mod h1:8QwxJvRImBH+Zl6Aa6MaIcs5YdlZSTKtzmPGzQqi9ng=
|
||||
github.com/oschwald/maxminddb-golang v1.6.0/go.mod h1:DUJFucBg2cvqx42YmDa/+xHvb0elJtOm3o4aFQ/nb/w=
|
||||
|
||||
@@ -56,7 +56,7 @@ func hijackTCPDNS(conn net.Conn, endpoint *binding.Endpoint) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
if !hijackAddress.Equal(net.IPv4zero) && !hijackAddress.Equal(net.IPv6zero) && !hijackAddress.Equal(endpoint.Target.IP) {
|
||||
if !hijackAddress.Equal(net.IPv4zero) && !hijackAddress.Equal(endpoint.Target.IP) {
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -109,7 +109,7 @@ func hijackDNS(payload []byte, endpoint *binding.Endpoint, sender redirect.UDPSe
|
||||
return false
|
||||
}
|
||||
|
||||
if !hijackAddress.Equal(net.IPv4zero) && !hijackAddress.Equal(net.IPv6zero) && !hijackAddress.Equal(endpoint.Target.IP) {
|
||||
if !hijackAddress.Equal(net.IPv4zero) && !hijackAddress.Equal(endpoint.Target.IP) {
|
||||
return false
|
||||
}
|
||||
|
||||
|
||||
@@ -45,11 +45,11 @@ class ProfileReceiver : BroadcastReceiver() {
|
||||
val metadata = ProfileDao.queryById(id)?.asProfile(context) ?: return
|
||||
val service = context.getSystemService<AlarmManager>() ?: return
|
||||
|
||||
val pendingIntent = cancelNextUpdate(context, id)
|
||||
|
||||
if (metadata.interval <= 0)
|
||||
return
|
||||
|
||||
val pendingIntent = cancelNextUpdate(context, id)
|
||||
|
||||
service.set(
|
||||
AlarmManager.RTC,
|
||||
metadata.lastModified + metadata.interval,
|
||||
|
||||
@@ -108,6 +108,8 @@ class ProfileService : BaseService() {
|
||||
ProfileDao.remove(id)
|
||||
}
|
||||
|
||||
ProfileReceiver.cancelNextUpdate(service, id)
|
||||
|
||||
service.resolveProfileFile(id).delete()
|
||||
service.resolveTempProfileFile(id).delete()
|
||||
service.resolveBaseDir(id).deleteRecursively()
|
||||
|
||||
@@ -13,13 +13,9 @@ import kotlinx.coroutines.launch
|
||||
|
||||
class TunService : VpnService(), CoroutineScope by MainScope() {
|
||||
companion object {
|
||||
// from https://github.com/shadowsocks/shadowsocks-android/blob/master/core/src/main/java/com/github/shadowsocks/bg/VpnService.kt
|
||||
private const val VPN_MTU = 9000
|
||||
private const val PRIVATE_VLAN4_SUBNET = 30
|
||||
private const val PRIVATE_VLAN4_CLIENT = "172.31.255.253"
|
||||
private const val PRIVATE_VLAN4_MIRROR = "172.31.255.254"
|
||||
private const val PRIVATE_VLAN_DNS = "198.18.0.1"
|
||||
private const val VLAN_ANY = "0.0.0.0/0"
|
||||
private const val TUN_MTU = 9000
|
||||
private const val TUN_DNS = "198.18.0.1"
|
||||
private const val IPV4_ANY = "0.0.0.0/0"
|
||||
}
|
||||
|
||||
private val service = this
|
||||
@@ -106,23 +102,27 @@ class TunService : VpnService(), CoroutineScope by MainScope() {
|
||||
}
|
||||
|
||||
private inner class TunConfigure(private val settings: ServiceSettings) : TunModule.Configure {
|
||||
private val network = generateTunNetwork()
|
||||
|
||||
override val builder: Builder
|
||||
get() = Builder()
|
||||
override val mtu: Int
|
||||
get() = VPN_MTU
|
||||
override val gateway: String
|
||||
get() = "$PRIVATE_VLAN4_CLIENT/$PRIVATE_VLAN4_SUBNET"
|
||||
override val mirror: String
|
||||
get() = PRIVATE_VLAN4_MIRROR
|
||||
get() = TUN_MTU
|
||||
override val gateway: String = network.copyOf().apply {
|
||||
this[3] = (this[3] + 1).toByte()
|
||||
}.asAddressString("/30")
|
||||
override val mirror: String = network.copyOf().apply {
|
||||
this[3] = (this[3] + 2).toByte()
|
||||
}.asAddressString()
|
||||
override val route: List<String>
|
||||
get() {
|
||||
return if (settings.get(ServiceSettings.BYPASS_PRIVATE_NETWORK))
|
||||
resources.getStringArray(R.array.bypass_private_route).toList()
|
||||
else
|
||||
listOf(VLAN_ANY)
|
||||
listOf(IPV4_ANY)
|
||||
}
|
||||
override val dnsAddress: String
|
||||
get() = PRIVATE_VLAN_DNS
|
||||
get() = TUN_DNS
|
||||
override val dnsHijacking: Boolean
|
||||
get() = settings.get(ServiceSettings.DNS_HIJACKING)
|
||||
override val allowApplications: Collection<String>
|
||||
@@ -139,7 +139,30 @@ class TunService : VpnService(), CoroutineScope by MainScope() {
|
||||
}
|
||||
|
||||
override fun onCreateTunFailure() {
|
||||
stopSelfForReason("Start VPN rejected by the system")
|
||||
stopSelfForReason("Establish VPN rejected by system")
|
||||
}
|
||||
|
||||
private fun generateTunNetwork(): ByteArray {
|
||||
val result = ByteArray(4)
|
||||
|
||||
// 18 bit namespace
|
||||
val offset = UserUtils.currentUserId % 0x40000
|
||||
val network = (0x3FFFF - offset) shl 2
|
||||
|
||||
result[0] = (172 or (network shr 24) and 0xFF).toByte()
|
||||
result[1] = (16 or (network shr 16) and 0xFF).toByte()
|
||||
result[2] = (0 or (network shr 8) and 0xFF).toByte()
|
||||
result[3] = (0 or (network shr 0) and 0xFF).toByte()
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
private fun ByteArray.asAddressString(suffix: String = ""): String {
|
||||
return "${this[0].toUnsignedString()}.${this[1].toUnsignedString()}.${this[2].toUnsignedString()}.${this[3].toUnsignedString()}$suffix"
|
||||
}
|
||||
|
||||
private fun Byte.toUnsignedString(): String {
|
||||
return (this.toInt() and 0xFF).toString()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -5,10 +5,12 @@ import android.net.VpnService
|
||||
import android.os.Build
|
||||
import com.github.kr328.clash.common.Global
|
||||
import com.github.kr328.clash.common.ids.PendingIds
|
||||
import com.github.kr328.clash.common.utils.Log
|
||||
import com.github.kr328.clash.core.Clash
|
||||
import com.github.kr328.clash.service.util.parseCIDR
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.withContext
|
||||
import java.lang.NullPointerException
|
||||
|
||||
class TunModule(private val service: VpnService) : Module() {
|
||||
interface Configure {
|
||||
@@ -33,6 +35,8 @@ class TunModule(private val service: VpnService) : Module() {
|
||||
|
||||
val builder = c.builder
|
||||
|
||||
Log.i("Tun start at ${c.gateway}, mirror at ${c.mirror}")
|
||||
|
||||
parseCIDR(c.gateway).let {
|
||||
builder.addAddress(it.ip, it.prefix)
|
||||
}
|
||||
@@ -63,8 +67,12 @@ class TunModule(private val service: VpnService) : Module() {
|
||||
builder.setMetered(false)
|
||||
}
|
||||
|
||||
val fd = builder.establish()
|
||||
?: return@withContext c.onCreateTunFailure()
|
||||
val fd = try {
|
||||
builder.establish() ?: throw NullPointerException()
|
||||
}
|
||||
catch (e: Exception) {
|
||||
return@withContext c.onCreateTunFailure()
|
||||
}
|
||||
|
||||
if (c.dnsHijacking) {
|
||||
Clash.startTunDevice(
|
||||
|
||||
@@ -4,9 +4,7 @@ import android.content.Context
|
||||
import androidx.room.Room
|
||||
import androidx.room.RoomDatabase
|
||||
import com.github.kr328.clash.common.Global
|
||||
import com.github.kr328.clash.service.data.DatabaseMigrations.VERSION_1_2
|
||||
import com.github.kr328.clash.service.data.DatabaseMigrations.VERSION_2_3
|
||||
import com.github.kr328.clash.service.data.DatabaseMigrations.VERSION_3_4
|
||||
import com.github.kr328.clash.service.data.migrations.MIGRATIONS
|
||||
import androidx.room.Database as DatabaseMetadata
|
||||
|
||||
@DatabaseMetadata(
|
||||
@@ -26,7 +24,7 @@ abstract class Database : RoomDatabase() {
|
||||
context.applicationContext,
|
||||
Database::class.java,
|
||||
"clash-config"
|
||||
).addMigrations(VERSION_1_2, VERSION_2_3, VERSION_3_4).build()
|
||||
).addMigrations(*MIGRATIONS).build()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,327 +0,0 @@
|
||||
package com.github.kr328.clash.service.data
|
||||
|
||||
import android.content.ContentValues
|
||||
import android.content.Context
|
||||
import android.database.sqlite.SQLiteDatabase
|
||||
import android.database.sqlite.SQLiteDatabase.CONFLICT_ABORT
|
||||
import android.database.sqlite.SQLiteDatabase.CONFLICT_REPLACE
|
||||
import androidx.core.app.NotificationManagerCompat
|
||||
import androidx.core.content.edit
|
||||
import androidx.core.database.getStringOrNull
|
||||
import androidx.room.migration.Migration
|
||||
import androidx.sqlite.db.SupportSQLiteDatabase
|
||||
import com.github.kr328.clash.common.Global
|
||||
import com.github.kr328.clash.common.utils.Log
|
||||
import com.github.kr328.clash.service.Constants
|
||||
import com.github.kr328.clash.service.settings.ServiceSettings
|
||||
import com.github.kr328.clash.service.util.resolveBaseDir
|
||||
import com.github.kr328.clash.service.util.resolveProfileFile
|
||||
import java.io.File
|
||||
|
||||
object DatabaseMigrations {
|
||||
val VERSION_1_2 = object : Migration(1, 2) {
|
||||
private fun process(database: SupportSQLiteDatabase) {
|
||||
database.execSQL("ALTER TABLE profiles RENAME TO _profiles")
|
||||
database.execSQL("ALTER TABLE profile_select_proxies RENAME TO _profile_select_proxies")
|
||||
|
||||
database.execSQL("CREATE TABLE IF NOT EXISTS `profiles` (`name` TEXT NOT NULL, `type` INTEGER NOT NULL, `uri` TEXT NOT NULL, `source` TEXT, `active` INTEGER NOT NULL, `last_update` INTEGER NOT NULL, `update_interval` INTEGER NOT NULL, `id` INTEGER NOT NULL, PRIMARY KEY(`id`))")
|
||||
database.execSQL("CREATE TABLE IF NOT EXISTS `profile_select_proxies` (`profile_id` INTEGER NOT NULL, `proxy` TEXT NOT NULL, `selected` TEXT NOT NULL, PRIMARY KEY(`profile_id`, `proxy`), FOREIGN KEY(`profile_id`) REFERENCES `profiles`(`id`) ON UPDATE CASCADE ON DELETE CASCADE )")
|
||||
|
||||
database.query("SELECT name, token, file, active, last_update, id FROM _profiles")
|
||||
.use { cursor ->
|
||||
|
||||
Global.application.filesDir.resolve(Constants.CLASH_DIR).listFiles()?.forEach {
|
||||
it.deleteRecursively()
|
||||
}
|
||||
|
||||
cursor.moveToFirst()
|
||||
while (!cursor.isAfterLast) {
|
||||
// old
|
||||
// name, token, file, active, last_update, id
|
||||
val name = cursor.getString(0)
|
||||
val token = cursor.getString(1)
|
||||
val file = cursor.getString(2)
|
||||
val active = cursor.getInt(3)
|
||||
val lastUpdate = cursor.getLong(4)
|
||||
val id = cursor.getLong(5)
|
||||
|
||||
// new
|
||||
// name, type, uri, source, active, last_update, update_interval, id
|
||||
val type = when {
|
||||
token.startsWith("url") -> ProfileEntity.TYPE_URL
|
||||
token.startsWith("file") -> ProfileEntity.TYPE_FILE
|
||||
else -> ProfileEntity.TYPE_UNKNOWN
|
||||
}
|
||||
|
||||
File(file).renameTo(Global.application.resolveProfileFile(id))
|
||||
Global.application.resolveBaseDir(id).mkdirs()
|
||||
|
||||
database.insert("profiles",
|
||||
CONFLICT_ABORT,
|
||||
ContentValues().apply {
|
||||
put("name", name)
|
||||
put("type", type)
|
||||
put("uri", token.removePrefix("url|").removePrefix("file|"))
|
||||
putNull("source")
|
||||
put("active", active)
|
||||
put("last_update", lastUpdate)
|
||||
put("update_interval", 0)
|
||||
put("id", id)
|
||||
})
|
||||
|
||||
cursor.moveToNext()
|
||||
}
|
||||
}
|
||||
|
||||
database.query("SELECT profile_id, proxy, selected FROM _profile_select_proxies ORDER BY id")
|
||||
.use { cursor ->
|
||||
cursor.moveToFirst()
|
||||
while (!cursor.isAfterLast) {
|
||||
// old
|
||||
// profile_id, proxy, selected, id
|
||||
val profileId = cursor.getLong(0)
|
||||
val proxy: String = cursor.getString(1)
|
||||
val selected = cursor.getString(2)
|
||||
|
||||
// new
|
||||
// profile_id, proxy, selected
|
||||
|
||||
database.insert("profile_select_proxies",
|
||||
CONFLICT_REPLACE,
|
||||
ContentValues().apply {
|
||||
put("profile_id", profileId)
|
||||
put("proxy", proxy)
|
||||
put("selected", selected)
|
||||
})
|
||||
|
||||
cursor.moveToNext()
|
||||
}
|
||||
}
|
||||
|
||||
database.execSQL("DROP TABLE IF EXISTS _profiles")
|
||||
database.execSQL("DROP TABLE IF EXISTS _profile_select_proxies")
|
||||
|
||||
// Migration settings
|
||||
val oldSettings = Global.application
|
||||
.getSharedPreferences("clash_service", Context.MODE_PRIVATE)
|
||||
val newSettings = ServiceSettings(
|
||||
Global.application
|
||||
.getSharedPreferences(Constants.SERVICE_SETTING_FILE_NAME, Context.MODE_PRIVATE)
|
||||
)
|
||||
|
||||
val accessMode = oldSettings
|
||||
.getInt("key_access_control_mode", 0)
|
||||
val accessPackages = oldSettings
|
||||
.getStringSet("ley_access_control_apps", emptySet())!! // just typo :)
|
||||
val dnsHijack = oldSettings
|
||||
.getBoolean("key_dns_hijacking_enabled", true)
|
||||
val bypassPrivate = oldSettings
|
||||
.getBoolean("key_bypass_private_network", true)
|
||||
|
||||
oldSettings.edit {
|
||||
clear()
|
||||
}
|
||||
|
||||
newSettings.commit {
|
||||
val newAccessMode = when (accessMode) {
|
||||
0 -> ServiceSettings.ACCESS_CONTROL_MODE_ALL
|
||||
1 -> ServiceSettings.ACCESS_CONTROL_MODE_WHITELIST
|
||||
2 -> ServiceSettings.ACCESS_CONTROL_MODE_BLACKLIST
|
||||
else -> ServiceSettings.ACCESS_CONTROL_MODE_ALL
|
||||
}
|
||||
|
||||
put(ServiceSettings.ACCESS_CONTROL_MODE, newAccessMode)
|
||||
put(ServiceSettings.ACCESS_CONTROL_PACKAGES, accessPackages)
|
||||
put(ServiceSettings.DNS_HIJACKING, dnsHijack)
|
||||
put(ServiceSettings.BYPASS_PRIVATE_NETWORK, bypassPrivate)
|
||||
}
|
||||
}
|
||||
|
||||
override fun migrate(database: SupportSQLiteDatabase) {
|
||||
try {
|
||||
process(database)
|
||||
Log.i("Database Migrated 1 -> 2")
|
||||
} catch (e: Exception) {
|
||||
Log.e("Migration failure", e)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
val VERSION_2_3 = object : Migration(2, 3) {
|
||||
override fun migrate(database: SupportSQLiteDatabase) {
|
||||
try {
|
||||
database.execSQL("ALTER TABLE profile_select_proxies RENAME TO _selected_proxies")
|
||||
database.execSQL("ALTER TABLE profiles RENAME TO _profiles")
|
||||
|
||||
database.execSQL("CREATE TABLE IF NOT EXISTS `profiles` (`name` TEXT NOT NULL, `type` INTEGER NOT NULL, `uri` TEXT NOT NULL, `source` TEXT, `active` INTEGER NOT NULL, `interval` INTEGER NOT NULL, `id` INTEGER NOT NULL, PRIMARY KEY(`id`))")
|
||||
database.execSQL("CREATE TABLE IF NOT EXISTS `selected_proxies` (`profile_id` INTEGER NOT NULL, `proxy` TEXT NOT NULL, `selected` TEXT NOT NULL, PRIMARY KEY(`profile_id`, `proxy`), FOREIGN KEY(`profile_id`) REFERENCES `profiles`(`id`) ON UPDATE CASCADE ON DELETE CASCADE )")
|
||||
|
||||
database.query("SELECT name, type, uri, source, active, update_interval, id FROM _profiles")
|
||||
.use { cursor ->
|
||||
cursor.moveToFirst()
|
||||
while (!cursor.isAfterLast) {
|
||||
// old
|
||||
// name, type, uri, source, active, last_update, update_interval(seconds), id
|
||||
// new
|
||||
// name, type, uri, source, active, interval(millis seconds), id
|
||||
val name = cursor.getString(0)
|
||||
val type = cursor.getInt(1)
|
||||
val uri = cursor.getString(2)
|
||||
val source = cursor.getStringOrNull(3)
|
||||
val active = cursor.getInt(4)
|
||||
val interval = cursor.getLong(5)
|
||||
val id = cursor.getLong(6)
|
||||
|
||||
database.insert("profiles",
|
||||
CONFLICT_ABORT,
|
||||
ContentValues().apply {
|
||||
put("name", name)
|
||||
put("type", type)
|
||||
put("uri", uri)
|
||||
put("source", source)
|
||||
put("active", active)
|
||||
put("interval", interval * 1000)
|
||||
put("id", id)
|
||||
})
|
||||
|
||||
cursor.moveToNext()
|
||||
}
|
||||
}
|
||||
|
||||
database.query("SELECT profile_id, proxy, selected FROM _selected_proxies")
|
||||
.use { cursor ->
|
||||
cursor.moveToFirst()
|
||||
while (!cursor.isAfterLast) {
|
||||
// just copy
|
||||
// profile_id, proxy, selected
|
||||
val profileId = cursor.getLong(0)
|
||||
val proxy = cursor.getString(1)
|
||||
val selected = cursor.getString(2)
|
||||
|
||||
database.insert("selected_proxies",
|
||||
CONFLICT_REPLACE,
|
||||
ContentValues().apply {
|
||||
put("profile_id", profileId)
|
||||
put("proxy", proxy)
|
||||
put("selected", selected)
|
||||
})
|
||||
|
||||
cursor.moveToNext()
|
||||
}
|
||||
}
|
||||
|
||||
database.execSQL("DROP TABLE IF EXISTS _profiles")
|
||||
database.execSQL("DROP TABLE IF EXISTS _selected_proxies")
|
||||
|
||||
val uiSp = Global.application
|
||||
.getSharedPreferences("ui", Context.MODE_PRIVATE)
|
||||
val srvSp = Global.application
|
||||
.getSharedPreferences("service", Context.MODE_PRIVATE)
|
||||
|
||||
srvSp.edit {
|
||||
putBoolean("enable_vpn", uiSp.getBoolean("enable_vpn", true))
|
||||
}
|
||||
|
||||
NotificationManagerCompat.from(Global.application).apply {
|
||||
deleteNotificationChannel("profile_service_status")
|
||||
deleteNotificationChannel("profile_service_result")
|
||||
}
|
||||
|
||||
Log.i("Database Migrated 2 -> 3")
|
||||
} catch (e: Exception) {
|
||||
Log.e("Migration failure", e)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
val VERSION_3_4 = object : Migration(3, 4) {
|
||||
override fun migrate(database: SupportSQLiteDatabase) {
|
||||
try {
|
||||
val profiles = mutableListOf<ProfileEntity>()
|
||||
try {
|
||||
database.query("SELECT name, type, uri, source, active, interval, id FROM profiles")
|
||||
.use { cursor ->
|
||||
cursor.moveToFirst()
|
||||
while (!cursor.isAfterLast) {
|
||||
// old
|
||||
// name, type, uri, source, active, last_update, update_interval(seconds), id
|
||||
// new
|
||||
// name, type, uri, source, active, interval(millis seconds), id
|
||||
val name = cursor.getString(0)
|
||||
val type = cursor.getInt(1)
|
||||
val uri = cursor.getString(2)
|
||||
val source = cursor.getStringOrNull(3)
|
||||
val active = cursor.getInt(4)
|
||||
val interval = cursor.getLong(5)
|
||||
val id = cursor.getLong(6)
|
||||
|
||||
profiles.add(ProfileEntity(name, type, uri, source, active != 0, interval, id))
|
||||
|
||||
cursor.moveToNext()
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (e: Exception) {
|
||||
Log.w("Query old data failure", e)
|
||||
}
|
||||
|
||||
val selectedProxies = mutableListOf<SelectedProxyEntity>()
|
||||
|
||||
try {
|
||||
database.query("SELECT profile_id, proxy, selected FROM selected_proxies")
|
||||
.use { cursor ->
|
||||
cursor.moveToFirst()
|
||||
while (!cursor.isAfterLast) {
|
||||
// just copy
|
||||
// profile_id, proxy, selected
|
||||
val profileId = cursor.getLong(0)
|
||||
val proxy = cursor.getString(1)
|
||||
val selected = cursor.getString(2)
|
||||
|
||||
selectedProxies.add(SelectedProxyEntity(profileId, proxy, selected))
|
||||
|
||||
cursor.moveToNext()
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (e: Exception) {
|
||||
Log.w("Query old data failure", e)
|
||||
}
|
||||
|
||||
database.execSQL("DROP TABLE IF EXISTS profile_select_proxies")
|
||||
database.execSQL("DROP TABLE IF EXISTS selected_proxies")
|
||||
database.execSQL("DROP TABLE IF EXISTS profiles")
|
||||
database.execSQL("DROP TABLE IF EXISTS _profile_select_proxies")
|
||||
database.execSQL("DROP TABLE IF EXISTS _selected_proxies")
|
||||
database.execSQL("DROP TABLE IF EXISTS _profiles")
|
||||
|
||||
database.execSQL("CREATE TABLE IF NOT EXISTS `profiles` (`name` TEXT NOT NULL, `type` INTEGER NOT NULL, `uri` TEXT NOT NULL, `source` TEXT, `active` INTEGER NOT NULL, `interval` INTEGER NOT NULL, `id` INTEGER NOT NULL, PRIMARY KEY(`id`))")
|
||||
database.execSQL("CREATE TABLE IF NOT EXISTS `selected_proxies` (`profile_id` INTEGER NOT NULL, `proxy` TEXT NOT NULL, `selected` TEXT NOT NULL, PRIMARY KEY(`profile_id`, `proxy`), FOREIGN KEY(`profile_id`) REFERENCES `profiles`(`id`) ON UPDATE CASCADE ON DELETE CASCADE )")
|
||||
|
||||
profiles.forEach {
|
||||
database.insert("profiles", CONFLICT_ABORT, ContentValues().apply {
|
||||
put("name", it.name)
|
||||
put("type", it.type)
|
||||
put("uri", it.uri)
|
||||
put("source", it.source)
|
||||
put("active", it.active)
|
||||
put("interval", it.interval)
|
||||
put("id", it.id)
|
||||
})
|
||||
}
|
||||
|
||||
selectedProxies.forEach {
|
||||
database.insert("selected_proxies", CONFLICT_REPLACE, ContentValues().apply {
|
||||
put("profile_id", it.profileId)
|
||||
put("proxy", it.proxy)
|
||||
put("selected", it.selected)
|
||||
})
|
||||
}
|
||||
|
||||
Log.i("Database Migrated 3 -> 4")
|
||||
} catch (e: Exception) {
|
||||
Log.e("Migration failure", e)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,144 @@
|
||||
package com.github.kr328.clash.service.data.migrations
|
||||
|
||||
import android.content.ContentValues
|
||||
import android.content.Context
|
||||
import android.database.sqlite.SQLiteDatabase
|
||||
import androidx.core.content.edit
|
||||
import androidx.room.migration.Migration
|
||||
import androidx.sqlite.db.SupportSQLiteDatabase
|
||||
import com.github.kr328.clash.common.Global
|
||||
import com.github.kr328.clash.common.utils.Log
|
||||
import com.github.kr328.clash.service.Constants
|
||||
import com.github.kr328.clash.service.data.ProfileEntity
|
||||
import com.github.kr328.clash.service.settings.ServiceSettings
|
||||
import com.github.kr328.clash.service.util.resolveBaseDir
|
||||
import com.github.kr328.clash.service.util.resolveProfileFile
|
||||
import java.io.File
|
||||
|
||||
object Migration12: Migration(1, 2) {
|
||||
private fun process(database: SupportSQLiteDatabase) {
|
||||
database.execSQL("ALTER TABLE profiles RENAME TO _profiles")
|
||||
database.execSQL("ALTER TABLE profile_select_proxies RENAME TO _profile_select_proxies")
|
||||
|
||||
database.execSQL("CREATE TABLE IF NOT EXISTS `profiles` (`name` TEXT NOT NULL, `type` INTEGER NOT NULL, `uri` TEXT NOT NULL, `source` TEXT, `active` INTEGER NOT NULL, `last_update` INTEGER NOT NULL, `update_interval` INTEGER NOT NULL, `id` INTEGER NOT NULL, PRIMARY KEY(`id`))")
|
||||
database.execSQL("CREATE TABLE IF NOT EXISTS `profile_select_proxies` (`profile_id` INTEGER NOT NULL, `proxy` TEXT NOT NULL, `selected` TEXT NOT NULL, PRIMARY KEY(`profile_id`, `proxy`), FOREIGN KEY(`profile_id`) REFERENCES `profiles`(`id`) ON UPDATE CASCADE ON DELETE CASCADE )")
|
||||
|
||||
database.query("SELECT name, token, file, active, last_update, id FROM _profiles")
|
||||
.use { cursor ->
|
||||
|
||||
Global.application.filesDir.resolve(Constants.CLASH_DIR).listFiles()?.forEach {
|
||||
it.deleteRecursively()
|
||||
}
|
||||
|
||||
cursor.moveToFirst()
|
||||
while (!cursor.isAfterLast) {
|
||||
// old
|
||||
// name, token, file, active, last_update, id
|
||||
val name = cursor.getString(0)
|
||||
val token = cursor.getString(1)
|
||||
val file = cursor.getString(2)
|
||||
val active = cursor.getInt(3)
|
||||
val lastUpdate = cursor.getLong(4)
|
||||
val id = cursor.getLong(5)
|
||||
|
||||
// new
|
||||
// name, type, uri, source, active, last_update, update_interval, id
|
||||
val type = when {
|
||||
token.startsWith("url") -> ProfileEntity.TYPE_URL
|
||||
token.startsWith("file") -> ProfileEntity.TYPE_FILE
|
||||
else -> ProfileEntity.TYPE_UNKNOWN
|
||||
}
|
||||
|
||||
File(file).renameTo(Global.application.resolveProfileFile(id))
|
||||
Global.application.resolveBaseDir(id).mkdirs()
|
||||
|
||||
database.insert("profiles",
|
||||
SQLiteDatabase.CONFLICT_ABORT,
|
||||
ContentValues().apply {
|
||||
put("name", name)
|
||||
put("type", type)
|
||||
put("uri", token.removePrefix("url|").removePrefix("file|"))
|
||||
putNull("source")
|
||||
put("active", active)
|
||||
put("last_update", lastUpdate)
|
||||
put("update_interval", 0)
|
||||
put("id", id)
|
||||
})
|
||||
|
||||
cursor.moveToNext()
|
||||
}
|
||||
}
|
||||
|
||||
database.query("SELECT profile_id, proxy, selected FROM _profile_select_proxies ORDER BY id")
|
||||
.use { cursor ->
|
||||
cursor.moveToFirst()
|
||||
while (!cursor.isAfterLast) {
|
||||
// old
|
||||
// profile_id, proxy, selected, id
|
||||
val profileId = cursor.getLong(0)
|
||||
val proxy: String = cursor.getString(1)
|
||||
val selected = cursor.getString(2)
|
||||
|
||||
// new
|
||||
// profile_id, proxy, selected
|
||||
|
||||
database.insert("profile_select_proxies",
|
||||
SQLiteDatabase.CONFLICT_REPLACE,
|
||||
ContentValues().apply {
|
||||
put("profile_id", profileId)
|
||||
put("proxy", proxy)
|
||||
put("selected", selected)
|
||||
})
|
||||
|
||||
cursor.moveToNext()
|
||||
}
|
||||
}
|
||||
|
||||
database.execSQL("DROP TABLE IF EXISTS _profiles")
|
||||
database.execSQL("DROP TABLE IF EXISTS _profile_select_proxies")
|
||||
|
||||
// Migration settings
|
||||
val oldSettings = Global.application
|
||||
.getSharedPreferences("clash_service", Context.MODE_PRIVATE)
|
||||
val newSettings = ServiceSettings(
|
||||
Global.application
|
||||
.getSharedPreferences(Constants.SERVICE_SETTING_FILE_NAME, Context.MODE_PRIVATE)
|
||||
)
|
||||
|
||||
val accessMode = oldSettings
|
||||
.getInt("key_access_control_mode", 0)
|
||||
val accessPackages = oldSettings
|
||||
.getStringSet("ley_access_control_apps", emptySet())!! // just typo :)
|
||||
val dnsHijack = oldSettings
|
||||
.getBoolean("key_dns_hijacking_enabled", true)
|
||||
val bypassPrivate = oldSettings
|
||||
.getBoolean("key_bypass_private_network", true)
|
||||
|
||||
oldSettings.edit {
|
||||
clear()
|
||||
}
|
||||
|
||||
newSettings.commit {
|
||||
val newAccessMode = when (accessMode) {
|
||||
0 -> ServiceSettings.ACCESS_CONTROL_MODE_ALL
|
||||
1 -> ServiceSettings.ACCESS_CONTROL_MODE_WHITELIST
|
||||
2 -> ServiceSettings.ACCESS_CONTROL_MODE_BLACKLIST
|
||||
else -> ServiceSettings.ACCESS_CONTROL_MODE_ALL
|
||||
}
|
||||
|
||||
put(ServiceSettings.ACCESS_CONTROL_MODE, newAccessMode)
|
||||
put(ServiceSettings.ACCESS_CONTROL_PACKAGES, accessPackages)
|
||||
put(ServiceSettings.DNS_HIJACKING, dnsHijack)
|
||||
put(ServiceSettings.BYPASS_PRIVATE_NETWORK, bypassPrivate)
|
||||
}
|
||||
}
|
||||
|
||||
override fun migrate(database: SupportSQLiteDatabase) {
|
||||
try {
|
||||
process(database)
|
||||
Log.i("Database Migrated 1 -> 2")
|
||||
} catch (e: Exception) {
|
||||
Log.e("Migration failure", e)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
package com.github.kr328.clash.service.data.migrations
|
||||
|
||||
import android.content.ContentValues
|
||||
import android.content.Context
|
||||
import android.database.sqlite.SQLiteDatabase
|
||||
import androidx.core.app.NotificationManagerCompat
|
||||
import androidx.core.content.edit
|
||||
import androidx.core.database.getStringOrNull
|
||||
import androidx.room.migration.Migration
|
||||
import androidx.sqlite.db.SupportSQLiteDatabase
|
||||
import com.github.kr328.clash.common.Global
|
||||
import com.github.kr328.clash.common.utils.Log
|
||||
|
||||
object Migration23: Migration(2, 3) {
|
||||
override fun migrate(database: SupportSQLiteDatabase) {
|
||||
try {
|
||||
database.execSQL("ALTER TABLE profile_select_proxies RENAME TO _selected_proxies")
|
||||
database.execSQL("ALTER TABLE profiles RENAME TO _profiles")
|
||||
|
||||
database.execSQL("CREATE TABLE IF NOT EXISTS `profiles` (`name` TEXT NOT NULL, `type` INTEGER NOT NULL, `uri` TEXT NOT NULL, `source` TEXT, `active` INTEGER NOT NULL, `interval` INTEGER NOT NULL, `id` INTEGER NOT NULL, PRIMARY KEY(`id`))")
|
||||
database.execSQL("CREATE TABLE IF NOT EXISTS `selected_proxies` (`profile_id` INTEGER NOT NULL, `proxy` TEXT NOT NULL, `selected` TEXT NOT NULL, PRIMARY KEY(`profile_id`, `proxy`), FOREIGN KEY(`profile_id`) REFERENCES `profiles`(`id`) ON UPDATE CASCADE ON DELETE CASCADE )")
|
||||
|
||||
database.query("SELECT name, type, uri, source, active, update_interval, id FROM _profiles")
|
||||
.use { cursor ->
|
||||
cursor.moveToFirst()
|
||||
while (!cursor.isAfterLast) {
|
||||
// old
|
||||
// name, type, uri, source, active, last_update, update_interval(seconds), id
|
||||
// new
|
||||
// name, type, uri, source, active, interval(millis seconds), id
|
||||
val name = cursor.getString(0)
|
||||
val type = cursor.getInt(1)
|
||||
val uri = cursor.getString(2)
|
||||
val source = cursor.getStringOrNull(3)
|
||||
val active = cursor.getInt(4)
|
||||
val interval = cursor.getLong(5)
|
||||
val id = cursor.getLong(6)
|
||||
|
||||
database.insert("profiles",
|
||||
SQLiteDatabase.CONFLICT_ABORT,
|
||||
ContentValues().apply {
|
||||
put("name", name)
|
||||
put("type", type)
|
||||
put("uri", uri)
|
||||
put("source", source)
|
||||
put("active", active)
|
||||
put("interval", interval * 1000)
|
||||
put("id", id)
|
||||
})
|
||||
|
||||
cursor.moveToNext()
|
||||
}
|
||||
}
|
||||
|
||||
database.query("SELECT profile_id, proxy, selected FROM _selected_proxies")
|
||||
.use { cursor ->
|
||||
cursor.moveToFirst()
|
||||
while (!cursor.isAfterLast) {
|
||||
// just copy
|
||||
// profile_id, proxy, selected
|
||||
val profileId = cursor.getLong(0)
|
||||
val proxy = cursor.getString(1)
|
||||
val selected = cursor.getString(2)
|
||||
|
||||
database.insert("selected_proxies",
|
||||
SQLiteDatabase.CONFLICT_REPLACE,
|
||||
ContentValues().apply {
|
||||
put("profile_id", profileId)
|
||||
put("proxy", proxy)
|
||||
put("selected", selected)
|
||||
})
|
||||
|
||||
cursor.moveToNext()
|
||||
}
|
||||
}
|
||||
|
||||
database.execSQL("DROP TABLE IF EXISTS _profiles")
|
||||
database.execSQL("DROP TABLE IF EXISTS _selected_proxies")
|
||||
|
||||
val uiSp = Global.application
|
||||
.getSharedPreferences("ui", Context.MODE_PRIVATE)
|
||||
val srvSp = Global.application
|
||||
.getSharedPreferences("service", Context.MODE_PRIVATE)
|
||||
|
||||
srvSp.edit {
|
||||
putBoolean("enable_vpn", uiSp.getBoolean("enable_vpn", true))
|
||||
}
|
||||
|
||||
NotificationManagerCompat.from(Global.application).apply {
|
||||
deleteNotificationChannel("profile_service_status")
|
||||
deleteNotificationChannel("profile_service_result")
|
||||
}
|
||||
|
||||
Log.i("Database Migrated 2 -> 3")
|
||||
} catch (e: Exception) {
|
||||
Log.e("Migration failure", e)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,113 @@
|
||||
package com.github.kr328.clash.service.data.migrations
|
||||
|
||||
import android.content.ContentValues
|
||||
import android.database.sqlite.SQLiteDatabase
|
||||
import androidx.core.database.getStringOrNull
|
||||
import androidx.room.migration.Migration
|
||||
import androidx.sqlite.db.SupportSQLiteDatabase
|
||||
import com.github.kr328.clash.common.utils.Log
|
||||
import com.github.kr328.clash.service.data.ProfileEntity
|
||||
import com.github.kr328.clash.service.data.SelectedProxyEntity
|
||||
|
||||
object Migration34: Migration(3, 4) {
|
||||
override fun migrate(database: SupportSQLiteDatabase) {
|
||||
try {
|
||||
val profiles = mutableListOf<ProfileEntity>()
|
||||
try {
|
||||
database.query("SELECT name, type, uri, source, active, interval, id FROM profiles")
|
||||
.use { cursor ->
|
||||
cursor.moveToFirst()
|
||||
while (!cursor.isAfterLast) {
|
||||
// old
|
||||
// name, type, uri, source, active, last_update, update_interval(seconds), id
|
||||
// new
|
||||
// name, type, uri, source, active, interval(millis seconds), id
|
||||
val name = cursor.getString(0)
|
||||
val type = cursor.getInt(1)
|
||||
val uri = cursor.getString(2)
|
||||
val source = cursor.getStringOrNull(3)
|
||||
val active = cursor.getInt(4)
|
||||
val interval = cursor.getLong(5)
|
||||
val id = cursor.getLong(6)
|
||||
|
||||
profiles.add(ProfileEntity(name, type, uri, source, active != 0, interval, id))
|
||||
|
||||
cursor.moveToNext()
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (e: Exception) {
|
||||
Log.w("Query old data failure", e)
|
||||
}
|
||||
|
||||
val selectedProxies = mutableListOf<SelectedProxyEntity>()
|
||||
|
||||
try {
|
||||
database.query("SELECT profile_id, proxy, selected FROM selected_proxies")
|
||||
.use { cursor ->
|
||||
cursor.moveToFirst()
|
||||
while (!cursor.isAfterLast) {
|
||||
// just copy
|
||||
// profile_id, proxy, selected
|
||||
val profileId = cursor.getLong(0)
|
||||
val proxy = cursor.getString(1)
|
||||
val selected = cursor.getString(2)
|
||||
|
||||
selectedProxies.add(SelectedProxyEntity(profileId, proxy, selected))
|
||||
|
||||
cursor.moveToNext()
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (e: Exception) {
|
||||
Log.w("Query old data failure", e)
|
||||
}
|
||||
|
||||
// Clean up database
|
||||
runCatching {
|
||||
database.execSQL("DROP TABLE IF EXISTS profile_select_proxies")
|
||||
database.execSQL("DROP TABLE IF EXISTS selected_proxies")
|
||||
database.execSQL("DROP TABLE IF EXISTS profiles")
|
||||
database.execSQL("DROP TABLE IF EXISTS _profile_select_proxies")
|
||||
database.execSQL("DROP TABLE IF EXISTS _selected_proxies")
|
||||
database.execSQL("DROP TABLE IF EXISTS _profiles")
|
||||
}
|
||||
runCatching {
|
||||
database.execSQL("DROP TABLE IF EXISTS profile_select_proxies")
|
||||
database.execSQL("DROP TABLE IF EXISTS selected_proxies")
|
||||
database.execSQL("DROP TABLE IF EXISTS profiles")
|
||||
database.execSQL("DROP TABLE IF EXISTS _profile_select_proxies")
|
||||
database.execSQL("DROP TABLE IF EXISTS _selected_proxies")
|
||||
database.execSQL("DROP TABLE IF EXISTS _profiles")
|
||||
}
|
||||
|
||||
database.execSQL("CREATE TABLE IF NOT EXISTS `profiles` (`name` TEXT NOT NULL, `type` INTEGER NOT NULL, `uri` TEXT NOT NULL, `source` TEXT, `active` INTEGER NOT NULL, `interval` INTEGER NOT NULL, `id` INTEGER NOT NULL, PRIMARY KEY(`id`))")
|
||||
database.execSQL("CREATE TABLE IF NOT EXISTS `selected_proxies` (`profile_id` INTEGER NOT NULL, `proxy` TEXT NOT NULL, `selected` TEXT NOT NULL, PRIMARY KEY(`profile_id`, `proxy`), FOREIGN KEY(`profile_id`) REFERENCES `profiles`(`id`) ON UPDATE CASCADE ON DELETE CASCADE )")
|
||||
|
||||
profiles.forEach {
|
||||
database.insert("profiles", SQLiteDatabase.CONFLICT_ABORT, ContentValues().apply {
|
||||
put("name", it.name)
|
||||
put("type", it.type)
|
||||
put("uri", it.uri)
|
||||
put("source", it.source)
|
||||
put("active", it.active)
|
||||
put("interval", it.interval)
|
||||
put("id", it.id)
|
||||
})
|
||||
}
|
||||
|
||||
selectedProxies.forEach {
|
||||
database.insert("selected_proxies",
|
||||
SQLiteDatabase.CONFLICT_REPLACE, ContentValues().apply {
|
||||
put("profile_id", it.profileId)
|
||||
put("proxy", it.proxy)
|
||||
put("selected", it.selected)
|
||||
})
|
||||
}
|
||||
|
||||
Log.i("Database Migrated 3 -> 4")
|
||||
} catch (e: Exception) {
|
||||
Log.e("Migration failure", e)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package com.github.kr328.clash.service.data.migrations
|
||||
|
||||
import androidx.room.migration.Migration
|
||||
|
||||
val MIGRATIONS: Array<Migration> = arrayOf(Migration12, Migration23, Migration34)
|
||||
@@ -0,0 +1,10 @@
|
||||
package com.github.kr328.clash.service.util
|
||||
|
||||
import android.os.Process
|
||||
|
||||
object UserUtils {
|
||||
private const val PER_USER_RANGE: Int = 100000
|
||||
|
||||
val currentUserId: Int
|
||||
get() = Process.myUid() / PER_USER_RANGE
|
||||
}
|
||||
Reference in new issue
Block a user