From 59e664ec5c08be5ce438736f964147fd0d12c483 Mon Sep 17 00:00:00 2001 From: dece Date: Tue, 15 Feb 2022 17:09:11 +0100 Subject: [PATCH] clean and add some docs --- .../java/dev/lowrespalmtree/comet/UriUtilsTest.kt | 9 ++++++--- .../java/dev/lowrespalmtree/comet/PageViewModel.kt | 4 ++-- .../main/java/dev/lowrespalmtree/comet/Request.kt | 14 ++++++++++++++ 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/app/src/androidTest/java/dev/lowrespalmtree/comet/UriUtilsTest.kt b/app/src/androidTest/java/dev/lowrespalmtree/comet/UriUtilsTest.kt index e442d3a..1690193 100644 --- a/app/src/androidTest/java/dev/lowrespalmtree/comet/UriUtilsTest.kt +++ b/app/src/androidTest/java/dev/lowrespalmtree/comet/UriUtilsTest.kt @@ -13,15 +13,18 @@ class UriUtilsTest { fun joinUrls() { assertEquals( "gemini://dece.space/some-file.gmi", - dev.lowrespalmtree.comet.utils.joinUrls("gemini://dece.space/", "some-file.gmi").toString() + dev.lowrespalmtree.comet.utils.joinUrls("gemini://dece.space/", "some-file.gmi") + .toString() ) assertEquals( "gemini://dece.space/some-file.gmi", - dev.lowrespalmtree.comet.utils.joinUrls("gemini://dece.space/", "./some-file.gmi").toString() + dev.lowrespalmtree.comet.utils.joinUrls("gemini://dece.space/", "./some-file.gmi") + .toString() ) assertEquals( "gemini://dece.space/some-file.gmi", - dev.lowrespalmtree.comet.utils.joinUrls("gemini://dece.space/dir1", "/some-file.gmi").toString() + dev.lowrespalmtree.comet.utils.joinUrls("gemini://dece.space/dir1", "/some-file.gmi") + .toString() ) assertEquals( "gemini://dece.space/dir1/other-file.gmi", diff --git a/app/src/main/java/dev/lowrespalmtree/comet/PageViewModel.kt b/app/src/main/java/dev/lowrespalmtree/comet/PageViewModel.kt index 843843f..eb834bc 100644 --- a/app/src/main/java/dev/lowrespalmtree/comet/PageViewModel.kt +++ b/app/src/main/java/dev/lowrespalmtree/comet/PageViewModel.kt @@ -195,8 +195,8 @@ class PageViewModel(@Suppress("unused") private val savedStateHandle: SavedState // We record the history entry here: it's nice because we have the main title available // and we're already in a coroutine for database access. - History.record(uri.toString(), mainTitle) - event.postValue(SuccessEvent(uri.toString())) + History.record(uriString, mainTitle) + event.postValue(SuccessEvent(uriString)) state.postValue(State.IDLE) } diff --git a/app/src/main/java/dev/lowrespalmtree/comet/Request.kt b/app/src/main/java/dev/lowrespalmtree/comet/Request.kt index 823332a..51c0aba 100644 --- a/app/src/main/java/dev/lowrespalmtree/comet/Request.kt +++ b/app/src/main/java/dev/lowrespalmtree/comet/Request.kt @@ -19,6 +19,12 @@ import javax.net.ssl.* class Request(private val uri: Uri, private val keyManager: KeyManager? = null) { private val port get() = if (uri.port > 0) uri.port else 1965 + /** + * Open and return the TLS socket with the server. + * + * If the server certificate present is fine according to our TOFU settings, the app can + * continue by calling `proceed` which will retrieve the data. + */ fun connect(protocol: String, connectionTimeout: Int, readTimeout: Int): SSLSocket { Log.d( TAG, @@ -34,6 +40,7 @@ class Request(private val uri: Uri, private val keyManager: KeyManager? = null) return socket } + /** Return a byte array channel carrying the data chunks sent by the server. */ fun proceed(socket: SSLSocket, scope: CoroutineScope): Channel { Log.d(TAG, "proceed") socket.outputStream.write("$uri\r\n".toByteArray()) @@ -63,6 +70,13 @@ class Request(private val uri: Uri, private val keyManager: KeyManager? = null) return channel } + /** + * Dummy KeyManager to be used when an client cert is to be used during the connection. + * + * This simply retrieves both the public cert and private key from the Android key store + * and implement dummy methods to return only this key pair. Some methods are left unimplemented + * because they should never be executed in the context we use the key manager in. + */ class KeyManager( private val alias: String, private val cert: X509Certificate,