Skip to content

Commit

Permalink
Fix empty fingerprint handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Iamlooker committed Sep 17, 2023
1 parent 86a5696 commit 0f49d69
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@ import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import io.ktor.client.*
import io.ktor.client.engine.*
import io.ktor.client.engine.okhttp.*
import io.ktor.http.*
import org.fdroid.index.IndexConverter

@Module
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import org.fdroid.index.v2.IndexV2

interface IndexDownloader {

suspend fun downloadIndexV1(repo: Repo): IndexV1
suspend fun downloadIndexV1(repo: Repo): Pair<String, IndexV1>

suspend fun downloadIndexV2(repo: Repo): IndexV2

suspend fun downloadIndexDiff(repo: Repo, name: String): IndexV2

suspend fun downloadEntry(repo: Repo): Entry
suspend fun downloadEntry(repo: Repo): Pair<String, Entry>

suspend fun determineIndexType(repo: Repo): IndexType

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package com.looker.core.data.fdroid.sync

import com.looker.core.common.extension.exceptCancellation
import com.looker.core.common.extension.fingerprint
import com.looker.core.data.utils.certificate
import com.looker.core.data.utils.codeSigner
import com.looker.core.model.newer.Repo
import com.looker.network.Downloader
import com.looker.network.NetworkResponse
Expand All @@ -14,6 +18,7 @@ import org.fdroid.index.v2.IndexV2
import java.io.File
import java.util.Date
import java.util.UUID
import java.util.jar.JarFile
import javax.inject.Inject

class IndexDownloaderImpl @Inject constructor(
Expand All @@ -23,19 +28,33 @@ class IndexDownloaderImpl @Inject constructor(
companion object {
private val parser = IndexParser

private fun File.parseIndexV1(): IndexV1 = parser.parseV1(inputStream())
private fun File.parseIndexV2(): IndexV2 = parser.parseV2(inputStream())
private fun File.parseEntry(): Entry = parser.parseEntry(inputStream())

private const val INDEX_V1_FILE_NAME = "index-v1.jar"
private const val INDEX_V2_FILE_NAME = "index-v2.json"
private const val ENTRY_FILE_NAME = "entry.jar"

private const val INDEX_V1_JSON = "index-v1.json"
private const val ENTRY_JSON = "entry.json"
}

override suspend fun downloadIndexV1(
repo: Repo
): IndexV1 = withContext(Dispatchers.Default) {
): Pair<String, IndexV1> = withContext(Dispatchers.IO) {
val jarFile = downloadIndexFile(repo, INDEX_V1_FILE_NAME)
val verifier = IndexV1Verifier(jarFile, null, repo.fingerprint)
verifier.getStreamAndVerify(parser::parseV1).second
try {
val verifier = IndexV1Verifier(jarFile, null, repo.fingerprint)
verifier.getStreamAndVerify(parser::parseV1)
} catch (e: SigningException) {
e.exceptCancellation()
JarFile(jarFile, true)
.getJarEntry(INDEX_V1_JSON)
.codeSigner
.certificate
.fingerprint() to jarFile.parseIndexV1()
}
}

override suspend fun downloadIndexV2(
Expand All @@ -55,15 +74,24 @@ class IndexDownloaderImpl @Inject constructor(

override suspend fun downloadEntry(
repo: Repo
): Entry = withContext(Dispatchers.Default) {
): Pair<String, Entry> = withContext(Dispatchers.IO) {
val jarFile = downloadIndexFile(repo, ENTRY_FILE_NAME)
val verifier = EntryVerifier(jarFile, null, repo.fingerprint)
verifier.getStreamAndVerify(parser::parseEntry).second
try {
val verifier = EntryVerifier(jarFile, null, repo.fingerprint)
verifier.getStreamAndVerify(parser::parseEntry)
} catch (e: SigningException) {
e.exceptCancellation()
JarFile(jarFile, true)
.getJarEntry(ENTRY_JSON)
.codeSigner
.certificate
.fingerprint() to jarFile.parseEntry()
}
}

override suspend fun determineIndexType(repo: Repo): IndexType {
val indexV2Exist = downloader.headCall(repo.indexUrl(ENTRY_FILE_NAME))
return if (indexV2Exist is NetworkResponse.Success) IndexType.ENTRY
val isIndexV2 = downloader.headCall(repo.indexUrl(ENTRY_FILE_NAME))
return if (isIndexV2 is NetworkResponse.Success) IndexType.ENTRY
else IndexType.INDEX_V1
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,20 @@ class IndexManager(

suspend fun getIndex(repos: List<Repo>): Map<Repo, IndexV2> =
withContext(Dispatchers.Default) {
repos.associateWith { repo ->
repos.associate { repo ->
when (indexDownloader.determineIndexType(repo)) {
IndexType.INDEX_V1 -> {
val indexV1 = indexDownloader.downloadIndexV1(repo)
converter.toIndexV2(indexV1)
val fingerprintAndIndex = indexDownloader.downloadIndexV1(repo)
repo.copy(fingerprint = fingerprintAndIndex.first) to
converter.toIndexV2(fingerprintAndIndex.second)
}

IndexType.ENTRY -> {
val entry = indexDownloader.downloadEntry(repo)
val diff = entry.getDiff(repo.versionInfo.timestamp)
if (diff == null) indexDownloader.downloadIndexV2(repo)
else indexDownloader.downloadIndexDiff(repo, diff.name)
val fingerprintAndEntry = indexDownloader.downloadEntry(repo)
val diff = fingerprintAndEntry.second.getDiff(repo.versionInfo.timestamp)
repo.copy(fingerprint = fingerprintAndEntry.first) to
if (diff == null) indexDownloader.downloadIndexV2(repo)
else indexDownloader.downloadIndexDiff(repo, diff.name)
}
}
}
Expand Down

0 comments on commit 0f49d69

Please sign in to comment.