Skip to content

Commit 7fb8fac

Browse files
2dustAnGgIt886
authored andcommitted
Adjusting the default listening port for hy2
1 parent e14646e commit 7fb8fac

File tree

4 files changed

+17
-7
lines changed

4 files changed

+17
-7
lines changed

app/src/main/kotlin/com/neko/v2ray/service/V2RayServiceManager.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ object V2RayServiceManager {
164164
MessageUtil.sendMsg2UI(service, AppConfig.MSG_STATE_START_SUCCESS, "")
165165
showNotification()
166166

167-
PluginUtil.runPlugin(service, config)
167+
PluginUtil.runPlugin(service, config, result.domainPort)
168168
} else {
169169
MessageUtil.sendMsg2UI(service, AppConfig.MSG_STATE_START_FAILURE, "")
170170
cancelNotification()

app/src/main/kotlin/com/neko/v2ray/util/PluginUtil.kt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,9 @@ import android.content.Context
44
import android.os.SystemClock
55
import android.util.Log
66
import com.google.gson.Gson
7-
import com.neko.v2ray.AppConfig
87
import com.neko.v2ray.AppConfig.ANG_PACKAGE
98
import com.neko.v2ray.dto.EConfigType
109
import com.neko.v2ray.dto.ServerConfig
11-
import com.neko.v2ray.util.MmkvManager.settingsStorage
1210
import com.neko.v2ray.util.fmt.Hysteria2Fmt
1311
import kotlinx.coroutines.CoroutineScope
1412
import kotlinx.coroutines.Dispatchers
@@ -25,14 +23,15 @@ object PluginUtil {
2523
// return PluginManager.init(name)!!
2624
// }
2725

28-
fun runPlugin(context: Context, config: ServerConfig?) {
26+
fun runPlugin(context: Context, config: ServerConfig?, domainPort: String?) {
2927
Log.d(packageName, "runPlugin")
3028

3129
val outbound = config?.getProxyOutbound() ?: return
3230
if (outbound.protocol.equals(EConfigType.HYSTERIA2.name, true)) {
3331
Log.d(packageName, "runPlugin $HYSTERIA2")
3432

35-
val socksPort = 100 + SettingsManager.getSocksPort()
33+
val socksPort = domainPort?.split(":")?.last()
34+
.let { if (it.isNullOrEmpty()) return else it.toInt() }
3635
val hy2Config = Hysteria2Fmt.toNativeConfig(config, socksPort) ?: return
3736

3837
val configFile = File(context.noBackupFilesDir, "hy2_${SystemClock.elapsedRealtime()}.json")

app/src/main/kotlin/com/neko/v2ray/util/Utils.kt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,17 @@ object Utils {
456456
}
457457
}
458458

459+
fun findFreePort(ports: List<Int>): Int {
460+
for (port in ports) {
461+
try {
462+
return ServerSocket(port).use { it.localPort }
463+
} catch (ex: IOException) {
464+
continue // try next port
465+
}
466+
}
459467

468+
// if the program gets here, no port in the range was found
469+
throw IOException("no free port found")
470+
}
460471
}
461472

app/src/main/kotlin/com/neko/v2ray/util/V2rayConfigUtil.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ object V2rayConfigUtil {
4343

4444
val result = getV2rayNonCustomConfig(context, config)
4545
//Log.d(ANG_PACKAGE, result.content)
46-
Log.d(ANG_PACKAGE, result.domainPort?:"")
46+
Log.d(ANG_PACKAGE, result.domainPort ?: "")
4747
return result
4848
} catch (e: Exception) {
4949
e.printStackTrace()
@@ -143,7 +143,7 @@ object V2rayConfigUtil {
143143

144144
private fun outbounds(v2rayConfig: V2rayConfig, outbound: V2rayConfig.OutboundBean, isPlugin: Boolean): Pair<Boolean, String> {
145145
if (isPlugin) {
146-
val socksPort = 100 + SettingsManager.getSocksPort()
146+
val socksPort = Utils.findFreePort(listOf(100 + SettingsManager.getSocksPort(), 0))
147147
val outboundNew = V2rayConfig.OutboundBean(
148148
mux = null,
149149
protocol = EConfigType.SOCKS.name.lowercase(),

0 commit comments

Comments
 (0)