| 1 | package tw.nekomimi.nekogram |
| 2 | |
| 3 | import cn.hutool.core.codec.Base64 |
| 4 | import com.v2ray.ang.V2RayConfig.SSR_PROTOCOL |
| 5 | import kotlinx.coroutines.runBlocking |
| 6 | import okhttp3.HttpUrl.Companion.toHttpUrl |
| 7 | import org.json.JSONObject |
| 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 java.util.* |
| 13 | import kotlin.concurrent.thread |
| 14 | import kotlin.properties.Delegates |
| 15 | |
| 16 | class ShadowsocksRLoader { |
| 17 | |
| 18 | lateinit var bean: Bean |
| 19 | var port by Delegates.notNull<Int>() |
| 20 | var shadowsocksProcess: GuardedProcessPool? = null |
| 21 | |
| 22 | fun initConfig(bean: Bean, port: Int) { |
| 23 | |
| 24 | this.bean = bean |
| 25 | this.port = port |
| 26 | |
| 27 | } |
| 28 | |
| 29 | fun start() { |
| 30 | |
| 31 | stop() |
| 32 | |
| 33 | val cacheCfg = File(ApplicationLoader.applicationContext.cacheDir, "ssr_cfg_${bean.hash}.json") |
| 34 | |
| 35 | cacheCfg.writeText(bean.toJson().toString()) |
| 36 | |
| 37 | shadowsocksProcess = GuardedProcessPool { |
| 38 | |
| 39 | FileLog.e(it) |
| 40 | |
| 41 | }.apply { |
| 42 | |
| 43 | runCatching { |
| 44 | |
| 45 | start(listOf(FileUtil.extLib("ssr-local").path, |
| 46 | "-b", "127.0.0.1", |
| 47 | "--host", bean.host, |
| 48 | "-t", "600", |
| 49 | "-c", cacheCfg.path, |
| 50 | "-l", port.toString())) { |
| 51 | |
| 52 | cacheCfg.delete() |
| 53 | |
| 54 | } |
| 55 | |
| 56 | }.onFailure { |
| 57 | |
| 58 | cacheCfg.delete() |
| 59 | |
| 60 | FileLog.e(it) |
| 61 | |
| 62 | } |
| 63 | |
| 64 | } |
| 65 | |
| 66 | } |
| 67 | |
| 68 | fun stop() { |
| 69 | |
| 70 | if (shadowsocksProcess != null) { |
| 71 | |
| 72 | val proc = shadowsocksProcess!! |
| 73 | |
| 74 | thread { |
| 75 | |
| 76 | runCatching { |
| 77 | |
| 78 | runBlocking { proc.close(this) } |
| 79 | |
| 80 | } |
| 81 | |
| 82 | } |
| 83 | |
| 84 | shadowsocksProcess = null |
| 85 | |
| 86 | } |
| 87 | |
| 88 | } |
| 89 | |
| 90 | data class Bean( |
| 91 | var host: String = "", |
| 92 | var remotePort: Int = 443, |
| 93 | var password: String = "", |
| 94 | var protocol: String = "origin", |
| 95 | var protocol_param: String = "", |
| 96 | var obfs: String = "plain", |
| 97 | var obfs_param: String = "", |
| 98 | var method: String = "aes-256-cfb", |
| 99 | var remarks: String? = null |
| 100 | ) { |
| 101 | |
| 102 | val hash get() = (host + remotePort + password + protocol + obfs + method).hashCode() |
| 103 | |
| 104 | override fun equals(other: Any?): Boolean { |
| 105 | return super.equals(other) || (other is Bean && hash == other.hash) |
| 106 | } |
| 107 | |
| 108 | /* |
| 109 | init { |
| 110 | |
| 111 | if (method !in methods) error("method $method not supported") |
| 112 | if (protocol !in protocols) error("protocol $protocol not supported") |
| 113 | if (obfs !in obfses) error("obfs $obfs not supported") |
| 114 | |
| 115 | } |
| 116 | */ |
| 117 | |
| 118 | fun toJson(): JSONObject = JSONObject().apply { |
| 119 | put("server", host) |
| 120 | put("server_port", remotePort) |
| 121 | put("password", password) |
| 122 | put("method", method) |
| 123 | put("protocol", protocol) |
| 124 | put("protocol_param", protocol_param) |
| 125 | put("obfs", obfs) |
| 126 | put("obfs_param", obfs_param) |
| 127 | put("remarks", remarks) |
| 128 | put("route", "all") |
| 129 | put("remote_dns", "8.8.8.8:53") |
| 130 | put("ipv6", true) |
| 131 | put("metered", false) |
| 132 | put("proxy_apps", JSONObject().apply { |
| 133 | put("enabled", false) |
| 134 | }) |
| 135 | put("udpdns", false) |
| 136 | } |
| 137 | |
| 138 | companion object { |
| 139 | |
| 140 | fun parse(url: String): Bean { |
| 141 | |
| 142 | val params = Base64.decodeStr(url.substringAfter(SSR_PROTOCOL)).split(":") |
| 143 | |
| 144 | val bean = Bean(params[0], |
| 145 | params[1].toInt(), |
| 146 | protocol = params[2], |
| 147 | method = params[3], |
| 148 | obfs = params[4], |
| 149 | password = Base64.decodeStr(params[5].substringBefore("/"))) |
| 150 | |
| 151 | val httpUrl = ("https://localhost" + params[5].substringAfter("/")).toHttpUrl() |
| 152 | |
| 153 | runCatching { |
| 154 | |
| 155 | bean.obfs_param = Base64.decodeStr(httpUrl.queryParameter("obfsparam")!!) |
| 156 | |
| 157 | } |
| 158 | |
| 159 | runCatching { |
| 160 | |
| 161 | bean.protocol_param = Base64.decodeStr(httpUrl.queryParameter("protoparam")!!) |
| 162 | |
| 163 | } |
| 164 | |
| 165 | runCatching { |
| 166 | |
| 167 | val remarks = httpUrl.queryParameter("remarks") |
| 168 | |
| 169 | if (remarks?.isNotBlank() == true) { |
| 170 | |
| 171 | bean.remarks = Base64.decodeStr(remarks) |
| 172 | |
| 173 | } |
| 174 | |
| 175 | } |
| 176 | |
| 177 | return bean |
| 178 | |
| 179 | } |
| 180 | |
| 181 | } |
| 182 | |
| 183 | override fun toString(): String { |
| 184 | |
| 185 | return "ssr://" + Base64.encodeUrlSafe("%s:%d:%s:%s:%s:%s/?obfsparam=%s&protoparam=%s&remarks=%s".format(Locale.ENGLISH, host, remotePort, protocol, method, obfs, |
| 186 | Base64.encodeUrlSafe("%s".format(Locale.ENGLISH, password)), |
| 187 | Base64.encodeUrlSafe("%s".format(Locale.ENGLISH, obfs_param)), |
| 188 | Base64.encodeUrlSafe("%s".format(Locale.ENGLISH, protocol_param)), |
| 189 | Base64.encodeUrlSafe("%s".format(Locale.ENGLISH, remarks ?: "")))) |
| 190 | } |
| 191 | |
| 192 | } |
| 193 | |
| 194 | companion object { |
| 195 | |
| 196 | val methods = arrayOf( |
| 197 | |
| 198 | "none", |
| 199 | "table", |
| 200 | "rc4", |
| 201 | "rc4-md5", |
| 202 | "rc4-md5-6", |
| 203 | "aes-128-cfb", |
| 204 | "aes-192-cfb", |
| 205 | "aes-256-cfb", |
| 206 | "aes-128-ctr", |
| 207 | "aes-192-ctr", |
| 208 | "aes-256-ctr", |
| 209 | "bf-cfb", |
| 210 | "camellia-128-cfb", |
| 211 | "camellia-192-cfb", |
| 212 | "camellia-256-cfb", |
| 213 | "salsa20", |
| 214 | "chacha20", |
| 215 | "chacha20-ietf" |
| 216 | |
| 217 | ) |
| 218 | |
| 219 | val protocols = arrayOf( |
| 220 | "origin", |
| 221 | "verify_simple", |
| 222 | "verify_sha1", |
| 223 | "auth_sha1", |
| 224 | "auth_sha1_v2", |
| 225 | "auth_sha1_v4", |
| 226 | "auth_aes128_sha1", |
| 227 | "auth_aes128_md5", |
| 228 | "auth_chain_a", |
| 229 | "auth_chain_b" |
| 230 | ) |
| 231 | |
| 232 | val obfses = arrayOf( |
| 233 | "plain", |
| 234 | "http_simple", |
| 235 | "http_post", |
| 236 | "tls_simple", |
| 237 | "tls1.2_ticket_auth" |
| 238 | ) |
| 239 | |
| 240 | } |
| 241 | |
| 242 | } |