74 lines
2.4 KiB
Text
74 lines
2.4 KiB
Text
import java.net.URI
|
|
import javax.xml.parsers.DocumentBuilderFactory
|
|
import org.w3c.dom.Element
|
|
import org.w3c.dom.Node
|
|
|
|
plugins {
|
|
id("buildlogic.kotlin-library-conventions")
|
|
}
|
|
|
|
dependencies {
|
|
api(project(":packages:serialize"))
|
|
implementation(project(":packages:rawr"))
|
|
implementation("commons-net:commons-net:3.11.1")
|
|
}
|
|
|
|
tasks.register("whois") {
|
|
group = "generation"
|
|
description = "Fetches latest WHOIS server mapping and generates WhoisServers.kt in source."
|
|
|
|
val outputDirectory = file("src/main/kotlin/rip/crit/commonip/proprietary")
|
|
outputs.file(outputDirectory.resolve("WhoisServers.kt"))
|
|
|
|
doLast {
|
|
val xmlUrl =
|
|
"https://raw.githubusercontent.com/whois-server-list/whois-server-list/refs/heads/master/whois-server-list.xml"
|
|
|
|
val inputStream = URI(xmlUrl).toURL().openStream()
|
|
val dbFactory = DocumentBuilderFactory.newInstance()
|
|
val dBuilder = dbFactory.newDocumentBuilder()
|
|
val xmlDoc = dBuilder.parse(inputStream)
|
|
xmlDoc.documentElement.normalize()
|
|
|
|
val serverMap = mutableMapOf<String, String>()
|
|
val domainNodes = xmlDoc.getElementsByTagName("domain")
|
|
|
|
for (i in 0 until domainNodes.length) {
|
|
val domainNode = domainNodes.item(i)
|
|
if (domainNode.nodeType == Node.ELEMENT_NODE) {
|
|
val domainElement = domainNode as Element
|
|
val domainName = domainElement.getAttribute("name")
|
|
|
|
val whoisServerNodes = domainElement.getElementsByTagName("whoisServer")
|
|
if (whoisServerNodes.length > 0) {
|
|
val whoisServerElement = whoisServerNodes.item(0) as Element
|
|
val host = whoisServerElement.getAttribute("host")
|
|
if (host.isNotBlank() && domainName.isNotBlank()) {
|
|
serverMap[domainName] = host
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
val mapEntries = StringBuilder()
|
|
serverMap.forEach { (tld, server) ->
|
|
mapEntries.append(" \"$tld\" to \"$server\",\n")
|
|
}
|
|
|
|
val outputFile = outputDirectory.resolve("WhoisServers.kt")
|
|
outputFile.parentFile.mkdirs()
|
|
outputFile.writeText(
|
|
"""
|
|
package rip.crit.commonip.proprietary
|
|
|
|
// Auto-generated file. Do not edit directly.
|
|
object WhoisServers {
|
|
val map: Map<String, String> = mapOf(
|
|
$mapEntries )
|
|
}
|
|
"""
|
|
.trimIndent()
|
|
)
|
|
println("Successfully generated ${outputFile.path} with ${serverMap.size} active TLD servers.")
|
|
}
|
|
}
|