| 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(), false) |
| 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 | |
| 146 | if (link.password.isNotBlank()) { |
| 147 | |
| 148 | // https://github.com/trojan-gfw/igniter/issues/318 |
| 149 | |
| 150 | vmess.id += ":" + link.password |
| 151 | |
| 152 | } |
| 153 | |
| 154 | vmess.requestHost = link.queryParameter("sni") ?: vmess.requestHost |
| 155 | vmess.remarks = link.fragment ?: "" |
| 156 | |
| 157 | return vmess |
| 158 | |
| 159 | } else if (server.startsWith(SOCKS_PROTOCOL)) { |
| 160 | var result = server.replace(SOCKS_PROTOCOL, "") |
| 161 | val indexSplit = result.indexOf("#") |
| 162 | if (indexSplit > 0) { |
| 163 | try { |
| 164 | vmess.remarks = Utils.urlDecode(result.substring(indexSplit + 1, result.length)) |
| 165 | } catch (e: Exception) { |
| 166 | e.printStackTrace() |
| 167 | } |
| 168 | |
| 169 | result = result.substring(0, indexSplit) |
| 170 | } |
| 171 | |
| 172 | //part decode |
| 173 | val indexS = result.indexOf(":") |
| 174 | if (indexS < 0) { |
| 175 | result = Base64.decodeStr(result) |
| 176 | } |
| 177 | |
| 178 | val legacyPattern = "^(.+?):(\\d+?)$".toRegex() |
| 179 | val match = legacyPattern.matchEntire(result) ?: error("invalid protocol") |
| 180 | vmess.address = match.groupValues[1] |
| 181 | if (vmess.address.firstOrNull() == '[' && vmess.address.lastOrNull() == ']') |
| 182 | vmess.address = vmess.address.substring(1, vmess.address.length - 1) |
| 183 | vmess.port = match.groupValues[2].toInt() |
| 184 | |
| 185 | return vmess |
| 186 | } else { |
| 187 | error("invalid protocol") |
| 188 | } |
| 189 | } catch (e: Exception) { |
| 190 | |
| 191 | throw IllegalArgumentException(e) |
| 192 | |
| 193 | } |
| 194 | |
| 195 | } |
| 196 | |
| 197 | private fun resolveSomeIOSAppShitCsvLink(csv: String): VmessBean { |
| 198 | |
| 199 | val args = csv.split(",") |
| 200 | |
| 201 | val bean = VmessBean() |
| 202 | |
| 203 | bean.configType = V2RayConfig.EConfigType.Vmess |
| 204 | bean.address = args[1] |
| 205 | bean.port = args[2].toInt() |
| 206 | bean.security = args[3] |
| 207 | bean.id = args[4].replace("\"", "") |
| 208 | |
| 209 | args.subList(5, args.size).forEach { |
| 210 | |
| 211 | when { |
| 212 | |
| 213 | it == "over-tls=true" -> { |
| 214 | |
| 215 | bean.streamSecurity = "tls" |
| 216 | |
| 217 | } |
| 218 | |
| 219 | it.startsWith("tls-host=") -> { |
| 220 | |
| 221 | bean.requestHost = it.substringAfter("=") |
| 222 | |
| 223 | } |
| 224 | |
| 225 | it.startsWith("obfs=") -> { |
| 226 | |
| 227 | bean.network = it.substringAfter("=") |
| 228 | |
| 229 | } |
| 230 | |
| 231 | it.startsWith("obfs-path=") || it.contains("Host:") -> { |
| 232 | |
| 233 | runCatching { |
| 234 | |
| 235 | bean.path = it |
| 236 | .substringAfter("obfs-path=\"") |
| 237 | .substringBefore("\"obfs") |
| 238 | |
| 239 | } |
| 240 | |
| 241 | runCatching { |
| 242 | |
| 243 | bean.requestHost = it |
| 244 | .substringAfter("Host:") |
| 245 | .substringBefore("[") |
| 246 | |
| 247 | } |
| 248 | |
| 249 | } |
| 250 | |
| 251 | } |
| 252 | |
| 253 | } |
| 254 | |
| 255 | return bean |
| 256 | |
| 257 | } |
| 258 | |
| 259 | /** |
| 260 | * upgrade |
| 261 | */ |
| 262 | private fun upgradeServerVersion(vmess: VmessBean): Int { |
| 263 | try { |
| 264 | if (vmess.configVersion == 2) { |
| 265 | return 0 |
| 266 | } |
| 267 | |
| 268 | when (vmess.network) { |
| 269 | "kcp" -> { |
| 270 | } |
| 271 | "ws" -> { |
| 272 | var path = "" |
| 273 | var host = "" |
| 274 | val lstParameter = vmess.requestHost.split(";") |
| 275 | if (lstParameter.size > 0) { |
| 276 | path = lstParameter.get(0).trim() |
| 277 | } |
| 278 | if (lstParameter.size > 1) { |
| 279 | path = lstParameter.get(0).trim() |
| 280 | host = lstParameter.get(1).trim() |
| 281 | } |
| 282 | vmess.path = path |
| 283 | vmess.requestHost = host |
| 284 | } |
| 285 | "h2" -> { |
| 286 | var path = "" |
| 287 | var host = "" |
| 288 | val lstParameter = vmess.requestHost.split(";") |
| 289 | if (lstParameter.isNotEmpty()) { |
| 290 | path = lstParameter[0].trim() |
| 291 | } |
| 292 | if (lstParameter.size > 1) { |
| 293 | path = lstParameter[0].trim() |
| 294 | host = lstParameter[1].trim() |
| 295 | } |
| 296 | vmess.path = path |
| 297 | vmess.requestHost = host |
| 298 | } |
| 299 | else -> { |
| 300 | } |
| 301 | } |
| 302 | vmess.configVersion = 2 |
| 303 | return 0 |
| 304 | } catch (e: Exception) { |
| 305 | e.printStackTrace() |
| 306 | return -1 |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | private fun resolveSimpleVmess1(server: String): VmessBean { |
| 311 | |
| 312 | val vmess = VmessBean() |
| 313 | |
| 314 | var result = server.replace(VMESS_PROTOCOL, "") |
| 315 | val indexSplit = result.indexOf("?") |
| 316 | if (indexSplit > 0) { |
| 317 | result = result.substring(0, indexSplit) |
| 318 | } |
| 319 | result = Base64.decodeStr(result) |
| 320 | |
| 321 | val arr1 = result.split('@') |
| 322 | if (arr1.count() != 2) { |
| 323 | return vmess |
| 324 | } |
| 325 | val arr21 = arr1[0].split(':') |
| 326 | val arr22 = arr1[1].split(':') |
| 327 | if (arr21.count() != 2 || arr21.count() != 2) { |
| 328 | return vmess |
| 329 | } |
| 330 | |
| 331 | vmess.address = arr22[0] |
| 332 | vmess.port = Utils.parseInt(arr22[1]) |
| 333 | vmess.security = arr21[0] |
| 334 | vmess.id = arr21[1] |
| 335 | |
| 336 | vmess.security = "chacha20-poly1305" |
| 337 | vmess.network = "tcp" |
| 338 | vmess.headerType = "none" |
| 339 | vmess.remarks = "" |
| 340 | vmess.alterId = 0 |
| 341 | |
| 342 | return vmess |
| 343 | } |
| 344 | |
| 345 | } |
| 346 | |
| 347 | lateinit var bean: VmessBean |
| 348 | |
| 349 | fun initConfig(config: VmessBean) { |
| 350 | |
| 351 | this.bean = config |
| 352 | |
| 353 | } |
| 354 | |
| 355 | fun start(): Int { |
| 356 | |
| 357 | var retry = 3 |
| 358 | |
| 359 | do { |
| 360 | |
| 361 | try { |
| 362 | |
| 363 | val port = Random.nextInt(4096, 32768) |
| 364 | |
| 365 | val conf = V2rayConfigUtil.getV2rayConfig(bean, port).content |
| 366 | |
| 367 | FileLog.d(conf) |
| 368 | |
| 369 | runCatching { |
| 370 | |
| 371 | Libv2ray.testConfig(conf) |
| 372 | |
| 373 | }.onFailure { |
| 374 | |
| 375 | FileLog.e(it) |
| 376 | |
| 377 | return -1 |
| 378 | |
| 379 | } |
| 380 | |
| 381 | point.configureFileContent = conf |
| 382 | point.domainName = V2rayConfigUtil.currDomain |
| 383 | |
| 384 | point.runLoop(true) |
| 385 | |
| 386 | return port |
| 387 | |
| 388 | } catch (e: Throwable) { |
| 389 | |
| 390 | retry-- |
| 391 | |
| 392 | if (retry <= 0) { |
| 393 | |
| 394 | FileLog.e(e) |
| 395 | |
| 396 | return -1 |
| 397 | |
| 398 | } |
| 399 | |
| 400 | } |
| 401 | |
| 402 | } while (true) |
| 403 | |
| 404 | } |
| 405 | |
| 406 | fun stop() { |
| 407 | |
| 408 | thread { |
| 409 | |
| 410 | runCatching { |
| 411 | |
| 412 | point.stopLoop() |
| 413 | |
| 414 | } |
| 415 | |
| 416 | } |
| 417 | |
| 418 | } |
| 419 | |
| 420 | class EmptyCallback : V2RayVPNServiceSupportsSet { |
| 421 | override fun onEmitStatus(p0: Long, p1: String?): Long { |
| 422 | return 0 |
| 423 | } |
| 424 | |
| 425 | override fun setup(p0: String?): Long { |
| 426 | return 0 |
| 427 | } |
| 428 | |
| 429 | override fun prepare(): Long { |
| 430 | return 0 |
| 431 | } |
| 432 | |
| 433 | override fun shutdown(): Long { |
| 434 | return 0 |
| 435 | } |
| 436 | |
| 437 | override fun protect(p0: Long): Boolean { |
| 438 | return true |
| 439 | } |
| 440 | } |
| 441 | |
| 442 | } |