mirror of
https://github.com/2dust/v2rayNG.git
synced 2026-10-29 06:31:54 +00:00
Simplify core error handling paths
https://github.com/2dust/v2rayNG/pull/6025
This commit is contained in:
1 parent
775546ce40
commit
fd48dcf9e0
6 files changed
+19
-70
No files matched your search
@@ -5,7 +5,6 @@ import android.text.TextUtils
|
|||||||
import com.google.gson.JsonArray
|
import com.google.gson.JsonArray
|
||||||
import com.google.gson.JsonObject
|
import com.google.gson.JsonObject
|
||||||
import com.v2ray.ang.AppConfig
|
import com.v2ray.ang.AppConfig
|
||||||
import com.v2ray.ang.R
|
|
||||||
import com.v2ray.ang.dto.ConfigResult
|
import com.v2ray.ang.dto.ConfigResult
|
||||||
import com.v2ray.ang.dto.CoreConfigContext
|
import com.v2ray.ang.dto.CoreConfigContext
|
||||||
import com.v2ray.ang.dto.V2rayConfig
|
import com.v2ray.ang.dto.V2rayConfig
|
||||||
@@ -38,8 +37,7 @@ object CoreConfigManager {
|
|||||||
?: return ConfigResult(
|
?: return ConfigResult(
|
||||||
status = false,
|
status = false,
|
||||||
guid = guid,
|
guid = guid,
|
||||||
errorMessage = "Failed to build config context",
|
errorMessage = "Failed to build config context"
|
||||||
displayMessage = context.getString(R.string.core_error_build_config_context)
|
|
||||||
)
|
)
|
||||||
if (configContext.isCustom) {
|
if (configContext.isCustom) {
|
||||||
return buildV2rayCustomConfig(configContext)
|
return buildV2rayCustomConfig(configContext)
|
||||||
@@ -47,15 +45,10 @@ object CoreConfigManager {
|
|||||||
return toConfigResult(configContext, buildUnifiedConfig(configContext))
|
return toConfigResult(configContext, buildUnifiedConfig(configContext))
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
LogUtil.e(AppConfig.TAG, "Failed to get V2ray config", e)
|
LogUtil.e(AppConfig.TAG, "Failed to get V2ray config", e)
|
||||||
val detail = e.message ?: e.javaClass.simpleName
|
|
||||||
return ConfigResult(
|
return ConfigResult(
|
||||||
status = false,
|
status = false,
|
||||||
guid = guid,
|
guid = guid,
|
||||||
errorMessage = "Failed to get V2ray config: $detail",
|
errorMessage = "Failed to get V2ray config: ${e.message ?: e.javaClass.simpleName}"
|
||||||
displayMessage = context.getString(
|
|
||||||
R.string.core_error_get_config_detail,
|
|
||||||
detail
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -71,8 +64,7 @@ object CoreConfigManager {
|
|||||||
?: return ConfigResult(
|
?: return ConfigResult(
|
||||||
status = false,
|
status = false,
|
||||||
guid = guid,
|
guid = guid,
|
||||||
errorMessage = "Failed to build config context",
|
errorMessage = "Failed to build config context"
|
||||||
displayMessage = context.getString(R.string.core_error_build_config_context)
|
|
||||||
)
|
)
|
||||||
if (configContext.isCustom) {
|
if (configContext.isCustom) {
|
||||||
return buildV2rayCustomConfig(configContext)
|
return buildV2rayCustomConfig(configContext)
|
||||||
@@ -83,15 +75,10 @@ object CoreConfigManager {
|
|||||||
return toConfigResult(configContext, v2rayConfig)
|
return toConfigResult(configContext, v2rayConfig)
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
LogUtil.e(AppConfig.TAG, "Failed to get V2ray config for speedtest", e)
|
LogUtil.e(AppConfig.TAG, "Failed to get V2ray config for speedtest", e)
|
||||||
val detail = e.message ?: e.javaClass.simpleName
|
|
||||||
return ConfigResult(
|
return ConfigResult(
|
||||||
status = false,
|
status = false,
|
||||||
guid = guid,
|
guid = guid,
|
||||||
errorMessage = "Failed to get V2ray config for speedtest: $detail",
|
errorMessage = "Failed to get V2ray config: ${e.message ?: e.javaClass.simpleName}"
|
||||||
displayMessage = context.getString(
|
|
||||||
R.string.core_error_get_speedtest_config_detail,
|
|
||||||
detail
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -105,8 +92,7 @@ object CoreConfigManager {
|
|||||||
?: return ConfigResult(
|
?: return ConfigResult(
|
||||||
status = false,
|
status = false,
|
||||||
guid = configContext.guid,
|
guid = configContext.guid,
|
||||||
errorMessage = "Custom config is empty",
|
errorMessage = "Failed to build config context, config is empty"
|
||||||
displayMessage = context.getString(R.string.core_error_custom_config_empty)
|
|
||||||
)
|
)
|
||||||
val result = ConfigResult(true, configContext.guid, raw)
|
val result = ConfigResult(true, configContext.guid, raw)
|
||||||
|
|
||||||
|
|||||||
@@ -58,12 +58,12 @@ object CoreNativeManager {
|
|||||||
*
|
*
|
||||||
* @return Version string of the V2Ray core
|
* @return Version string of the V2Ray core
|
||||||
*/
|
*/
|
||||||
fun getLibVersion(): String? {
|
fun getLibVersion(): String {
|
||||||
return try {
|
return try {
|
||||||
Libv2ray.checkVersionX()
|
Libv2ray.checkVersionX()
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
LogUtil.e(AppConfig.TAG, "Failed to check V2Ray version", e)
|
LogUtil.e(AppConfig.TAG, "Failed to check V2Ray version", e)
|
||||||
null
|
"Unknown"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -53,11 +53,6 @@ object CoreServiceManager {
|
|||||||
/** Tun descriptor the core was started with, null in the proxy only and root run modes. */
|
/** Tun descriptor the core was started with, null in the proxy only and root run modes. */
|
||||||
private var currentVpnInterface: ParcelFileDescriptor? = null
|
private var currentVpnInterface: ParcelFileDescriptor? = null
|
||||||
|
|
||||||
private class CoreStartException(
|
|
||||||
logMessage: String,
|
|
||||||
val displayMessage: String,
|
|
||||||
) : IllegalStateException(logMessage)
|
|
||||||
|
|
||||||
var serviceControl: SoftReference<ServiceControl>? = null
|
var serviceControl: SoftReference<ServiceControl>? = null
|
||||||
set(value) {
|
set(value) {
|
||||||
field = value
|
field = value
|
||||||
@@ -101,22 +96,15 @@ object CoreServiceManager {
|
|||||||
try {
|
try {
|
||||||
doStartCoreLoop(service, vpnInterface)
|
doStartCoreLoop(service, vpnInterface)
|
||||||
return true
|
return true
|
||||||
} catch (e: CoreStartException) {
|
|
||||||
reportCoreStartFailure(service, e.message.orEmpty(), e.displayMessage, e)
|
|
||||||
return false
|
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
val message = e.message?.takeUnless { it.isBlank() } ?: e.javaClass.simpleName
|
val message = e.message?.takeUnless { it.isBlank() } ?: e.javaClass.simpleName
|
||||||
reportCoreStartFailure(service, message, message, e)
|
LogUtil.e(AppConfig.TAG, "StartCore-Manager: $message", e)
|
||||||
|
MessageHelper.sendMsg2UI(service, AppConfig.MSG_STATE_START_FAILURE, message)
|
||||||
|
NotificationManager.cancelNotification()
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun reportCoreStartFailure(service: Service, logMessage: String, displayMessage: String, error: Throwable) {
|
|
||||||
LogUtil.e(AppConfig.TAG, "StartCore-Manager: $logMessage", error)
|
|
||||||
MessageHelper.sendMsg2UI(service, AppConfig.MSG_STATE_START_FAILURE, displayMessage)
|
|
||||||
NotificationManager.cancelNotification()
|
|
||||||
}
|
|
||||||
|
|
||||||
@Throws(Exception::class)
|
@Throws(Exception::class)
|
||||||
private fun doStartCoreLoop(service: Service, vpnInterface: ParcelFileDescriptor?) {
|
private fun doStartCoreLoop(service: Service, vpnInterface: ParcelFileDescriptor?) {
|
||||||
val mFilter = IntentFilter(AppConfig.BROADCAST_ACTION_SERVICE)
|
val mFilter = IntentFilter(AppConfig.BROADCAST_ACTION_SERVICE)
|
||||||
@@ -132,27 +120,14 @@ object CoreServiceManager {
|
|||||||
|
|
||||||
@Throws(Exception::class)
|
@Throws(Exception::class)
|
||||||
private fun launchCore(service: Service, vpnInterface: ParcelFileDescriptor?, isReload: Boolean = false) {
|
private fun launchCore(service: Service, vpnInterface: ParcelFileDescriptor?, isReload: Boolean = false) {
|
||||||
val guid = MmkvManager.getSelectServer()
|
val guid = MmkvManager.getSelectServer() ?: error("No server selected")
|
||||||
?: throw CoreStartException(
|
val config = MmkvManager.decodeServerConfig(guid) ?: error("Failed to decode server config")
|
||||||
logMessage = "No server selected",
|
|
||||||
displayMessage = service.getString(R.string.core_error_no_server_selected)
|
|
||||||
)
|
|
||||||
val config = MmkvManager.decodeServerConfig(guid)
|
|
||||||
?: throw CoreStartException(
|
|
||||||
logMessage = "Failed to decode server config",
|
|
||||||
displayMessage = service.getString(R.string.core_error_decode_server_config)
|
|
||||||
)
|
|
||||||
|
|
||||||
LogUtil.i(AppConfig.TAG, "StartCore-Manager: Starting core loop for ${config.remarks}")
|
LogUtil.i(AppConfig.TAG, "StartCore-Manager: Starting core loop for ${config.remarks}")
|
||||||
val result = CoreConfigManager.getV2rayConfig(service, guid)
|
val result = CoreConfigManager.getV2rayConfig(service, guid)
|
||||||
LogUtil.d(AppConfig.TAG, result.content)
|
LogUtil.d(AppConfig.TAG, result.content)
|
||||||
if (!result.status) {
|
if (!result.status) {
|
||||||
throw CoreStartException(
|
error(result.errorMessage.ifBlank { "Failed to get V2Ray config" })
|
||||||
logMessage = result.errorMessage.ifBlank { "Failed to get V2Ray config" },
|
|
||||||
displayMessage = result.displayMessage.ifBlank {
|
|
||||||
service.getString(R.string.core_error_get_config)
|
|
||||||
}
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
currentConfig = config
|
currentConfig = config
|
||||||
@@ -174,10 +149,7 @@ object CoreServiceManager {
|
|||||||
coreController.startLoop(result.content, tunFd)
|
coreController.startLoop(result.content, tunFd)
|
||||||
|
|
||||||
if (!isRunning()) {
|
if (!isRunning()) {
|
||||||
throw CoreStartException(
|
error("Core failed to start")
|
||||||
logMessage = "Core failed to start",
|
|
||||||
displayMessage = service.getString(R.string.core_error_start)
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (browserDialer != null) {
|
if (browserDialer != null) {
|
||||||
@@ -288,10 +260,6 @@ object CoreServiceManager {
|
|||||||
|
|
||||||
LogUtil.i(AppConfig.TAG, "StartCore-Manager: Core reload finished")
|
LogUtil.i(AppConfig.TAG, "StartCore-Manager: Core reload finished")
|
||||||
true
|
true
|
||||||
} catch (e: CoreStartException) {
|
|
||||||
LogUtil.e(AppConfig.TAG, "StartCore-Manager: Failed to reload core: ${e.message}", e)
|
|
||||||
MessageHelper.sendMsg2UI(service, AppConfig.MSG_STATE_START_FAILURE, e.displayMessage)
|
|
||||||
false
|
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
val message = e.message?.takeUnless { it.isBlank() } ?: e.javaClass.simpleName
|
val message = e.message?.takeUnless { it.isBlank() } ?: e.javaClass.simpleName
|
||||||
LogUtil.e(AppConfig.TAG, "StartCore-Manager: Failed to reload core: $message", e)
|
LogUtil.e(AppConfig.TAG, "StartCore-Manager: Failed to reload core: $message", e)
|
||||||
@@ -353,16 +321,14 @@ object CoreServiceManager {
|
|||||||
time = coreController.measureDelay(SettingsManager.getDelayTestUrl())
|
time = coreController.measureDelay(SettingsManager.getDelayTestUrl())
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
LogUtil.e(AppConfig.TAG, "StartCore-Manager: Failed to measure delay", e)
|
LogUtil.e(AppConfig.TAG, "StartCore-Manager: Failed to measure delay", e)
|
||||||
errorStr = e.message?.substringAfter("\":")
|
errorStr = e.message?.substringAfter("\":") ?: "empty message"
|
||||||
?: service.getString(R.string.connection_test_empty_message)
|
|
||||||
}
|
}
|
||||||
if (time == -1L) {
|
if (time == -1L) {
|
||||||
try {
|
try {
|
||||||
time = coreController.measureDelay(SettingsManager.getDelayTestUrl(true))
|
time = coreController.measureDelay(SettingsManager.getDelayTestUrl(true))
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
LogUtil.e(AppConfig.TAG, "StartCore-Manager: Failed to measure delay", e)
|
LogUtil.e(AppConfig.TAG, "StartCore-Manager: Failed to measure delay", e)
|
||||||
errorStr = e.message?.substringAfter("\":")
|
errorStr = e.message?.substringAfter("\":") ?: "empty message"
|
||||||
?: service.getString(R.string.connection_test_empty_message)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -526,4 +492,4 @@ object CoreServiceManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -4,9 +4,6 @@ data class ConfigResult(
|
|||||||
var status: Boolean,
|
var status: Boolean,
|
||||||
var guid: String? = null,
|
var guid: String? = null,
|
||||||
var content: String = "",
|
var content: String = "",
|
||||||
/** Stable English diagnostic text suitable for Logcat. */
|
|
||||||
var errorMessage: String = "",
|
var errorMessage: String = "",
|
||||||
/** Localized message suitable for the UI. */
|
|
||||||
var displayMessage: String = "",
|
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -53,7 +53,7 @@ fun AboutScreen(onBackClick: () -> Unit) {
|
|||||||
val context = LocalContext.current
|
val context = LocalContext.current
|
||||||
var showOssDialog by remember { mutableStateOf(false) }
|
var showOssDialog by remember { mutableStateOf(false) }
|
||||||
|
|
||||||
val libVersion = CoreNativeManager.getLibVersion() ?: stringResource(R.string.value_unknown)
|
val libVersion = CoreNativeManager.getLibVersion()
|
||||||
val versionText = "v${BuildConfig.VERSION_NAME} ($libVersion)"
|
val versionText = "v${BuildConfig.VERSION_NAME} ($libVersion)"
|
||||||
val appIdText = BuildConfig.APPLICATION_ID
|
val appIdText = BuildConfig.APPLICATION_ID
|
||||||
|
|
||||||
|
|||||||
@@ -59,7 +59,7 @@ fun CheckUpdateScreen(
|
|||||||
val showUpdateDialog by viewModel.showUpdateDialog.collectAsStateWithLifecycle()
|
val showUpdateDialog by viewModel.showUpdateDialog.collectAsStateWithLifecycle()
|
||||||
val updateResult by viewModel.updateResult.collectAsStateWithLifecycle()
|
val updateResult by viewModel.updateResult.collectAsStateWithLifecycle()
|
||||||
|
|
||||||
val libVersion = CoreNativeManager.getLibVersion() ?: stringResource(R.string.value_unknown)
|
val libVersion = CoreNativeManager.getLibVersion()
|
||||||
val versionText = "v${BuildConfig.VERSION_NAME} ($libVersion)"
|
val versionText = "v${BuildConfig.VERSION_NAME} ($libVersion)"
|
||||||
|
|
||||||
Scaffold(
|
Scaffold(
|
||||||
|
|||||||
Reference in new issue
Block a user