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
|
2020-12-25 05:35:21 +03:30
|
|
|
import eu.kanade.tachiyomi.source.SourceFactory
|
2020-12-24 01:22:01 +03:30
|
|
|
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-25 13:00:18 +03:30
|
|
|
import ir.armor.tachidesk.database.dataclass.ExtensionDataClass
|
|
|
|
|
import ir.armor.tachidesk.database.dataclass.SourceDataClass
|
|
|
|
|
import ir.armor.tachidesk.database.entity.ExtensionEntity
|
|
|
|
|
import ir.armor.tachidesk.database.entity.SourceEntity
|
2020-12-24 19:37:27 +03:30
|
|
|
import ir.armor.tachidesk.database.makeDataBaseTables
|
2020-12-25 13:00:18 +03:30
|
|
|
import ir.armor.tachidesk.database.table.*
|
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 {
|
2020-12-25 05:35:21 +03:30
|
|
|
val extensionRecord = getExtensionList(true).first { it.apkName == apkName }
|
2020-12-25 02:39:30 +03:30
|
|
|
val fileNameWithoutType = apkName.substringBefore(".apk")
|
2020-12-25 06:15:52 +03:30
|
|
|
val dirPathWithoutType = "${Config.extensionsRoot}/$fileNameWithoutType"
|
2020-12-25 02:39:30 +03:30
|
|
|
|
|
|
|
|
// 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()
|
2020-12-25 05:35:21 +03:30
|
|
|
val apkToDownload = api.getApkUrl(extensionRecord)
|
2020-12-25 02:39:30 +03:30
|
|
|
|
|
|
|
|
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)
|
2020-12-25 05:35:21 +03:30
|
|
|
println(className)
|
2020-12-25 02:39:30 +03:30
|
|
|
// dex -> jar
|
|
|
|
|
Dex2jarCmd.main(dexFilePath, "-o", jarFilePath, "--force")
|
|
|
|
|
|
2020-12-25 06:15:52 +03:30
|
|
|
// clean up
|
2020-12-25 02:39:30 +03:30
|
|
|
File(apkFilePath).delete()
|
2020-12-25 06:15:52 +03:30
|
|
|
File(dexFilePath).delete()
|
2020-12-25 02:39:30 +03:30
|
|
|
|
2020-12-25 05:35:21 +03:30
|
|
|
// update sources of the extension
|
|
|
|
|
val child = URLClassLoader(arrayOf<URL>(URL("file:$jarFilePath")), this::class.java.classLoader)
|
|
|
|
|
val classToLoad = Class.forName(className, true, child)
|
|
|
|
|
val instance = classToLoad.newInstance()
|
|
|
|
|
|
|
|
|
|
val extensionId = transaction {
|
|
|
|
|
return@transaction ExtensionsTable.select { ExtensionsTable.name eq extensionRecord.name }.first()[ExtensionsTable.id]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (instance is HttpSource) {// single source
|
|
|
|
|
val httpSource = instance as HttpSource
|
|
|
|
|
transaction {
|
|
|
|
|
// SourceEntity.new {
|
|
|
|
|
// sourceId = httpSource.id
|
|
|
|
|
// name = httpSource.name
|
|
|
|
|
// this.extension = ExtensionEntity.find { ExtensionsTable.name eq extension.name }.first().id
|
|
|
|
|
// }
|
|
|
|
|
if (SourcesTable.select { SourcesTable.sourceId eq httpSource.id }.count() == 0L) {
|
|
|
|
|
SourcesTable.insert {
|
|
|
|
|
it[this.sourceId] = httpSource.id
|
|
|
|
|
it[name] = httpSource.name
|
|
|
|
|
it[this.lang] = httpSource.lang
|
|
|
|
|
it[extension] = extensionId
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// println(httpSource.id)
|
|
|
|
|
// println(httpSource.name)
|
|
|
|
|
// println()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} else { // multi source
|
|
|
|
|
val sourceFactory = instance as SourceFactory
|
|
|
|
|
transaction {
|
|
|
|
|
sourceFactory.createSources().forEachIndexed { index, source ->
|
|
|
|
|
val httpSource = source as HttpSource
|
|
|
|
|
if (SourcesTable.select { SourcesTable.sourceId eq httpSource.id }.count() == 0L) {
|
|
|
|
|
SourcesTable.insert {
|
|
|
|
|
it[this.sourceId] = httpSource.id
|
|
|
|
|
it[name] = httpSource.name
|
|
|
|
|
it[this.lang] = httpSource.lang
|
|
|
|
|
it[extension] = extensionId
|
|
|
|
|
it[partOfFactorySource] = true
|
|
|
|
|
it[positionInFactorySource] = index
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// println(httpSource.id)
|
|
|
|
|
// println(httpSource.name)
|
|
|
|
|
// println()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// update extension info
|
2020-12-25 02:39:30 +03:30
|
|
|
transaction {
|
2020-12-25 05:35:21 +03:30
|
|
|
ExtensionsTable.update({ ExtensionsTable.name eq extensionRecord.name }) {
|
2020-12-25 02:39:30 +03:30
|
|
|
it[installed] = true
|
|
|
|
|
it[classFQName] = className
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
return 201 // we downloaded successfully
|
2020-12-25 05:35:21 +03:30
|
|
|
} else {
|
2020-12-25 02:39:30 +03:30
|
|
|
return 302
|
|
|
|
|
}
|
2020-12-24 02:08:03 +03:30
|
|
|
}
|
|
|
|
|
|
2020-12-25 06:15:52 +03:30
|
|
|
fun getHttpSource(sourceId: Long): HttpSource {
|
|
|
|
|
return transaction {
|
|
|
|
|
val sourceRecord = SourceEntity.find { SourcesTable.sourceId eq sourceId }.first()
|
|
|
|
|
val extensionId = sourceRecord.extension.id.value
|
|
|
|
|
val extensionRecord = ExtensionEntity.get(extensionId)
|
|
|
|
|
val apkName = extensionRecord.apkName
|
|
|
|
|
val className = extensionRecord.classFQName
|
|
|
|
|
val jarName = apkName.substringBefore(".apk") + ".jar"
|
|
|
|
|
val jarPath = "${Config.extensionsRoot}/$jarName"
|
|
|
|
|
|
|
|
|
|
println(jarPath)
|
|
|
|
|
|
|
|
|
|
val child = URLClassLoader(arrayOf<URL>(URL("file:$jarPath")), this::class.java.classLoader)
|
|
|
|
|
val classToLoad = Class.forName(className, true, child)
|
|
|
|
|
val instance = classToLoad.newInstance()
|
|
|
|
|
|
|
|
|
|
if (sourceRecord.partOfFactorySource) {
|
|
|
|
|
return@transaction (instance as SourceFactory).createSources()[sourceRecord.positionInFactorySource!!] as HttpSource
|
|
|
|
|
} else {
|
|
|
|
|
return@transaction instance as HttpSource
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fun getSourceList(): List<SourceDataClass> {
|
|
|
|
|
return transaction {
|
|
|
|
|
return@transaction SourcesTable.selectAll().map {
|
|
|
|
|
SourceDataClass(
|
|
|
|
|
it[SourcesTable.sourceId],
|
|
|
|
|
it[SourcesTable.name],
|
|
|
|
|
it[SourcesTable.lang],
|
|
|
|
|
ExtensionsTable.select { ExtensionsTable.id eq it[SourcesTable.extension] }.first()[ExtensionsTable.iconUrl],
|
|
|
|
|
getHttpSource(it[SourcesTable.sourceId]).supportsLatest
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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")
|
2020-12-25 03:08:08 +03:30
|
|
|
println(apkName)
|
2020-12-25 02:39:30 +03:30
|
|
|
ctx.status(
|
|
|
|
|
downloadApk(apkName)
|
|
|
|
|
)
|
|
|
|
|
}
|
2020-12-25 06:15:52 +03:30
|
|
|
app.get("/api/v1/sources/") { ctx ->
|
|
|
|
|
ctx.json(getSourceList())
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-25 02:39:30 +03:30
|
|
|
|
2020-12-23 17:52:05 +03:30
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|