From 47efdf165df5da90f7e66757e7bfbac16ff681ba Mon Sep 17 00:00:00 2001 From: rhunk <101876869+rhunk@users.noreply.github.com> Date: Fri, 16 Aug 2024 12:57:45 +0200 Subject: [PATCH] feat(core/security_features): better explanation of login response --- .../snapenhance/RemoteSharedLibraryManager.kt | 2 +- .../ui/manager/pages/home/HomeRootSection.kt | 9 ++- app/src/main/res/drawable/ic_codeberg.xml | 11 ++++ common/build.gradle.kts | 2 +- .../core/features/impl/SecurityFeatures.kt | 60 +++++++++++++++---- 5 files changed, 70 insertions(+), 14 deletions(-) create mode 100644 app/src/main/res/drawable/ic_codeberg.xml diff --git a/app/src/main/kotlin/me/rhunk/snapenhance/RemoteSharedLibraryManager.kt b/app/src/main/kotlin/me/rhunk/snapenhance/RemoteSharedLibraryManager.kt index 0566a453..0cd142b7 100644 --- a/app/src/main/kotlin/me/rhunk/snapenhance/RemoteSharedLibraryManager.kt +++ b/app/src/main/kotlin/me/rhunk/snapenhance/RemoteSharedLibraryManager.kt @@ -103,7 +103,7 @@ class RemoteSharedLibraryManager( 0, Intent().apply { action = Intent.ACTION_VIEW - data = "https://github.com/SnapEnhance/resources".toUri() + data = "https://codeberg.org/SnapEnhance/resources".toUri() flags = Intent.FLAG_ACTIVITY_NEW_TASK }, PendingIntent.FLAG_UPDATE_CURRENT diff --git a/app/src/main/kotlin/me/rhunk/snapenhance/ui/manager/pages/home/HomeRootSection.kt b/app/src/main/kotlin/me/rhunk/snapenhance/ui/manager/pages/home/HomeRootSection.kt index eae76af4..8d00f7ed 100644 --- a/app/src/main/kotlin/me/rhunk/snapenhance/ui/manager/pages/home/HomeRootSection.kt +++ b/app/src/main/kotlin/me/rhunk/snapenhance/ui/manager/pages/home/HomeRootSection.kt @@ -178,6 +178,11 @@ class HomeRootSection : Routes.Route() { .fillMaxWidth() .padding(all = 10.dp) ) { + ExternalLinkIcon( + imageVector = ImageVector.vectorResource(id = R.drawable.ic_codeberg), + link = "https://codeberg.org/SnapEnhance/SnapEnhance" + ) + ExternalLinkIcon( imageVector = ImageVector.vectorResource(id = R.drawable.ic_telegram), link = "https://t.me/snapenhance" @@ -189,8 +194,8 @@ class HomeRootSection : Routes.Route() { ) ExternalLinkIcon( - size = 36.dp, - modifier = Modifier.offset(y = (-2).dp), + size = 38.dp, + modifier = Modifier.offset(x = (-3).dp, y = (-3).dp), imageVector = Icons.AutoMirrored.Default.Help, link = "https://github.com/rhunk/SnapEnhance/wiki" ) diff --git a/app/src/main/res/drawable/ic_codeberg.xml b/app/src/main/res/drawable/ic_codeberg.xml new file mode 100644 index 00000000..12833d58 --- /dev/null +++ b/app/src/main/res/drawable/ic_codeberg.xml @@ -0,0 +1,11 @@ + + + + + + + + + + + diff --git a/common/build.gradle.kts b/common/build.gradle.kts index ed177af6..a6acb5f6 100644 --- a/common/build.gradle.kts +++ b/common/build.gradle.kts @@ -30,7 +30,7 @@ android { standardOutput = gitHash } buildConfigField("String", "GIT_HASH", "\"${gitHash.toString(Charsets.UTF_8).trim()}\"") - buildConfigField("String", "SIF_ENDPOINT", "\"${properties["debug_sif_endpoint"]?.toString() ?: "https://raw.githubusercontent.com/SnapEnhance/resources/main/sif"}\"") + buildConfigField("String", "SIF_ENDPOINT", "\"${properties["debug_sif_endpoint"]?.toString() ?: "https://codeberg.org/SnapEnhance/resources/raw/branch/main/sif"}\"") } compileOptions { diff --git a/core/src/main/kotlin/me/rhunk/snapenhance/core/features/impl/SecurityFeatures.kt b/core/src/main/kotlin/me/rhunk/snapenhance/core/features/impl/SecurityFeatures.kt index dd5f3349..f3f49c7f 100644 --- a/core/src/main/kotlin/me/rhunk/snapenhance/core/features/impl/SecurityFeatures.kt +++ b/core/src/main/kotlin/me/rhunk/snapenhance/core/features/impl/SecurityFeatures.kt @@ -5,11 +5,7 @@ import androidx.compose.foundation.background import androidx.compose.foundation.layout.padding import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.material3.Text -import androidx.compose.runtime.LaunchedEffect -import androidx.compose.runtime.getValue -import androidx.compose.runtime.mutableStateOf -import androidx.compose.runtime.remember -import androidx.compose.runtime.setValue +import androidx.compose.runtime.* import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.Color @@ -18,22 +14,66 @@ import androidx.compose.ui.unit.sp import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.delay import kotlinx.coroutines.withContext +import me.rhunk.snapenhance.common.util.protobuf.ProtoEditor +import me.rhunk.snapenhance.common.util.protobuf.ProtoReader +import me.rhunk.snapenhance.core.event.events.impl.UnaryCallEvent import me.rhunk.snapenhance.core.features.Feature +import me.rhunk.snapenhance.core.util.ktx.setObjectField +import java.io.FileDescriptor class SecurityFeatures : Feature("Security Features") { - private fun transact(option: Int) = Os.prctl(option, 0, 0, 0, 0) + private fun transact(option: Int, option2: Long) = kotlin.runCatching { Os.prctl(option, option2, 0, 0, 0) }.getOrNull() private val token by lazy { - runCatching { transact(0) }.getOrNull() + transact(0, 0) } private fun getStatus() = token?.run { - transact(this).toString(2).padStart(32, '0').count { it == '1' } + transact(this, 0)?.toString(2)?.padStart(32, '0')?.count { it == '1' } } override fun init() { token // pre init token + context.event.subscribe(UnaryCallEvent::class) { event -> + if (!event.uri.contains("/Login")) return@subscribe + + // intercept login response + event.addResponseCallback { + val response = ProtoReader(buffer) + val isBlocked = when { + event.uri.contains("TLv") -> response.getVarInt(1) == 14L + else -> response.getVarInt(1) == 16L + } + + val errorDataIndex = when { + response.contains(11) -> 11 + response.contains(10) -> 10 + response.contains(8) -> 8 + else -> return@addResponseCallback + } + + if (isBlocked) { + val status = transact(token ?: return@addResponseCallback, 1)?.let { + val buffer = ByteArray(8192) + val fd = FileDescriptor().apply { + setObjectField("descriptor", it) + } + val read = Os.read(fd, buffer, 0, buffer.size) + Os.close(fd) + buffer.copyOfRange(0, read).decodeToString() + }!! + + buffer = ProtoEditor(buffer).apply { + edit(errorDataIndex) { + remove(1) + addString(1, status) + } + }.toByteArray() + } + } + } + context.inAppOverlay.addCustomComposable { var statusText by remember { mutableStateOf("") @@ -49,10 +89,10 @@ class SecurityFeatures : Feature("Security Features") { withContext(Dispatchers.Main) { if (status == null || status == 0) { textColor = Color.Red - statusText = "sif not loaded. Can't get status" + statusText = "SIF not loaded!" } else { textColor = Color.Green - statusText = "sif = $status" + statusText = "SIF = $status" } } delay(1000)