Adjusting the default listening port for hy2
This commit is contained in:
@@ -164,7 +164,7 @@ object V2RayServiceManager {
|
|||||||
MessageUtil.sendMsg2UI(service, AppConfig.MSG_STATE_START_SUCCESS, "")
|
MessageUtil.sendMsg2UI(service, AppConfig.MSG_STATE_START_SUCCESS, "")
|
||||||
showNotification()
|
showNotification()
|
||||||
|
|
||||||
PluginUtil.runPlugin(service, config)
|
PluginUtil.runPlugin(service, config, result.domainPort)
|
||||||
} else {
|
} else {
|
||||||
MessageUtil.sendMsg2UI(service, AppConfig.MSG_STATE_START_FAILURE, "")
|
MessageUtil.sendMsg2UI(service, AppConfig.MSG_STATE_START_FAILURE, "")
|
||||||
cancelNotification()
|
cancelNotification()
|
||||||
|
|||||||
@@ -4,11 +4,9 @@ import android.content.Context
|
|||||||
import android.os.SystemClock
|
import android.os.SystemClock
|
||||||
import android.util.Log
|
import android.util.Log
|
||||||
import com.google.gson.Gson
|
import com.google.gson.Gson
|
||||||
import com.v2ray.ang.AppConfig
|
|
||||||
import com.v2ray.ang.AppConfig.ANG_PACKAGE
|
import com.v2ray.ang.AppConfig.ANG_PACKAGE
|
||||||
import com.v2ray.ang.dto.EConfigType
|
import com.v2ray.ang.dto.EConfigType
|
||||||
import com.v2ray.ang.dto.ServerConfig
|
import com.v2ray.ang.dto.ServerConfig
|
||||||
import com.v2ray.ang.util.MmkvManager.settingsStorage
|
|
||||||
import com.v2ray.ang.util.fmt.Hysteria2Fmt
|
import com.v2ray.ang.util.fmt.Hysteria2Fmt
|
||||||
import kotlinx.coroutines.CoroutineScope
|
import kotlinx.coroutines.CoroutineScope
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
@@ -25,14 +23,15 @@ object PluginUtil {
|
|||||||
// return PluginManager.init(name)!!
|
// return PluginManager.init(name)!!
|
||||||
// }
|
// }
|
||||||
|
|
||||||
fun runPlugin(context: Context, config: ServerConfig?) {
|
fun runPlugin(context: Context, config: ServerConfig?, domainPort: String?) {
|
||||||
Log.d(packageName, "runPlugin")
|
Log.d(packageName, "runPlugin")
|
||||||
|
|
||||||
val outbound = config?.getProxyOutbound() ?: return
|
val outbound = config?.getProxyOutbound() ?: return
|
||||||
if (outbound.protocol.equals(EConfigType.HYSTERIA2.name, true)) {
|
if (outbound.protocol.equals(EConfigType.HYSTERIA2.name, true)) {
|
||||||
Log.d(packageName, "runPlugin $HYSTERIA2")
|
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 hy2Config = Hysteria2Fmt.toNativeConfig(config, socksPort) ?: return
|
||||||
|
|
||||||
val configFile = File(context.noBackupFilesDir, "hy2_${SystemClock.elapsedRealtime()}.json")
|
val configFile = File(context.noBackupFilesDir, "hy2_${SystemClock.elapsedRealtime()}.json")
|
||||||
|
|||||||
@@ -453,6 +453,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")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -43,7 +43,7 @@ object V2rayConfigUtil {
|
|||||||
|
|
||||||
val result = getV2rayNonCustomConfig(context, config)
|
val result = getV2rayNonCustomConfig(context, config)
|
||||||
//Log.d(ANG_PACKAGE, result.content)
|
//Log.d(ANG_PACKAGE, result.content)
|
||||||
Log.d(ANG_PACKAGE, result.domainPort?:"")
|
Log.d(ANG_PACKAGE, result.domainPort ?: "")
|
||||||
return result
|
return result
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
e.printStackTrace()
|
e.printStackTrace()
|
||||||
@@ -143,7 +143,7 @@ object V2rayConfigUtil {
|
|||||||
|
|
||||||
private fun outbounds(v2rayConfig: V2rayConfig, outbound: V2rayConfig.OutboundBean, isPlugin: Boolean): Pair<Boolean, String> {
|
private fun outbounds(v2rayConfig: V2rayConfig, outbound: V2rayConfig.OutboundBean, isPlugin: Boolean): Pair<Boolean, String> {
|
||||||
if (isPlugin) {
|
if (isPlugin) {
|
||||||
val socksPort = 100 + SettingsManager.getSocksPort()
|
val socksPort = Utils.findFreePort(listOf(100 + SettingsManager.getSocksPort(), 0))
|
||||||
val outboundNew = V2rayConfig.OutboundBean(
|
val outboundNew = V2rayConfig.OutboundBean(
|
||||||
mux = null,
|
mux = null,
|
||||||
protocol = EConfigType.SOCKS.name.lowercase(),
|
protocol = EConfigType.SOCKS.name.lowercase(),
|
||||||
|
|||||||
Reference in New Issue
Block a user