Request: add a TLS version preference

This commit is contained in:
dece 2022-02-04 19:09:44 +01:00
parent 1d69c075a1
commit 4282567f5f
6 changed files with 25 additions and 22 deletions

View file

@ -118,11 +118,13 @@ class PageFragment : Fragment(), PageAdapter.Listener {
when (uri.scheme) { when (uri.scheme) {
"gemini" -> { "gemini" -> {
val prefs = PreferenceManager.getDefaultSharedPreferences(requireContext()) val prefs = PreferenceManager.getDefaultSharedPreferences(requireContext())
val protocol =
prefs.getString("tls_version", Request.DEFAULT_TLS_VERSION)!!
val connectionTimeout = val connectionTimeout =
prefs.getInt("connection_timeout", Request.DEFAULT_CONNECTION_TIMEOUT_SEC) prefs.getInt("connection_timeout", Request.DEFAULT_CONNECTION_TIMEOUT_SEC)
val readTimeout = val readTimeout =
prefs.getInt("read_timeout", Request.DEFAULT_READ_TIMEOUT_SEC) prefs.getInt("read_timeout", Request.DEFAULT_READ_TIMEOUT_SEC)
vm.sendGeminiRequest(uri, connectionTimeout, readTimeout) vm.sendGeminiRequest(uri, protocol, connectionTimeout, readTimeout)
} }
else -> openUnknownScheme(uri) else -> openUnknownScheme(uri)
} }

View file

@ -52,7 +52,7 @@ class PageViewModel(@Suppress("unused") private val savedStateHandle: SavedState
* The URI must be valid, absolute and with a gemini scheme. * The URI must be valid, absolute and with a gemini scheme.
*/ */
@ExperimentalCoroutinesApi @ExperimentalCoroutinesApi
fun sendGeminiRequest(uri: Uri, connectionTimeout: Int, readTimeout: Int, redirects: Int = 0) { fun sendGeminiRequest(uri: Uri, protocol: String, connectionTimeout: Int, readTimeout: Int, redirects: Int = 0) {
Log.d(TAG, "sendRequest: URI \"$uri\"") Log.d(TAG, "sendRequest: URI \"$uri\"")
loadingUrl = uri loadingUrl = uri
state.postValue(State.CONNECTING) state.postValue(State.CONNECTING)
@ -60,7 +60,7 @@ class PageViewModel(@Suppress("unused") private val savedStateHandle: SavedState
requestJob = viewModelScope.launch(Dispatchers.IO) { requestJob = viewModelScope.launch(Dispatchers.IO) {
val response = try { val response = try {
val request = Request(uri) val request = Request(uri)
val socket = request.connect(connectionTimeout, readTimeout) val socket = request.connect(protocol, connectionTimeout, readTimeout)
val channel = request.proceed(socket, this) val channel = request.proceed(socket, this)
Response.from(channel, viewModelScope) Response.from(channel, viewModelScope)
} catch (e: Exception) { } catch (e: Exception) {
@ -71,7 +71,7 @@ class PageViewModel(@Suppress("unused") private val savedStateHandle: SavedState
signalError( signalError(
when (e) { when (e) {
is UnknownHostException -> "Unknown host \"${uri.authority}\"." is UnknownHostException -> "Unknown host \"${uri.authority}\"."
is ConnectException -> "Can't connect to this server: ${e.localizedMessage}." is ConnectException -> "Can't connect to this server: ${e.message}."
is SocketTimeoutException -> "Connection timed out." is SocketTimeoutException -> "Connection timed out."
is CancellationException -> "Connection cancelled: ${e.message}." is CancellationException -> "Connection cancelled: ${e.message}."
else -> "Oops, something failed!" else -> "Oops, something failed!"

View file

@ -17,9 +17,9 @@ import javax.net.ssl.X509TrustManager
class Request(private val uri: Uri) { class Request(private val uri: Uri) {
private val port get() = if (uri.port > 0) uri.port else 1965 private val port get() = if (uri.port > 0) uri.port else 1965
fun connect(connectionTimeout: Int, readTimeout: Int): SSLSocket { fun connect(protocol: String, connectionTimeout: Int, readTimeout: Int): SSLSocket {
Log.d(TAG, "connect") Log.d(TAG, "connect: $protocol, c.to. $connectionTimeout, r.to. $readTimeout")
val context = SSLContext.getInstance("TLSv1.2") val context = SSLContext.getInstance(protocol)
context.init(null, arrayOf(TrustManager()), null) context.init(null, arrayOf(TrustManager()), null)
val socket = context.socketFactory.createSocket() as SSLSocket val socket = context.socketFactory.createSocket() as SSLSocket
socket.soTimeout = readTimeout * 1000 socket.soTimeout = readTimeout * 1000
@ -70,6 +70,7 @@ class Request(private val uri: Uri) {
companion object { companion object {
private const val TAG = "Request" private const val TAG = "Request"
const val DEFAULT_TLS_VERSION = "TLSv1.3"
const val DEFAULT_CONNECTION_TIMEOUT_SEC = 10 const val DEFAULT_CONNECTION_TIMEOUT_SEC = 10
const val DEFAULT_READ_TIMEOUT_SEC = 10 const val DEFAULT_READ_TIMEOUT_SEC = 10
} }

View file

@ -1,12 +1,11 @@
<resources> <resources>
<!-- Reply Preference --> <string-array name="tls_version_entries">
<string-array name="reply_entries"> <item>TLS v1.3</item>
<item>Reply</item> <item>TLS v1.2</item>
<item>Reply to all</item>
</string-array> </string-array>
<string-array name="reply_values"> <string-array name="tls_version_values">
<item>reply</item> <item>TLSv1.3</item>
<item>reply_all</item> <item>TLSv1.2</item>
</string-array> </string-array>
</resources> </resources>

View file

@ -46,5 +46,6 @@
<string name="edit">Edit</string> <string name="edit">Edit</string>
<string name="identity_usages">Active URL path</string> <string name="identity_usages">Active URL path</string>
<string name="input_common_name">Enter a name to use as the certificate\'s subject common name. This can be left empty.</string> <string name="input_common_name">Enter a name to use as the certificate\'s subject common name. This can be left empty.</string>
<string name="tls_version">TLS version</string>
</resources> </resources>

View file

@ -13,18 +13,18 @@
app:key="home_set" app:key="home_set"
app:title="@string/pref_home_set" /> app:title="@string/pref_home_set" />
<ListPreference
app:defaultValue="reply"
app:entries="@array/reply_entries"
app:entryValues="@array/reply_values"
app:key="reply"
app:title="@string/reply_title"
app:useSimpleSummaryProvider="true" />
</PreferenceCategory> </PreferenceCategory>
<PreferenceCategory app:title="@string/pref_protocol_header"> <PreferenceCategory app:title="@string/pref_protocol_header">
<DropDownPreference
app:key="tls_version"
app:entries="@array/tls_version_entries"
app:entryValues="@array/tls_version_values"
app:defaultValue="TLSv1.3"
app:useSimpleSummaryProvider="true"
app:title="@string/tls_version" />
<SeekBarPreference <SeekBarPreference
android:defaultValue="10" android:defaultValue="10"
android:max="60" android:max="60"