Move custom-rules.acl to PublicDatabase
Less files to backup. Less constants to keep track of.
This commit is contained in:
@@ -22,11 +22,10 @@ package com.github.shadowsocks
|
||||
|
||||
import android.app.backup.BackupAgentHelper
|
||||
import android.app.backup.FileBackupHelper
|
||||
import com.github.shadowsocks.acl.Acl
|
||||
import com.github.shadowsocks.utils.Key
|
||||
|
||||
@Deprecated("Only used in API level < 23. For 6.0+, Auto Backup for Apps is used.")
|
||||
class ConfigBackupHelper : BackupAgentHelper() {
|
||||
override fun onCreate() = addHelper("com.github.shadowsocks.database.profile", FileBackupHelper(this,
|
||||
"../databases/" + Key.DB_PROFILE, "../databases/" + Key.DB_PUBLIC, Acl.CUSTOM_RULES + ".acl"))
|
||||
"../databases/" + Key.DB_PROFILE, "../databases/" + Key.DB_PUBLIC))
|
||||
}
|
||||
|
||||
@@ -24,11 +24,10 @@ import android.content.Context
|
||||
import android.support.v7.util.SortedList
|
||||
import android.util.Log
|
||||
import com.github.shadowsocks.App.Companion.app
|
||||
import com.github.shadowsocks.preference.DataStore
|
||||
import com.github.shadowsocks.utils.Subnet
|
||||
import com.github.shadowsocks.utils.asIterable
|
||||
import com.j256.ormlite.field.DatabaseField
|
||||
import java.io.File
|
||||
import java.io.FileNotFoundException
|
||||
import java.io.Reader
|
||||
import java.net.URL
|
||||
|
||||
@@ -42,24 +41,26 @@ class Acl {
|
||||
const val GFWLIST = "gfwlist"
|
||||
const val CHINALIST = "china-list"
|
||||
const val CUSTOM_RULES = "custom-rules"
|
||||
const val CUSTOM_RULES_FLATTENED = "custom-rules-flattened"
|
||||
|
||||
val networkAclParser = "^IMPORT_URL\\s*<(.+)>\\s*$".toRegex()
|
||||
|
||||
fun getFile(id: String, context: Context = app.deviceContext) = File(context.filesDir, id + ".acl")
|
||||
|
||||
val customRules: Acl get() {
|
||||
val acl = Acl()
|
||||
try {
|
||||
acl.fromId(CUSTOM_RULES)
|
||||
var customRules: Acl
|
||||
get() {
|
||||
val acl = Acl()
|
||||
val str = DataStore.publicStore.getString(CUSTOM_RULES) ?: return acl
|
||||
acl.fromReader(str.reader())
|
||||
if (!acl.bypass) {
|
||||
acl.subnets.clear()
|
||||
acl.hostnames.clear()
|
||||
acl.bypass = true
|
||||
}
|
||||
} catch (_: FileNotFoundException) { }
|
||||
acl.bypass = true
|
||||
return acl
|
||||
}
|
||||
return acl
|
||||
}
|
||||
set(value) = DataStore.publicStore.putString(CUSTOM_RULES, if ((!value.bypass ||
|
||||
value.subnets.size() == 0 && value.hostnames.size() == 0) && value.urls.size() == 0)
|
||||
null else value.toString())
|
||||
fun save(id: String, acl: Acl) = getFile(id).writeText(acl.toString())
|
||||
}
|
||||
|
||||
@@ -84,13 +85,9 @@ class Acl {
|
||||
override fun compareNonNull(o1: URL, o2: URL): Int = ordering.compare(o1, o2)
|
||||
}
|
||||
|
||||
@DatabaseField(generatedId = true)
|
||||
var id = 0
|
||||
val hostnames = SortedList(String::class.java, StringSorter)
|
||||
val subnets = SortedList(Subnet::class.java, SubnetSorter)
|
||||
val urls = SortedList(URL::class.java, URLSorter)
|
||||
|
||||
@DatabaseField
|
||||
var bypass = false
|
||||
|
||||
fun clear(): Acl {
|
||||
|
||||
@@ -157,7 +157,7 @@ class CustomRulesFragment : ToolbarFragment(), Toolbar.OnMenuItemClickListener {
|
||||
if (!savePending) {
|
||||
savePending = true
|
||||
list.post {
|
||||
Acl.save(Acl.CUSTOM_RULES, acl)
|
||||
Acl.customRules = acl
|
||||
savePending = false
|
||||
}
|
||||
}
|
||||
|
||||
@@ -195,8 +195,7 @@ object BaseService {
|
||||
|
||||
val aclFile: File? get() {
|
||||
val route = profile!!.route
|
||||
return if (route == Acl.ALL) null else
|
||||
Acl.getFile(if (route == Acl.CUSTOM_RULES) Acl.CUSTOM_RULES_FLATTENED else route)
|
||||
return if (route == Acl.ALL) null else Acl.getFile(route)
|
||||
}
|
||||
|
||||
fun changeState(s: Int, msg: String? = null) {
|
||||
@@ -378,7 +377,7 @@ object BaseService {
|
||||
}
|
||||
|
||||
if (profile.route == Acl.CUSTOM_RULES)
|
||||
Acl.save(Acl.CUSTOM_RULES_FLATTENED, Acl.customRules.flatten(10))
|
||||
Acl.save(Acl.CUSTOM_RULES, Acl.customRules.flatten(10))
|
||||
|
||||
data.plugin = PluginConfiguration(profile.plugin ?: "").selectedOptions
|
||||
data.pluginPath = PluginManager.init(data.plugin)
|
||||
|
||||
@@ -139,9 +139,7 @@ object PrivateDatabase : OrmLiteSqliteOpenHelper(app, Key.DB_PROFILE, null, 25)
|
||||
}
|
||||
|
||||
if (oldVersion < 25) {
|
||||
kvPairDao.queryBuilder().where().`in`("key",
|
||||
Key.id, Key.tfo, Key.serviceMode, Key.portProxy, Key.portLocalDns, Key.portTransproxy).query()
|
||||
.forEach { PublicDatabase.kvPairDao.createOrUpdate(it) }
|
||||
PublicDatabase.onUpgrade(database, 0, -1)
|
||||
}
|
||||
} catch (ex: Exception) {
|
||||
app.track(ex)
|
||||
|
||||
@@ -22,6 +22,7 @@ package com.github.shadowsocks.database
|
||||
|
||||
import android.database.sqlite.SQLiteDatabase
|
||||
import com.github.shadowsocks.App.Companion.app
|
||||
import com.github.shadowsocks.acl.Acl
|
||||
import com.github.shadowsocks.utils.Key
|
||||
import com.j256.ormlite.android.AndroidDatabaseConnection
|
||||
import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper
|
||||
@@ -29,7 +30,7 @@ import com.j256.ormlite.dao.Dao
|
||||
import com.j256.ormlite.support.ConnectionSource
|
||||
import com.j256.ormlite.table.TableUtils
|
||||
|
||||
object PublicDatabase : OrmLiteSqliteOpenHelper(app.deviceContext, Key.DB_PUBLIC, null, 1) {
|
||||
object PublicDatabase : OrmLiteSqliteOpenHelper(app.deviceContext, Key.DB_PUBLIC, null, 2) {
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
val kvPairDao: Dao<KeyValuePair, String?> by lazy { getDao(KeyValuePair::class.java) as Dao<KeyValuePair, String?> }
|
||||
|
||||
@@ -38,7 +39,17 @@ object PublicDatabase : OrmLiteSqliteOpenHelper(app.deviceContext, Key.DB_PUBLIC
|
||||
}
|
||||
|
||||
override fun onUpgrade(database: SQLiteDatabase?, connectionSource: ConnectionSource?,
|
||||
oldVersion: Int, newVersion: Int) { }
|
||||
oldVersion: Int, newVersion: Int) {
|
||||
if (oldVersion < 1) {
|
||||
PrivateDatabase.kvPairDao.queryBuilder().where().`in`("key",
|
||||
Key.id, Key.tfo, Key.serviceMode, Key.portProxy, Key.portLocalDns, Key.portTransproxy).query()
|
||||
.forEach { kvPairDao.createOrUpdate(it) }
|
||||
}
|
||||
|
||||
if (oldVersion < 2) {
|
||||
kvPairDao.createOrUpdate(KeyValuePair(Acl.CUSTOM_RULES).put(Acl().fromId(Acl.CUSTOM_RULES).toString()))
|
||||
}
|
||||
}
|
||||
|
||||
override fun onDowngrade(db: SQLiteDatabase?, oldVersion: Int, newVersion: Int) {
|
||||
val connection = AndroidDatabaseConnection(db, true)
|
||||
|
||||
Reference in New Issue
Block a user