feat: add ip geo
This commit is contained in:
2 files changed
+46
-12
No files matched your search
@@ -9,6 +9,8 @@ package cn.rtast.fancybot.commands.lookup
|
|||||||
|
|
||||||
import cn.rtast.fancybot.annotations.CommandDescription
|
import cn.rtast.fancybot.annotations.CommandDescription
|
||||||
import cn.rtast.fancybot.configManager
|
import cn.rtast.fancybot.configManager
|
||||||
|
import cn.rtast.fancybot.entity.IPGeo
|
||||||
|
import cn.rtast.fancybot.util.Http
|
||||||
import cn.rtast.rob.entity.GroupMessage
|
import cn.rtast.rob.entity.GroupMessage
|
||||||
import cn.rtast.rob.util.BaseCommand
|
import cn.rtast.rob.util.BaseCommand
|
||||||
import cn.rtast.rob.util.ob.MessageChain
|
import cn.rtast.rob.util.ob.MessageChain
|
||||||
@@ -27,12 +29,29 @@ class NslookupCommand : BaseCommand() {
|
|||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
private const val MC_SRV_PREFIX = "_minecraft._tcp"
|
private const val MC_SRV_PREFIX = "_minecraft._tcp"
|
||||||
|
private const val IP_INFO_API = "https://ipinfo.io/###/json"
|
||||||
private val env = Hashtable<String, String>().also {
|
private val env = Hashtable<String, String>().also {
|
||||||
it["java.naming.factory.initial"] = "com.sun.jndi.dns.DnsContextFactory"
|
it["java.naming.factory.initial"] = "com.sun.jndi.dns.DnsContextFactory"
|
||||||
}
|
}
|
||||||
private val dirContext = InitialDirContext(env)
|
private val dirContext = InitialDirContext(env)
|
||||||
|
|
||||||
|
private fun srv(host: String): MutableList<String> {
|
||||||
|
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
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun geo(ip: String) = Http.get<IPGeo>(IP_INFO_API.replace("###", ip))
|
||||||
|
|
||||||
override suspend fun executeGroup(listener: OneBotListener, message: GroupMessage, args: List<String>) {
|
override suspend fun executeGroup(listener: OneBotListener, message: GroupMessage, args: List<String>) {
|
||||||
val host = args.first()
|
val host = args.first()
|
||||||
val nsType = if (args.size == 3) args[1] else "common"
|
val nsType = if (args.size == 3) args[1] else "common"
|
||||||
@@ -41,16 +60,7 @@ class NslookupCommand : BaseCommand() {
|
|||||||
if (nsType == "srv") {
|
if (nsType == "srv") {
|
||||||
when (srvType) {
|
when (srvType) {
|
||||||
"mc" -> {
|
"mc" -> {
|
||||||
val srvDomain = "$MC_SRV_PREFIX.$host"
|
val srvRecords = srv(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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
val messages = mutableListOf<MessageChain>()
|
val messages = mutableListOf<MessageChain>()
|
||||||
srvRecords.forEach {
|
srvRecords.forEach {
|
||||||
val srv = it.split(" ")
|
val srv = it.split(" ")
|
||||||
@@ -58,10 +68,16 @@ class NslookupCommand : BaseCommand() {
|
|||||||
val weight = srv[1].toInt()
|
val weight = srv[1].toInt()
|
||||||
val port = srv[2].toInt()
|
val port = srv[2].toInt()
|
||||||
val host = srv.last()
|
val host = srv.last()
|
||||||
|
val ip = InetAddress.getByName(host).hostAddress
|
||||||
|
val geo = this.geo(ip)
|
||||||
val msg = MessageChain.Builder()
|
val msg = MessageChain.Builder()
|
||||||
.addText("解析后的地址: $host:$port")
|
.addText("解析后的地址: $host:$port")
|
||||||
.addNewLine()
|
.addNewLine()
|
||||||
.addText("权重: $weight | 优先级: $priority")
|
.addText("权重: $weight | 优先级: $priority")
|
||||||
|
.addNewLine()
|
||||||
|
.addText("$host -> $ip")
|
||||||
|
.addNewLine()
|
||||||
|
.addText("国家: ${geo.country} 地区: ${geo.region} 城市: ${geo.city} | 所属组织: ${geo.org}")
|
||||||
.build()
|
.build()
|
||||||
messages.add(msg)
|
messages.add(msg)
|
||||||
}
|
}
|
||||||
@@ -69,9 +85,12 @@ class NslookupCommand : BaseCommand() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
val ip = withContext(Dispatchers.IO) { InetAddress.getByName(host) }.hostAddress
|
||||||
|
val geo = this.geo(ip)
|
||||||
val msg = MessageChain.Builder()
|
val msg = MessageChain.Builder()
|
||||||
.addText("${host}解析后的IP地址为: ")
|
.addText("$host -> $ip")
|
||||||
.addText(withContext(Dispatchers.IO) { InetAddress.getByName(host) }.hostAddress)
|
.addNewLine()
|
||||||
|
.addText("国家: ${geo.country} 地区: ${geo.region} 城市: ${geo.city} | 所属组织: ${geo.org}")
|
||||||
.build()
|
.build()
|
||||||
message.reply(msg)
|
message.reply(msg)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,15 @@
|
|||||||
|
/*
|
||||||
|
* Copyright © 2024 RTAkland
|
||||||
|
* Author: RTAkland
|
||||||
|
* Date: 2024/10/23
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
package cn.rtast.fancybot.entity
|
||||||
|
|
||||||
|
data class IPGeo(
|
||||||
|
val city: String,
|
||||||
|
val region: String,
|
||||||
|
val country: String,
|
||||||
|
val org: String,
|
||||||
|
)
|
||||||
Reference in New Issue
Block a user