Skip to content

Commit

Permalink
Fixes hashes bug, changed to use HashType
Browse files Browse the repository at this point in the history
  • Loading branch information
Dawsson committed Jul 2, 2024
1 parent c6b0416 commit b627df8
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ object LocalPluginCache : PluginCache<LocalPlugin>() {
name = "PluginPortal",
version = PluginPortal.instance.description.version,
platform = MarketplacePlatform.MODRINTH,
sha256 = calculateSHA256(pluginPortalJarFile),
sha512 = calculateSHA512(pluginPortalJarFile),
sha256 = HashType.SHA256.hash(pluginPortalJarFile),
sha512 = HashType.SHA512.hash(pluginPortalJarFile),
installedAt = System.currentTimeMillis(),
)

val pluginsInFolder = File(Config.INSTALL_DIRECTORY).listFiles()?.filter(File::isJarFile)?.associateBy { calculateSHA256(it) }
val pluginsInFolder = File(Config.INSTALL_DIRECTORY).listFiles()?.filter(File::isJarFile)?.associateBy { HashType.SHA256.hash(it) }

val text = getPluginsFile().readText()
if (text.isEmpty()) {
Expand Down Expand Up @@ -96,13 +96,12 @@ object LocalPluginCache : PluginCache<LocalPlugin>() {

return files.filter { file -> file.isFile }
.filter { file -> file.name.endsWith(".jar") }
.firstOrNull { file -> calculateSHA256(file) == sha256 }
.firstOrNull { file -> HashType.SHA256.hash(file) == sha256 }
}


private fun getPluginsFile() = File(PluginPortal.instance.dataFolder, "plugins.json").createIfNotExists()


fun searchPluginsWithFeedback(
audience: Audience,
name: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ object MarketplacePluginCache : PluginCache<Plugin>() {
.appendPrimary(platform.name).appendSecondary("...")
)


val targetMessage = "${plugin.name} from ${platform.name} with ID ${plugin.id}"
PortalLogger.log(audience, PortalLogger.Action.INITIATED_INSTALL, targetMessage)

Expand All @@ -128,7 +127,7 @@ object MarketplacePluginCache : PluginCache<Plugin>() {
}

/**
* Sorts the plugins by relevance to the query, this also takes downloads etc. into accoutnt
* Sorts the plugins by relevance to the query, this also takes downloads etc. into account
*/
fun List<Plugin>.sortedByRelevance(query: String): List<Plugin> = sortedByDescending {
it.totalDownloads * if (query.equals(it.name, true)) 50 else 1 // Arbitrary bias to exact matches
Expand Down
20 changes: 19 additions & 1 deletion plugin/src/main/kotlin/gg/flyte/pluginportal/plugin/util/File.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package gg.flyte.pluginportal.plugin.util
import java.io.File
import java.net.HttpURLConnection
import java.net.URL
import java.security.MessageDigest

fun isValidDownload(url: String): Boolean {
return isJarDownloadUrl(url)
Expand All @@ -22,4 +23,21 @@ fun isJarDownloadUrl(url: String): Boolean {
} ?: false
}

fun File.isJarFile() = isFile && extension == "jar"
fun File.isJarFile() = isFile && extension == "jar"

fun hash(data: ByteArray, algo: String = "SHA-256"): String {
return MessageDigest
.getInstance(algo)
.digest(data)
.joinToString("") { byte -> "%02x".format(byte) }
}

private fun calculateSHA256(file: File): String = hash(file.readBytes())
private fun calculateSHA1(file: File): String = hash(file.readBytes(), "SHA-1")
private fun calculateSHA512(file: File): String = hash(file.readBytes(), "SHA-512")

enum class HashType(val hash: (File) -> String) {
SHA256(::calculateSHA256),
SHA1(::calculateSHA1),
SHA512(::calculateSHA512)
}
24 changes: 6 additions & 18 deletions plugin/src/main/kotlin/gg/flyte/pluginportal/plugin/util/Http.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ fun Plugin.download(marketplacePlatform: MarketplacePlatform, targetDirectory: S
name = name,
version = platforms[marketplacePlatform]?.download!!.version,
platform = marketplacePlatform,
sha256 = calculateSHA256(file),
sha512 = calculateSHA512(file),
sha256 = HashType.SHA256.hash(file),
sha512 = HashType.SHA512.hash(file),
installedAt = System.currentTimeMillis(),
)
)
Expand All @@ -41,7 +41,7 @@ fun Plugin.download(marketplacePlatform: MarketplacePlatform, targetDirectory: S
return true
}

fun download(url: URL, file: File, audience: Audience?): File? = try {
fun download(url: URL, file: File, audience: Audience?): File? = runCatching {
url.openConnection().apply {
setRequestProperty("User-Agent", "Mozilla/5.0")
connect()
Expand All @@ -50,8 +50,8 @@ fun download(url: URL, file: File, audience: Audience?): File? = try {
file.outputStream().use { output -> input.copyTo(output) }
}
file
} catch (e: Exception) {
e.printStackTrace()
}.onFailure {
it.printStackTrace()
audience?.sendMessage(
text("\n").append(
status(Status.FAILURE, "An error occurred while downloading\n")
Expand All @@ -61,16 +61,4 @@ fun download(url: URL, file: File, audience: Audience?): File? = try {
).append(endLine())
)
)
null
}

fun hash(data: ByteArray, algo: String = "SHA-256"): String {
return MessageDigest
.getInstance(algo)
.digest(data)
.joinToString { "%02x".format(it) }
}
fun calculateSHA256(file: File): String = hash(file.readBytes())
fun calculateSHA1(file: File): String = hash(file.readBytes(), "SHA-1")

fun calculateSHA512(file: File): String = hash(file.readBytes(), "SHA-512")
}.getOrNull()

0 comments on commit b627df8

Please sign in to comment.