| 1 | package tw.nekomimi.nekogram |
| 2 | |
| 3 | import cn.hutool.core.codec.Base64 |
| 4 | import com.google.gson.Gson |
| 5 | import com.v2ray.ang.V2RayConfig |
| 6 | import com.v2ray.ang.V2RayConfig.SOCKS_PROTOCOL |
| 7 | import com.v2ray.ang.V2RayConfig.SS_PROTOCOL |
| 8 | import com.v2ray.ang.V2RayConfig.VMESS1_PROTOCOL |
| 9 | import com.v2ray.ang.V2RayConfig.VMESS_PROTOCOL |
| 10 | import com.v2ray.ang.dto.AngConfig.VmessBean |
| 11 | import com.v2ray.ang.dto.VmessQRCode |
| 12 | import com.v2ray.ang.util.Utils |
| 13 | import com.v2ray.ang.util.V2rayConfigUtil |
| 14 | import libv2ray.Libv2ray |
| 15 | import libv2ray.V2RayPoint |
| 16 | import libv2ray.V2RayVPNServiceSupportsSet |
| 17 | import okhttp3.HttpUrl.Companion.toHttpUrl |
| 18 | import org.telegram.messenger.FileLog |
| 19 | import org.telegram.messenger.LocaleController |
| 20 | import org.telegram.messenger.R |
| 21 | import kotlin.concurrent.thread |
| 22 | import kotlin.random.Random |
| 23 | |
| 24 | class VmessLoader { |
| 25 | |
| 26 | private val point = Libv2ray.newV2RayPoint(EmptyCallback(), true) |
| 27 | |
| 28 | companion object { |
| 29 | |
| 30 | fun parseVmess1Link(server: String): VmessBean { |
| 31 | |
| 32 | val lnk = ("https://" + server.substringAfter(VMESS1_PROTOCOL)).toHttpUrl() |
| 33 | |
| 34 | val bean = VmessBean() |
| 35 | |
| 36 | bean.configType = V2RayConfig.EConfigType.Vmess |
| 37 | bean.address = lnk.host |
| 38 | bean.port = lnk.port |
| 39 | bean.id = lnk.username |
| 40 | bean.remarks = lnk.fragment ?: "" |
| 41 | |
| 42 | lnk.queryParameterNames.forEach { |
| 43 | |
| 44 | when (it) { |
| 45 | |
| 46 | "tls" -> if (lnk.queryParameter(it) == "true") bean.streamSecurity = "tls" |
| 47 | |
| 48 | "network" -> { |
| 49 | |
| 50 | bean.network = lnk.queryParameter(it)!! |
| 51 | |
| 52 | if (bean.network in arrayOf("http", "ws")) { |
| 53 | |
| 54 | bean.path = Utils.urlDecode(lnk.encodedPath) |
| 55 | |
| 56 | } |
| 57 | |
| 58 | } |
| 59 | |
| 60 | "header" -> { |
| 61 | |
| 62 | bean.headerType = lnk.queryParameter(it)!! |
| 63 | |
| 64 | } |
| 65 | |
| 66 | } |
| 67 | |
| 68 | } |
| 69 | |
| 70 | return bean |
| 71 | |
| 72 | } |
| 73 | |
| 74 | @JvmStatic |
| 75 | fun parseVmessLink(server: String): VmessBean { |
| 76 | |
| 77 | try { |
| 78 | if (server.isBlank()) error("empty link") |
| 79 | |
| 80 | var vmess = VmessBean() |
| 81 | |
| 82 | if (server.startsWith(VMESS_PROTOCOL)) { |
| 83 | |
| 84 | val indexSplit = server.indexOf("?") |
| 85 | if (indexSplit > 0) { |
| 86 | vmess = resolveSimpleVmess1(server) |
| 87 | } else { |
| 88 | |
| 89 | var result = server.replace(VMESS_PROTOCOL, "") |
| 90 | result = Base64.decodeStr(result) |
| 91 | if (result.isBlank()) { |
| 92 | error("invalid url format") |
| 93 | } |
| 94 | |
| 95 | if (result.contains("= vmess")) { |
| 96 | |
| 97 | vmess = resolveSomeIOSAppShitCsvLink(result) |
| 98 | |
| 99 | } else { |
| 100 | |
| 101 | val vmessQRCode = Gson().fromJson(result, VmessQRCode::class.java) |
| 102 | if (vmessQRCode.add.isBlank() |
| 103 | || vmessQRCode.port.isBlank() |
| 104 | || vmessQRCode.id.isBlank() |
| 105 | || vmessQRCode.aid.isBlank() |
| 106 | || vmessQRCode.net.isBlank() |
| 107 | ) { |
| 108 | error("invalid protocol") |
| 109 | } |
| 110 | |
| 111 | vmess.configType = V2RayConfig.EConfigType.Vmess |
| 112 | vmess.security = "auto" |
| 113 | vmess.network = "tcp" |
| 114 | vmess.headerType = "none" |
| 115 | |
| 116 | vmess.configVersion = Utils.parseInt(vmessQRCode.v) |
| 117 | vmess.remarks = vmessQRCode.ps |
| 118 | vmess.address = vmessQRCode.add |
| 119 | vmess.port = Utils.parseInt(vmessQRCode.port) |
| 120 | vmess.id = vmessQRCode.id |
| 121 | vmess.alterId = Utils.parseInt(vmessQRCode.aid) |
| 122 | vmess.network = vmessQRCode.net |
| 123 | vmess.headerType = vmessQRCode.type |
| 124 | vmess.requestHost = vmessQRCode.host |
| 125 | vmess.path = vmessQRCode.path |
| 126 | vmess.streamSecurity = vmessQRCode.tls |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | upgradeServerVersion(vmess) |
| 131 | |
| 132 | return vmess |
| 133 | } else if (server.startsWith(VMESS1_PROTOCOL)) { |
| 134 | |
| 135 | return parseVmess1Link(server) |
| 136 | |
| 137 | } else if (server.startsWith(SS_PROTOCOL)) { |
| 138 | var result = server.replace(SS_PROTOCOL, "") |
| 139 | val indexSplit = result.indexOf("#") |
| 140 | if (indexSplit > 0) { |
| 141 | try { |
| 142 | vmess.remarks = Utils.urlDecode(result.substring(indexSplit + 1, result.length)) |
| 143 | } catch (e: Exception) { |
| 144 | e.printStackTrace() |
| 145 | } |
| 146 | |
| 147 | result = result.substring(0, indexSplit) |
| 148 | } |
| 149 | |
| 150 | //part decode |
| 151 | val indexS = result.indexOf("@") |
| 152 | if (indexS > 0) { |
| 153 | result = Base64.decodeStr(result.substring(0, indexS)) + result.substring(indexS, result.length) |
| 154 | } else { |
| 155 | result = Base64.decodeStr(result) |
| 156 | } |
| 157 | |
| 158 | val legacyPattern = "^(.+?):(.*)@(.+?):(\\d+?)$".toRegex() |
| 159 | val match = legacyPattern.matchEntire(result) ?: error("invalid protocol") |
| 160 | vmess.security = match.groupValues[1].toLowerCase() |
| 161 | vmess.id = match.groupValues[2] |
| 162 | vmess.address = match.groupValues[3] |
| 163 | if (vmess.address.firstOrNull() == '[' && vmess.address.lastOrNull() == ']') |
| 164 | vmess.address = vmess.address.substring(1, vmess.address.length - 1) |
| 165 | vmess.port = match.groupValues[4].toInt() |
| 166 | |
| 167 | return vmess |
| 168 | |
| 169 | } else if (server.startsWith(SOCKS_PROTOCOL)) { |
| 170 | var result = server.replace(SOCKS_PROTOCOL, "") |
| 171 | val indexSplit = result.indexOf("#") |
| 172 | if (indexSplit > 0) { |
| 173 | try { |
| 174 | vmess.remarks = Utils.urlDecode(result.substring(indexSplit + 1, result.length)) |
| 175 | } catch (e: Exception) { |
| 176 | e.printStackTrace() |
| 177 | } |
| 178 | |
| 179 | result = result.substring(0, indexSplit) |
| 180 | } |
| 181 | |
| 182 | //part decode |
| 183 | val indexS = result.indexOf(":") |
| 184 | if (indexS < 0) { |
| 185 | result = Base64.decodeStr(result) |
| 186 | } |
| 187 | |
| 188 | val legacyPattern = "^(.+?):(\\d+?)$".toRegex() |
| 189 | val match = legacyPattern.matchEntire(result) ?: error("invalid protocol") |
| 190 | vmess.address = match.groupValues[1] |
| 191 | if (vmess.address.firstOrNull() == '[' && vmess.address.lastOrNull() == ']') |
| 192 | vmess.address = vmess.address.substring(1, vmess.address.length - 1) |
| 193 | vmess.port = match.groupValues[2].toInt() |
| 194 | |
| 195 | return vmess |
| 196 | } else { |
| 197 | error("invalid protocol") |
| 198 | } |
| 199 | } catch (e: Exception) { |
| 200 | |
| 201 | throw IllegalArgumentException(e) |
| 202 | |
| 203 | } |
| 204 | |
| 205 | } |
| 206 | |
| 207 | private fun resolveSomeIOSAppShitCsvLink(csv: String): VmessBean { |
| 208 | |
| 209 | val args = csv.split(",") |
| 210 | |
| 211 | val bean = VmessBean() |
| 212 | |
| 213 | bean.configType = V2RayConfig.EConfigType.Vmess |
| 214 | bean.address = args[1] |
| 215 | bean.port = args[2].toInt() |
| 216 | bean.security = args[3] |
| 217 | bean.id = args[4].replace("\"", "") |
| 218 | |
| 219 | args.subList(5, args.size).forEach { |
| 220 | |
| 221 | when { |
| 222 | |
| 223 | it == "over-tls=true" -> { |
| 224 | |
| 225 | bean.streamSecurity = "tls" |
| 226 | |
| 227 | } |
| 228 | |
| 229 | it.startsWith("tls-host=") -> { |
| 230 | |
| 231 | bean.requestHost = it.substringAfter("=") |
| 232 | |
| 233 | } |
| 234 | |
| 235 | it.startsWith("obfs=") -> { |
| 236 | |
| 237 | bean.network = it.substringAfter("=") |
| 238 | |
| 239 | } |
| 240 | |
| 241 | it.startsWith("obfs-path=") || it.contains("Host:") -> { |
| 242 | |
| 243 | runCatching { |
| 244 | |
| 245 | bean.path = it |
| 246 | .substringAfter("obfs-path=\"") |
| 247 | .substringBefore("\"obfs") |
| 248 | |
| 249 | } |
| 250 | |
| 251 | runCatching { |
| 252 | |
| 253 | bean.requestHost = it |
| 254 | .substringAfter("Host:") |
| 255 | .substringBefore("[") |
| 256 | |
| 257 | } |
| 258 | |
| 259 | } |
| 260 | |
| 261 | } |
| 262 | |
| 263 | } |
| 264 | |
| 265 | return bean |
| 266 | |
| 267 | } |
| 268 | |
| 269 | /** |
| 270 | * upgrade |
| 271 | */ |
| 272 | private fun upgradeServerVersion(vmess: VmessBean): Int { |
| 273 | try { |
| 274 | if (vmess.configVersion == 2) { |
| 275 | return 0 |
| 276 | } |
| 277 | |
| 278 | when (vmess.network) { |
| 279 | "kcp" -> { |
| 280 | } |
| 281 | "ws" -> { |
| 282 | var path = "" |
| 283 | var host = "" |
| 284 | val lstParameter = vmess.requestHost.split(";") |
| 285 | if (lstParameter.size > 0) { |
| 286 | path = lstParameter.get(0).trim() |
| 287 | } |
| 288 | if (lstParameter.size > 1) { |
| 289 | path = lstParameter.get(0).trim() |
| 290 | host = lstParameter.get(1).trim() |
| 291 | } |
| 292 | vmess.path = path |
| 293 | vmess.requestHost = host |
| 294 | } |
| 295 | "h2" -> { |
| 296 | var path = "" |
| 297 | var host = "" |
| 298 | val lstParameter = vmess.requestHost.split(";") |
| 299 | if (lstParameter.isNotEmpty()) { |
| 300 | path = lstParameter[0].trim() |
| 301 | } |
| 302 | if (lstParameter.size > 1) { |
| 303 | path = lstParameter[0].trim() |
| 304 | host = lstParameter[1].trim() |
| 305 | } |
| 306 | vmess.path = path |
| 307 | vmess.requestHost = host |
| 308 | } |
| 309 | else -> { |
| 310 | } |
| 311 | } |
| 312 | vmess.configVersion = 2 |
| 313 | return 0 |
| 314 | } catch (e: Exception) { |
| 315 | e.printStackTrace() |
| 316 | return -1 |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | private fun resolveSimpleVmess1(server: String): VmessBean { |
| 321 | |
| 322 | val vmess = VmessBean() |
| 323 | |
| 324 | var result = server.replace(VMESS_PROTOCOL, "") |
| 325 | val indexSplit = result.indexOf("?") |
| 326 | if (indexSplit > 0) { |
| 327 | result = result.substring(0, indexSplit) |
| 328 | } |
| 329 | result = Base64.decodeStr(result) |
| 330 | |
| 331 | val arr1 = result.split('@') |
| 332 | if (arr1.count() != 2) { |
| 333 | return vmess |
| 334 | } |
| 335 | val arr21 = arr1[0].split(':') |
| 336 | val arr22 = arr1[1].split(':') |
| 337 | if (arr21.count() != 2 || arr21.count() != 2) { |
| 338 | return vmess |
| 339 | } |
| 340 | |
| 341 | vmess.address = arr22[0] |
| 342 | vmess.port = Utils.parseInt(arr22[1]) |
| 343 | vmess.security = arr21[0] |
| 344 | vmess.id = arr21[1] |
| 345 | |
| 346 | vmess.security = "chacha20-poly1305" |
| 347 | vmess.network = "tcp" |
| 348 | vmess.headerType = "none" |
| 349 | vmess.remarks = "" |
| 350 | vmess.alterId = 0 |
| 351 | |
| 352 | return vmess |
| 353 | } |
| 354 | |
| 355 | } |
| 356 | |
| 357 | lateinit var bean: VmessBean |
| 358 | |
| 359 | fun initConfig(config: VmessBean) { |
| 360 | |
| 361 | this.bean = config |
| 362 | |
| 363 | } |
| 364 | |
| 365 | fun start(): Int { |
| 366 | |
| 367 | var retry = 3 |
| 368 | |
| 369 | do { |
| 370 | |
| 371 | try { |
| 372 | |
| 373 | val port = Random.nextInt(4096, 32768) |
| 374 | |
| 375 | val conf = V2rayConfigUtil.getV2rayConfig(bean, port).content |
| 376 | |
| 377 | runCatching { |
| 378 | |
| 379 | Libv2ray.testConfig(conf) |
| 380 | |
| 381 | }.onFailure { |
| 382 | |
| 383 | FileLog.e(it) |
| 384 | |
| 385 | return -1 |
| 386 | |
| 387 | } |
| 388 | |
| 389 | point.configureFileContent = conf |
| 390 | point.domainName = V2rayConfigUtil.currDomain |
| 391 | |
| 392 | point.runLoop(true) |
| 393 | |
| 394 | return port |
| 395 | |
| 396 | } catch (e: Throwable) { |
| 397 | |
| 398 | retry-- |
| 399 | |
| 400 | if (retry <= 0) { |
| 401 | |
| 402 | FileLog.e(e) |
| 403 | |
| 404 | return -1 |
| 405 | |
| 406 | } |
| 407 | |
| 408 | } |
| 409 | |
| 410 | } while (true) |
| 411 | |
| 412 | } |
| 413 | |
| 414 | fun stop() { |
| 415 | |
| 416 | thread { |
| 417 | |
| 418 | runCatching { |
| 419 | |
| 420 | point.stopLoop() |
| 421 | |
| 422 | } |
| 423 | |
| 424 | } |
| 425 | |
| 426 | } |
| 427 | |
| 428 | class EmptyCallback : V2RayVPNServiceSupportsSet { |
| 429 | override fun onEmitStatus(p0: Long, p1: String?): Long { |
| 430 | return 0 |
| 431 | } |
| 432 | |
| 433 | override fun setup(p0: String?): Long { |
| 434 | return 0 |
| 435 | } |
| 436 | |
| 437 | override fun prepare(): Long { |
| 438 | return 0 |
| 439 | } |
| 440 | |
| 441 | override fun shutdown(): Long { |
| 442 | return 0 |
| 443 | } |
| 444 | |
| 445 | override fun protect(p0: Long): Boolean { |
| 446 | return true |
| 447 | } |
| 448 | } |
| 449 | |
| 450 | } |