forked from starifly/NekoBoxForAndroid
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProfileManager.kt
More file actions
243 lines (212 loc) · 7.61 KB
/
Copy pathProfileManager.kt
File metadata and controls
243 lines (212 loc) · 7.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package io.nekohasekai.sagernet.database
import android.database.sqlite.SQLiteCantOpenDatabaseException
import io.nekohasekai.sagernet.R
import io.nekohasekai.sagernet.aidl.TrafficData
import io.nekohasekai.sagernet.fmt.AbstractBean
import io.nekohasekai.sagernet.ktx.Logs
import io.nekohasekai.sagernet.ktx.app
import io.nekohasekai.sagernet.ktx.applyDefaultValues
import java.io.IOException
import java.sql.SQLException
import java.util.*
object ProfileManager {
interface Listener {
suspend fun onAdd(profile: ProxyEntity)
suspend fun onUpdated(data: TrafficData)
suspend fun onUpdated(profile: ProxyEntity, noTraffic: Boolean)
suspend fun onRemoved(groupId: Long, profileId: Long)
}
interface RuleListener {
suspend fun onAdd(rule: RuleEntity)
suspend fun onUpdated(rule: RuleEntity)
suspend fun onRemoved(ruleId: Long)
suspend fun onCleared()
}
private val listeners = ArrayList<Listener>()
private val ruleListeners = ArrayList<RuleListener>()
suspend fun iterator(what: suspend Listener.() -> Unit) {
synchronized(listeners) {
listeners.toList()
}.forEach { listener ->
what(listener)
}
}
suspend fun ruleIterator(what: suspend RuleListener.() -> Unit) {
val ruleListeners = synchronized(ruleListeners) {
ruleListeners.toList()
}
for (listener in ruleListeners) {
what(listener)
}
}
fun addListener(listener: Listener) {
synchronized(listeners) {
listeners.add(listener)
}
}
fun removeListener(listener: Listener) {
synchronized(listeners) {
listeners.remove(listener)
}
}
fun addListener(listener: RuleListener) {
synchronized(ruleListeners) {
ruleListeners.add(listener)
}
}
fun removeListener(listener: RuleListener) {
synchronized(ruleListeners) {
ruleListeners.remove(listener)
}
}
suspend fun createProfile(groupId: Long, bean: AbstractBean): ProxyEntity {
bean.applyDefaultValues()
val profile = ProxyEntity(groupId = groupId).apply {
id = 0
putBean(bean)
userOrder = SagerDatabase.proxyDao.nextOrder(groupId) ?: 1
}
profile.id = SagerDatabase.proxyDao.addProxy(profile)
iterator { onAdd(profile) }
return profile
}
suspend fun updateProfile(profile: ProxyEntity) {
SagerDatabase.proxyDao.updateProxy(profile)
iterator { onUpdated(profile, false) }
}
suspend fun updateProfile(profiles: List<ProxyEntity>) {
SagerDatabase.proxyDao.updateProxy(profiles)
profiles.forEach {
iterator { onUpdated(it, false) }
}
}
suspend fun updateTraffic(profileId: Long, rx: Long, tx: Long) {
SagerDatabase.proxyDao.updateTraffic(profileId, rx, tx)
}
suspend fun deleteProfile2(groupId: Long, profileId: Long) {
if (SagerDatabase.proxyDao.deleteById(profileId) == 0) return
if (DataStore.selectedProxy == profileId) {
DataStore.selectedProxy = 0L
}
}
suspend fun deleteProfile(groupId: Long, profileId: Long) {
if (SagerDatabase.proxyDao.deleteById(profileId) == 0) return
if (DataStore.selectedProxy == profileId) {
DataStore.selectedProxy = 0L
}
iterator { onRemoved(groupId, profileId) }
if (SagerDatabase.proxyDao.countByGroup(groupId) > 1) {
GroupManager.rearrange(groupId)
}
}
fun getProfile(profileId: Long): ProxyEntity? {
if (profileId == 0L) return null
return try {
SagerDatabase.proxyDao.getById(profileId)
} catch (ex: SQLiteCantOpenDatabaseException) {
throw IOException(ex)
} catch (ex: SQLException) {
Logs.w(ex)
null
}
}
fun getProfiles(profileIds: List<Long>): List<ProxyEntity> {
if (profileIds.isEmpty()) return listOf()
return try {
SagerDatabase.proxyDao.getEntities(profileIds)
} catch (ex: SQLiteCantOpenDatabaseException) {
throw IOException(ex)
} catch (ex: SQLException) {
Logs.w(ex)
listOf()
}
}
// postUpdate: post to listeners, don't change the DB
suspend fun postUpdate(profileId: Long, noTraffic: Boolean = false) {
postUpdate(getProfile(profileId) ?: return, noTraffic)
}
suspend fun postUpdate(profile: ProxyEntity, noTraffic: Boolean = false) {
iterator { onUpdated(profile, noTraffic) }
}
suspend fun postUpdate(data: TrafficData) {
iterator { onUpdated(data) }
}
suspend fun createRule(rule: RuleEntity, post: Boolean = true): RuleEntity {
rule.userOrder = SagerDatabase.rulesDao.nextOrder() ?: 1
rule.id = SagerDatabase.rulesDao.createRule(rule)
if (post) {
ruleIterator { onAdd(rule) }
}
return rule
}
suspend fun updateRule(rule: RuleEntity) {
SagerDatabase.rulesDao.updateRule(rule)
ruleIterator { onUpdated(rule) }
}
suspend fun deleteRule(ruleId: Long) {
SagerDatabase.rulesDao.deleteById(ruleId)
ruleIterator { onRemoved(ruleId) }
}
suspend fun deleteRules(rules: List<RuleEntity>) {
SagerDatabase.rulesDao.deleteRules(rules)
ruleIterator {
rules.forEach {
onRemoved(it.id)
}
}
}
suspend fun getRules(): List<RuleEntity> {
var rules = SagerDatabase.rulesDao.allRules()
if (rules.isEmpty() && !DataStore.rulesFirstCreate) {
DataStore.rulesFirstCreate = true
createRule(
RuleEntity(
name = app.getString(R.string.route_opt_block_quic),
port = "443",
network = "udp",
outbound = -2
)
)
createRule(
RuleEntity(
name = app.getString(R.string.route_opt_block_ads),
domains = "geosite:category-ads-all",
outbound = -2
)
)
val fuckedCountry = mutableListOf("cn:中国")
if (Locale.getDefault().country != Locale.CHINA.country) {
// non-Chinese users
fuckedCountry += "ir:Iran"
fuckedCountry += "ru:Russia"
}
for (c in fuckedCountry) {
val country = c.substringBefore(":")
val displayCountry = c.substringAfter(":")
//
if (country == "cn") createRule(
RuleEntity(
name = app.getString(R.string.route_play_store, displayCountry),
domains = "domain:googleapis.cn\ndomain:xn--ngstr-lra8j.com\ndomain:xn--ngstr-cn-8za9o.com",
), false
)
createRule(
RuleEntity(
name = app.getString(R.string.route_bypass_domain, displayCountry),
domains = "geosite:$country",
outbound = -1
), false
)
createRule(
RuleEntity(
name = app.getString(R.string.route_bypass_ip, displayCountry),
ip = "geoip:$country",
outbound = -1
), false
)
}
rules = SagerDatabase.rulesDao.allRules()
}
return rules
}
}