mirror of
https://github.com/vitorpamplona/amethyst.git
synced 2025-10-09 19:22:39 +02:00
Fixes more random relay hint errors
This commit is contained in:
@@ -24,6 +24,7 @@ import android.util.Log
|
|||||||
import androidx.collection.LruCache
|
import androidx.collection.LruCache
|
||||||
import kotlinx.coroutines.CancellationException
|
import kotlinx.coroutines.CancellationException
|
||||||
import org.czeal.rfc3986.URIReference
|
import org.czeal.rfc3986.URIReference
|
||||||
|
import java.lang.RuntimeException
|
||||||
import kotlin.contracts.ExperimentalContracts
|
import kotlin.contracts.ExperimentalContracts
|
||||||
|
|
||||||
val normalizedUrls = LruCache<String, NormalizedRelayUrl>(5000)
|
val normalizedUrls = LruCache<String, NormalizedRelayUrl>(5000)
|
||||||
@@ -53,7 +54,14 @@ class RelayUrlNormalizer {
|
|||||||
fun isHttpSuffix(url: String) = url[4] == ':' && url[5] == '/' && url[6] == '/'
|
fun isHttpSuffix(url: String) = url[4] == ':' && url[5] == '/' && url[6] == '/'
|
||||||
|
|
||||||
fun isRelayUrl(url: String): Boolean {
|
fun isRelayUrl(url: String): Boolean {
|
||||||
val trimmed = url.trim().ifEmpty { return false }
|
if (url.length < 3) return false
|
||||||
|
|
||||||
|
val trimmed =
|
||||||
|
if (url[0].isWhitespace() || url[url.length - 1].isWhitespace()) {
|
||||||
|
url.trim()
|
||||||
|
} else {
|
||||||
|
url
|
||||||
|
}
|
||||||
|
|
||||||
// fast
|
// fast
|
||||||
if (isRelaySchemePrefix(trimmed)) {
|
if (isRelaySchemePrefix(trimmed)) {
|
||||||
@@ -78,9 +86,14 @@ class RelayUrlNormalizer {
|
|||||||
|
|
||||||
@OptIn(ExperimentalContracts::class)
|
@OptIn(ExperimentalContracts::class)
|
||||||
fun fix(url: String): String? {
|
fun fix(url: String): String? {
|
||||||
val trimmed = url.trim()
|
if (url.length < 3) return null
|
||||||
|
|
||||||
if (trimmed.isEmpty()) return null
|
val trimmed =
|
||||||
|
if (url[0].isWhitespace() || url[url.length - 1].isWhitespace()) {
|
||||||
|
url.trim()
|
||||||
|
} else {
|
||||||
|
url
|
||||||
|
}
|
||||||
|
|
||||||
// fast for good wss:// urls
|
// fast for good wss:// urls
|
||||||
if (isRelaySchemePrefix(trimmed)) {
|
if (isRelaySchemePrefix(trimmed)) {
|
||||||
@@ -100,12 +113,30 @@ class RelayUrlNormalizer {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// fast for good ww:// urls
|
||||||
|
if (trimmed.startsWith("ww://")) {
|
||||||
|
return "wss://${trimmed.drop(5)}"
|
||||||
|
}
|
||||||
|
|
||||||
|
// fast for good ww:// urls
|
||||||
|
if (trimmed.startsWith("was://")) {
|
||||||
|
return "wss://${trimmed.drop(6)}"
|
||||||
|
}
|
||||||
|
|
||||||
|
// fast for good ww:// urls
|
||||||
|
if (trimmed.startsWith("Wws://")) {
|
||||||
|
return "wss://${trimmed.drop(6)}"
|
||||||
|
}
|
||||||
|
|
||||||
if (trimmed.contains("://")) {
|
if (trimmed.contains("://")) {
|
||||||
// some other scheme we cannot connect to.
|
// some other scheme we cannot connect to.
|
||||||
Log.w("RelayUrlNormalizer", "Rejected relay URL: $url")
|
Log.w("RelayUrlNormalizer", "Rejected relay URL: $url")
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Log.w("RelayUrlNormalizer", "Rejected relay URL: $url")
|
||||||
|
throw RuntimeException("RelayError $trimmed")
|
||||||
|
|
||||||
return if (isOnion(trimmed) || isLocalHost(trimmed)) {
|
return if (isOnion(trimmed) || isLocalHost(trimmed)) {
|
||||||
"ws://$trimmed"
|
"ws://$trimmed"
|
||||||
} else {
|
} else {
|
||||||
@@ -114,7 +145,7 @@ class RelayUrlNormalizer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun normalize(url: String): NormalizedRelayUrl {
|
fun normalize(url: String): NormalizedRelayUrl {
|
||||||
normalizedUrls.get(url)?.let { return it }
|
normalizedUrls[url]?.let { return it }
|
||||||
|
|
||||||
return try {
|
return try {
|
||||||
val fixed = fix(url) ?: return NormalizedRelayUrl(url)
|
val fixed = fix(url) ?: return NormalizedRelayUrl(url)
|
||||||
@@ -142,6 +173,7 @@ class RelayUrlNormalizer {
|
|||||||
}
|
}
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
if (e is CancellationException) throw e
|
if (e is CancellationException) throw e
|
||||||
|
e.printStackTrace()
|
||||||
Log.w("NormalizedRelayUrl", "Rejected Error $url")
|
Log.w("NormalizedRelayUrl", "Rejected Error $url")
|
||||||
null
|
null
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user