From 8f4d28393e2bd4d0554949e60633a956e21c5ba9 Mon Sep 17 00:00:00 2001 From: Graham Date: Fri, 7 Oct 2022 22:00:31 +0100 Subject: [PATCH] Download XTEA keys from HDOS Signed-off-by: Graham --- .../org/openrs2/archive/ArchiveModule.kt | 2 + .../openrs2/archive/key/HdosKeyDownloader.kt | 57 +++++++++++++++++++ .../org/openrs2/archive/key/KeySource.kt | 3 +- .../openrs2/archive/migrations/V18__hdos.sql | 2 + .../org/openrs2/archive/templates/index.html | 1 + 5 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 archive/src/main/kotlin/org/openrs2/archive/key/HdosKeyDownloader.kt create mode 100644 archive/src/main/resources/org/openrs2/archive/migrations/V18__hdos.sql diff --git a/archive/src/main/kotlin/org/openrs2/archive/ArchiveModule.kt b/archive/src/main/kotlin/org/openrs2/archive/ArchiveModule.kt index 32b6b87b5a..0536bb5cff 100644 --- a/archive/src/main/kotlin/org/openrs2/archive/ArchiveModule.kt +++ b/archive/src/main/kotlin/org/openrs2/archive/ArchiveModule.kt @@ -5,6 +5,7 @@ import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule import com.google.inject.AbstractModule import com.google.inject.Scopes import com.google.inject.multibindings.Multibinder +import org.openrs2.archive.key.HdosKeyDownloader import org.openrs2.archive.key.KeyDownloader import org.openrs2.archive.key.OpenOsrsKeyDownloader import org.openrs2.archive.key.PolarKeyDownloader @@ -45,6 +46,7 @@ public object ArchiveModule : AbstractModule() { .addBinding().to(JavaTimeModule::class.java) val keyBinder = Multibinder.newSetBinder(binder(), KeyDownloader::class.java) + keyBinder.addBinding().to(HdosKeyDownloader::class.java) keyBinder.addBinding().to(OpenOsrsKeyDownloader::class.java) keyBinder.addBinding().to(PolarKeyDownloader::class.java) keyBinder.addBinding().to(RuneLiteKeyDownloader::class.java) diff --git a/archive/src/main/kotlin/org/openrs2/archive/key/HdosKeyDownloader.kt b/archive/src/main/kotlin/org/openrs2/archive/key/HdosKeyDownloader.kt new file mode 100644 index 0000000000..5c8fd4e326 --- /dev/null +++ b/archive/src/main/kotlin/org/openrs2/archive/key/HdosKeyDownloader.kt @@ -0,0 +1,57 @@ +package org.openrs2.archive.key + +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.future.await +import kotlinx.coroutines.withContext +import org.openrs2.crypto.XteaKey +import org.openrs2.http.checkStatusCode +import java.net.URI +import java.net.http.HttpClient +import java.net.http.HttpRequest +import java.net.http.HttpResponse +import java.time.Duration +import javax.inject.Inject +import javax.inject.Singleton + +@Singleton +public class HdosKeyDownloader @Inject constructor( + private val client: HttpClient +) : KeyDownloader(KeySource.HDOS) { + override suspend fun getMissingUrls(seenUrls: Set): Set { + return setOf(ENDPOINT) + } + + override suspend fun download(url: String): Sequence { + val request = HttpRequest.newBuilder(URI(url)) + .GET() + .timeout(Duration.ofSeconds(30)) + .build() + + val response = client.sendAsync(request, HttpResponse.BodyHandlers.ofInputStream()).await() + response.checkStatusCode() + + return withContext(Dispatchers.IO) { + response.body().use { input -> + input.bufferedReader().use { reader -> + val keys = mutableSetOf() + + for (line in reader.lineSequence()) { + val parts = line.split(',') + if (parts.size < 3) { + continue + } + + val key = XteaKey.fromHexOrNull(parts[2]) ?: continue + keys += key + } + + keys.asSequence() + } + } + } + } + + private companion object { + private const val ENDPOINT = "https://api.hdos.dev/keys/get" + } +} diff --git a/archive/src/main/kotlin/org/openrs2/archive/key/KeySource.kt b/archive/src/main/kotlin/org/openrs2/archive/key/KeySource.kt index 878dd21456..b5fb9ada22 100644 --- a/archive/src/main/kotlin/org/openrs2/archive/key/KeySource.kt +++ b/archive/src/main/kotlin/org/openrs2/archive/key/KeySource.kt @@ -5,5 +5,6 @@ public enum class KeySource { DISK, OPENOSRS, POLAR, - RUNELITE + RUNELITE, + HDOS } diff --git a/archive/src/main/resources/org/openrs2/archive/migrations/V18__hdos.sql b/archive/src/main/resources/org/openrs2/archive/migrations/V18__hdos.sql new file mode 100644 index 0000000000..0c9568b54e --- /dev/null +++ b/archive/src/main/resources/org/openrs2/archive/migrations/V18__hdos.sql @@ -0,0 +1,2 @@ +-- @formatter:off +ALTER TYPE key_source ADD VALUE 'hdos'; diff --git a/archive/src/main/resources/org/openrs2/archive/templates/index.html b/archive/src/main/resources/org/openrs2/archive/templates/index.html index ba5fb85e13..df08c7961a 100644 --- a/archive/src/main/resources/org/openrs2/archive/templates/index.html +++ b/archive/src/main/resources/org/openrs2/archive/templates/index.html @@ -52,6 +52,7 @@