All Downloads are FREE. Search and download functionalities are using the official Maven repository.

main.com.xml.guard.tasks.DictionaryTask.kt Maven / Gradle / Ivy

package com.xml.guard.tasks

import com.xml.guard.entensions.GuardExtension
import com.xml.guard.utils.inPackageNameBlackList
import org.gradle.api.DefaultTask
import org.gradle.api.tasks.TaskAction
import java.io.File
import javax.inject.Inject
import kotlin.math.roundToInt
import kotlin.random.Random

open class DictionaryTask @Inject constructor(
    private val guardExtension: GuardExtension,
    private val variantName: String,
) : DefaultTask() {

    init {
        group = "guard"
    }

    private val dictionaryFile1 by lazy { project.file("proguard_dictionary1.txt") }
    private val dictionaryFile2 by lazy { project.file("proguard_dictionary2.txt") }
    private val dictionaryFile3 by lazy { project.file("proguard_dictionary3.txt") }

    private val dictionaryChars by lazy { guardExtension.dictionaryChars }

    @TaskAction
    fun execute() {
        if (dictionaryChars.isEmpty()) {
            throw IllegalArgumentException("dictionaryChars cannot be empty")
        }

        println()
        println("Obfuscated dictionary generation --- start")

        startTask(dictionaryFile1, dictionaryLowercase = false)
        startTask(dictionaryFile2, dictionaryLowercase = false)
        startTask(dictionaryFile3, dictionaryLowercase = true)

        println()
        println("Configure the following obfuscation rules:")
        println("# ============================= Proguard Dictionary =============================")
        println("-obfuscationdictionary ${dictionaryFile1.name}")
        println("-classobfuscationdictionary ${dictionaryFile2.name}")
        println("-packageobfuscationdictionary ${dictionaryFile3.name}")
        println("# ============================= Proguard Dictionary =============================")

        println()
        println("Obfuscated dictionary generation --- end")
    }

    private fun startTask(dictionaryFile: File, dictionaryLowercase: Boolean) {
        val dictionaries = mutableListOf()
        val dictionaryLines = Random.nextInt(100000, 200000)

        for (i in 0 until dictionaryLines) {
            var dictionary = ""
            while (true) {
                val len = Random.nextInt(3, 10)
                for (j in 0 until len) {
                    val firstChar = j == 0
                    dictionary += getRandomChar(dictionaryChars, firstChar)
                }
                if (dictionaryLowercase) {
                    dictionary = dictionary.lowercase()
                }
                if (!dictionary.inPackageNameBlackList()) {
                    dictionaries.add(dictionary)
                    break
                }
            }
        }
        val text = dictionaries.distinct().joinToString("\n")
        dictionaryFile.writeText(text)
    }

    private fun getRandomChar(chars: List, firstChar: Boolean): String {
        val index = (Random.nextDouble() * chars.size).toInt()
        val char = chars.getOrNull(index)?.trim() ?: ""
        if (firstChar && (char >= "0" && char <= "9")) {
            return getRandomChar(chars, true)
        }
        return char
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy