forked from starifly/NekoBoxForAndroid
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProtocols.kt
More file actions
68 lines (51 loc) · 1.74 KB
/
Copy pathProtocols.kt
File metadata and controls
68 lines (51 loc) · 1.74 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
package moe.matsuri.nb4a
import android.content.Context
import io.nekohasekai.sagernet.R
import io.nekohasekai.sagernet.database.ProxyEntity.Companion.TYPE_NEKO
import io.nekohasekai.sagernet.fmt.AbstractBean
import io.nekohasekai.sagernet.ktx.app
import io.nekohasekai.sagernet.ktx.getColorAttr
import moe.matsuri.nb4a.proxy.config.ConfigBean
// Settings for all protocols, built-in or plugin
object Protocols {
// Deduplication
class Deduplication(
val bean: AbstractBean, val type: String
) {
fun hash(): String {
if (bean is ConfigBean) {
return bean.config
}
return bean.serverAddress + bean.serverPort + type
}
override fun hashCode(): Int {
return hash().toByteArray().contentHashCode()
}
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false
other as Deduplication
return hash() == other.hash()
}
}
// Display
fun Context.getProtocolColor(type: Int): Int {
return when (type) {
TYPE_NEKO -> getColorAttr(android.R.attr.textColorPrimary)
else -> getColorAttr(R.attr.protocolColor)
}
}
// Test
fun genFriendlyMsg(msg: String): String {
val msgL = msg.lowercase()
return when {
msgL.contains("timeout") || msgL.contains("deadline") -> {
app.getString(R.string.connection_test_timeout_error)
}
msgL.contains("refused") || msgL.contains("closed pipe") -> {
app.getString(R.string.connection_test_refused)
}
else -> msg
}
}
}