dns fix
This commit is contained in:
+6
-23
@@ -9,34 +9,18 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
func LookupMinecraftSRV(host string, port uint16) (hostRet string, portRet uint16) {
|
||||
hostRet = host // in case of error
|
||||
portRet = port
|
||||
if port != 25565 {
|
||||
return
|
||||
}
|
||||
_, srvs, err := net.LookupSRV("minecraft", "tcp", host)
|
||||
if err != nil || len(srvs) == 0 {
|
||||
return
|
||||
}
|
||||
target := strings.TrimSuffix(srvs[0].Target, ".")
|
||||
hostRet = target
|
||||
portRet = srvs[0].Port
|
||||
return
|
||||
}
|
||||
|
||||
type DNSCheckFunc func(addr string) error
|
||||
|
||||
// DoDns resolves a DNS name to an IP address
|
||||
// It should resolve a minecraft compatible address into something net.Dial
|
||||
func DoDns(in string) (out string, err error) {
|
||||
func DoDns(in string) (out string, host string, portN uint16, err error) {
|
||||
return DoDNSChecked(in, nil)
|
||||
}
|
||||
|
||||
// DoDNSChecked resolves a DNS name to an IP address
|
||||
// It should resolve a minecraft compatible address into something net.Dial
|
||||
// It also optionally accepts a function to "check" the address
|
||||
func DoDNSChecked(in string, f DNSCheckFunc) (out string, err error) { // Bad code should match minecraft
|
||||
func DoDNSChecked(in string, f DNSCheckFunc) (connectTo string, host string, portN uint16, err error) {
|
||||
// https://mcsrc.dev/1/26.1-pre-1/net/minecraft/client/multiplayer/resolver/ServerAddress#L34
|
||||
host, port, err := net.SplitHostPort(in)
|
||||
if err != nil {
|
||||
@@ -61,7 +45,7 @@ func DoDNSChecked(in string, f DNSCheckFunc) (out string, err error) { // Bad co
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
portN := uint16(porti)
|
||||
portN = uint16(porti)
|
||||
outIP, err := net.ResolveIPAddr("ip", host)
|
||||
if err != nil {
|
||||
return
|
||||
@@ -70,12 +54,11 @@ func DoDNSChecked(in string, f DNSCheckFunc) (out string, err error) { // Bad co
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
out = outIP.String() + ":" + port
|
||||
connectTo = outIP.String() + ":" + port
|
||||
if portN != 25565 {
|
||||
|
||||
return
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second) // otherwise this takes 10+ seconds
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second) // otherwise this takes 10+ seconds
|
||||
defer cancel()
|
||||
|
||||
r := &net.Resolver{}
|
||||
@@ -95,6 +78,6 @@ func DoDNSChecked(in string, f DNSCheckFunc) (out string, err error) { // Bad co
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
out = outIP.String() + ":" + port
|
||||
connectTo = outIP.String() + ":" + port
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user