2020-12-23 17:52:05 +03:30
|
|
|
package ir.armor.tachidesk
|
|
|
|
|
|
2020-12-24 01:09:54 +03:30
|
|
|
import com.googlecode.dex2jar.tools.Dex2jarCmd
|
2020-12-24 01:22:01 +03:30
|
|
|
import eu.kanade.tachiyomi.extension.api.ExtensionGithubApi
|
2020-12-25 02:39:30 +03:30
|
|
|
import eu.kanade.tachiyomi.extension.model.Extension
|
2020-12-24 01:22:01 +03:30
|
|
|
import eu.kanade.tachiyomi.network.NetworkHelper
|
|
|
|
|
import eu.kanade.tachiyomi.source.model.MangasPage
|
|
|
|
|
import eu.kanade.tachiyomi.source.online.HttpSource
|
2020-12-24 02:08:03 +03:30
|
|
|
import io.javalin.Javalin
|
2020-12-24 19:37:27 +03:30
|
|
|
import ir.armor.tachidesk.database.makeDataBaseTables
|
2020-12-25 02:39:30 +03:30
|
|
|
import ir.armor.tachidesk.database.model.ExtensionDataClass
|
2020-12-24 19:37:27 +03:30
|
|
|
import ir.armor.tachidesk.database.model.ExtensionsTable
|
2020-12-24 01:22:01 +03:30
|
|
|
import kotlinx.coroutines.runBlocking
|
|
|
|
|
import okhttp3.Request
|
|
|
|
|
import okio.buffer
|
|
|
|
|
import okio.sink
|
2020-12-25 02:39:30 +03:30
|
|
|
import org.jetbrains.exposed.sql.*
|
2020-12-24 01:22:01 +03:30
|
|
|
import java.io.File
|
|
|
|
|
import java.net.URL
|
|
|
|
|
import java.net.URLClassLoader
|
2020-12-24 19:37:27 +03:30
|
|
|
import org.jetbrains.exposed.sql.transactions.transaction
|
2020-12-24 02:08:03 +03:30
|
|
|
|
2020-12-23 19:52:16 +03:30
|
|
|
class Main {
|
2020-12-23 17:52:05 +03:30
|
|
|
companion object {
|
2020-12-25 02:39:30 +03:30
|
|
|
var lastExtensionCheck: Long = 0
|
|
|
|
|
|
|
|
|
|
|
2020-12-24 01:22:01 +03:30
|
|
|
@JvmStatic
|
2020-12-25 02:39:30 +03:30
|
|
|
fun downloadAPKFile(url: String, apkPath: String) {
|
2020-12-24 01:22:01 +03:30
|
|
|
val request = Request.Builder().url(url).build()
|
|
|
|
|
val response = NetworkHelper().client.newCall(request).execute();
|
|
|
|
|
|
|
|
|
|
val downloadedFile = File(apkPath)
|
|
|
|
|
val sink = downloadedFile.sink().buffer()
|
|
|
|
|
sink.writeAll(response.body!!.source())
|
|
|
|
|
sink.close()
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-23 17:52:05 +03:30
|
|
|
@JvmStatic
|
2020-12-25 02:39:30 +03:30
|
|
|
fun testExtensionExecution() {
|
|
|
|
|
File(Config.extensionsRoot).mkdirs()
|
2020-12-24 01:22:01 +03:30
|
|
|
var sourcePkg = ""
|
|
|
|
|
|
|
|
|
|
// get list of extensions
|
|
|
|
|
var apkToDownload: String = ""
|
|
|
|
|
runBlocking {
|
|
|
|
|
val api = ExtensionGithubApi()
|
|
|
|
|
val source = api.findExtensions().first {
|
|
|
|
|
api.getApkUrl(it).endsWith("killsixbilliondemons-v1.2.3.apk")
|
|
|
|
|
}
|
|
|
|
|
apkToDownload = api.getApkUrl(source)
|
|
|
|
|
sourcePkg = source.pkgName
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
val apkFileName = apkToDownload.split("/").last()
|
2020-12-25 02:39:30 +03:30
|
|
|
val apkFilePath = "${Config.extensionsRoot}/$apkFileName"
|
2020-12-24 01:22:01 +03:30
|
|
|
val zipDirPath = apkFilePath.substringBefore(".apk")
|
|
|
|
|
val jarFilePath = "$zipDirPath.jar"
|
|
|
|
|
val dexFilePath = "$zipDirPath.dex"
|
2020-12-24 00:27:19 +03:30
|
|
|
|
2020-12-24 01:22:01 +03:30
|
|
|
// download apk file
|
2020-12-25 02:39:30 +03:30
|
|
|
downloadAPKFile(apkToDownload, apkFilePath)
|
2020-12-24 01:09:54 +03:30
|
|
|
|
|
|
|
|
|
2020-12-24 01:22:01 +03:30
|
|
|
val className = APKExtractor.extract_dex_and_read_className(apkFilePath, dexFilePath)
|
|
|
|
|
// dex -> jar
|
|
|
|
|
Dex2jarCmd.main(dexFilePath, "-o", jarFilePath, "--force")
|
2020-12-24 01:09:54 +03:30
|
|
|
|
2020-12-24 19:37:27 +03:30
|
|
|
val child = URLClassLoader(arrayOf<URL>(URL("file:$jarFilePath")), this::class.java.classLoader)
|
2020-12-24 01:22:01 +03:30
|
|
|
val classToLoad = Class.forName(className, true, child)
|
|
|
|
|
val instance = classToLoad.newInstance() as HttpSource
|
|
|
|
|
val result = instance.fetchPopularManga(1)
|
|
|
|
|
val mangasPage = result.toBlocking().first() as MangasPage
|
|
|
|
|
mangasPage.mangas.forEach {
|
|
|
|
|
println(it.title)
|
|
|
|
|
}
|
2020-12-25 02:39:30 +03:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fun extensionDatabaseIsEmtpy(): Boolean {
|
|
|
|
|
return transaction {
|
|
|
|
|
return@transaction ExtensionsTable.selectAll().count() == 0L
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fun getExtensionList(offline: Boolean = false): List<ExtensionDataClass> {
|
|
|
|
|
// update if 60 seconds has passed or requested offline and database is empty
|
|
|
|
|
if (lastExtensionCheck + 60 * 1000 < System.currentTimeMillis() || (offline && extensionDatabaseIsEmtpy())) {
|
|
|
|
|
println("Getting extensions list from the internet")
|
|
|
|
|
lastExtensionCheck = System.currentTimeMillis()
|
|
|
|
|
var foundExtensions: List<Extension.Available>
|
|
|
|
|
runBlocking {
|
|
|
|
|
val api = ExtensionGithubApi()
|
|
|
|
|
foundExtensions = api.findExtensions()
|
|
|
|
|
transaction {
|
|
|
|
|
foundExtensions.forEach { foundExtension ->
|
|
|
|
|
val extensionRecord = ExtensionsTable.select { ExtensionsTable.name eq foundExtension.name }.firstOrNull()
|
|
|
|
|
if (extensionRecord != null) {
|
|
|
|
|
// update the record
|
|
|
|
|
ExtensionsTable.update({ ExtensionsTable.name eq foundExtension.name }) {
|
|
|
|
|
it[name] = foundExtension.name
|
|
|
|
|
it[pkgName] = foundExtension.pkgName
|
|
|
|
|
it[versionName] = foundExtension.versionName
|
|
|
|
|
it[versionCode] = foundExtension.versionCode
|
|
|
|
|
it[lang] = foundExtension.lang
|
|
|
|
|
it[isNsfw] = foundExtension.isNsfw
|
|
|
|
|
it[apkName] = foundExtension.apkName
|
|
|
|
|
it[iconUrl] = foundExtension.iconUrl
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// insert new record
|
|
|
|
|
ExtensionsTable.insert {
|
|
|
|
|
it[name] = foundExtension.name
|
|
|
|
|
it[pkgName] = foundExtension.pkgName
|
|
|
|
|
it[versionName] = foundExtension.versionName
|
|
|
|
|
it[versionCode] = foundExtension.versionCode
|
|
|
|
|
it[lang] = foundExtension.lang
|
|
|
|
|
it[isNsfw] = foundExtension.isNsfw
|
|
|
|
|
it[apkName] = foundExtension.apkName
|
|
|
|
|
it[iconUrl] = foundExtension.iconUrl
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return transaction {
|
|
|
|
|
return@transaction ExtensionsTable.selectAll().map {
|
|
|
|
|
ExtensionDataClass(
|
|
|
|
|
it[ExtensionsTable.name],
|
|
|
|
|
it[ExtensionsTable.pkgName],
|
|
|
|
|
it[ExtensionsTable.versionName],
|
|
|
|
|
it[ExtensionsTable.versionCode],
|
|
|
|
|
it[ExtensionsTable.lang],
|
|
|
|
|
it[ExtensionsTable.isNsfw],
|
|
|
|
|
it[ExtensionsTable.apkName],
|
|
|
|
|
it[ExtensionsTable.iconUrl],
|
|
|
|
|
it[ExtensionsTable.installed],
|
|
|
|
|
it[ExtensionsTable.classFQName]
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fun downloadApk(apkName: String): Int {
|
|
|
|
|
val extension = getExtensionList(true).first { it.apkName == apkName }
|
|
|
|
|
val fileNameWithoutType = apkName.substringBefore(".apk")
|
|
|
|
|
val dirPathWithoutType = "${Config.extensionsRoot}/$apkName"
|
|
|
|
|
|
|
|
|
|
// check if we don't have the dex file already downloaded
|
|
|
|
|
val dexPath = "${Config.extensionsRoot}/$fileNameWithoutType.jar"
|
|
|
|
|
if (!File(dexPath).exists()) {
|
|
|
|
|
runBlocking {
|
|
|
|
|
val api = ExtensionGithubApi()
|
|
|
|
|
val apkToDownload = api.getApkUrl(extension)
|
|
|
|
|
|
|
|
|
|
val apkFilePath = "$dirPathWithoutType.apk"
|
|
|
|
|
val jarFilePath = "$dirPathWithoutType.jar"
|
|
|
|
|
val dexFilePath = "$dirPathWithoutType.dex"
|
|
|
|
|
|
|
|
|
|
// download apk file
|
|
|
|
|
downloadAPKFile(apkToDownload, apkFilePath)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
val className: String = APKExtractor.extract_dex_and_read_className(apkFilePath, dexFilePath)
|
|
|
|
|
|
|
|
|
|
// dex -> jar
|
|
|
|
|
Dex2jarCmd.main(dexFilePath, "-o", jarFilePath, "--force")
|
|
|
|
|
|
|
|
|
|
File(apkFilePath).delete()
|
|
|
|
|
|
|
|
|
|
transaction {
|
|
|
|
|
ExtensionsTable.update({ ExtensionsTable.name eq extension.name }) {
|
|
|
|
|
it[installed] = true
|
|
|
|
|
it[classFQName] = className
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
return 201 // we downloaded successfully
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
return 302
|
|
|
|
|
}
|
2020-12-24 02:08:03 +03:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@JvmStatic
|
|
|
|
|
fun main(args: Array<String>) {
|
2020-12-25 02:39:30 +03:30
|
|
|
// make sure everything we need exists
|
2020-12-24 19:37:27 +03:30
|
|
|
File(Config.dataRoot).mkdirs()
|
2020-12-25 02:39:30 +03:30
|
|
|
File(Config.extensionsRoot).mkdirs()
|
2020-12-24 19:37:27 +03:30
|
|
|
makeDataBaseTables()
|
|
|
|
|
|
|
|
|
|
|
2020-12-25 02:39:30 +03:30
|
|
|
val app = Javalin.create().start(4567)
|
|
|
|
|
|
|
|
|
|
app.before() { ctx ->
|
|
|
|
|
ctx.header("Access-Control-Allow-Origin", "*") // allow the client which is running on another port
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
app.get("/api/v1/extensions") { ctx ->
|
|
|
|
|
ctx.json(getExtensionList())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app.get("/api/v1/extensions/install/:apkName") { ctx ->
|
|
|
|
|
val apkName = ctx.pathParam("apkName")
|
|
|
|
|
ctx.status(
|
|
|
|
|
downloadApk(apkName)
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-23 17:52:05 +03:30
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|