From de22e16cd408682d11e7698c75a580be22365338 Mon Sep 17 00:00:00 2001 From: 2dust <31833384+2dust@users.noreply.github.com> Date: Mon, 30 Sep 2024 13:31:10 +0800 Subject: [PATCH] Adjusting the default listening port for hy2 --- .../com/v2ray/ang/service/V2RayServiceManager.kt | 2 +- .../src/main/kotlin/com/v2ray/ang/util/PluginUtil.kt | 7 +++---- .../app/src/main/kotlin/com/v2ray/ang/util/Utils.kt | 11 +++++++++++ .../main/kotlin/com/v2ray/ang/util/V2rayConfigUtil.kt | 4 ++-- 4 files changed, 17 insertions(+), 7 deletions(-) diff --git a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/service/V2RayServiceManager.kt b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/service/V2RayServiceManager.kt index fea3dd49..3c3b2116 100644 --- a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/service/V2RayServiceManager.kt +++ b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/service/V2RayServiceManager.kt @@ -164,7 +164,7 @@ object V2RayServiceManager { MessageUtil.sendMsg2UI(service, AppConfig.MSG_STATE_START_SUCCESS, "") showNotification() - PluginUtil.runPlugin(service, config) + PluginUtil.runPlugin(service, config, result.domainPort) } else { MessageUtil.sendMsg2UI(service, AppConfig.MSG_STATE_START_FAILURE, "") cancelNotification() diff --git a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/PluginUtil.kt b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/PluginUtil.kt index 37de5eac..d649e861 100644 --- a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/PluginUtil.kt +++ b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/PluginUtil.kt @@ -4,11 +4,9 @@ import android.content.Context import android.os.SystemClock import android.util.Log import com.google.gson.Gson -import com.v2ray.ang.AppConfig import com.v2ray.ang.AppConfig.ANG_PACKAGE import com.v2ray.ang.dto.EConfigType import com.v2ray.ang.dto.ServerConfig -import com.v2ray.ang.util.MmkvManager.settingsStorage import com.v2ray.ang.util.fmt.Hysteria2Fmt import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers @@ -25,14 +23,15 @@ object PluginUtil { // return PluginManager.init(name)!! // } - fun runPlugin(context: Context, config: ServerConfig?) { + fun runPlugin(context: Context, config: ServerConfig?, domainPort: String?) { Log.d(packageName, "runPlugin") val outbound = config?.getProxyOutbound() ?: return if (outbound.protocol.equals(EConfigType.HYSTERIA2.name, true)) { Log.d(packageName, "runPlugin $HYSTERIA2") - val socksPort = 100 + SettingsManager.getSocksPort() + val socksPort = domainPort?.split(":")?.last() + .let { if (it.isNullOrEmpty()) return else it.toInt() } val hy2Config = Hysteria2Fmt.toNativeConfig(config, socksPort) ?: return val configFile = File(context.noBackupFilesDir, "hy2_${SystemClock.elapsedRealtime()}.json") diff --git a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/Utils.kt b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/Utils.kt index 062cef60..687fac9b 100644 --- a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/Utils.kt +++ b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/Utils.kt @@ -453,6 +453,17 @@ object Utils { } } + fun findFreePort(ports: List): Int { + for (port in ports) { + try { + return ServerSocket(port).use { it.localPort } + } catch (ex: IOException) { + continue // try next port + } + } + // if the program gets here, no port in the range was found + throw IOException("no free port found") + } } diff --git a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/V2rayConfigUtil.kt b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/V2rayConfigUtil.kt index 4783b30d..9f196980 100644 --- a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/V2rayConfigUtil.kt +++ b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/V2rayConfigUtil.kt @@ -43,7 +43,7 @@ object V2rayConfigUtil { val result = getV2rayNonCustomConfig(context, config) //Log.d(ANG_PACKAGE, result.content) - Log.d(ANG_PACKAGE, result.domainPort?:"") + Log.d(ANG_PACKAGE, result.domainPort ?: "") return result } catch (e: Exception) { e.printStackTrace() @@ -143,7 +143,7 @@ object V2rayConfigUtil { private fun outbounds(v2rayConfig: V2rayConfig, outbound: V2rayConfig.OutboundBean, isPlugin: Boolean): Pair { if (isPlugin) { - val socksPort = 100 + SettingsManager.getSocksPort() + val socksPort = Utils.findFreePort(listOf(100 + SettingsManager.getSocksPort(), 0)) val outboundNew = V2rayConfig.OutboundBean( mux = null, protocol = EConfigType.SOCKS.name.lowercase(),