Files
FancyBot/src/main/kotlin/cn/rtast/fancybot/commands/lookup/NslookupCommand.kt
T

102 lines
4.1 KiB
Kotlin
Raw Normal View History

2024-09-06 18:03:14 +08:00
/*
* Copyright © 2024 RTAkland
* Author: RTAkland
* Date: 2024/9/6
*/
2024-09-13 12:44:10 +08:00
package cn.rtast.fancybot.commands.lookup
2024-09-06 18:03:14 +08:00
import cn.rtast.fancybot.annotations.CommandDescription
2024-10-21 17:58:11 +08:00
import cn.rtast.fancybot.configManager
2024-10-23 13:29:56 +08:00
import cn.rtast.fancybot.entity.IPGeo
import cn.rtast.fancybot.util.Http
2024-09-06 18:03:14 +08:00
import cn.rtast.rob.entity.GroupMessage
import cn.rtast.rob.util.BaseCommand
import cn.rtast.rob.util.ob.MessageChain
2024-11-07 19:09:08 +08:00
import cn.rtast.rob.util.ob.toNode
2024-09-25 18:18:28 +08:00
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
2024-09-06 18:03:14 +08:00
import java.net.InetAddress
2024-09-25 18:23:27 +08:00
import java.net.UnknownHostException
2024-10-21 17:58:11 +08:00
import java.util.Hashtable
import javax.naming.directory.InitialDirContext
2024-09-06 18:03:14 +08:00
@CommandDescription("解析域名获取IP地址")
2024-09-06 18:03:14 +08:00
class NslookupCommand : BaseCommand() {
override val commandNames = listOf("/ns")
2024-09-06 18:03:14 +08:00
2024-10-21 17:58:11 +08:00
companion object {
private const val MC_SRV_PREFIX = "_minecraft._tcp"
2024-10-23 13:29:56 +08:00
private const val IP_INFO_API = "https://ipinfo.io/###/json"
2024-10-21 17:58:11 +08:00
private val env = Hashtable<String, String>().also {
it["java.naming.factory.initial"] = "com.sun.jndi.dns.DnsContextFactory"
}
private val dirContext = InitialDirContext(env)
2024-10-23 13:29:56 +08:00
2024-10-23 13:32:46 +08:00
fun srv(host: String): MutableList<String> {
2024-10-23 13:29:56 +08:00
val srvDomain = "$MC_SRV_PREFIX.$host"
val attrs = dirContext.getAttributes(srvDomain, arrayOf("SRV"))
val srvRecords = mutableListOf<String>()
val srvAttr = attrs.get("SRV")
if (srvAttr != null) {
for (i in 0 until srvAttr.size()) {
val srvRecord = srvAttr.get(i) as String
srvRecords.add(srvRecord)
}
}
return srvRecords
}
2024-10-21 17:58:11 +08:00
}
2024-10-23 13:29:56 +08:00
private fun geo(ip: String) = Http.get<IPGeo>(IP_INFO_API.replace("###", ip))
2024-10-31 14:02:04 +08:00
override suspend fun executeGroup(message: GroupMessage, args: List<String>) {
2024-10-21 17:58:11 +08:00
val host = args.first()
2024-10-21 20:27:14 +08:00
val nsType = if (args.size == 3) args[1] else "common"
val srvType = if (args.size == 3) args.last() else "mc"
2024-09-25 18:23:27 +08:00
try {
2024-10-21 17:58:11 +08:00
if (nsType == "srv") {
when (srvType) {
"mc" -> {
2024-10-23 13:29:56 +08:00
val srvRecords = srv(host)
2024-10-21 17:58:11 +08:00
val messages = mutableListOf<MessageChain>()
srvRecords.forEach {
val srv = it.split(" ")
val priority = srv.first().toInt()
val weight = srv[1].toInt()
val port = srv[2].toInt()
val host = srv.last()
2024-10-23 13:29:56 +08:00
val ip = InetAddress.getByName(host).hostAddress
val geo = this.geo(ip)
2024-10-21 17:58:11 +08:00
val msg = MessageChain.Builder()
.addText("解析后的地址: $host:$port")
.addNewLine()
.addText("权重: $weight | 优先级: $priority")
2024-10-23 13:29:56 +08:00
.addNewLine()
.addText("$host -> $ip")
.addNewLine()
.addText("国家: ${geo.country} 地区: ${geo.region} 城市: ${geo.city} | 所属组织: ${geo.org}")
2024-10-21 17:58:11 +08:00
.build()
messages.add(msg)
}
2024-11-07 19:09:08 +08:00
message.reply(messages.toNode(configManager.selfId))
2024-10-21 17:58:11 +08:00
}
}
} else {
2024-10-23 13:29:56 +08:00
val ip = withContext(Dispatchers.IO) { InetAddress.getByName(host) }.hostAddress
val geo = this.geo(ip)
2024-10-21 17:58:11 +08:00
val msg = MessageChain.Builder()
2024-10-23 13:29:56 +08:00
.addText("$host -> $ip")
.addNewLine()
.addText("国家: ${geo.country} 地区: ${geo.region} 城市: ${geo.city} | 所属组织: ${geo.org}")
2024-10-21 17:58:11 +08:00
.build()
message.reply(msg)
}
2024-09-25 18:23:27 +08:00
} catch (_: UnknownHostException) {
message.reply("未知的主机名: $host")
} catch (e: Exception) {
message.reply("未知错误: ${e.message}")
}
2024-09-06 18:03:14 +08:00
}
}