feat: кастомное ядро — валидация ELF, редизайн карточки, inline-ошибка

This commit is contained in:
samosvalishe committed 2026-04-06 00:01:21 +03:00
1 parent 1d27a01448
commit 4a649a18cd
4 files changed
+119 -29

No files matched your search

@@ -108,17 +108,45 @@ class LocalProxyManager(private val context: Context) {
}
}
suspend fun setCustomKernel(uri: Uri) = withContext(Dispatchers.IO) {
suspend fun setCustomKernel(uri: Uri): String? = withContext(Dispatchers.IO) {
try {
val MAX_SIZE = 100L * 1024 * 1024 // 100 MB
val ELF_MAGIC = byteArrayOf(0x7F, 0x45, 0x4C, 0x46) // \x7FELF
val inputStream = context.contentResolver.openInputStream(uri)
?: return@withContext "Не удалось открыть файл"
// Читаем первые 4 байта для проверки ELF-магии
val header = ByteArray(4)
val headerRead = inputStream.read(header)
inputStream.close()
if (headerRead < 4 || !header.contentEquals(ELF_MAGIC)) {
return@withContext "Файл не является ELF-бинарником. Убедитесь, что загружаете правильное ядро"
}
// Копируем файл
val dest = File(context.filesDir, "custom_vkturn")
context.contentResolver.openInputStream(uri)?.use { input ->
dest.outputStream().use { output -> input.copyTo(output) }
}
if (dest.length() == 0L) {
dest.delete()
return@withContext "Файл пустой"
}
if (dest.length() > MAX_SIZE) {
dest.delete()
return@withContext "Файл слишком большой (максимум 100 МБ)"
}
dest.setExecutable(true)
withContext(Dispatchers.Main) { _customKernelExists.value = true }
ProxyServiceState.addLog("Кастомное ядро установлено: ${dest.length() / 1024} KB")
ProxyServiceState.addLog("Кастомное ядро установлено: ${dest.length() / 1024} КБ")
null
} catch (e: Exception) {
ProxyServiceState.addLog("Ошибка установки ядра: ${e.message}")
"Ошибка: ${e.message}"
}
}
@@ -130,6 +158,7 @@ class LocalProxyManager(private val context: Context) {
fun clearState() {
_proxyState.value = ProxyState.Idle
File(context.filesDir, "custom_vkturn").delete()
_customKernelExists.value = false
}
@@ -13,17 +13,19 @@ import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.imePadding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.FilledTonalButton
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.DividerDefaults
import androidx.compose.material3.HorizontalDivider
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.OutlinedButton
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Scaffold
import androidx.compose.material3.SegmentedButton
@@ -45,6 +47,7 @@ import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import com.freeturn.app.R
@@ -69,6 +72,7 @@ fun ClientSetupScreen(
val sshConfig by viewModel.sshConfig.collectAsStateWithLifecycle()
val proxyListen by viewModel.proxyListen.collectAsStateWithLifecycle()
val customKernelExists by viewModel.customKernelExists.collectAsStateWithLifecycle()
val kernelError by viewModel.kernelError.collectAsStateWithLifecycle()
val context = LocalContext.current
@@ -255,36 +259,55 @@ fun ClientSetupScreen(
Row(
modifier = Modifier
.fillMaxWidth()
.padding(horizontal = 16.dp, vertical = 12.dp),
.padding(horizontal = 16.dp, vertical = 14.dp),
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.SpaceBetween
) {
Column(modifier = Modifier.weight(1f)) {
Text(
if (customKernelExists) stringResource(R.string.custom_core) else stringResource(R.string.builtin_core),
style = MaterialTheme.typography.bodyMedium
Row(
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.spacedBy(12.dp),
modifier = Modifier.weight(1f)
) {
Icon(
painter = painterResource(
if (customKernelExists) R.drawable.check_circle_24px
else R.drawable.memory_24px
),
contentDescription = null,
tint = if (customKernelExists)
MaterialTheme.colorScheme.primary
else
MaterialTheme.colorScheme.onSurfaceVariant
)
Text(
if (customKernelExists) stringResource(R.string.loaded_from_memory)
else stringResource(R.string.from_apk),
style = MaterialTheme.typography.labelSmall,
color = MaterialTheme.colorScheme.onSurface.copy(alpha = 0.55f)
)
}
Row(horizontalArrangement = Arrangement.spacedBy(8.dp)) {
if (customKernelExists) {
OutlinedButton(
onClick = {
HapticUtil.perform(context, HapticUtil.Pattern.CLICK)
viewModel.clearCustomKernel()
},
colors = ButtonDefaults.outlinedButtonColors(
contentColor = MaterialTheme.colorScheme.error
)
) { Text(stringResource(R.string.reset)) }
Column(verticalArrangement = Arrangement.spacedBy(2.dp)) {
Text(
if (customKernelExists) stringResource(R.string.custom_core)
else stringResource(R.string.builtin_core),
style = MaterialTheme.typography.bodyMedium
)
Text(
if (customKernelExists) stringResource(R.string.loaded_from_memory)
else stringResource(R.string.from_apk),
style = MaterialTheme.typography.labelSmall,
color = MaterialTheme.colorScheme.onSurface.copy(alpha = 0.55f)
)
}
Button(onClick = {
}
if (customKernelExists) {
IconButton(onClick = {
HapticUtil.perform(context, HapticUtil.Pattern.CLICK)
viewModel.clearCustomKernel()
}) {
Icon(
painter = painterResource(R.drawable.delete_24px),
contentDescription = stringResource(R.string.reset),
tint = MaterialTheme.colorScheme.error
)
}
} else {
FilledTonalButton(onClick = {
HapticUtil.perform(context, HapticUtil.Pattern.CLICK)
viewModel.clearKernelError()
kernelPickerLauncher.launch(arrayOf("*/*"))
}) {
Text(stringResource(R.string.btn_load))
@@ -293,6 +316,25 @@ fun ClientSetupScreen(
}
}
if (kernelError != null) {
Row(
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.spacedBy(8.dp)
) {
Icon(
painter = painterResource(R.drawable.error_24px),
contentDescription = null,
tint = MaterialTheme.colorScheme.error,
modifier = Modifier.size(16.dp)
)
Text(
kernelError!!,
style = MaterialTheme.typography.labelMedium,
color = MaterialTheme.colorScheme.error
)
}
}
// Кнопка «Завершить» — только в онбординг-флоу
if (showFinishButton && onFinish != null) {
Spacer(Modifier.height(8.dp))
@@ -175,14 +175,23 @@ class MainViewModel(application: Application) : AndroidViewModel(application) {
}
// ── Custom kernel ──────────────────────────────────────────────────────
private val _kernelError = MutableStateFlow<String?>(null)
val kernelError: StateFlow<String?> = _kernelError.asStateFlow()
fun setCustomKernel(uri: Uri) {
viewModelScope.launch { proxyManager.setCustomKernel(uri) }
viewModelScope.launch {
_kernelError.value = proxyManager.setCustomKernel(uri)
}
}
fun clearCustomKernel() {
proxyManager.clearCustomKernel()
}
fun clearKernelError() {
_kernelError.value = null
}
fun resetAllSettings(context: Context) {
viewModelScope.launch {
if (ProxyServiceState.isRunning.value) {
+10
View File
@@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="960"
android:viewportHeight="960"
android:tint="?attr/colorControlNormal">
<path
android:fillColor="@android:color/white"
android:pathData="M360,600L360,360L600,360L600,600L360,600ZM440,520L520,520L520,440L440,440L440,520ZM360,840L360,760L280,760Q247,760 223.5,736.5Q200,713 200,680L200,600L120,600L120,520L200,520L200,440L120,440L120,360L200,360L200,280Q200,247 223.5,223.5Q247,200 280,200L360,200L360,120L440,120L440,200L520,200L520,120L600,120L600,200L680,200Q713,200 736.5,223.5Q760,247 760,280L760,360L840,360L840,440L760,440L760,520L840,520L840,600L760,600L760,680Q760,713 736.5,736.5Q713,760 680,760L600,760L600,840L520,840L520,760L440,760L440,840L360,840ZM680,680Q680,680 680,680Q680,680 680,680L680,280Q680,280 680,280Q680,280 680,280L280,280Q280,280 280,280Q280,280 280,280L280,680Q280,680 280,680Q280,680 280,680L680,680ZM480,480L480,480L480,480L480,480Z"/>
</vector>