Skip to content

Commit

Permalink
feat(core): validate SHA1 hashes of Yarn and Quilt cached metadata (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
macbrayne authored Dec 18, 2023
1 parent dbcd133 commit f5a5364
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ import com.fasterxml.jackson.core.JacksonException
import com.fasterxml.jackson.databind.JsonNode
import com.fasterxml.jackson.databind.ObjectMapper
import me.kcra.takenaka.core.Workspace
import me.kcra.takenaka.core.util.copyTo
import me.kcra.takenaka.core.util.readTree
import me.kcra.takenaka.core.util.*
import mu.KotlinLogging
import java.net.URL

Expand Down Expand Up @@ -70,17 +69,25 @@ class QuiltMetadataProvider(val workspace: Workspace, private val xmlMapper: Obj
private fun readMetadata(): JsonNode {
val file = workspace[METADATA]

val metadataLocation = "https://maven.quiltmc.org/repository/release/org/quiltmc/quilt-mappings/maven-metadata.xml"

if (relaxedCache && METADATA in workspace) {
try {
return xmlMapper.readTree(file).apply {
logger.info { "read cached Quilt mappings metadata" }
URL("$metadataLocation.sha1").httpRequest {
if (it.readText() == file.getChecksum(sha1Digest)) {
try {
return xmlMapper.readTree(file).apply {
logger.info { "read cached Quilt mappings metadata" }
}
} catch (e: JacksonException) {
logger.warn(e) { "failed to read cached Quilt mappings metadata, fetching it again" }
}
} else {
logger.warn { "cached Quilt mappings metadata is outdated or corrupt, fetching it again" }
}
} catch (e: JacksonException) {
logger.warn(e) { "failed to read cached Quilt mappings metadata, fetching it again" }
}
}

URL("https://maven.quiltmc.org/repository/release/org/quiltmc/quilt-mappings/maven-metadata.xml").copyTo(file)
URL(metadataLocation).copyTo(file)

logger.info { "fetched Quilt metadata" }
return xmlMapper.readTree(file)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ import com.fasterxml.jackson.core.JacksonException
import com.fasterxml.jackson.databind.JsonNode
import com.fasterxml.jackson.databind.ObjectMapper
import me.kcra.takenaka.core.Workspace
import me.kcra.takenaka.core.util.copyTo
import me.kcra.takenaka.core.util.readTree
import me.kcra.takenaka.core.util.*
import mu.KotlinLogging
import java.net.URL

Expand Down Expand Up @@ -78,17 +77,25 @@ class YarnMetadataProvider(val workspace: Workspace, private val xmlMapper: Obje
private fun readMetadata(): JsonNode {
val file = workspace[METADATA]

val metadataLocation = "https://maven.fabricmc.net/net/fabricmc/yarn/maven-metadata.xml"

if (relaxedCache && METADATA in workspace) {
try {
return xmlMapper.readTree(file).apply {
logger.info { "read cached Yarn metadata" }
URL("$metadataLocation.sha1").httpRequest {
if (it.readText() == file.getChecksum(sha1Digest)) {
try {
return xmlMapper.readTree(file).apply {
logger.info { "read cached Yarn mappings metadata" }
}
} catch (e: JacksonException) {
logger.warn(e) { "failed to read cached Yarn mappings metadata, fetching it again" }
}
} else {
logger.warn { "cached Yarn mappings metadata is outdated or corrupt, fetching it again" }
}
} catch (e: JacksonException) {
logger.warn(e) { "failed to read cached Yarn metadata, fetching it again" }
}
}

URL("https://maven.fabricmc.net/net/fabricmc/yarn/maven-metadata.xml").copyTo(file)
URL(metadataLocation).copyTo(file)

logger.info { "fetched Yarn metadata" }
return xmlMapper.readTree(file)
Expand Down

0 comments on commit f5a5364

Please sign in to comment.