RuleEntity.kt 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /******************************************************************************
  2. * *
  3. * Copyright (C) 2021 by nekohasekai <[email protected]> *
  4. * *
  5. * This program is free software: you can redistribute it and/or modify *
  6. * it under the terms of the GNU General Public License as published by *
  7. * the Free Software Foundation, either version 3 of the License, or *
  8. * (at your option) any later version. *
  9. * *
  10. * This program is distributed in the hope that it will be useful, *
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  13. * GNU General Public License for more details. *
  14. * *
  15. * You should have received a copy of the GNU General Public License *
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>. *
  17. * *
  18. ******************************************************************************/
  19. package io.nekohasekai.sagernet.database
  20. import android.os.Parcelable
  21. import androidx.room.*
  22. import io.nekohasekai.sagernet.R
  23. import io.nekohasekai.sagernet.ktx.app
  24. import kotlinx.parcelize.Parcelize
  25. @Entity(tableName = "rules")
  26. @Parcelize
  27. data class RuleEntity(
  28. @PrimaryKey(autoGenerate = true) var id: Long = 0L,
  29. var name: String = "",
  30. var userOrder: Long = 0L,
  31. var enabled: Boolean = false,
  32. var domains: String = "",
  33. var ip: String = "",
  34. var port: String = "",
  35. var sourcePort: String = "",
  36. var network: String = "",
  37. var source: String = "",
  38. var protocol: String = "",
  39. var attrs: String = "",
  40. var outbound: Long = 0,
  41. var reverse: Boolean = false,
  42. var redirect: String = "",
  43. var packages: List<String> = listOf(),
  44. ) : Parcelable {
  45. fun isBypassRule(): Boolean {
  46. return (domains.isNotBlank() && ip.isBlank() || ip.isNotBlank() && domains.isBlank()) && port.isBlank() && sourcePort.isBlank() && network.isBlank() && source.isBlank() && protocol.isBlank() && attrs.isBlank() && !reverse && redirect.isBlank() && outbound == -1L && packages.isEmpty()
  47. }
  48. fun displayName(): String {
  49. return name.takeIf { it.isNotBlank() } ?: "Rule $id"
  50. }
  51. fun mkSummary(): String {
  52. var summary = ""
  53. if (domains.isNotBlank()) summary += "$domains\n"
  54. if (ip.isNotBlank()) summary += "$ip\n"
  55. if (sourcePort.isNotBlank()) summary += "$sourcePort\n"
  56. if (network.isNotBlank()) summary += "$network\n"
  57. if (source.isNotBlank()) summary += "$source\n"
  58. if (protocol.isNotBlank()) summary += "$protocol\n"
  59. if (attrs.isNotBlank()) summary += "$attrs\n"
  60. if (reverse) summary += "$redirect\n"
  61. if (packages.isNotEmpty()) summary += app.getString(
  62. R.string.apps_message, packages.size
  63. ) + "\n"
  64. val lines = summary.trim().split("\n")
  65. return if (lines.size > 3) {
  66. lines.subList(0, 3).joinToString("\n", postfix = "\n...")
  67. } else {
  68. summary.trim()
  69. }
  70. }
  71. fun displayOutbound(): String {
  72. if (reverse) {
  73. return app.getString(R.string.route_reverse)
  74. }
  75. return when (outbound) {
  76. 0L -> app.getString(R.string.route_proxy)
  77. -1L -> app.getString(R.string.route_bypass)
  78. -2L -> app.getString(R.string.route_block)
  79. else -> ProfileManager.getProfile(outbound)?.displayName()
  80. ?: app.getString(R.string.route_proxy)
  81. }
  82. }
  83. @androidx.room.Dao
  84. interface Dao {
  85. @Query("SELECT * from rules WHERE (packages != '') AND enabled = 1")
  86. fun checkVpnNeeded(): List<RuleEntity>
  87. @Query("SELECT * FROM rules ORDER BY userOrder")
  88. fun allRules(): List<RuleEntity>
  89. @Query("SELECT * FROM rules WHERE enabled = :enabled ORDER BY userOrder")
  90. fun enabledRules(enabled: Boolean = true): List<RuleEntity>
  91. @Query("SELECT MAX(userOrder) + 1 FROM rules")
  92. fun nextOrder(): Long?
  93. @Query("SELECT * FROM rules WHERE id = :ruleId")
  94. fun getById(ruleId: Long): RuleEntity?
  95. @Query("DELETE FROM rules WHERE id = :ruleId")
  96. fun deleteById(ruleId: Long): Int
  97. @Delete
  98. fun deleteRule(rule: RuleEntity)
  99. @Delete
  100. fun deleteRules(rules: List<RuleEntity>)
  101. @Insert
  102. fun createRule(rule: RuleEntity): Long
  103. @Update
  104. fun updateRule(rule: RuleEntity)
  105. @Update
  106. fun updateRules(rules: List<RuleEntity>)
  107. @Query("DELETE FROM rules")
  108. fun deleteAll()
  109. }
  110. }