Compare commits

..
4 Commits
Author SHA1 Message Date
Kr328 2afc7262eb Update version 2020-04-22 11:25:01 +08:00
Kr328 94a0282c70 Fix tun start at second user 2020-04-22 11:05:10 +08:00
Kr328 96b78829ab Fix cancel profile auto update 2020-04-22 00:25:22 +08:00
Kr328 35d5a90b2a Improve database migrations 2020-04-21 23:50:01 +08:00
11 changed files with 412 additions and 343 deletions

No files matched your search

+2 -2
View File
@@ -10,8 +10,8 @@ buildscript {
this["gMinSdkVersion"] = 24
this["gTargetSdkVersion"] = 29
this["gVersionCode"] = 10207
this["gVersionName"] = "1.2.7"
this["gVersionCode"] = 10208
this["gVersionName"] = "1.2.8"
this["gKotlinVersion"] = kotlinVersion
this["gKotlinCoroutineVersion"] = "1.3.5"
@@ -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,
@@ -2,6 +2,7 @@ package com.github.kr328.clash.service
import android.content.Intent
import android.net.VpnService
import android.os.Process
import com.github.kr328.clash.service.clash.ClashRuntime
import com.github.kr328.clash.service.clash.module.*
import com.github.kr328.clash.service.settings.ServiceSettings
@@ -13,11 +14,7 @@ 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"
}
@@ -106,14 +103,18 @@ 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
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))
@@ -141,6 +142,29 @@ class TunService : VpnService(), CoroutineScope by MainScope() {
override fun onCreateTunFailure() {
stopSelfForReason("Start VPN rejected by the 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()
}
}
private fun stopSelfForReason(reason: String?) {
@@ -5,6 +5,7 @@ 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
@@ -33,6 +34,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)
}
@@ -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
}