diff --git a/.github/workflows/create_release.yml b/.github/workflows/create_release.yml new file mode 100644 index 0000000..425a40e --- /dev/null +++ b/.github/workflows/create_release.yml @@ -0,0 +1,96 @@ +name: create release + +on: + push: + branches: [ master ] + paths: + - "**/workflows-trigger.properties" + +jobs: + + current-time: + runs-on: ubuntu-latest + name: get current time + outputs: + currentTime: ${{steps.currentTime.outputs.formattedTime}} + steps: + - id: currentTime + uses: josStorer/get-current-time@v2 + with: + format: YYYY.MM.DD + utcOffset: "+08:00" + + create-release-distribution: + strategy: + matrix: + os: [ windows-latest , ubuntu-latest , macos-13 , macos-14 ] + runs-on: ${{ matrix.os }} + name: create release distribution + needs: current-time + + steps: + - if: matrix.os != 'macos-14' + name: setup jdk + uses: actions/setup-java@v4 + with: + java-version: "18" + distribution: "zulu" + architecture: x64 + + - if: matrix.os == 'macos-14' + name: setup jdk + uses: actions/setup-java@v4 + with: + java-version: "18" + distribution: "zulu" + architecture: aarch64 + + - name: checkout + uses: actions/checkout@v4 + + - name: grant execute permission for gradlew + run: chmod +x gradlew + + - name: packageReleaseDistributionForCurrentOS + run: ./gradlew packageReleaseDistributionForCurrentOS + + - if: matrix.os == 'windows-latest' + name: rename File + run: | + mv ./build/compose/binaries/main-release/exe/compose-multiplatform-xlog-decode-1.0.0.exe ./build/compose/binaries/main-release/exe/compose-multiplatform-xlog-decode-windows-x64.exe + + - if: matrix.os == 'windows-latest' + name: zip AppImage + uses: thedoctor0/zip-release@0.7.6 + with: + type: "zip" + filename: "compose-multiplatform-xlog-decode-windows-x64.zip" + directory: "./build/compose/binaries/main-release/app/compose-multiplatform-xlog-decode" + + - if: matrix.os == 'ubuntu-latest' + name: rename File + run: | + mv /home/runner/work/compose-multiplatform-xlog-decode/compose-multiplatform-xlog-decode/build/compose/binaries/main-release/deb/compose-multiplatform-xlog-decode_1.0.0_amd64.deb /home/runner/work/compose-multiplatform-xlog-decode/compose-multiplatform-xlog-decode/build/compose/binaries/main-release/deb/compose-multiplatform-xlog-decode-linux-amd64.deb + mv /home/runner/work/compose-multiplatform-xlog-decode/compose-multiplatform-xlog-decode/build/compose/binaries/main-release/rpm/compose-multiplatform-xlog-decode-1.0.0-1.x86_64.rpm /home/runner/work/compose-multiplatform-xlog-decode/compose-multiplatform-xlog-decode/build/compose/binaries/main-release/rpm/compose-multiplatform-xlog-decode-linux-x86_64.rpm + + - if: matrix.os == 'macos-13' + name: rename File + run: | + mv /Users/runner/work/compose-multiplatform-xlog-decode/compose-multiplatform-xlog-decode/build/compose/binaries/main-release/dmg/compose-multiplatform-xlog-decode-1.0.0.dmg /Users/runner/work/compose-multiplatform-xlog-decode/compose-multiplatform-xlog-decode/build/compose/binaries/main-release/dmg/compose-multiplatform-xlog-decode-mac-x64.dmg + + - if: matrix.os == 'macos-14' + name: rename File + run: | + mv /Users/runner/work/compose-multiplatform-xlog-decode/compose-multiplatform-xlog-decode/build/compose/binaries/main-release/dmg/compose-multiplatform-xlog-decode-1.0.0.dmg /Users/runner/work/compose-multiplatform-xlog-decode/compose-multiplatform-xlog-decode/build/compose/binaries/main-release/dmg/compose-multiplatform-xlog-decode-mac-arm64.dmg + + - name: create a release + uses: ncipollo/release-action@v1 + with: + artifacts: "**/compose-multiplatform-xlog-decode-windows-x64.exe,**/compose-multiplatform-xlog-decode-windows-x64.zip,**/*.deb,**/*.rpm,**/*.dmg" + body: "create by workflows" + allowUpdates: true + artifactErrorsFailBuild: false + generateReleaseNotes: false + tag: ${{needs.current-time.outputs.currentTime}} + name: ${{needs.current-time.outputs.currentTime}} + token: ${{secrets.ACTION_TOKEN}} \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..444787f --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +/.idea +/.kotlin +/config +**/build +.gradle \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..261eeb9 --- /dev/null +++ b/LICENSE @@ -0,0 +1,201 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/README.md b/README.md new file mode 100644 index 0000000..f694860 --- /dev/null +++ b/README.md @@ -0,0 +1,5 @@ +# compose-multiplatform-xlog-decode + +一个跨平台的 xlog 日志解析工具,支持 Windows、MacOS、Linux 三个平台 + +![](https://github.com/leavesCZY/compose-multiplatform-xlog-decode/assets/30774063/57187495-9dd6-4644-afb6-154c31944a40) \ No newline at end of file diff --git a/build.gradle.kts b/build.gradle.kts new file mode 100644 index 0000000..fbbab08 --- /dev/null +++ b/build.gradle.kts @@ -0,0 +1,126 @@ +import org.jetbrains.compose.desktop.application.dsl.TargetFormat +import org.jetbrains.kotlin.gradle.dsl.JvmTarget + +plugins { + alias(libs.plugins.jetbrains.kotlin.jvm) + alias(libs.plugins.jetbrains.compose) + alias(libs.plugins.jetbrains.compose.compiler) +} + +group = "github.leavesczy" +version = "1.0.0" + +kotlin { + jvmToolchain(18) + compilerOptions { + jvmTarget.set(JvmTarget.JVM_18) + optIn.set( + setOf( + "androidx.compose.ui.ExperimentalComposeUiApi", + "androidx.compose.foundation.ExperimentalFoundationApi", + "androidx.compose.material3.ExperimentalMaterial3Api" + ) + ) + } +} + +tasks { + withType { + exclude( + "META-INF/*.MF", + "META-INF/*.RSA", + "META-INF/*.SF", + "META-INF/*.EC", + "META-INF/*.DSA", + "META-INF/*.LIST", + "META-INF/*.kotlin_module", + "META-INF/LICENSE", + "META-INF/LICENSE.txt", + ) + } +} + +dependencies { + implementation(project(":core")) + implementation(compose.desktop.currentOs) + implementation(compose.material3) + implementation(compose.materialIconsExtended) + implementation(libs.androidx.datastore.preferences.core) + implementation(libs.jetbrains.lifecycle.viewmodel.compose) +} + +enum class OS(val id: String) { + Linux("linux"), + Windows("windows"), + MacOS("macos") +} + +val currentOS: OS by lazy { + val os = System.getProperty("os.name") + when { + os.equals("Mac OS X", ignoreCase = true) -> OS.MacOS + os.startsWith("Win", ignoreCase = true) -> OS.Windows + os.startsWith("Linux", ignoreCase = true) -> OS.Linux + else -> error("Unknown OS name: $os") + } +} + +compose.desktop { + application { + mainClass = "github.leavesczy.xlog.decode.MainKt" + val mPackageName = "compose-multiplatform-xlog-decode" + nativeDistributions { + includeAllModules = false + modules = arrayListOf("jdk.unsupported", "java.desktop", "java.logging") + when (currentOS) { + OS.Windows -> { + targetFormats(TargetFormat.AppImage, TargetFormat.Exe) + } + + OS.MacOS -> { + targetFormats(TargetFormat.Dmg) + } + + OS.Linux -> { + targetFormats(TargetFormat.Deb, TargetFormat.Rpm) + } + } + packageName = mPackageName + packageVersion = "1.0.0" + description = "compose multiplatform xlog decode" + copyright = "© 2024 leavesCZY. All rights reserved." + vendor = "leavesCZY" + val resourcesDir = project.file("src/main/resources") + windows { + menuGroup = packageName + dirChooser = true + perUserInstall = true + shortcut = true + menu = true + upgradeUuid = "D542171E-5CDC-428E-BF21-68FBAD85369F" + iconFile.set(resourcesDir.resolve("windows_launch_icon.ico")) + installationPath = packageName + } + macOS { + bundleID = mPackageName + setDockNameSameAsPackageName = true + appStore = true + iconFile.set(resourcesDir.resolve("macos_launch_icon.icns")) + } + linux { + shortcut = true + menuGroup = mPackageName + iconFile.set(resourcesDir.resolve("linux_launch_icon.png")) + } + } + buildTypes.release { + proguard { + isEnabled.set(true) + obfuscate.set(true) + optimize.set(true) + joinOutputJars.set(true) + configurationFiles.from("proguard-rules.pro") + } + } + } +} \ No newline at end of file diff --git a/core/build.gradle.kts b/core/build.gradle.kts new file mode 100644 index 0000000..7862d03 --- /dev/null +++ b/core/build.gradle.kts @@ -0,0 +1,18 @@ +import org.jetbrains.kotlin.gradle.dsl.JvmTarget + +plugins { + alias(libs.plugins.jetbrains.kotlin.jvm) +} + +kotlin { + jvmToolchain(18) + compilerOptions { + jvmTarget.set(JvmTarget.JVM_18) + } +} + +dependencies { + implementation(files("libs/bcprov-jdk18on-1.78.1.jar")) +// implementation(libs.bouncycastle.bcprov.jdk18on) + implementation(libs.luben.zstd.jni) +} \ No newline at end of file diff --git a/core/libs/bcprov-jdk18on-1.78.1.jar b/core/libs/bcprov-jdk18on-1.78.1.jar new file mode 100644 index 0000000..fee419e Binary files /dev/null and b/core/libs/bcprov-jdk18on-1.78.1.jar differ diff --git a/core/log/AppednerModeAsync_ZLIB_HasCrypt.xlog b/core/log/AppednerModeAsync_ZLIB_HasCrypt.xlog new file mode 100644 index 0000000..8f20f29 Binary files /dev/null and b/core/log/AppednerModeAsync_ZLIB_HasCrypt.xlog differ diff --git a/core/log/AppednerModeAsync_ZLIB_NoCrypt.xlog b/core/log/AppednerModeAsync_ZLIB_NoCrypt.xlog new file mode 100644 index 0000000..7e13bf5 Binary files /dev/null and b/core/log/AppednerModeAsync_ZLIB_NoCrypt.xlog differ diff --git a/core/src/main/kotlin/Main.kt b/core/src/main/kotlin/Main.kt new file mode 100644 index 0000000..5050682 --- /dev/null +++ b/core/src/main/kotlin/Main.kt @@ -0,0 +1,42 @@ +import github.leavesczy.xlog.decode.core.LogDecode +import github.leavesczy.xlog.decode.core.Logger +import java.io.File +import java.text.SimpleDateFormat +import java.util.* + +/** + * @Author: leavesCZY + * @Date: 2024/6/4 14:17 + * @Desc: + */ +fun main() { + val privateKey = "a763761dabcadf7762cc1dc569e0d64eec757fdedd0aecc02817d5693ac83d74" + val publicKey = + "9208edf99ad9825d75c14d88bcc39e3c53c2a2bea193e20d5b3a0a933b6eb4b44dec5757aad56b754c3cb672981d893f3b12222c9c1573740322ad9dc62dd332" + + val zlibHasCryptLogFile = File("core/log/AppednerModeAsync_ZLIB_HasCrypt.xlog") + decodeFile(logFile = zlibHasCryptLogFile, privateKey = privateKey) + + val zlibNoCryptLogFile = File("core/log/AppednerModeAsync_ZLIB_NoCrypt.xlog") + decodeFile(logFile = zlibNoCryptLogFile, privateKey = "") +} + +private fun decodeFile(logFile: File, privateKey: String) { + val logDecode = LogDecode(logger = object : Logger { + override fun debug(log: () -> Any) { + println(log().toString()) + } + + override fun error(log: () -> Any) { + println(log().toString()) + } + }) + val logFileName = logFile.nameWithoutExtension + val outFileName = logFileName + "_" + SimpleDateFormat("yyyy_MM_dd_HH_mm_ss").format(Date()) + ".txt" + val outFile = File("core/build/$outFileName") + logDecode.decodeFile( + privateKey = privateKey, + logFile = logFile, + outFile = outFile + ) +} \ No newline at end of file diff --git a/core/src/main/kotlin/github/leavesczy/xlog/decode/core/DecompressUtils.kt b/core/src/main/kotlin/github/leavesczy/xlog/decode/core/DecompressUtils.kt new file mode 100644 index 0000000..34a2d11 --- /dev/null +++ b/core/src/main/kotlin/github/leavesczy/xlog/decode/core/DecompressUtils.kt @@ -0,0 +1,37 @@ +package github.leavesczy.xlog.decode.core + +import com.github.luben.zstd.ZstdInputStream +import java.io.ByteArrayInputStream +import java.io.ByteArrayOutputStream +import java.util.zip.Inflater +import java.util.zip.InflaterOutputStream + +/** + * @Author: leavesCZY + * @Date: 2024/6/4 14:16 + * @Desc: + */ +internal object DecompressUtils { + + fun zlibDecompress(data: ByteArray): ByteArray { + val byteArrayOutputStream = ByteArrayOutputStream() + val outputStream = InflaterOutputStream(byteArrayOutputStream, Inflater(true)) + outputStream.write(data) + outputStream.close() + return byteArrayOutputStream.toByteArray() + } + + fun zstdDecompress(data: ByteArray): ByteArray { + val byteArrayInputStream = ByteArrayInputStream(data) + val byteArrayOutputStream = ByteArrayOutputStream() + val zstdInputStream = ZstdInputStream(byteArrayInputStream) + val bytes = ByteArray(1000000) + val bytesRead = zstdInputStream.read(bytes, 0, 1000000) + byteArrayOutputStream.write(bytes, 0, bytesRead) + zstdInputStream.close() + byteArrayInputStream.close() + byteArrayOutputStream.close() + return byteArrayOutputStream.toByteArray() + } + +} \ No newline at end of file diff --git a/core/src/main/kotlin/github/leavesczy/xlog/decode/core/DecryptUtils.kt b/core/src/main/kotlin/github/leavesczy/xlog/decode/core/DecryptUtils.kt new file mode 100644 index 0000000..dee48b1 --- /dev/null +++ b/core/src/main/kotlin/github/leavesczy/xlog/decode/core/DecryptUtils.kt @@ -0,0 +1,111 @@ +package github.leavesczy.xlog.decode.core + +import org.bouncycastle.jce.ECNamedCurveTable +import org.bouncycastle.jce.interfaces.ECPrivateKey +import org.bouncycastle.jce.interfaces.ECPublicKey +import org.bouncycastle.jce.provider.BouncyCastleProvider +import org.bouncycastle.jce.spec.ECPrivateKeySpec +import org.bouncycastle.jce.spec.ECPublicKeySpec +import java.math.BigInteger +import java.nio.ByteBuffer +import java.nio.ByteOrder +import java.security.* +import javax.crypto.KeyAgreement + +/** + * @Author: leavesCZY + * @Date: 2024/6/4 14:17 + * @Desc: + */ +object DecryptUtils { + + init { + Security.addProvider(BouncyCastleProvider()) + } + + data class SecretKey(val privateKey: String, val publicKey: String) + + fun generateKeyPair(): SecretKey { + val curveParameterSpec = ECNamedCurveTable.getParameterSpec("secp256k1") + val keyPairGenerator = KeyPairGenerator.getInstance("ECDH", "BC") + keyPairGenerator.initialize(curveParameterSpec) + val keyPair = keyPairGenerator.generateKeyPair() + val privateKey = (keyPair.private as ECPrivateKey).d.toString(16) + val eCPublicKey = keyPair.public as ECPublicKey + val publicKey = eCPublicKey.q.rawXCoord.toString() + eCPublicKey.q.rawYCoord.toString() + return SecretKey(privateKey = privateKey, publicKey = publicKey) + } + + fun getECDHKey(publicKey: ByteArray, privateKey: ByteArray): ByteArray { + val keyAgreement = KeyAgreement.getInstance("ECDH", "BC") + keyAgreement.init(loadPrivateKey(data = privateKey)) + keyAgreement.doPhase(loadPublicKey(data = publicKey), true) + return keyAgreement.generateSecret() + } + + private fun loadPublicKey(data: ByteArray): PublicKey { + val parameterSpec = ECNamedCurveTable.getParameterSpec("secp256k1") + val keySpec = ECPublicKeySpec(parameterSpec.curve.decodePoint(data), parameterSpec) + val keyAgreement = KeyFactory.getInstance("ECDH", "BC") + return keyAgreement.generatePublic(keySpec) + } + + private fun loadPrivateKey(data: ByteArray): PrivateKey { + val parameterSpec = ECNamedCurveTable.getParameterSpec("secp256k1") + val keySpec = ECPrivateKeySpec(BigInteger(1, data), parameterSpec) + val keyFactory = KeyFactory.getInstance("ECDH", "BC") + return keyFactory.generatePrivate(keySpec) + } + + fun teaDecrypt(encryptedData: ByteArray, key: ByteArray): ByteArray { + val num = encryptedData.size shr 3 shl 3 + val ret = ByteBuffer.allocate(encryptedData.size).order(ByteOrder.LITTLE_ENDIAN) + var i = 0 + while (i < num) { + val sv = ByteArray(8) + ByteBuffer.wrap(encryptedData, i, 8)[sv] + val x = teaDecipher(sv, key) + ret.put(x) + i += 8 + } + val remain = ByteArray(encryptedData.size - num) + ByteBuffer.wrap(encryptedData, num, encryptedData.size - num)[remain] + ret.put(remain) + return ret.array() + } + + private fun longToArray(x: Long, y: Long): ByteArray { + val buffer = ByteBuffer.allocate(8).order(ByteOrder.LITTLE_ENDIAN) + buffer.putInt(x.toInt()) + buffer.putInt(y.toInt()) + return buffer.array() + } + + private fun bytesToInt(b: ByteArray, offset: Int): Int { + return b[offset].toInt() and 0xFF or ( + (b[offset + 1].toInt() and 0xFF) shl 8) or ( + (b[offset + 2].toInt() and 0xFF) shl 16) or ( + (b[offset + 3].toInt() and 0xFF) shl 24) + } + + private fun teaDecipher(byteArray: ByteArray, k: ByteArray): ByteArray { + val op = 0xffffffffL + val delta = 0x9E3779B9L + var s = (delta shl 4) and op + var v0 = bytesToInt(byteArray, 0).toLong() and 0x0FFFFFFFFL + var v1 = bytesToInt(byteArray, 4).toLong() and 0x0FFFFFFFFL + val k1 = bytesToInt(k, 0).toLong() and 0x0FFFFFFFFL + val k2 = bytesToInt(k, 4).toLong() and 0x0FFFFFFFFL + val k3 = bytesToInt(k, 8).toLong() and 0x0FFFFFFFFL + val k4 = bytesToInt(k, 12).toLong() and 0x0FFFFFFFFL + var cnt = 16 + while (cnt > 0) { + v1 = (v1 - (((v0 shl 4) + k3) xor (v0 + s) xor ((v0 shr 5) + k4))) and op + v0 = (v0 - (((v1 shl 4) + k1) xor (v1 + s) xor ((v1 shr 5) + k2))) and op + s = (s - delta) and op + cnt-- + } + return longToArray(v0, v1) + } + +} \ No newline at end of file diff --git a/core/src/main/kotlin/github/leavesczy/xlog/decode/core/LogDecode.kt b/core/src/main/kotlin/github/leavesczy/xlog/decode/core/LogDecode.kt new file mode 100644 index 0000000..d1dd1d0 --- /dev/null +++ b/core/src/main/kotlin/github/leavesczy/xlog/decode/core/LogDecode.kt @@ -0,0 +1,254 @@ +package github.leavesczy.xlog.decode.core + +import java.io.DataInputStream +import java.io.File +import java.io.FileInputStream +import java.nio.ByteBuffer +import java.nio.ByteOrder + +/** + * @Author: leavesCZY + * @Date: 2024/6/6 15:46 + * @Desc: + */ +class LogDecode(private val logger: Logger) { + + //magic start(char) + //seq(uint16_t) + //begin hour(char) + //end hour(char) + //length(uint32_t) + //crypt key(uint32_t) + //log + //magic end(char) + private sealed class Magic(val byteSize: Int) { + data object MagicStart : Magic(byteSize = 1) + data object Seq : Magic(byteSize = 2) + data object BeginHour : Magic(byteSize = 1) + data object EndHour : Magic(byteSize = 1) + data object Length : Magic(byteSize = 4) + data class CryptKey(val length: Int) : Magic(byteSize = length) + data object MagicEnd : Magic(byteSize = 1) { + const val MARK: Byte = 0x00 + } + } + + private enum class MagicStartMark(val mark: Byte) { + NoCompressStart(mark = 0x03), + NoCompressStart1(mark = 0x06), + NoCompressNoCryptStart(mark = 0x08), + + CompressStart(mark = 0x04), + CompressStart1(mark = 0x05), + CompressStart2(mark = 0x07), + CompressNoCryptStart(mark = 0x09), + + SyncZstdStart(mark = 0x0A), + SyncNoCryptZstdStart(mark = 0x0B), + + AsyncZstdStart(mark = 0x0C), + AsyncNoCryptZstdStart(mark = 0x0D) + } + + private class LogSpace( + val magicStartMark: MagicStartMark, + val cryptKeyMagic: Magic.CryptKey, + val teaKey: ByteArray?, + val log: ByteArray + ) { + + override fun toString(): String { + return "LogSpace(magicStartMark=$magicStartMark, cryptKeyMagic=$cryptKeyMagic, teaKeySize=${teaKey?.size}, logSize=${log.size})" + } + + } + + private val marksSizeBeforeLengthMagic = Magic.MagicStart.byteSize + + Magic.Seq.byteSize + + Magic.BeginHour.byteSize + + Magic.EndHour.byteSize + + private val marksSizeBeforeCryptKeyMagic = marksSizeBeforeLengthMagic + + Magic.Length.byteSize + + fun decodeFile(privateKey: String, logFile: File, outFile: File) { + val logFileInputStream = FileInputStream(logFile) + val lodFileDataInputStream = DataInputStream(logFileInputStream) + val outFileBufferedWriter = outFile.outputStream().bufferedWriter() + try { + val lofBuffer = ByteArray(lodFileDataInputStream.available()) + lodFileDataInputStream.readFully(lofBuffer) + var lofBufferOffset = 0 + while (true) { + val logSpace = decodeLogSpace( + privateKey = privateKey, + buffer = lofBuffer, + offset = lofBufferOffset + ) + if (logSpace == null) { + logger.debug { + "finish!!!" + } + logger.debug { + "-----------------------------------------------------------------------" + } + } else { + val log = buildString { + append("logSpace: $logSpace") + append("\n") + val teaKey = logSpace.teaKey + if (teaKey == null) { + append("teaKey: null") + } else { + append("teaKey: ${StringUtils.byteArrayToHexString(bytes = teaKey)}") + } + } + logger.debug { + log + } + } + if (logSpace == null) { + break + } + val decryptedDecompressedLog = decodeLogSpace(logSpace = logSpace) + outFileBufferedWriter.append(String(bytes = decryptedDecompressedLog)) + val logSpaceSize = marksSizeBeforeCryptKeyMagic + + logSpace.cryptKeyMagic.byteSize + + logSpace.log.size + + Magic.MagicEnd.byteSize + lofBufferOffset += logSpaceSize + } + outFileBufferedWriter.flush() + } finally { + logFileInputStream.close() + lodFileDataInputStream.close() + outFileBufferedWriter.close() + } + } + + private fun decodeLogSpace(privateKey: String, buffer: ByteArray, offset: Int): LogSpace? { + val bufferSize = buffer.size + if (offset < 0 || offset >= bufferSize) { + return null + } + for (index in offset.. { + val teaKey = logSpace.teaKey!! + val decryptedLogBuffer = DecryptUtils.teaDecrypt( + encryptedData = logSpace.log, + key = teaKey + ) + if (magicStart == MagicStartMark.CompressStart2) { + DecompressUtils.zlibDecompress(data = decryptedLogBuffer) + } else { + DecompressUtils.zstdDecompress(data = decryptedLogBuffer) + } + } + + MagicStartMark.AsyncNoCryptZstdStart -> { + DecompressUtils.zstdDecompress(data = logSpace.log) + } + + MagicStartMark.CompressStart, MagicStartMark.CompressNoCryptStart -> { + DecompressUtils.zlibDecompress(data = logSpace.log) + } + + MagicStartMark.NoCompressStart, MagicStartMark.NoCompressStart1, + MagicStartMark.NoCompressNoCryptStart, MagicStartMark.CompressStart1, + MagicStartMark.SyncZstdStart, MagicStartMark.SyncNoCryptZstdStart -> { + logSpace.log + } + } + } + + private fun decodeCryptKeyMagic(magicStartMark: MagicStartMark): Magic.CryptKey { + return when (magicStartMark) { + MagicStartMark.NoCompressStart, + MagicStartMark.CompressStart, + MagicStartMark.CompressStart1 -> { + Magic.CryptKey(length = 4) + } + + MagicStartMark.NoCompressStart1, + MagicStartMark.CompressStart2, + MagicStartMark.NoCompressNoCryptStart, + MagicStartMark.CompressNoCryptStart, + MagicStartMark.SyncZstdStart, + MagicStartMark.SyncNoCryptZstdStart, + MagicStartMark.AsyncZstdStart, + MagicStartMark.AsyncNoCryptZstdStart -> { + Magic.CryptKey(length = 64) + } + } + } + +} \ No newline at end of file diff --git a/core/src/main/kotlin/github/leavesczy/xlog/decode/core/Logger.kt b/core/src/main/kotlin/github/leavesczy/xlog/decode/core/Logger.kt new file mode 100644 index 0000000..e758fd0 --- /dev/null +++ b/core/src/main/kotlin/github/leavesczy/xlog/decode/core/Logger.kt @@ -0,0 +1,14 @@ +package github.leavesczy.xlog.decode.core + +/** + * @Author: leavesCZY + * @Date: 2024/6/4 14:17 + * @Desc: + */ +interface Logger { + + fun debug(log: () -> Any) + + fun error(log: () -> Any) + +} \ No newline at end of file diff --git a/core/src/main/kotlin/github/leavesczy/xlog/decode/core/StringUtils.kt b/core/src/main/kotlin/github/leavesczy/xlog/decode/core/StringUtils.kt new file mode 100644 index 0000000..a240f01 --- /dev/null +++ b/core/src/main/kotlin/github/leavesczy/xlog/decode/core/StringUtils.kt @@ -0,0 +1,32 @@ +package github.leavesczy.xlog.decode.core + +/** + * @Author: leavesCZY + * @Date: 2024/6/4 14:17 + * @Desc: + */ +internal object StringUtils { + + fun hexStringToByteArray(hexString: String): ByteArray { + val len = hexString.length / 2 + val bytes = ByteArray(len) + for (i in 0 until len) { + val byte = hexString.substring(i * 2, (i + 1) * 2).toInt(16) + bytes[i] = byte.toByte() + } + return bytes + } + + fun byteArrayToHexString(bytes: ByteArray): String { + val sb = StringBuilder() + for (b in bytes) { + val str = Integer.toHexString(b.toInt() and 0xFF) + if (str.length < 2) { + sb.append(0) + } + sb.append(str) + } + return sb.toString() + } + +} \ No newline at end of file diff --git a/gradle.properties b/gradle.properties new file mode 100644 index 0000000..9de8045 --- /dev/null +++ b/gradle.properties @@ -0,0 +1,2 @@ +kotlin.code.style=official +org.gradle.jvmargs=-Xmx2048M -Dfile.encoding=UTF-8 -Dkotlin.daemon.jvm.options\="-Xmx2048M" \ No newline at end of file diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml new file mode 100644 index 0000000..f294362 --- /dev/null +++ b/gradle/libs.versions.toml @@ -0,0 +1,19 @@ +[versions] +jetbrains-kotlin-plugin = "2.0.0" +jetbrains-compose-plugin = "1.6.11" + +androidx-datastore = "1.1.1" +luben-zstd-jni = "1.5.6-3" +bouncycastle-bcprov-jdk18on = "1.78.1" +jetbrains-lifecycle-viewmodel-compose = "2.8.0" + +[plugins] +jetbrains-kotlin-jvm = { id = "org.jetbrains.kotlin.jvm", version.ref = "jetbrains-kotlin-plugin" } +jetbrains-compose-compiler = { id = "org.jetbrains.kotlin.plugin.compose", version.ref = "jetbrains-kotlin-plugin" } +jetbrains-compose = { id = "org.jetbrains.compose", version.ref = "jetbrains-compose-plugin" } + +[libraries] +androidx-datastore-preferences-core = { module = "androidx.datastore:datastore-preferences-core", version.ref = "androidx-datastore" } +jetbrains-lifecycle-viewmodel-compose = { module = "org.jetbrains.androidx.lifecycle:lifecycle-viewmodel-compose", version.ref = "jetbrains-lifecycle-viewmodel-compose" } +luben-zstd-jni = { module = "com.github.luben:zstd-jni", version.ref = "luben-zstd-jni" } +bouncycastle-bcprov-jdk18on = { module = "org.bouncycastle:bcprov-jdk18on", version.ref = "bouncycastle-bcprov-jdk18on" } \ No newline at end of file diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 0000000..7f93135 Binary files /dev/null and b/gradle/wrapper/gradle-wrapper.jar differ diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 0000000..a8382d7 --- /dev/null +++ b/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,5 @@ +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-8.7-bin.zip +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists \ No newline at end of file diff --git a/gradlew b/gradlew new file mode 100644 index 0000000..1aa94a4 --- /dev/null +++ b/gradlew @@ -0,0 +1,249 @@ +#!/bin/sh + +# +# Copyright © 2015-2021 the original authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +############################################################################## +# +# Gradle start up script for POSIX generated by Gradle. +# +# Important for running: +# +# (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is +# noncompliant, but you have some other compliant shell such as ksh or +# bash, then to run this script, type that shell name before the whole +# command line, like: +# +# ksh Gradle +# +# Busybox and similar reduced shells will NOT work, because this script +# requires all of these POSIX shell features: +# * functions; +# * expansions «$var», «${var}», «${var:-default}», «${var+SET}», +# «${var#prefix}», «${var%suffix}», and «$( cmd )»; +# * compound commands having a testable exit status, especially «case»; +# * various built-in commands including «command», «set», and «ulimit». +# +# Important for patching: +# +# (2) This script targets any POSIX shell, so it avoids extensions provided +# by Bash, Ksh, etc; in particular arrays are avoided. +# +# The "traditional" practice of packing multiple parameters into a +# space-separated string is a well documented source of bugs and security +# problems, so this is (mostly) avoided, by progressively accumulating +# options in "$@", and eventually passing that to Java. +# +# Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS, +# and GRADLE_OPTS) rely on word-splitting, this is performed explicitly; +# see the in-line comments for details. +# +# There are tweaks for specific operating systems such as AIX, CygWin, +# Darwin, MinGW, and NonStop. +# +# (3) This script is generated from the Groovy template +# https://github.com/gradle/gradle/blob/HEAD/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt +# within the Gradle project. +# +# You can find Gradle at https://github.com/gradle/gradle/. +# +############################################################################## + +# Attempt to set APP_HOME + +# Resolve links: $0 may be a link +app_path=$0 + +# Need this for daisy-chained symlinks. +while + APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path + [ -h "$app_path" ] +do + ls=$( ls -ld "$app_path" ) + link=${ls#*' -> '} + case $link in #( + /*) app_path=$link ;; #( + *) app_path=$APP_HOME$link ;; + esac +done + +# This is normally unused +# shellcheck disable=SC2034 +APP_BASE_NAME=${0##*/} +# Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036) +APP_HOME=$( cd "${APP_HOME:-./}" > /dev/null && pwd -P ) || exit + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD=maximum + +warn () { + echo "$*" +} >&2 + +die () { + echo + echo "$*" + echo + exit 1 +} >&2 + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +nonstop=false +case "$( uname )" in #( + CYGWIN* ) cygwin=true ;; #( + Darwin* ) darwin=true ;; #( + MSYS* | MINGW* ) msys=true ;; #( + NONSTOP* ) nonstop=true ;; +esac + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD=$JAVA_HOME/jre/sh/java + else + JAVACMD=$JAVA_HOME/bin/java + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD=java + if ! command -v java >/dev/null 2>&1 + then + die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +fi + +# Increase the maximum file descriptors if we can. +if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then + case $MAX_FD in #( + max*) + # In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC2039,SC3045 + MAX_FD=$( ulimit -H -n ) || + warn "Could not query maximum file descriptor limit" + esac + case $MAX_FD in #( + '' | soft) :;; #( + *) + # In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC2039,SC3045 + ulimit -n "$MAX_FD" || + warn "Could not set maximum file descriptor limit to $MAX_FD" + esac +fi + +# Collect all arguments for the java command, stacking in reverse order: +# * args from the command line +# * the main class name +# * -classpath +# * -D...appname settings +# * --module-path (only if needed) +# * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables. + +# For Cygwin or MSYS, switch paths to Windows format before running java +if "$cygwin" || "$msys" ; then + APP_HOME=$( cygpath --path --mixed "$APP_HOME" ) + CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" ) + + JAVACMD=$( cygpath --unix "$JAVACMD" ) + + # Now convert the arguments - kludge to limit ourselves to /bin/sh + for arg do + if + case $arg in #( + -*) false ;; # don't mess with options #( + /?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath + [ -e "$t" ] ;; #( + *) false ;; + esac + then + arg=$( cygpath --path --ignore --mixed "$arg" ) + fi + # Roll the args list around exactly as many times as the number of + # args, so each arg winds up back in the position where it started, but + # possibly modified. + # + # NB: a `for` loop captures its iteration list before it begins, so + # changing the positional parameters here affects neither the number of + # iterations, nor the values presented in `arg`. + shift # remove old arg + set -- "$@" "$arg" # push replacement arg + done +fi + + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' + +# Collect all arguments for the java command: +# * DEFAULT_JVM_OPTS, JAVA_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments, +# and any embedded shellness will be escaped. +# * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be +# treated as '${Hostname}' itself on the command line. + +set -- \ + "-Dorg.gradle.appname=$APP_BASE_NAME" \ + -classpath "$CLASSPATH" \ + org.gradle.wrapper.GradleWrapperMain \ + "$@" + +# Stop when "xargs" is not available. +if ! command -v xargs >/dev/null 2>&1 +then + die "xargs is not available" +fi + +# Use "xargs" to parse quoted args. +# +# With -n1 it outputs one arg per line, with the quotes and backslashes removed. +# +# In Bash we could simply go: +# +# readarray ARGS < <( xargs -n1 <<<"$var" ) && +# set -- "${ARGS[@]}" "$@" +# +# but POSIX shell has neither arrays nor command substitution, so instead we +# post-process each arg (as a line of input to sed) to backslash-escape any +# character that might be a shell metacharacter, then use eval to reverse +# that process (while maintaining the separation between arguments), and wrap +# the whole thing up as a single "set" statement. +# +# This will of course break if any of these variables contains a newline or +# an unmatched quote. +# + +eval "set -- $( + printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" | + xargs -n1 | + sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' | + tr '\n' ' ' + )" '"$@"' + +exec "$JAVACMD" "$@" diff --git a/gradlew.bat b/gradlew.bat new file mode 100644 index 0000000..2578005 --- /dev/null +++ b/gradlew.bat @@ -0,0 +1,92 @@ +@rem +@rem Copyright 2015 the original author or authors. +@rem +@rem Licensed under the Apache License, Version 2.0 (the "License"); +@rem you may not use this file except in compliance with the License. +@rem You may obtain a copy of the License at +@rem +@rem https://www.apache.org/licenses/LICENSE-2.0 +@rem +@rem Unless required by applicable law or agreed to in writing, software +@rem distributed under the License is distributed on an "AS IS" BASIS, +@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +@rem See the License for the specific language governing permissions and +@rem limitations under the License. +@rem + +@if "%DEBUG%"=="" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%"=="" set DIRNAME=. +@rem This is normally unused +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Resolve any "." and ".." in APP_HOME to make it shorter. +for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if %ERRORLEVEL% equ 0 goto execute + +echo. +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto execute + +echo. +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* + +:end +@rem End local scope for the variables with windows NT shell +if %ERRORLEVEL% equ 0 goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +set EXIT_CODE=%ERRORLEVEL% +if %EXIT_CODE% equ 0 set EXIT_CODE=1 +if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE% +exit /b %EXIT_CODE% + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega \ No newline at end of file diff --git a/proguard-rules.pro b/proguard-rules.pro new file mode 100644 index 0000000..6c8104e --- /dev/null +++ b/proguard-rules.pro @@ -0,0 +1,11 @@ +-ignorewarnings +-optimizationpasses 6 +-keepattributes SourceFile,LineNumberTable +-renamesourcefileattribute SourceFile + +-dontwarn kotlinx.datetime.** +-keep class org.bouncycastle.** { *; } +-keep class org.lwjgl.** { *; } +-keepclassmembers class * extends androidx.datastore.preferences.protobuf.GeneratedMessageLite { + ; +} \ No newline at end of file diff --git a/settings.gradle.kts b/settings.gradle.kts new file mode 100644 index 0000000..a266aa5 --- /dev/null +++ b/settings.gradle.kts @@ -0,0 +1,19 @@ +@file:Suppress("UnstableApiUsage") + +pluginManagement { + repositories { + gradlePluginPortal() + mavenCentral() + google() + } +} + +dependencyResolutionManagement { + repositories { + mavenCentral() + google() + } +} + +rootProject.name = "compose-multiplatform-xlog-decode" +include("core") \ No newline at end of file diff --git a/src/main/kotlin/github/leavesczy/xlog/decode/DataStoreManager.kt b/src/main/kotlin/github/leavesczy/xlog/decode/DataStoreManager.kt new file mode 100644 index 0000000..a99c991 --- /dev/null +++ b/src/main/kotlin/github/leavesczy/xlog/decode/DataStoreManager.kt @@ -0,0 +1,61 @@ +package github.leavesczy.xlog.decode + +import androidx.datastore.preferences.core.* +import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.map +import java.io.File + +/** + * @Author: leavesCZY + * @Date: 2024/6/6 15:47 + * @Desc: + */ +object DataStoreManager { + + private val dataStores = PreferenceDataStoreFactory.create { + File("${System.getProperty("user.dir")}/config/compose-multiplatform-xlog-decode.preferences_pb") + } + + private val PRIVATE_KEY = stringPreferencesKey("private_key") + + private val THEME = intPreferencesKey("theme") + + private val AUT_OPEN_FILE_WHEN_PARSING_IS_SUCCESSFUL = booleanPreferencesKey("autOpenFileWhenParsingIsSuccessful") + + fun privateKeyFlow(): Flow { + return dataStores.data.map { preferences -> + preferences[PRIVATE_KEY] ?: "" + } + } + + fun themeFlow(): Flow { + return dataStores.data.map { preferences -> + preferences[THEME] ?: -1 + } + } + + fun autOpenFileWhenParsingIsSuccessful(): Flow { + return dataStores.data.map { preferences -> + preferences[AUT_OPEN_FILE_WHEN_PARSING_IS_SUCCESSFUL] ?: true + } + } + + suspend fun updatePrivateKey(privateKey: String) { + dataStores.edit { settings -> + settings[PRIVATE_KEY] = privateKey + } + } + + suspend fun updateTheme(theme: Int) { + dataStores.edit { settings -> + settings[THEME] = theme + } + } + + suspend fun autOpenFileWhenParsingIsSuccessful(autoOpen: Boolean) { + dataStores.edit { settings -> + settings[AUT_OPEN_FILE_WHEN_PARSING_IS_SUCCESSFUL] = autoOpen + } + } + +} \ No newline at end of file diff --git a/src/main/kotlin/github/leavesczy/xlog/decode/LogDecodeViewModel.kt b/src/main/kotlin/github/leavesczy/xlog/decode/LogDecodeViewModel.kt new file mode 100644 index 0000000..de2b0c5 --- /dev/null +++ b/src/main/kotlin/github/leavesczy/xlog/decode/LogDecodeViewModel.kt @@ -0,0 +1,193 @@ +package github.leavesczy.xlog.decode + +import androidx.compose.foundation.ScrollState +import androidx.compose.runtime.getValue +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.setValue +import androidx.lifecycle.ViewModel +import androidx.lifecycle.viewModelScope +import github.leavesczy.xlog.decode.core.DecryptUtils +import github.leavesczy.xlog.decode.core.LogDecode +import github.leavesczy.xlog.decode.core.Logger +import github.leavesczy.xlog.decode.ui.* +import kotlinx.coroutines.* +import kotlinx.coroutines.flow.first +import java.awt.Desktop +import java.io.File +import java.io.PrintWriter +import java.io.StringWriter +import java.text.SimpleDateFormat +import java.util.* + +/** + * @Author: leavesCZY + * @Date: 2024/6/4 14:16 + * @Desc: + */ +class LogDecodeViewModel : ViewModel(viewModelScope = CoroutineScope(SupervisorJob() + Dispatchers.Default)) { + + var mainPageViewState by mutableStateOf( + value = MainPageViewState( + page = Page.Main, + privateKey = "", + logPath = "", + runtimeLog = "", + logScrollState = ScrollState(initial = 0), + onInputPrivateKey = ::onInputPrivateKey, + onInputLogFilePath = ::onInputLogFilePath, + decodeLog = ::decodeLog, + openFile = ::openFile, + switchPage = ::switchPage + ) + ) + private set + + var cryptKeyPageViewState by mutableStateOf( + value = CryptKeyPageViewState( + privateKey = "", + publicKey = "", + generateKeyPair = ::generateKeyPair + ) + ) + private set + + var settingsPageViewState by mutableStateOf( + value = SettingsPageViewState( + theme = Theme.Light, + autOpenFileWhenParsingIsSuccessful = false, + switchTheme = ::switchTheme, + updateAutOpenFileWhenParsingIsSuccessful = ::updateAutOpenFileWhenParsingIsSuccessful + ) + ) + private set + + private val logDecode = LogDecode(logger = object : Logger { + override fun debug(log: () -> Any) { + appendLog(log = log) + } + + override fun error(log: () -> Any) { + appendLog(log = log) + } + }) + + init { + viewModelScope.launch { + initView() + } + } + + private suspend fun initView() { + val privateKey = DataStoreManager.privateKeyFlow().first() + val themeType = DataStoreManager.themeFlow().first() + val theme = Theme.entries.find { it.type == themeType } ?: settingsPageViewState.theme + val autOpenFileWhenParsingIsSuccessful = DataStoreManager.autOpenFileWhenParsingIsSuccessful().first() + if (mainPageViewState.privateKey != privateKey) { + mainPageViewState = mainPageViewState.copy(privateKey = privateKey) + } + if (settingsPageViewState.theme != theme || settingsPageViewState.autOpenFileWhenParsingIsSuccessful != autOpenFileWhenParsingIsSuccessful) { + settingsPageViewState = settingsPageViewState.copy( + theme = theme, + autOpenFileWhenParsingIsSuccessful = autOpenFileWhenParsingIsSuccessful + ) + } + } + + private fun onInputPrivateKey(privateKey: String) { + mainPageViewState = mainPageViewState.copy(privateKey = privateKey) + viewModelScope.launch { + DataStoreManager.updatePrivateKey(privateKey = privateKey) + } + } + + private fun onInputLogFilePath(logPath: String) { + mainPageViewState = mainPageViewState.copy(logPath = logPath) + } + + private suspend fun decodeLog(): File? { + return withContext(context = Dispatchers.Default) { + val logPath = mainPageViewState.logPath + val logFile = File(logPath) + val outFile = buildOutFile(logFile = logFile) + try { + logDecode.decodeFile( + privateKey = mainPageViewState.privateKey, + logFile = logFile, + outFile = outFile + ) + appendLog { + "解密成功,文件路径:" + outFile.absolutePath + } + autoOpenFileIfNeed(file = outFile) + outFile + } catch (throwable: Throwable) { + outFile.delete() + appendLog { + val stringWriter = StringWriter() + throwable.printStackTrace(PrintWriter(stringWriter, true)) + stringWriter.buffer.toString() + } + null + } + } + } + + private fun buildOutFile(logFile: File): File { + val logFileName = logFile.nameWithoutExtension + val outFileName = logFileName + "_" + SimpleDateFormat("yyyy_MM_dd_HH_mm_ss").format(Date()) + ".txt" + return File(logFile.parentFile, outFileName) + } + + private fun appendLog(log: () -> Any) { + val mLog = log().toString() + if (mLog.isNotBlank()) { + mainPageViewState = mainPageViewState.copy(runtimeLog = mainPageViewState.runtimeLog + mLog + "\n\n") + } + } + + private suspend fun openFile(file: File) { + withContext(context = Dispatchers.IO) { + if (Desktop.isDesktopSupported()) { + val desktop = Desktop.getDesktop() + if (desktop.isSupported(Desktop.Action.OPEN)) { + desktop.open(file) + } + } + } + } + + private suspend fun autoOpenFileIfNeed(file: File) { + if (settingsPageViewState.autOpenFileWhenParsingIsSuccessful) { + openFile(file = file) + } + } + + private fun generateKeyPair() { + viewModelScope.launch { + val keyPair = DecryptUtils.generateKeyPair() + cryptKeyPageViewState = cryptKeyPageViewState.copy( + privateKey = keyPair.privateKey, + publicKey = keyPair.publicKey + ) + } + } + + private fun switchPage(page: Page) { + mainPageViewState = mainPageViewState.copy(page = page) + } + + private fun switchTheme(theme: Theme) { + settingsPageViewState = settingsPageViewState.copy(theme = theme) + viewModelScope.launch { + DataStoreManager.updateTheme(theme = theme.type) + } + } + + private fun updateAutOpenFileWhenParsingIsSuccessful(autoOpen: Boolean) { + settingsPageViewState = settingsPageViewState.copy(autOpenFileWhenParsingIsSuccessful = autoOpen) + viewModelScope.launch { + DataStoreManager.autOpenFileWhenParsingIsSuccessful(autoOpen = autoOpen) + } + } + +} \ No newline at end of file diff --git a/src/main/kotlin/github/leavesczy/xlog/decode/Main.kt b/src/main/kotlin/github/leavesczy/xlog/decode/Main.kt new file mode 100644 index 0000000..fdb5f4c --- /dev/null +++ b/src/main/kotlin/github/leavesczy/xlog/decode/Main.kt @@ -0,0 +1,130 @@ +package github.leavesczy.xlog.decode + +import androidx.compose.foundation.layout.* +import androidx.compose.material3.* +import androidx.compose.runtime.Composable +import androidx.compose.runtime.remember +import androidx.compose.ui.Alignment +import androidx.compose.ui.Modifier +import androidx.compose.ui.res.painterResource +import androidx.compose.ui.unit.DpSize +import androidx.compose.ui.unit.dp +import androidx.compose.ui.window.Window +import androidx.compose.ui.window.WindowPosition +import androidx.compose.ui.window.application +import androidx.compose.ui.window.rememberWindowState +import androidx.lifecycle.viewmodel.compose.viewModel +import github.leavesczy.xlog.decode.ui.CryptKeyPage +import github.leavesczy.xlog.decode.ui.MainPage +import github.leavesczy.xlog.decode.ui.Page +import github.leavesczy.xlog.decode.ui.SettingsPage +import github.leavesczy.xlog.decode.ui.theme.AppTheme +import java.awt.Toolkit + +/** + * @Author: leavesCZY + * @Date: 2024/6/5 16:54 + * @Desc: + */ +fun main() = application { + Window( + title = "compose-multiplatform-xlog-decode", + resizable = true, + icon = painterResource(resourcePath = "application_icon.png"), + state = rememberWindowState( + size = preferredWindowSize(), + position = WindowPosition.Aligned(alignment = Alignment.Center) + ), + onCloseRequest = ::exitApplication + ) { + Main() + } +} + +@Composable +private fun Main() { + val logDecodeViewModel = viewModel { LogDecodeViewModel() } + val pageViewState = logDecodeViewModel.mainPageViewState + AppTheme(theme = logDecodeViewModel.settingsPageViewState.theme) { + val snackBarHostState = remember { + SnackbarHostState() + } + Scaffold( + modifier = Modifier + .fillMaxSize(), + snackbarHost = { + SnackbarHost( + modifier = Modifier, + hostState = snackBarHostState + ) + } + ) { padding -> + Row( + modifier = Modifier + .fillMaxSize() + .padding(paddingValues = padding), + verticalAlignment = Alignment.CenterVertically + ) { + NavigationRail( + modifier = Modifier + .fillMaxHeight() + ) { + Column( + modifier = Modifier + .fillMaxHeight(), + horizontalAlignment = Alignment.CenterHorizontally, + verticalArrangement = Arrangement.spacedBy( + space = 24.dp, + alignment = Alignment.CenterVertically + ) + ) { + for (page in Page.entries) { + NavigationRailItem( + modifier = Modifier, + selected = pageViewState.page == page, + alwaysShowLabel = true, + label = { + Text(text = page.title) + }, + icon = { + Icon( + imageVector = page.icon, + contentDescription = page.title + ) + }, + onClick = { + pageViewState.switchPage(page) + } + ) + } + } + } + when (pageViewState.page) { + Page.Main -> { + MainPage( + pageViewState = pageViewState, + snackBarHostState = snackBarHostState + ) + } + + Page.CryptKey -> { + CryptKeyPage(pageViewState = logDecodeViewModel.cryptKeyPageViewState) + } + + Page.Settings -> { + SettingsPage(pageViewState = logDecodeViewModel.settingsPageViewState) + } + } + } + } + } +} + +private fun preferredWindowSize(): DpSize { + val screenSize = Toolkit.getDefaultToolkit().screenSize + val screenWidth = screenSize.width + val screenHeight = screenSize.height + val preferredWidth = screenWidth * 0.50f + val preferredHeight = screenHeight * 0.60f + return DpSize(preferredWidth.dp, preferredHeight.dp) +} \ No newline at end of file diff --git a/src/main/kotlin/github/leavesczy/xlog/decode/ui/CryptKeyPage.kt b/src/main/kotlin/github/leavesczy/xlog/decode/ui/CryptKeyPage.kt new file mode 100644 index 0000000..e1c6e0c --- /dev/null +++ b/src/main/kotlin/github/leavesczy/xlog/decode/ui/CryptKeyPage.kt @@ -0,0 +1,75 @@ +package github.leavesczy.xlog.decode.ui + +import androidx.compose.foundation.layout.* +import androidx.compose.foundation.shape.RoundedCornerShape +import androidx.compose.material3.Button +import androidx.compose.material3.OutlinedTextField +import androidx.compose.material3.Text +import androidx.compose.runtime.Composable +import androidx.compose.ui.Alignment +import androidx.compose.ui.Modifier +import androidx.compose.ui.unit.dp + +/** + * @Author: leavesCZY + * @Date: 2024/6/4 14:31 + * @Desc: + */ +@Composable +fun CryptKeyPage(pageViewState: CryptKeyPageViewState) { + Column( + modifier = Modifier + .fillMaxSize() + .padding(horizontal = 18.dp, vertical = 18.dp), + horizontalAlignment = Alignment.CenterHorizontally, + verticalArrangement = Arrangement.spacedBy(space = 10.dp) + ) { + ReadOnlyTextField( + modifier = Modifier + .fillMaxWidth(), + value = pageViewState.privateKey, + label = "私钥" + ) + ReadOnlyTextField( + modifier = Modifier + .fillMaxWidth(), + value = pageViewState.publicKey, + label = "公钥" + ) + Button( + modifier = Modifier + .fillMaxWidth(fraction = 0.3f), + onClick = pageViewState.generateKeyPair + ) { + Text( + modifier = Modifier, + text = "生成密钥" + ) + } + } +} + +@Composable +private fun ReadOnlyTextField( + modifier: Modifier = Modifier, + value: String, + label: String? = null +) { + OutlinedTextField( + modifier = modifier, + value = value, + readOnly = true, + shape = RoundedCornerShape(size = 16.dp), + label = if (label.isNullOrBlank()) { + null + } else { + { + Text( + modifier = Modifier, + text = label + ) + } + }, + onValueChange = {} + ) +} \ No newline at end of file diff --git a/src/main/kotlin/github/leavesczy/xlog/decode/ui/MainPage.kt b/src/main/kotlin/github/leavesczy/xlog/decode/ui/MainPage.kt new file mode 100644 index 0000000..39a3f29 --- /dev/null +++ b/src/main/kotlin/github/leavesczy/xlog/decode/ui/MainPage.kt @@ -0,0 +1,278 @@ +package github.leavesczy.xlog.decode.ui + +import androidx.compose.foundation.* +import androidx.compose.foundation.layout.* +import androidx.compose.foundation.shape.RoundedCornerShape +import androidx.compose.foundation.text.selection.SelectionContainer +import androidx.compose.material3.* +import androidx.compose.runtime.* +import androidx.compose.ui.* +import androidx.compose.ui.awt.ComposeWindow +import androidx.compose.ui.draw.clip +import androidx.compose.ui.draw.drawWithCache +import androidx.compose.ui.geometry.CornerRadius +import androidx.compose.ui.graphics.Color +import androidx.compose.ui.graphics.PathEffect +import androidx.compose.ui.graphics.drawscope.Stroke +import androidx.compose.ui.platform.LocalDensity +import androidx.compose.ui.unit.Dp +import androidx.compose.ui.unit.dp +import androidx.compose.ui.unit.sp +import kotlinx.coroutines.launch +import java.awt.FileDialog +import java.io.File +import java.net.URI +import kotlin.io.path.pathString +import kotlin.io.path.toPath + +/** + * @Author: leavesCZY + * @Date: 2024/6/4 14:15 + * @Desc: + */ +private const val xLogFileExtension = "xlog" + +@Composable +fun MainPage( + pageViewState: MainPageViewState, + snackBarHostState: SnackbarHostState +) { + val coroutineScope = rememberCoroutineScope() + Column( + modifier = Modifier + .fillMaxSize() + .padding(horizontal = 18.dp, vertical = 18.dp), + horizontalAlignment = Alignment.CenterHorizontally, + verticalArrangement = Arrangement.spacedBy(space = 10.dp) + ) { + PrivateKey( + privateKey = pageViewState.privateKey, + onInputPrivateKey = pageViewState.onInputPrivateKey + ) + LogFilePath( + logPath = pageViewState.logPath, + onInputLogFilePath = { + if (it.endsWith(suffix = xLogFileExtension)) { + pageViewState.onInputLogFilePath(it) + } else { + coroutineScope.launch { + snackBarHostState.showSnackbar(message = "请选择 $xLogFileExtension 文件") + } + } + } + ) + Button( + modifier = Modifier + .fillMaxWidth(fraction = 0.3f), + onClick = { + coroutineScope.launch { + val logPath = pageViewState.logPath + if (logPath.isBlank()) { + snackBarHostState.showSnackbar( + message = "请先选择日志文件", + duration = SnackbarDuration.Short + ) + } else { + val outFile = pageViewState.decodeLog() + if (outFile != null) { + val result = snackBarHostState.showSnackbar( + message = "解密成功,文件路径:" + outFile.absolutePath, + actionLabel = "打开文件", + withDismissAction = true, + duration = SnackbarDuration.Short + ) + when (result) { + SnackbarResult.ActionPerformed -> { + pageViewState.openFile(outFile) + } + + SnackbarResult.Dismissed -> { + + } + } + } + } + } + } + ) { + Text( + modifier = Modifier, + text = "解密日志" + ) + } + RuntimeLog( + log = pageViewState.runtimeLog, + scrollState = pageViewState.logScrollState + ) + } +} + +@Composable +private fun PrivateKey( + privateKey: String, + onInputPrivateKey: (String) -> Unit +) { + OutlinedTextField( + modifier = Modifier + .fillMaxWidth(), + value = privateKey, + shape = RoundedCornerShape(size = 16.dp), + label = { + Text( + modifier = Modifier, + text = "如果日志有进行加密则输入私钥,否则无需输入" + ) + }, + onValueChange = onInputPrivateKey + ) +} + +@Composable +private fun LogFilePath( + logPath: String, + onInputLogFilePath: (String) -> Unit +) { + Box( + modifier = Modifier + .fillMaxWidth() + ) { + var isDragging by remember { + mutableStateOf(value = false) + } + OutlinedTextField( + modifier = Modifier + .fillMaxWidth() + .dashedBorder( + color = if (isDragging) { + MaterialTheme.colorScheme.primary + } else { + Color.Transparent + }, + strokeWidth = 2.dp, + radius = 16.dp + ) + .onExternalDrag( + onDragStart = { + isDragging = true + }, + onDragExit = { + isDragging = false + }, + onDrop = { value -> + isDragging = false + val dragData = value.dragData + if (dragData is DragData.FilesList) { + val file = dragData.readFiles().firstNotNullOfOrNull { + val path = URI(it).toPath().pathString + val file = File(path) + if (file.exists() && file.isFile) { + file + } else { + null + } + } + if (file != null) { + onInputLogFilePath(file.absolutePath) + } + } + } + ), + value = logPath, + readOnly = true, + shape = RoundedCornerShape(size = 16.dp), + label = { + Text( + modifier = Modifier, + text = "点击选择日志文件,或者拖动日志文件到此处" + ) + }, + onValueChange = {} + ) + Box( + modifier = Modifier + .matchParentSize() + .clip(shape = RoundedCornerShape(size = 16.dp)) + .clickable { + val fileDialog = FileDialog(ComposeWindow(), "请选择 $xLogFileExtension 文件") + fileDialog.apply { + mode = FileDialog.LOAD + isMultipleMode = false + setFilenameFilter { _, name -> + name.endsWith(suffix = xLogFileExtension) + } + isVisible = true + val fileDirectory = fileDialog.directory + val fileName = fileDialog.file + if (!fileDirectory.isNullOrBlank() && !fileName.isNullOrBlank()) { + val filePath = fileDirectory + fileName + onInputLogFilePath(filePath) + } + } + } + ) + } +} + +@Composable +private fun RuntimeLog( + log: String, + scrollState: ScrollState +) { + LaunchedEffect(key1 = log.length) { + scrollState.animateScrollTo(value = scrollState.maxValue) + } + Box( + modifier = Modifier + .fillMaxWidth() + .padding(vertical = 10.dp) + ) { + SelectionContainer( + modifier = Modifier + .fillMaxWidth() + ) { + Text( + modifier = Modifier + .fillMaxWidth() + .verticalScroll(state = scrollState), + text = log, + fontSize = 16.sp + ) + } + VerticalScrollbar( + modifier = Modifier + .align(Alignment.CenterEnd) + .fillMaxHeight(), + adapter = rememberScrollbarAdapter(scrollState = scrollState) + ) + } +} + +private fun Modifier.dashedBorder( + strokeWidth: Dp, + color: Color, + radius: Dp +) = composed( + factory = { + val density = LocalDensity.current + val strokeWidthPx = with(density) { + strokeWidth.toPx() + } + val cornerRadius = with(density) { + radius.toPx() + } + then( + other = Modifier.drawWithCache { + onDrawBehind { + drawRoundRect( + color = color, + style = Stroke( + width = strokeWidthPx, + pathEffect = PathEffect.dashPathEffect(intervals = floatArrayOf(12f, 12f), phase = 6f) + ), + cornerRadius = CornerRadius(x = cornerRadius, y = cornerRadius) + ) + } + } + ) + } +) \ No newline at end of file diff --git a/src/main/kotlin/github/leavesczy/xlog/decode/ui/Models.kt b/src/main/kotlin/github/leavesczy/xlog/decode/ui/Models.kt new file mode 100644 index 0000000..8990669 --- /dev/null +++ b/src/main/kotlin/github/leavesczy/xlog/decode/ui/Models.kt @@ -0,0 +1,56 @@ +package github.leavesczy.xlog.decode.ui + +import androidx.compose.foundation.ScrollState +import androidx.compose.material.icons.Icons +import androidx.compose.material.icons.outlined.Key +import androidx.compose.material.icons.outlined.Loop +import androidx.compose.material.icons.outlined.Settings +import androidx.compose.runtime.Stable +import androidx.compose.ui.graphics.vector.ImageVector +import java.io.File + +/** + * @Author: leavesCZY + * @Date: 2024/6/4 14:15 + * @Desc: + */ +enum class Page(val title: String, val icon: ImageVector) { + Main(title = "Log", icon = Icons.Outlined.Loop), + CryptKey(title = "密钥", icon = Icons.Outlined.Key), + Settings(title = "设置", icon = Icons.Outlined.Settings) +} + +enum class Theme(val type: Int) { + System(type = 0), + Light(type = 1), + Dark(type = 2) +} + +@Stable +data class MainPageViewState( + val page: Page, + val privateKey: String, + val logPath: String, + val runtimeLog: String, + val logScrollState: ScrollState, + val onInputPrivateKey: (String) -> Unit, + val onInputLogFilePath: (String) -> Unit, + val decodeLog: suspend () -> File?, + val openFile: suspend (File) -> Unit, + val switchPage: (Page) -> Unit +) + +@Stable +data class CryptKeyPageViewState( + val privateKey: String, + val publicKey: String, + val generateKeyPair: () -> Unit +) + +@Stable +data class SettingsPageViewState( + val theme: Theme, + val autOpenFileWhenParsingIsSuccessful: Boolean, + val switchTheme: (Theme) -> Unit, + val updateAutOpenFileWhenParsingIsSuccessful: (Boolean) -> Unit +) \ No newline at end of file diff --git a/src/main/kotlin/github/leavesczy/xlog/decode/ui/SettingsPage.kt b/src/main/kotlin/github/leavesczy/xlog/decode/ui/SettingsPage.kt new file mode 100644 index 0000000..3d7f3e6 --- /dev/null +++ b/src/main/kotlin/github/leavesczy/xlog/decode/ui/SettingsPage.kt @@ -0,0 +1,95 @@ +package github.leavesczy.xlog.decode.ui + +import androidx.compose.foundation.layout.* +import androidx.compose.foundation.shape.RoundedCornerShape +import androidx.compose.material.icons.Icons +import androidx.compose.material.icons.filled.Check +import androidx.compose.material3.* +import androidx.compose.runtime.Composable +import androidx.compose.ui.Alignment +import androidx.compose.ui.Modifier +import androidx.compose.ui.unit.dp + +/** + * @Author: leavesCZY + * @Date: 2024/6/4 14:16 + * @Desc: + */ +@Composable +fun SettingsPage(pageViewState: SettingsPageViewState) { + Column( + modifier = Modifier + .fillMaxSize(), + horizontalAlignment = Alignment.CenterHorizontally, + verticalArrangement = Arrangement.spacedBy( + space = 20.dp, + alignment = Alignment.CenterVertically + ) + ) { + Row( + modifier = Modifier + .fillMaxWidth(), + verticalAlignment = Alignment.CenterVertically, + horizontalArrangement = Arrangement.spacedBy( + space = 20.dp, + alignment = Alignment.CenterHorizontally + ) + ) { + Text( + modifier = Modifier, + text = "主题" + ) + SingleChoiceSegmentedButtonRow( + modifier = Modifier, + space = (-20).dp + ) { + Theme.entries.forEach { + SegmentedButton( + modifier = Modifier, + selected = it == pageViewState.theme, + shape = RoundedCornerShape(size = 20.dp), + label = { + Text( + modifier = Modifier, + text = it.name + ) + }, + onClick = { + pageViewState.switchTheme(it) + } + ) + } + } + } + Row( + modifier = Modifier + .fillMaxWidth(), + verticalAlignment = Alignment.CenterVertically, + horizontalArrangement = Arrangement.spacedBy( + space = 20.dp, + alignment = Alignment.CenterHorizontally + ) + ) { + Text( + modifier = Modifier, + text = "解析成功后自动打开文件" + ) + val checked = pageViewState.autOpenFileWhenParsingIsSuccessful + Switch( + checked = checked, + onCheckedChange = pageViewState.updateAutOpenFileWhenParsingIsSuccessful, + thumbContent = if (checked) { + { + Icon( + imageVector = Icons.Filled.Check, + contentDescription = null, + modifier = Modifier.size(SwitchDefaults.IconSize), + ) + } + } else { + null + } + ) + } + } +} \ No newline at end of file diff --git a/src/main/kotlin/github/leavesczy/xlog/decode/ui/theme/Color.kt b/src/main/kotlin/github/leavesczy/xlog/decode/ui/theme/Color.kt new file mode 100644 index 0000000..0005fc0 --- /dev/null +++ b/src/main/kotlin/github/leavesczy/xlog/decode/ui/theme/Color.kt @@ -0,0 +1,75 @@ +package github.leavesczy.xlog.decode.ui.theme + +import androidx.compose.ui.graphics.Color + +val primaryLight = Color(0xFF415F91) +val onPrimaryLight = Color(0xFFFFFFFF) +val primaryContainerLight = Color(0xFFD6E3FF) +val onPrimaryContainerLight = Color(0xFF001B3E) +val secondaryLight = Color(0xFF565F71) +val onSecondaryLight = Color(0xFFFFFFFF) +val secondaryContainerLight = Color(0xFFDAE2F9) +val onSecondaryContainerLight = Color(0xFF131C2B) +val tertiaryLight = Color(0xFF705575) +val onTertiaryLight = Color(0xFFFFFFFF) +val tertiaryContainerLight = Color(0xFFFAD8FD) +val onTertiaryContainerLight = Color(0xFF28132E) +val errorLight = Color(0xFFBA1A1A) +val onErrorLight = Color(0xFFFFFFFF) +val errorContainerLight = Color(0xFFFFDAD6) +val onErrorContainerLight = Color(0xFF410002) +val backgroundLight = Color(0xFFF9F9FF) +val onBackgroundLight = Color(0xFF191C20) +val surfaceLight = Color(0xFFF9F9FF) +val onSurfaceLight = Color(0xFF191C20) +val surfaceVariantLight = Color(0xFFE0E2EC) +val onSurfaceVariantLight = Color(0xFF44474E) +val outlineLight = Color(0xFF74777F) +val outlineVariantLight = Color(0xFFC4C6D0) +val scrimLight = Color(0xFF000000) +val inverseSurfaceLight = Color(0xFF2E3036) +val inverseOnSurfaceLight = Color(0xFFF0F0F7) +val inversePrimaryLight = Color(0xFFAAC7FF) +val surfaceDimLight = Color(0xFFD9D9E0) +val surfaceBrightLight = Color(0xFFF9F9FF) +val surfaceContainerLowestLight = Color(0xFFFFFFFF) +val surfaceContainerLowLight = Color(0xFFF3F3FA) +val surfaceContainerLight = Color(0xFFEDEDF4) +val surfaceContainerHighLight = Color(0xFFE7E8EE) +val surfaceContainerHighestLight = Color(0xFFE2E2E9) + +val primaryDark = Color(0xFFAAC7FF) +val onPrimaryDark = Color(0xFF0A305F) +val primaryContainerDark = Color(0xFF284777) +val onPrimaryContainerDark = Color(0xFFD6E3FF) +val secondaryDark = Color(0xFFBEC6DC) +val onSecondaryDark = Color(0xFF283141) +val secondaryContainerDark = Color(0xFF3E4759) +val onSecondaryContainerDark = Color(0xFFDAE2F9) +val tertiaryDark = Color(0xFFDDBCE0) +val onTertiaryDark = Color(0xFF3F2844) +val tertiaryContainerDark = Color(0xFF573E5C) +val onTertiaryContainerDark = Color(0xFFFAD8FD) +val errorDark = Color(0xFFFFB4AB) +val onErrorDark = Color(0xFF690005) +val errorContainerDark = Color(0xFF93000A) +val onErrorContainerDark = Color(0xFFFFDAD6) +val backgroundDark = Color(0xFF111318) +val onBackgroundDark = Color(0xFFE2E2E9) +val surfaceDark = Color(0xFF111318) +val onSurfaceDark = Color(0xFFE2E2E9) +val surfaceVariantDark = Color(0xFF44474E) +val onSurfaceVariantDark = Color(0xFFC4C6D0) +val outlineDark = Color(0xFF8E9099) +val outlineVariantDark = Color(0xFF44474E) +val scrimDark = Color(0xFF000000) +val inverseSurfaceDark = Color(0xFFE2E2E9) +val inverseOnSurfaceDark = Color(0xFF2E3036) +val inversePrimaryDark = Color(0xFF415F91) +val surfaceDimDark = Color(0xFF111318) +val surfaceBrightDark = Color(0xFF37393E) +val surfaceContainerLowestDark = Color(0xFF0C0E13) +val surfaceContainerLowDark = Color(0xFF191C20) +val surfaceContainerDark = Color(0xFF1D2024) +val surfaceContainerHighDark = Color(0xFF282A2F) +val surfaceContainerHighestDark = Color(0xFF33353A) \ No newline at end of file diff --git a/src/main/kotlin/github/leavesczy/xlog/decode/ui/theme/Theme.kt b/src/main/kotlin/github/leavesczy/xlog/decode/ui/theme/Theme.kt new file mode 100644 index 0000000..398171f --- /dev/null +++ b/src/main/kotlin/github/leavesczy/xlog/decode/ui/theme/Theme.kt @@ -0,0 +1,129 @@ +package github.leavesczy.xlog.decode.ui.theme + +import androidx.compose.foundation.isSystemInDarkTheme +import androidx.compose.material3.MaterialTheme +import androidx.compose.material3.Typography +import androidx.compose.material3.darkColorScheme +import androidx.compose.material3.lightColorScheme +import androidx.compose.runtime.Composable +import androidx.compose.ui.text.TextStyle +import androidx.compose.ui.text.font.FontFamily +import androidx.compose.ui.text.font.FontWeight +import androidx.compose.ui.unit.sp +import github.leavesczy.xlog.decode.ui.Theme + +private val Typography = Typography( + bodyLarge = TextStyle( + fontFamily = FontFamily.SansSerif, + fontWeight = FontWeight.Normal, + fontSize = 16.sp, + lineHeight = 24.sp, + letterSpacing = 0.5.sp + ) +) + +private val lightScheme = lightColorScheme( + primary = primaryLight, + onPrimary = onPrimaryLight, + primaryContainer = primaryContainerLight, + onPrimaryContainer = onPrimaryContainerLight, + secondary = secondaryLight, + onSecondary = onSecondaryLight, + secondaryContainer = secondaryContainerLight, + onSecondaryContainer = onSecondaryContainerLight, + tertiary = tertiaryLight, + onTertiary = onTertiaryLight, + tertiaryContainer = tertiaryContainerLight, + onTertiaryContainer = onTertiaryContainerLight, + error = errorLight, + onError = onErrorLight, + errorContainer = errorContainerLight, + onErrorContainer = onErrorContainerLight, + background = backgroundLight, + onBackground = onBackgroundLight, + surface = surfaceLight, + onSurface = onSurfaceLight, + surfaceVariant = surfaceVariantLight, + onSurfaceVariant = onSurfaceVariantLight, + outline = outlineLight, + outlineVariant = outlineVariantLight, + scrim = scrimLight, + inverseSurface = inverseSurfaceLight, + inverseOnSurface = inverseOnSurfaceLight, + inversePrimary = inversePrimaryLight, + surfaceDim = surfaceDimLight, + surfaceBright = surfaceBrightLight, + surfaceContainerLowest = surfaceContainerLowestLight, + surfaceContainerLow = surfaceContainerLowLight, + surfaceContainer = surfaceContainerLight, + surfaceContainerHigh = surfaceContainerHighLight, + surfaceContainerHighest = surfaceContainerHighestLight, +) + +private val darkScheme = darkColorScheme( + primary = primaryDark, + onPrimary = onPrimaryDark, + primaryContainer = primaryContainerDark, + onPrimaryContainer = onPrimaryContainerDark, + secondary = secondaryDark, + onSecondary = onSecondaryDark, + secondaryContainer = secondaryContainerDark, + onSecondaryContainer = onSecondaryContainerDark, + tertiary = tertiaryDark, + onTertiary = onTertiaryDark, + tertiaryContainer = tertiaryContainerDark, + onTertiaryContainer = onTertiaryContainerDark, + error = errorDark, + onError = onErrorDark, + errorContainer = errorContainerDark, + onErrorContainer = onErrorContainerDark, + background = backgroundDark, + onBackground = onBackgroundDark, + surface = surfaceDark, + onSurface = onSurfaceDark, + surfaceVariant = surfaceVariantDark, + onSurfaceVariant = onSurfaceVariantDark, + outline = outlineDark, + outlineVariant = outlineVariantDark, + scrim = scrimDark, + inverseSurface = inverseSurfaceDark, + inverseOnSurface = inverseOnSurfaceDark, + inversePrimary = inversePrimaryDark, + surfaceDim = surfaceDimDark, + surfaceBright = surfaceBrightDark, + surfaceContainerLowest = surfaceContainerLowestDark, + surfaceContainerLow = surfaceContainerLowDark, + surfaceContainer = surfaceContainerDark, + surfaceContainerHigh = surfaceContainerHighDark, + surfaceContainerHighest = surfaceContainerHighestDark, +) + +@Composable +fun AppTheme( + theme: Theme, + content: @Composable () -> Unit +) { + val darkTheme = when (theme) { + Theme.System -> { + isSystemInDarkTheme() + } + + Theme.Light -> { + false + } + + Theme.Dark -> { + true + } + } + val colors = if (darkTheme) { + darkScheme + } else { + lightScheme + } + MaterialTheme( + colorScheme = colors, + typography = Typography, + content = content + ) +} \ No newline at end of file diff --git a/src/main/resources/application_icon.png b/src/main/resources/application_icon.png new file mode 100644 index 0000000..886ed64 Binary files /dev/null and b/src/main/resources/application_icon.png differ diff --git a/src/main/resources/linux_launch_icon.png b/src/main/resources/linux_launch_icon.png new file mode 100644 index 0000000..886ed64 Binary files /dev/null and b/src/main/resources/linux_launch_icon.png differ diff --git a/src/main/resources/macos_launch_icon.icns b/src/main/resources/macos_launch_icon.icns new file mode 100644 index 0000000..c59e867 Binary files /dev/null and b/src/main/resources/macos_launch_icon.icns differ diff --git a/src/main/resources/windows_launch_icon.ico b/src/main/resources/windows_launch_icon.ico new file mode 100644 index 0000000..85ca3f6 Binary files /dev/null and b/src/main/resources/windows_launch_icon.ico differ diff --git a/workflows-trigger.properties b/workflows-trigger.properties new file mode 100644 index 0000000..a50c2d1 --- /dev/null +++ b/workflows-trigger.properties @@ -0,0 +1 @@ +trigger=1 \ No newline at end of file