From c330a1e70cd6820fa2e365b12d86df0609bb0fd2 Mon Sep 17 00:00:00 2001 From: acite <1498045907@qq.com> Date: Sun, 14 Sep 2025 01:02:39 +0800 Subject: [PATCH] [optimize] merge network write --- .../acitelight/aether/service/AbyssStream.kt | 17 +++++---- .../aether/service/AbyssTunnelProxy.kt | 5 +++ .../acitelight/aether/service/ApiClient.kt | 5 +++ .../acitelight/aether/service/ApiInterface.kt | 5 +++ .../com/acitelight/aether/view/MeScreen.kt | 35 +++++++++++++++---- 5 files changed, 54 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/com/acitelight/aether/service/AbyssStream.kt b/app/src/main/java/com/acitelight/aether/service/AbyssStream.kt index b657ddc..20b124f 100644 --- a/app/src/main/java/com/acitelight/aether/service/AbyssStream.kt +++ b/app/src/main/java/com/acitelight/aether/service/AbyssStream.kt @@ -278,15 +278,15 @@ class AbyssStream private constructor( private fun sendPlaintextChunk(plaintext: ByteArray) { if (closed) throw IllegalStateException("AbyssStream closed") - val ciphertextAndTag: ByteArray val nonce = ByteArray(NONCE_LEN) + val ciphertextAndTag: ByteArray val counterValue: Long synchronized(sendLock) { counterValue = sendCounter.getAndIncrement() } + System.arraycopy(sendSalt, 0, nonce, 0, NONCE_SALT_LEN) - val bb = ByteBuffer.wrap(nonce, NONCE_SALT_LEN, 8) - bb.putLong(counterValue) + ByteBuffer.wrap(nonce, NONCE_SALT_LEN, 8).putLong(counterValue) try { ciphertextAndTag = aeadEncrypt(nonce, plaintext) @@ -295,17 +295,22 @@ class AbyssStream private constructor( } val payloadLen = ciphertextAndTag.size - val header = ByteBuffer.allocate(4).putInt(payloadLen).array() + // header + ciphertextAndTag 一次性合并 + val packet = ByteBuffer.allocate(4 + payloadLen) + .putInt(payloadLen) + .put(ciphertextAndTag) + .array() + try { synchronized(output) { - output.write(header) - if (payloadLen > 0) output.write(ciphertextAndTag) + output.write(packet) output.flush() } } finally { // clear sensitive ciphertextAndTag.fill(0) plaintext.fill(0) + packet.fill(0) } } diff --git a/app/src/main/java/com/acitelight/aether/service/AbyssTunnelProxy.kt b/app/src/main/java/com/acitelight/aether/service/AbyssTunnelProxy.kt index 21ec020..022a8d5 100644 --- a/app/src/main/java/com/acitelight/aether/service/AbyssTunnelProxy.kt +++ b/app/src/main/java/com/acitelight/aether/service/AbyssTunnelProxy.kt @@ -1,6 +1,7 @@ package com.acitelight.aether.service +import android.util.Log import com.acitelight.aether.service.AuthManager.db64 import kotlinx.coroutines.* import kotlinx.coroutines.flow.first @@ -102,7 +103,9 @@ class AbyssTunnelProxy @Inject constructor( val read = localIn.read(buffer) if (read <= 0) break + Log.i("Delay Analyze", "Read $read Bytes from HttpClient") abyss.write(buffer, 0, read) + Log.i("Delay Analyze", "Wrote $read Bytes to Remote Abyss") } } @@ -113,7 +116,9 @@ class AbyssTunnelProxy @Inject constructor( val n = abyss.read(buffer, 0, buffer.size) if (n <= 0) break + Log.i("Delay Analyze", "Read $n Bytes from Remote Abyss") localOut.write(buffer, 0, n) + Log.i("Delay Analyze", "Wrote $n Bytes to HttpClient") } } } \ No newline at end of file diff --git a/app/src/main/java/com/acitelight/aether/service/ApiClient.kt b/app/src/main/java/com/acitelight/aether/service/ApiClient.kt index a5085cb..07f9d2d 100644 --- a/app/src/main/java/com/acitelight/aether/service/ApiClient.kt +++ b/app/src/main/java/com/acitelight/aether/service/ApiClient.kt @@ -224,6 +224,11 @@ object ApiClient { } api = createRetrofit().create(ApiInterface::class.java) + + Log.i("Delay Analyze", "Start Abyss Hello") + val h = api!!.hello() + Log.i("Delay Analyze", "Abyss Hello: ${h.string()}") + return base } catch (e: Exception) { api = null diff --git a/app/src/main/java/com/acitelight/aether/service/ApiInterface.kt b/app/src/main/java/com/acitelight/aether/service/ApiInterface.kt index 21e55e4..af82ff6 100644 --- a/app/src/main/java/com/acitelight/aether/service/ApiInterface.kt +++ b/app/src/main/java/com/acitelight/aether/service/ApiInterface.kt @@ -4,12 +4,14 @@ import com.acitelight.aether.model.BookMark import com.acitelight.aether.model.ChallengeResponse import com.acitelight.aether.model.ComicResponse import com.acitelight.aether.model.VideoResponse +import okhttp3.Response import okhttp3.ResponseBody import retrofit2.http.Body import retrofit2.http.GET import retrofit2.http.POST import retrofit2.http.Path import retrofit2.http.Query +import retrofit2.http.Streaming interface ApiInterface { @GET("api/video") @@ -56,4 +58,7 @@ interface ApiInterface { @Path("user") user: String, @Body challengeResponse: ChallengeResponse ): ResponseBody + + @GET("api/abyss") + suspend fun hello(): ResponseBody } diff --git a/app/src/main/java/com/acitelight/aether/view/MeScreen.kt b/app/src/main/java/com/acitelight/aether/view/MeScreen.kt index 086c486..c3b37cc 100644 --- a/app/src/main/java/com/acitelight/aether/view/MeScreen.kt +++ b/app/src/main/java/com/acitelight/aether/view/MeScreen.kt @@ -1,5 +1,6 @@ package com.acitelight.aether.view +import android.util.Log import android.widget.Toast import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Column @@ -36,8 +37,13 @@ import androidx.compose.ui.text.input.KeyboardType import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.sp import androidx.hilt.navigation.compose.hiltViewModel +import androidx.lifecycle.viewModelScope import androidx.lifecycle.viewmodel.compose.viewModel +import com.acitelight.aether.service.ApiClient.api import com.acitelight.aether.viewModel.MeScreenViewModel +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext @Composable fun MeScreen(meScreenViewModel: MeScreenViewModel = hiltViewModel()) { @@ -189,13 +195,28 @@ fun MeScreen(meScreenViewModel: MeScreenViewModel = hiltViewModel()) { Spacer(modifier = Modifier.height(24.dp)) // Save Button - Button( - onClick = { - meScreenViewModel.updateServer(url, cert, context) - }, - modifier = Modifier.fillMaxWidth() - ) { - Text("Save") + Row{ + Button( + onClick = { + meScreenViewModel.updateServer(url, cert, context) + }, + modifier = Modifier.fillMaxWidth(0.5f) + ) { + Text("Save") + } + + Button( + onClick = { + meScreenViewModel.viewModelScope.launch { + Log.i("Delay Analyze", "Start Abyss Hello") + val h = api!!.hello() + Log.i("Delay Analyze", "Abyss Hello: ${h.string()}") + } + }, + modifier = Modifier.fillMaxWidth(0.5f) + ) { + Text("Ping") + } } } }