| 1 | package tw.nekomimi.nekogram |
| 2 | |
| 3 | import android.annotation.SuppressLint |
| 4 | import cn.hutool.core.codec.Base64 |
| 5 | import com.github.shadowsocks.plugin.PluginConfiguration |
| 6 | import com.github.shadowsocks.plugin.PluginManager |
| 7 | import com.github.shadowsocks.plugin.PluginOptions |
| 8 | import com.v2ray.ang.V2RayConfig.SS_PROTOCOL |
| 9 | import kotlinx.coroutines.runBlocking |
| 10 | import okhttp3.HttpUrl |
| 11 | import okhttp3.HttpUrl.Companion.toHttpUrlOrNull |
| 12 | import org.json.JSONObject |
| 13 | import org.telegram.messenger.ApplicationLoader |
| 14 | import org.telegram.messenger.FileLog |
| 15 | import tw.nekomimi.nekogram.utils.AlertUtil |
| 16 | import tw.nekomimi.nekogram.utils.FileUtil |
| 17 | import java.io.File |
| 18 | import kotlin.concurrent.thread |
| 19 | import kotlin.properties.Delegates |
| 20 | |
| 21 | @SuppressLint("NewApi") |
| 22 | class ShadowsocksLoader { |
| 23 | |
| 24 | lateinit var bean: Bean |
| 25 | var port by Delegates.notNull<Int>() |
| 26 | var shadowsocksProcess: 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, "ss_cfg_${bean.hash}.json") |
| 40 | |
| 41 | shadowsocksProcess = GuardedProcessPool { |
| 42 | |
| 43 | FileLog.e(it) |
| 44 | |
| 45 | }.apply { |
| 46 | |
| 47 | runCatching { |
| 48 | |
| 49 | cacheCfg.writeText(bean.toJson().toString()) |
| 50 | |
| 51 | start(listOf(FileUtil.extLib("ss-local").path, |
| 52 | "--local-addr", "127.0.0.1:$port", |
| 53 | "--config", cacheCfg.path)) { |
| 54 | |
| 55 | cacheCfg.delete() |
| 56 | |
| 57 | } |
| 58 | |
| 59 | }.onFailure { it -> |
| 60 | |
| 61 | AlertUtil.showToast("${it.javaClass.simpleName}: ${it.message}") |
| 62 | |
| 63 | cacheCfg.delete() |
| 64 | |
| 65 | FileLog.e(it) |
| 66 | |
| 67 | } |
| 68 | |
| 69 | } |
| 70 | |
| 71 | } |
| 72 | |
| 73 | fun stop() { |
| 74 | |
| 75 | if (shadowsocksProcess != null) { |
| 76 | |
| 77 | val proc = shadowsocksProcess!! |
| 78 | |
| 79 | thread { |
| 80 | |
| 81 | runCatching { |
| 82 | |
| 83 | runBlocking { proc.close(this) } |
| 84 | |
| 85 | } |
| 86 | |
| 87 | } |
| 88 | |
| 89 | shadowsocksProcess = null |
| 90 | |
| 91 | } |
| 92 | |
| 93 | } |
| 94 | |
| 95 | data class Bean( |
| 96 | var host: String = "", |
| 97 | var remotePort: Int = 443, |
| 98 | var password: String = "", |
| 99 | var method: String = "aes-256-cfb", |
| 100 | var plugin: String = "", |
| 101 | var remarks: String? = null |
| 102 | ) { |
| 103 | |
| 104 | init { |
| 105 | |
| 106 | if (method == "plain") method = "none" |
| 107 | |
| 108 | val pl = PluginConfiguration(plugin) |
| 109 | |
| 110 | if (pl.selected.contains("v2ray") && pl.selected != "v2ray-plugin") { |
| 111 | |
| 112 | pl.pluginsOptions["v2ray-plugin"] = pl.getOptions().apply { id = "v2ray-plugin" } |
| 113 | pl.pluginsOptions.remove(pl.selected) |
| 114 | pl.selected = "v2ray-plugin" |
| 115 | |
| 116 | // reslove v2ray plugin |
| 117 | |
| 118 | } |
| 119 | |
| 120 | if (pl.selected == "obfs") { |
| 121 | |
| 122 | pl.pluginsOptions["obfs-local"] = pl.getOptions().apply { id = "obfs-local" } |
| 123 | pl.pluginsOptions.remove(pl.selected) |
| 124 | pl.selected = "obfs-local" |
| 125 | |
| 126 | // reslove clash obfs |
| 127 | |
| 128 | } |
| 129 | |
| 130 | plugin = pl.toString() |
| 131 | |
| 132 | |
| 133 | } |
| 134 | |
| 135 | override fun equals(other: Any?): Boolean { |
| 136 | return super.equals(other) || (other is Bean && hash == other.hash) |
| 137 | } |
| 138 | |
| 139 | /* |
| 140 | init { |
| 141 | |
| 142 | if (method !in methods) error("method $method not supported") |
| 143 | |
| 144 | } |
| 145 | */ |
| 146 | |
| 147 | val hash = (host + remotePort + password + method).hashCode() |
| 148 | |
| 149 | val pluginInitResult by lazy { |
| 150 | PluginManager.init(PluginConfiguration(plugin ?: "")) |
| 151 | } |
| 152 | |
| 153 | fun toJson(): JSONObject = JSONObject().apply { |
| 154 | put("server", host) |
| 155 | put("server_port", remotePort) |
| 156 | put("password", password) |
| 157 | put("method", method) |
| 158 | put("ipv6", true) |
| 159 | if (pluginInitResult != null) { |
| 160 | put("plugin", pluginInitResult!!.first) |
| 161 | put("plugin_opts", pluginInitResult!!.second.toString()) |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | companion object { |
| 166 | |
| 167 | fun parseJson(ssObj: JSONObject): Bean { |
| 168 | var pluginStr = "" |
| 169 | val pId = ssObj.optString("plugin") |
| 170 | if (!pId.isNullOrBlank()) { |
| 171 | val plugin = PluginOptions(pId, ssObj.optString("plugin_opts")) |
| 172 | pluginStr = plugin.toString(false) |
| 173 | } |
| 174 | return Bean( |
| 175 | ssObj.getString("server"), |
| 176 | ssObj.getInt("server_port"), |
| 177 | ssObj.getString("password"), |
| 178 | ssObj.getString("method"), |
| 179 | pluginStr, |
| 180 | ssObj.optString("remarks") |
| 181 | ) |
| 182 | } |
| 183 | |
| 184 | fun parse(url: String): Bean { |
| 185 | |
| 186 | if (url.contains("@")) { |
| 187 | |
| 188 | // ss-android style |
| 189 | |
| 190 | val link = url.replace(SS_PROTOCOL, "https://").toHttpUrlOrNull() |
| 191 | ?: error("invalid ss-android link $url") |
| 192 | |
| 193 | if (link.password.isNotBlank()) { |
| 194 | |
| 195 | return Bean( |
| 196 | link.host, |
| 197 | link.port, |
| 198 | link.password, |
| 199 | link.username, |
| 200 | link.queryParameter("plugin") ?: "", |
| 201 | link.fragment |
| 202 | ) |
| 203 | |
| 204 | } |
| 205 | |
| 206 | val methodAndPswd = Base64.decodeStr(link.username) |
| 207 | |
| 208 | return Bean( |
| 209 | link.host, |
| 210 | link.port, |
| 211 | methodAndPswd.substringAfter(":"), |
| 212 | methodAndPswd.substringBefore(":"), |
| 213 | link.queryParameter("plugin") ?: "", |
| 214 | link.fragment |
| 215 | ) |
| 216 | |
| 217 | } else { |
| 218 | |
| 219 | // v2rayNG style |
| 220 | |
| 221 | var v2Url = url |
| 222 | |
| 223 | if (v2Url.contains("#")) v2Url = v2Url.substringBefore("#") |
| 224 | |
| 225 | val link = ("https://" + Base64.decodeStr(v2Url.substringAfter(SS_PROTOCOL))).toHttpUrlOrNull() |
| 226 | ?: error("invalid v2rayNG link $url") |
| 227 | |
| 228 | return Bean( |
| 229 | link.host, |
| 230 | link.port, |
| 231 | link.password, |
| 232 | link.username, |
| 233 | "", |
| 234 | link.fragment |
| 235 | ) |
| 236 | |
| 237 | } |
| 238 | |
| 239 | } |
| 240 | |
| 241 | } |
| 242 | |
| 243 | override fun toString(): String { |
| 244 | |
| 245 | val url = HttpUrl.Builder() |
| 246 | .scheme("https") |
| 247 | .encodedUsername(Base64.encodeUrlSafe("$method:$password")) |
| 248 | .host(host) |
| 249 | .port(remotePort) |
| 250 | |
| 251 | if (!remarks.isNullOrBlank()) url.fragment(remarks) |
| 252 | |
| 253 | if (plugin.isNotBlank()) url.addQueryParameter("plugin", plugin) |
| 254 | |
| 255 | return url.build().toString().replace("https://", "ss://") |
| 256 | |
| 257 | } |
| 258 | |
| 259 | } |
| 260 | |
| 261 | companion object { |
| 262 | |
| 263 | val methods = arrayOf( |
| 264 | |
| 265 | "none", |
| 266 | "rc4-md5", |
| 267 | "aes-128-cfb", |
| 268 | "aes-192-cfb", |
| 269 | "aes-256-cfb", |
| 270 | "aes-128-ctr", |
| 271 | "aes-192-ctr", |
| 272 | "aes-256-ctr", |
| 273 | "bf-cfb", |
| 274 | "camellia-128-cfb", |
| 275 | "camellia-192-cfb", |
| 276 | "camellia-256-cfb", |
| 277 | "salsa20", |
| 278 | "chacha20", |
| 279 | "chacha20-ietf", |
| 280 | "aes-128-gcm", |
| 281 | "aes-192-gcm", |
| 282 | "aes-256-gcm", |
| 283 | "chacha20-ietf-poly1305", |
| 284 | "xchacha20-ietf-poly1305" |
| 285 | |
| 286 | ) |
| 287 | |
| 288 | } |
| 289 | |
| 290 | } |