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