Merge pull request #16 from SKBotNL/main
Support wildcard domain blocklists
This commit was merged in pull request #16.
This commit is contained in:
7 files changed
+60
-26
No files matched your search
@@ -1,6 +1,3 @@
|
||||
[submodule "libs/Utilities-OG"]
|
||||
path = libs/Utilities-OG
|
||||
url = https://github.com/true-og/Utilities-OG
|
||||
[submodule "libs/Vanish-OG"]
|
||||
path = libs/Vanish-OG
|
||||
url = https://github.com/true-og/Vanish-OG
|
||||
+3
-3
@@ -7,7 +7,7 @@ plugins {
|
||||
id("com.diffplug.spotless") version "8.1.0" // Import Spotless plugin.
|
||||
id("com.gradleup.shadow") version "8.3.9" // Import Shadow plugin.
|
||||
eclipse // Import Eclipse plugin.
|
||||
kotlin("jvm") version "2.1.21" // Import Kotlin JVM plugin.
|
||||
kotlin("jvm") version "2.4.10" // Import Kotlin JVM plugin.
|
||||
}
|
||||
|
||||
/* ---------------------------- Java / Kotlin -------------------------- */
|
||||
@@ -54,8 +54,8 @@ dependencies {
|
||||
compileOnly(
|
||||
files("libs/AnnouncerPlus-1.4.1.jar")
|
||||
) // Import AnnouncerPlus API (binary-compatible with AnnouncerPlus-OG API).
|
||||
compileOnlyApi(project(":libs:Utilities-OG")) // Import TrueOG Network Utilities-OG Java API (from source).
|
||||
compileOnlyApi(project(":libs:Vanish-OG")) // Import TrueOG Network Vanish-OG Java API (from source).
|
||||
compileOnlyApi("net.trueog.utilities-og:Utilities-OG") // Import TrueOG Network Utilities-OG Java API (from source).
|
||||
compileOnlyApi("de.myzelyam.vanishog:Vanish-OG") // Import TrueOG Network Vanish-OG Java API (from source).
|
||||
compileOnly("net.luckperms:api:5.5") // Import LuckPerms API.
|
||||
compileOnly(
|
||||
"io.github.miniplaceholders:miniplaceholders-api:2.2.3"
|
||||
|
||||
Submodule libs/Utilities-OG deleted from b5a370e124.
+1
-7
@@ -4,10 +4,4 @@ ProcessBuilder("sh", "bootstrap.sh").directory(rootDir).inheritIO().start().let
|
||||
if (it.waitFor() != 0) throw GradleException("bootstrap.sh failed")
|
||||
}
|
||||
|
||||
file("libs")
|
||||
.listFiles()
|
||||
?.filter { it.isDirectory && !it.name.startsWith(".") }
|
||||
?.forEach { dir ->
|
||||
include(":libs:${dir.name}")
|
||||
project(":libs:${dir.name}").projectDir = dir
|
||||
}
|
||||
file("libs").listFiles()?.filter { it.isDirectory && !it.name.startsWith(".") }?.forEach { dir -> includeBuild(dir) }
|
||||
@@ -1,7 +1,8 @@
|
||||
package nl.skbotnl.chatog.util
|
||||
|
||||
import java.io.IOException
|
||||
import java.net.URI
|
||||
import java.util.concurrent.TimeUnit
|
||||
import kotlin.time.Duration.Companion.days
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.launch
|
||||
import nl.skbotnl.chatog.ChatOG.Companion.config
|
||||
@@ -10,16 +11,16 @@ import nl.skbotnl.chatog.ChatOG.Companion.scope
|
||||
|
||||
internal class BlocklistManager {
|
||||
private val lock = Any()
|
||||
private val blockList = mutableListOf<String>()
|
||||
private val blockedDomainTrie = DomainTrie()
|
||||
|
||||
init {
|
||||
plugin.logger.info("Loading the blocklists...")
|
||||
refresh()
|
||||
plugin.logger.info("Loaded the blocklists!")
|
||||
|
||||
scope.launch {
|
||||
while (true) {
|
||||
delay(TimeUnit.DAYS.toMillis(1))
|
||||
delay(1.days)
|
||||
plugin.logger.info("Refreshing the blocklists...")
|
||||
refresh()
|
||||
}
|
||||
}
|
||||
@@ -27,15 +28,21 @@ internal class BlocklistManager {
|
||||
|
||||
private fun refresh() {
|
||||
synchronized(lock) {
|
||||
blockList.clear()
|
||||
blockedDomainTrie.clear()
|
||||
config.blocklist.blocklists.forEach { blocklist ->
|
||||
URI(blocklist).toURL().openStream().use { input ->
|
||||
input.bufferedReader().use { bufferedReader ->
|
||||
bufferedReader.lines().forEach { if (!it.startsWith("#")) blockList.add(it) }
|
||||
try {
|
||||
URI(blocklist).toURL().openStream().use { input ->
|
||||
input.bufferedReader().use { bufferedReader ->
|
||||
bufferedReader.lines().forEach { if (!it.startsWith("#")) blockedDomainTrie.insert(it) }
|
||||
}
|
||||
}
|
||||
} catch (_: IOException) {
|
||||
plugin.logger.severe("Failed to load the blocklists")
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
plugin.logger.info("Loaded the blocklists!")
|
||||
}
|
||||
|
||||
private val urlRegex = Regex("(?:https?://)?([a-zA-Z0-9.-]+\\.[a-zA-Z]{2,})")
|
||||
@@ -43,6 +50,6 @@ internal class BlocklistManager {
|
||||
fun checkUrl(url: String): Boolean {
|
||||
val match = urlRegex.find(url) ?: return false
|
||||
val baseUrl = match.groups[1]?.value ?: return false
|
||||
return synchronized(lock) { blockList.contains(baseUrl) }
|
||||
return synchronized(lock) { blockedDomainTrie.contains(baseUrl) }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package nl.skbotnl.chatog.util
|
||||
|
||||
class DomainTrie {
|
||||
class Node : HashMap<String, Node>()
|
||||
|
||||
private val root = Node()
|
||||
|
||||
fun insert(domain: String) {
|
||||
var node = root
|
||||
val labels = domain.split('.').asReversed()
|
||||
labels.forEachIndexed { index, label ->
|
||||
node =
|
||||
if (index == labels.lastIndex) {
|
||||
Node().also { node[label] = it }
|
||||
} else {
|
||||
val child = node[label]
|
||||
if (child != null) {
|
||||
if (child.isEmpty()) return // Terminal node, subdomains already handled
|
||||
child
|
||||
} else {
|
||||
Node().also { node[label] = it }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun contains(domain: String): Boolean {
|
||||
var node = root
|
||||
for (label in domain.split('.').asReversed()) {
|
||||
node = node[label] ?: return false
|
||||
if (node.isEmpty()) return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
fun clear() = root.clear()
|
||||
}
|
||||
@@ -4,10 +4,10 @@ prefix: <dark_gray>[<green>Chat<white>-<dark_red>OG<dark_gray>]
|
||||
|
||||
blocklist:
|
||||
enabled: true
|
||||
# Only blocklists with just a list of domains
|
||||
# Only wildcard domain blocklists are supported
|
||||
blocklists:
|
||||
- https://raw.githubusercontent.com/hagezi/dns-blocklists/main/domains/multi.txt
|
||||
- https://raw.githubusercontent.com/hagezi/dns-blocklists/main/wildcard/nsfw-onlydomains.txt
|
||||
- https://cdn.jsdelivr.net/gh/hagezi/dns-blocklists@latest/wildcard/multi-onlydomains.txt
|
||||
- https://cdn.jsdelivr.net/gh/hagezi/dns-blocklists@latest/wildcard/nsfw-onlydomains.txt
|
||||
|
||||
# ---Redis---
|
||||
redis:
|
||||
|
||||
Reference in new issue
Block a user