Removes local relays from NIP65 lists.
This commit is contained in:
@@ -21,6 +21,7 @@
|
|||||||
package com.vitorpamplona.quartz.nip01Core.relay.filters
|
package com.vitorpamplona.quartz.nip01Core.relay.filters
|
||||||
|
|
||||||
import com.vitorpamplona.quartz.nip01Core.core.Event
|
import com.vitorpamplona.quartz.nip01Core.core.Event
|
||||||
|
import com.vitorpamplona.quartz.nip01Core.tags.addressables.Address
|
||||||
|
|
||||||
class Filter(
|
class Filter(
|
||||||
val ids: List<String>? = null,
|
val ids: List<String>? = null,
|
||||||
@@ -46,4 +47,23 @@ class Filter(
|
|||||||
limit: Int? = this.limit,
|
limit: Int? = this.limit,
|
||||||
search: String? = this.search,
|
search: String? = this.search,
|
||||||
) = Filter(ids, authors, kinds, tags, since, until, limit, search)
|
) = Filter(ids, authors, kinds, tags, since, until, limit, search)
|
||||||
|
|
||||||
|
init {
|
||||||
|
// TODO: Remove this in production
|
||||||
|
ids?.forEach {
|
||||||
|
if (it.length != 64) throw IllegalArgumentException("Invalid id length $it on ${toJson()}")
|
||||||
|
}
|
||||||
|
authors?.forEach {
|
||||||
|
if (it.length != 64) throw IllegalArgumentException("Invalid author length $it on ${toJson()}")
|
||||||
|
}
|
||||||
|
tags?.get("#p")?.forEach {
|
||||||
|
if (it.length != 64) throw IllegalArgumentException("Invalid p-tag length $it on ${toJson()}")
|
||||||
|
}
|
||||||
|
tags?.get("#e")?.forEach {
|
||||||
|
if (it.length != 64) throw IllegalArgumentException("Invalid e-tag length $it on ${toJson()}")
|
||||||
|
}
|
||||||
|
tags?.get("#a")?.forEach {
|
||||||
|
if (Address.parse(it) != null) throw IllegalArgumentException("Invalid a-tag length $it on ${toJson()}")
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+2
-2
@@ -41,6 +41,6 @@ fun NormalizedRelayUrl.toHttp() =
|
|||||||
"https://$url"
|
"https://$url"
|
||||||
}
|
}
|
||||||
|
|
||||||
fun NormalizedRelayUrl.isOnion() = url.endsWith(".onion/")
|
fun NormalizedRelayUrl.isOnion() = url.contains(".onion/")
|
||||||
|
|
||||||
fun NormalizedRelayUrl.isLocalHost() = url.contains("127.0.0.1") || url.contains("localhost") || url.contains(".local:")
|
fun NormalizedRelayUrl.isLocalHost() = RelayUrlNormalizer.isLocalHost(this.url)
|
||||||
|
|||||||
+7
-2
@@ -27,9 +27,14 @@ val normalizedUrls = LruCache<String, NormalizedRelayUrl>(5000)
|
|||||||
|
|
||||||
class RelayUrlNormalizer {
|
class RelayUrlNormalizer {
|
||||||
companion object {
|
companion object {
|
||||||
fun isLocalHost(url: String) = url.contains("127.0.0.1") || url.contains("localhost") || url.contains(".local:")
|
fun isLocalHost(url: String) =
|
||||||
|
url.contains("127.0.0.1") ||
|
||||||
|
url.contains("localhost") ||
|
||||||
|
url.contains("//umbrel:") ||
|
||||||
|
url.contains("192.168.") ||
|
||||||
|
url.contains(".local:")
|
||||||
|
|
||||||
fun isOnion(url: String) = url.endsWith(".onion") || url.endsWith(".onion/")
|
fun isOnion(url: String) = url.endsWith(".onion") || url.contains(".onion/")
|
||||||
|
|
||||||
private fun norm(url: String) =
|
private fun norm(url: String) =
|
||||||
NormalizedRelayUrl(
|
NormalizedRelayUrl(
|
||||||
|
|||||||
@@ -23,6 +23,7 @@ package com.vitorpamplona.quartz.nip17Dm.settings.tags
|
|||||||
import com.vitorpamplona.quartz.nip01Core.core.has
|
import com.vitorpamplona.quartz.nip01Core.core.has
|
||||||
import com.vitorpamplona.quartz.nip01Core.relay.normalizer.NormalizedRelayUrl
|
import com.vitorpamplona.quartz.nip01Core.relay.normalizer.NormalizedRelayUrl
|
||||||
import com.vitorpamplona.quartz.nip01Core.relay.normalizer.RelayUrlNormalizer
|
import com.vitorpamplona.quartz.nip01Core.relay.normalizer.RelayUrlNormalizer
|
||||||
|
import com.vitorpamplona.quartz.nip01Core.relay.normalizer.isLocalHost
|
||||||
import com.vitorpamplona.quartz.utils.ensure
|
import com.vitorpamplona.quartz.utils.ensure
|
||||||
|
|
||||||
class RelayTag {
|
class RelayTag {
|
||||||
@@ -41,7 +42,9 @@ class RelayTag {
|
|||||||
ensure(tag[0] == TAG_NAME) { return null }
|
ensure(tag[0] == TAG_NAME) { return null }
|
||||||
ensure(tag[1].isNotEmpty()) { return null }
|
ensure(tag[1].isNotEmpty()) { return null }
|
||||||
|
|
||||||
val relay = RelayUrlNormalizer.normalizeOrNull(tag[1]) ?: return null
|
val relay = RelayUrlNormalizer.normalizeOrNull(tag[1])
|
||||||
|
|
||||||
|
ensure(relay != null && !relay.isLocalHost()) { return null }
|
||||||
|
|
||||||
return relay
|
return relay
|
||||||
}
|
}
|
||||||
|
|||||||
+10
-1
@@ -24,6 +24,7 @@ import androidx.compose.runtime.Immutable
|
|||||||
import com.vitorpamplona.quartz.nip01Core.core.has
|
import com.vitorpamplona.quartz.nip01Core.core.has
|
||||||
import com.vitorpamplona.quartz.nip01Core.relay.normalizer.NormalizedRelayUrl
|
import com.vitorpamplona.quartz.nip01Core.relay.normalizer.NormalizedRelayUrl
|
||||||
import com.vitorpamplona.quartz.nip01Core.relay.normalizer.RelayUrlNormalizer
|
import com.vitorpamplona.quartz.nip01Core.relay.normalizer.RelayUrlNormalizer
|
||||||
|
import com.vitorpamplona.quartz.nip01Core.relay.normalizer.isLocalHost
|
||||||
import com.vitorpamplona.quartz.utils.ensure
|
import com.vitorpamplona.quartz.utils.ensure
|
||||||
import kotlin.text.isBlank
|
import kotlin.text.isBlank
|
||||||
|
|
||||||
@@ -79,6 +80,10 @@ class AdvertisedRelayInfo(
|
|||||||
ensure(match(tag)) { return null }
|
ensure(match(tag)) { return null }
|
||||||
ensure(AdvertisedRelayType.isRead(tag.getOrNull(2))) { return null }
|
ensure(AdvertisedRelayType.isRead(tag.getOrNull(2))) { return null }
|
||||||
|
|
||||||
|
val relay = RelayUrlNormalizer.normalizeOrNull(tag[1])
|
||||||
|
|
||||||
|
ensure(relay != null && !relay.isLocalHost()) { return null }
|
||||||
|
|
||||||
return RelayUrlNormalizer.normalizeOrNull(tag[1])
|
return RelayUrlNormalizer.normalizeOrNull(tag[1])
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -87,7 +92,11 @@ class AdvertisedRelayInfo(
|
|||||||
ensure(match(tag)) { return null }
|
ensure(match(tag)) { return null }
|
||||||
ensure(AdvertisedRelayType.isWrite(tag.getOrNull(2))) { return null }
|
ensure(AdvertisedRelayType.isWrite(tag.getOrNull(2))) { return null }
|
||||||
|
|
||||||
return RelayUrlNormalizer.normalizeOrNull(tag[1])
|
val relay = RelayUrlNormalizer.normalizeOrNull(tag[1])
|
||||||
|
|
||||||
|
ensure(relay != null && !relay.isLocalHost()) { return null }
|
||||||
|
|
||||||
|
return relay
|
||||||
}
|
}
|
||||||
|
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
|
|||||||
Reference in New Issue
Block a user