allow user to specify tls version

This commit is contained in:
Jonathan Fisher 2020-11-09 22:44:36 +00:00
parent 6c7f6d4b19
commit 3e0c9e9728
5 changed files with 23 additions and 12 deletions

View File

@ -6,6 +6,7 @@ import oppen.ariane.Ariane
import oppen.toURI
import com.google.common.truth.Truth.assertThat
import oppen.ariane.io.GemState
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
@ -26,13 +27,13 @@ class GeminiDatasourceTests {
private val capsuleIndex = 3
private fun setTLSProtocol(protocol: String){
gemini = Datasource.factory(InstrumentationRegistry.getInstrumentation().targetContext, protocol)
@Before
private fun setup(){
gemini = Datasource.factory(InstrumentationRegistry.getInstrumentation().targetContext)
}
@Test
fun arianeHomePageTest(){
setTLSProtocol("TLSv1")
var hasRequested = false
var hasResponded = false
@ -58,12 +59,9 @@ class GeminiDatasourceTests {
@Test
fun aCapsuleTest(){
setTLSProtocol("TLSv1.3")
var hasRequested = false
var hasResponded = false
gemini.request(capsules[capsuleIndex].toURI()){ state ->
when(state){

View File

@ -8,8 +8,8 @@ interface Datasource {
fun request(uri: URI, onUpdate: (state: GemState) -> Unit)
companion object{
fun factory(context: Context, protocol: String): Datasource {
return GeminiDatasource(context, protocol)
fun factory(context: Context): Datasource {
return GeminiDatasource(context)
}
}
}

View File

@ -2,6 +2,7 @@ package oppen.ariane.io.gemini
import android.content.Context
import androidx.core.net.toUri
import androidx.preference.PreferenceManager
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import oppen.ariane.io.GemState
@ -21,10 +22,11 @@ const val GEMINI_SCHEME = "gemini"
*
*/
class GeminiDatasource(
private val context: Context,
private val protocol: String): Datasource {
private val context: Context): Datasource {
private val prefs = PreferenceManager.getDefaultSharedPreferences(context)
private var last: URI? = null
override fun request(uri: URI, onUpdate: (state: GemState) -> Unit) {
//Any inputted uri starting with a colon is an app-specific command, eg. :prefs :settings
@ -124,6 +126,10 @@ class GeminiDatasource(
last = uri
val port = if(uri.port == -1) 1965 else uri.port
val protocol = prefs.getString("tls_protocol", "TLS")
println("REQ_PROTOCOL: $protocol")
val sslContext = SSLContext.getInstance(protocol)
sslContext.init(null, trustAllCerts, SecureRandom())
@ -133,7 +139,12 @@ class GeminiDatasource(
try {
socket = factory.createSocket(uri.host, port) as SSLSocket
socket.enabledCipherSuites = factory.supportedCipherSuites
socket.enabledProtocols = socket.supportedProtocols
when (protocol) {
"TLS" -> socket.enabledProtocols = socket.supportedProtocols
else -> socket.enabledProtocols = arrayOf(protocol)
}
socket.startHandshake()
}catch(ce: ConnectException){
println("socket error: $ce")

View File

@ -102,7 +102,7 @@ class GemActivity : AppCompatActivity() {
model.initialise(
home = home ?: Ariane.DEFAULT_HOME_CAPSULE,
gemini = Datasource.factory(this, "TLSv1.2"),
gemini = Datasource.factory(this),
bookmarks = BookmarksDatasource.getDefault(applicationContext)
){ state ->

View File

@ -24,6 +24,8 @@ class SettingsFragment: PreferenceFragmentCompat(), Preference.OnPreferenceChang
tlsCategory.title = "TLS Config"
screen.addPreference(tlsCategory)
//todo - need to add the default "TLS" option here,
// otherwise once set in this screen user can't get that default back
val sslContext = SSLContext.getInstance("TLS")
sslContext.init(null, null, SecureRandom())
val factory: SSLSocketFactory = sslContext.socketFactory