Skip to content
This repository has been archived by the owner on Nov 14, 2024. It is now read-only.

Commit

Permalink
Adjusting the default listening port for hy2
Browse files Browse the repository at this point in the history
  • Loading branch information
2dust authored and AnGgIt86 committed Sep 30, 2024
1 parent e14646e commit 7fb8fac
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
7 changes: 3 additions & 4 deletions app/src/main/kotlin/com/neko/v2ray/util/PluginUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@ import android.content.Context
import android.os.SystemClock
import android.util.Log
import com.google.gson.Gson
import com.neko.v2ray.AppConfig
import com.neko.v2ray.AppConfig.ANG_PACKAGE
import com.neko.v2ray.dto.EConfigType
import com.neko.v2ray.dto.ServerConfig
import com.neko.v2ray.util.MmkvManager.settingsStorage
import com.neko.v2ray.util.fmt.Hysteria2Fmt
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
Expand All @@ -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")
Expand Down
11 changes: 11 additions & 0 deletions app/src/main/kotlin/com/neko/v2ray/util/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,17 @@ object Utils {
}
}

fun findFreePort(ports: List<Int>): 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")
}
}

4 changes: 2 additions & 2 deletions app/src/main/kotlin/com/neko/v2ray/util/V2rayConfigUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -143,7 +143,7 @@ object V2rayConfigUtil {

private fun outbounds(v2rayConfig: V2rayConfig, outbound: V2rayConfig.OutboundBean, isPlugin: Boolean): Pair<Boolean, String> {
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(),
Expand Down

0 comments on commit 7fb8fac

Please sign in to comment.