Skip to content

Commit

Permalink
Add IDN support, fix #17
Browse files Browse the repository at this point in the history
Update ktor
  • Loading branch information
Under-estimate committed Jul 5, 2023
1 parent 63cab02 commit bfacb19
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 11 deletions.
9 changes: 5 additions & 4 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,21 @@ plugins {
}

group = "org.zrnq"
version = "1.1.14"
version = "1.1.15"

repositories {
mavenCentral()
maven("https://maven.aliyun.com/repository/public")
mavenCentral()
}


dependencies {
implementation(kotlin("reflect"))
implementation("com.alibaba:fastjson:1.2.83")
implementation("dnsjava:dnsjava:3.5.0")
implementation("io.ktor:ktor-server-core:2.2.2")
implementation("io.ktor:ktor-server-netty:2.2.2")
implementation("io.ktor:ktor-server-core:2.3.1")
implementation("io.ktor:ktor-server-netty:2.3.1")
implementation("org.gnu.inet:libidn:1.15")
}

tasks.create("CopyToLib", Copy::class) {
Expand Down
9 changes: 5 additions & 4 deletions src/main/kotlin/org/zrnq/mclient/MClient.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package org.zrnq.mclient

import gnu.inet.encoding.IDNA
import org.xbill.DNS.*
import org.zrnq.mclient.output.AbstractOutputHandler
import java.awt.Color
import java.awt.RenderingHints
import java.awt.image.BufferedImage
import java.lang.StringBuilder
import java.net.Inet4Address
import java.net.InetSocketAddress
import java.net.Socket
Expand All @@ -28,15 +28,16 @@ fun pingInternal(target : String, outputHandler : AbstractOutputHandler) {
try {
outputHandler.beforePing()
val option = target.split(":")
val encodedDomain = IDNA.toASCII(option[0])
val addressList = mutableListOf<Pair<String, Int>>()
val nameSet = mutableSetOf<String>()
if(option.size > 1) {
addressList.add(option[0] to option[1].toInt())
addressList.add(encodedDomain to option[1].toInt())
} else {
addressList.add(option[0] to 25565)
addressList.add(encodedDomain to 25565)
for(dnsResolver in dnsResolvers) {
runCatching {
dnsResolver.query("$addressPrefix${option[0]}.")
dnsResolver.query("$addressPrefix${encodedDomain}.")
}.fold({
it.forEach { rec ->
check(rec is SRVRecord)
Expand Down
15 changes: 14 additions & 1 deletion src/main/kotlin/org/zrnq/mclient/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package org.zrnq.mclient
import com.alibaba.fastjson.JSON
import com.alibaba.fastjson.JSONArray
import com.alibaba.fastjson.JSONObject
import gnu.inet.encoding.IDNA
import java.awt.Color
import java.awt.Graphics2D
import java.awt.Toolkit
Expand Down Expand Up @@ -211,4 +212,16 @@ val colorSequence = listOf(
"#ff55ff",
"#ffff55",
"#ffffff"
)
)


fun String.isValidURL() : Boolean {
if(!this.matches(Regex("^[^:.]+(\\.[^:.]+)+(:[0-9]{1,5})?$"))) return false
val segment = this.split(":")
return try {
IDNA.toASCII(segment[0])
true
} catch (e : Exception) {
false
}
}
6 changes: 6 additions & 0 deletions src/main/kotlin/org/zrnq/mclient/output/OutputHandlers.kt
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ class GUIOutputHandler : AbstractOutputHandler() {
addKeyListener(object : KeyAdapter() {
override fun keyPressed(e : KeyEvent) {
if(e.keyCode != KeyEvent.VK_ENTER) return
if(!text.isValidURL()) {
resultLabel.icon = null
resultLabel.text = "Invalid URL"
mainFrame.pack()
return
}
thread {
pingInternal(text, this@GUIOutputHandler)
progress.isVisible = false
Expand Down
5 changes: 3 additions & 2 deletions src/main/kotlin/org/zrnq/mcmotd/QueryCommand.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import net.mamoe.mirai.console.command.SimpleCommand
import net.mamoe.mirai.console.util.sendAnsiMessage
import net.mamoe.mirai.message.data.At
import net.mamoe.mirai.utils.ExternalResource.Companion.toExternalResource
import org.zrnq.mclient.isValidURL
import org.zrnq.mclient.output.APIOutputHandler
import org.zrnq.mclient.renderBasicInfoImage
import org.zrnq.mclient.secondToReadableTime
Expand Down Expand Up @@ -46,7 +47,7 @@ object QueryCommand : SimpleCommand(McMotd, "mcp", description = "获取指定M
}
}
}
if(target.matches(Regex("^[a-zA-Z0-9\\-_]+\\.[a-zA-Z0-9\\-_.]+[a-zA-Z0-9\\-_](:[0-9]{1,5})?$")))
if(target.isValidURL())
doPing(target)
else
reply("服务器地址格式错误,请指定形如: mc.example.com 或者 mc.example.com:25565 的地址")
Expand Down Expand Up @@ -74,7 +75,7 @@ object BindCommand : SimpleCommand(McMotd, "mcadd", description = "为当前群
reply("服务器名称已存在:$name")
return
}
if(!address.matches(Regex("^[a-zA-Z0-9\\-_]+\\.[a-zA-Z0-9\\-_.]+[a-zA-Z0-9\\-_](:[0-9]{1,5})?$"))) {
if(!address.isValidURL()) {
reply("服务器地址格式错误,请指定形如: mc.example.com 或者 mc.example.com:25565 的地址")
return
}
Expand Down

0 comments on commit bfacb19

Please sign in to comment.