| 1 | package tw.nekomimi.nekogram |
| 2 | |
| 3 | import android.annotation.SuppressLint |
| 4 | import com.v2ray.ang.V2RayConfig |
| 5 | import kotlinx.coroutines.runBlocking |
| 6 | import okhttp3.HttpUrl |
| 7 | import okhttp3.HttpUrl.Companion.toHttpUrlOrNull |
| 8 | import org.telegram.messenger.ApplicationLoader |
| 9 | import org.telegram.messenger.FileLog |
| 10 | import tw.nekomimi.nekogram.utils.FileUtil |
| 11 | import java.io.File |
| 12 | import kotlin.concurrent.thread |
| 13 | import kotlin.properties.Delegates |
| 14 | |
| 15 | class RelayBatonLoader { |
| 16 | |
| 17 | companion object { |
| 18 | |
| 19 | @SuppressLint("AuthLeak") |
| 20 | const val publicServer = "rb://anonymous:nya@net.neko.services" |
| 21 | |
| 22 | } |
| 23 | |
| 24 | lateinit var bean: Bean |
| 25 | var port by Delegates.notNull<Int>() |
| 26 | var rbProcess: GuardedProcessPool? = null |
| 27 | |
| 28 | fun initConfig(bean: Bean, port: Int) { |
| 29 | |
| 30 | this.bean = bean |
| 31 | this.port = port |
| 32 | |
| 33 | } |
| 34 | |
| 35 | fun start() { |
| 36 | |
| 37 | stop() |
| 38 | |
| 39 | val cacheCfg = File(ApplicationLoader.applicationContext.cacheDir, "config.toml") |
| 40 | |
| 41 | cacheCfg.writeText(bean.toToml(port)) |
| 42 | |
| 43 | val geoip = File(ApplicationLoader.applicationContext.cacheDir, "GeoLite2-Country.mmdb") |
| 44 | |
| 45 | if (!geoip.isFile) { |
| 46 | |
| 47 | geoip.createNewFile() |
| 48 | |
| 49 | geoip.outputStream().use { out -> |
| 50 | |
| 51 | ApplicationLoader.applicationContext.assets.open("GeoLite2-Country.mmdb").use { |
| 52 | |
| 53 | it.copyTo(out) |
| 54 | |
| 55 | } |
| 56 | |
| 57 | } |
| 58 | |
| 59 | } |
| 60 | |
| 61 | rbProcess = GuardedProcessPool { |
| 62 | |
| 63 | FileLog.e(it) |
| 64 | |
| 65 | }.apply { |
| 66 | |
| 67 | runCatching { |
| 68 | |
| 69 | start(listOf(FileUtil.extLib("relaybaton").path, "client")) { |
| 70 | |
| 71 | cacheCfg.delete() |
| 72 | |
| 73 | } |
| 74 | |
| 75 | }.onFailure { |
| 76 | |
| 77 | cacheCfg.delete() |
| 78 | |
| 79 | FileLog.e(it) |
| 80 | |
| 81 | } |
| 82 | |
| 83 | } |
| 84 | |
| 85 | } |
| 86 | |
| 87 | fun stop() { |
| 88 | |
| 89 | if (rbProcess != null) { |
| 90 | |
| 91 | val proc = rbProcess!! |
| 92 | |
| 93 | thread { |
| 94 | |
| 95 | runCatching { |
| 96 | |
| 97 | runBlocking { proc.close(this) } |
| 98 | |
| 99 | } |
| 100 | |
| 101 | } |
| 102 | |
| 103 | rbProcess = null |
| 104 | |
| 105 | } |
| 106 | |
| 107 | } |
| 108 | |
| 109 | data class Bean( |
| 110 | var server: String = "", |
| 111 | var username: String = "", |
| 112 | var password: String = "", |
| 113 | var esni: Boolean = true, |
| 114 | var remarks: String? = null |
| 115 | ) { |
| 116 | |
| 117 | override fun equals(other: Any?): Boolean { |
| 118 | return super.equals(other) || (other is Bean && hash == other.hash) |
| 119 | } |
| 120 | |
| 121 | val hash = (server + username + password).hashCode() |
| 122 | |
| 123 | fun toToml(port: Int) = """ |
| 124 | [log] |
| 125 | file="./log.txt" |
| 126 | level="error" |
| 127 | |
| 128 | [dns] |
| 129 | type="dot" |
| 130 | server="cloudflare-dns.com" |
| 131 | addr="1.0.0.1:853" |
| 132 | local_resolve=false |
| 133 | |
| 134 | [clients] |
| 135 | port=$port |
| 136 | [[clients.client]] |
| 137 | id="1" |
| 138 | server="$server" |
| 139 | username="$username" |
| 140 | password="$password" |
| 141 | esni=$esni |
| 142 | timeout=15 |
| 143 | |
| 144 | [routes] |
| 145 | geoip_file="GeoLite2-Country.mmdb" |
| 146 | [[routes.route]] |
| 147 | type="default" |
| 148 | cond="" |
| 149 | target="1" |
| 150 | """ |
| 151 | |
| 152 | companion object { |
| 153 | |
| 154 | fun parse(url: String): Bean { |
| 155 | |
| 156 | // ss-android style |
| 157 | |
| 158 | val link = url.replace(V2RayConfig.RB_PROTOCOL, "https://").toHttpUrlOrNull() ?: error("invalid relaybaton link $url") |
| 159 | |
| 160 | return Bean( |
| 161 | link.host, |
| 162 | link.username, |
| 163 | link.password, |
| 164 | link.queryParameter("esni").takeIf { it in arrayOf("true", "false") }?.toBoolean() ?: true, |
| 165 | link.fragment |
| 166 | ) |
| 167 | |
| 168 | } |
| 169 | |
| 170 | } |
| 171 | |
| 172 | override fun toString(): String { |
| 173 | |
| 174 | val url = HttpUrl.Builder() |
| 175 | .scheme("https") |
| 176 | .username(username) |
| 177 | .password(password) |
| 178 | .host(server) |
| 179 | |
| 180 | if (!remarks.isNullOrBlank()) url.fragment(remarks) |
| 181 | |
| 182 | if (!esni) url.addQueryParameter("esni","false") |
| 183 | |
| 184 | return url.build().toString().replace("https://", V2RayConfig.RB_PROTOCOL) |
| 185 | |
| 186 | } |
| 187 | |
| 188 | } |
| 189 | |
| 190 | |
| 191 | } |