Commit 870fd9e6 authored by Mygod's avatar Mygod

Move custom-rules.acl to PublicDatabase

Less files to backup. Less constants to keep track of.
parent 4e71722a
...@@ -22,7 +22,6 @@ package com.github.shadowsocks.acl ...@@ -22,7 +22,6 @@ package com.github.shadowsocks.acl
import org.junit.Assert import org.junit.Assert
import org.junit.Test import org.junit.Test
import java.io.StringReader
class AclTest { class AclTest {
companion object { companion object {
...@@ -36,6 +35,6 @@ class AclTest { ...@@ -36,6 +35,6 @@ class AclTest {
@Test @Test
fun parse() { fun parse() {
Assert.assertEquals(INPUT1, Acl().fromReader(StringReader(INPUT1)).toString()); Assert.assertEquals(INPUT1, Acl().fromReader(INPUT1.reader()).toString());
} }
} }
...@@ -22,11 +22,10 @@ package com.github.shadowsocks ...@@ -22,11 +22,10 @@ package com.github.shadowsocks
import android.app.backup.BackupAgentHelper import android.app.backup.BackupAgentHelper
import android.app.backup.FileBackupHelper import android.app.backup.FileBackupHelper
import com.github.shadowsocks.acl.Acl
import com.github.shadowsocks.utils.Key import com.github.shadowsocks.utils.Key
@Deprecated("Only used in API level < 23. For 6.0+, Auto Backup for Apps is used.") @Deprecated("Only used in API level < 23. For 6.0+, Auto Backup for Apps is used.")
class ConfigBackupHelper : BackupAgentHelper() { class ConfigBackupHelper : BackupAgentHelper() {
override fun onCreate() = addHelper("com.github.shadowsocks.database.profile", FileBackupHelper(this, 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 ...@@ -24,11 +24,10 @@ import android.content.Context
import android.support.v7.util.SortedList import android.support.v7.util.SortedList
import android.util.Log import android.util.Log
import com.github.shadowsocks.App.Companion.app import com.github.shadowsocks.App.Companion.app
import com.github.shadowsocks.preference.DataStore
import com.github.shadowsocks.utils.Subnet import com.github.shadowsocks.utils.Subnet
import com.github.shadowsocks.utils.asIterable import com.github.shadowsocks.utils.asIterable
import com.j256.ormlite.field.DatabaseField
import java.io.File import java.io.File
import java.io.FileNotFoundException
import java.io.Reader import java.io.Reader
import java.net.URL import java.net.URL
...@@ -42,24 +41,26 @@ class Acl { ...@@ -42,24 +41,26 @@ class Acl {
const val GFWLIST = "gfwlist" const val GFWLIST = "gfwlist"
const val CHINALIST = "china-list" const val CHINALIST = "china-list"
const val CUSTOM_RULES = "custom-rules" const val CUSTOM_RULES = "custom-rules"
const val CUSTOM_RULES_FLATTENED = "custom-rules-flattened"
val networkAclParser = "^IMPORT_URL\\s*<(.+)>\\s*$".toRegex() val networkAclParser = "^IMPORT_URL\\s*<(.+)>\\s*$".toRegex()
fun getFile(id: String, context: Context = app.deviceContext) = File(context.filesDir, id + ".acl") fun getFile(id: String, context: Context = app.deviceContext) = File(context.filesDir, id + ".acl")
val customRules: Acl get() { var customRules: Acl
get() {
val acl = Acl() val acl = Acl()
try { val str = DataStore.publicStore.getString(CUSTOM_RULES) ?: return acl
acl.fromId(CUSTOM_RULES) acl.fromReader(str.reader())
if (!acl.bypass) { if (!acl.bypass) {
acl.subnets.clear() acl.subnets.clear()
acl.hostnames.clear() acl.hostnames.clear()
}
} catch (_: FileNotFoundException) { }
acl.bypass = true 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()) fun save(id: String, acl: Acl) = getFile(id).writeText(acl.toString())
} }
...@@ -84,13 +85,9 @@ class Acl { ...@@ -84,13 +85,9 @@ class Acl {
override fun compareNonNull(o1: URL, o2: URL): Int = ordering.compare(o1, o2) 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 hostnames = SortedList(String::class.java, StringSorter)
val subnets = SortedList(Subnet::class.java, SubnetSorter) val subnets = SortedList(Subnet::class.java, SubnetSorter)
val urls = SortedList(URL::class.java, URLSorter) val urls = SortedList(URL::class.java, URLSorter)
@DatabaseField
var bypass = false var bypass = false
fun clear(): Acl { fun clear(): Acl {
......
...@@ -157,7 +157,7 @@ class CustomRulesFragment : ToolbarFragment(), Toolbar.OnMenuItemClickListener { ...@@ -157,7 +157,7 @@ class CustomRulesFragment : ToolbarFragment(), Toolbar.OnMenuItemClickListener {
if (!savePending) { if (!savePending) {
savePending = true savePending = true
list.post { list.post {
Acl.save(Acl.CUSTOM_RULES, acl) Acl.customRules = acl
savePending = false savePending = false
} }
} }
......
...@@ -195,8 +195,7 @@ object BaseService { ...@@ -195,8 +195,7 @@ object BaseService {
val aclFile: File? get() { val aclFile: File? get() {
val route = profile!!.route val route = profile!!.route
return if (route == Acl.ALL) null else return if (route == Acl.ALL) null else Acl.getFile(route)
Acl.getFile(if (route == Acl.CUSTOM_RULES) Acl.CUSTOM_RULES_FLATTENED else route)
} }
fun changeState(s: Int, msg: String? = null) { fun changeState(s: Int, msg: String? = null) {
...@@ -378,7 +377,7 @@ object BaseService { ...@@ -378,7 +377,7 @@ object BaseService {
} }
if (profile.route == Acl.CUSTOM_RULES) 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.plugin = PluginConfiguration(profile.plugin ?: "").selectedOptions
data.pluginPath = PluginManager.init(data.plugin) data.pluginPath = PluginManager.init(data.plugin)
......
...@@ -139,9 +139,7 @@ object PrivateDatabase : OrmLiteSqliteOpenHelper(app, Key.DB_PROFILE, null, 25) ...@@ -139,9 +139,7 @@ object PrivateDatabase : OrmLiteSqliteOpenHelper(app, Key.DB_PROFILE, null, 25)
} }
if (oldVersion < 25) { if (oldVersion < 25) {
kvPairDao.queryBuilder().where().`in`("key", PublicDatabase.onUpgrade(database, 0, -1)
Key.id, Key.tfo, Key.serviceMode, Key.portProxy, Key.portLocalDns, Key.portTransproxy).query()
.forEach { PublicDatabase.kvPairDao.createOrUpdate(it) }
} }
} catch (ex: Exception) { } catch (ex: Exception) {
app.track(ex) app.track(ex)
......
...@@ -22,6 +22,7 @@ package com.github.shadowsocks.database ...@@ -22,6 +22,7 @@ package com.github.shadowsocks.database
import android.database.sqlite.SQLiteDatabase import android.database.sqlite.SQLiteDatabase
import com.github.shadowsocks.App.Companion.app import com.github.shadowsocks.App.Companion.app
import com.github.shadowsocks.acl.Acl
import com.github.shadowsocks.utils.Key import com.github.shadowsocks.utils.Key
import com.j256.ormlite.android.AndroidDatabaseConnection import com.j256.ormlite.android.AndroidDatabaseConnection
import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper
...@@ -29,7 +30,7 @@ import com.j256.ormlite.dao.Dao ...@@ -29,7 +30,7 @@ import com.j256.ormlite.dao.Dao
import com.j256.ormlite.support.ConnectionSource import com.j256.ormlite.support.ConnectionSource
import com.j256.ormlite.table.TableUtils 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") @Suppress("UNCHECKED_CAST")
val kvPairDao: Dao<KeyValuePair, String?> by lazy { getDao(KeyValuePair::class.java) as Dao<KeyValuePair, String?> } 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 ...@@ -38,7 +39,17 @@ object PublicDatabase : OrmLiteSqliteOpenHelper(app.deviceContext, Key.DB_PUBLIC
} }
override fun onUpgrade(database: SQLiteDatabase?, connectionSource: ConnectionSource?, 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) { override fun onDowngrade(db: SQLiteDatabase?, oldVersion: Int, newVersion: Int) {
val connection = AndroidDatabaseConnection(db, true) val connection = AndroidDatabaseConnection(db, true)
......
...@@ -4,7 +4,5 @@ ...@@ -4,7 +4,5 @@
<include domain="database" path="profile.db"/> <include domain="database" path="profile.db"/>
<!-- No device storage yet in Android 6.0 --> <!-- No device storage yet in Android 6.0 -->
<include domain="database" path="config.db"/> <include domain="database" path="config.db"/>
<include domain="file" path="custom-rules.acl"/>
<include domain="device_database" path="config.db"/> <include domain="device_database" path="config.db"/>
<include domain="device_file" path="custom-rules.acl"/>
</full-backup-content> </full-backup-content>
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment