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