Support SRV records in net.Dial and bot.PingAndListContext
This commit is contained in:
44
bot/mcbot.go
44
bot/mcbot.go
@ -6,7 +6,6 @@ package bot
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net"
|
||||
"strconv"
|
||||
|
||||
@ -18,55 +17,24 @@ import (
|
||||
|
||||
// ProtocolVersion is the protocol version number of minecraft net protocol
|
||||
const ProtocolVersion = 757
|
||||
const DefaultPort = 25565
|
||||
const DefaultPort = mcnet.DefaultPort
|
||||
|
||||
// JoinServer connect a Minecraft server for playing the game.
|
||||
// Using roughly the same way to parse address as minecraft.
|
||||
func (c *Client) JoinServer(addr string) (err error) {
|
||||
return c.join(&net.Dialer{}, addr)
|
||||
return c.join(context.Background(), &mcnet.DefaultDialer, addr)
|
||||
}
|
||||
|
||||
// JoinServerWithDialer is similar to JoinServer but using a Dialer.
|
||||
func (c *Client) JoinServerWithDialer(d *net.Dialer, addr string) (err error) {
|
||||
return c.join(d, addr)
|
||||
return c.join(context.Background(), &mcnet.Dialer{Dialer: d}, addr)
|
||||
}
|
||||
|
||||
// parseAddress will look up SRV records for the address
|
||||
func parseAddress(r *net.Resolver, addr string) (string, error) {
|
||||
var port uint16
|
||||
var addrErr *net.AddrError
|
||||
host, portStr, err := net.SplitHostPort(addr)
|
||||
if err != nil {
|
||||
if errors.As(err, &addrErr) {
|
||||
host, port = addr, DefaultPort
|
||||
} else {
|
||||
return "", err
|
||||
}
|
||||
} else {
|
||||
if portInt, err := strconv.ParseUint(portStr, 10, 16); err != nil {
|
||||
port = DefaultPort
|
||||
} else {
|
||||
port = uint16(portInt)
|
||||
}
|
||||
}
|
||||
|
||||
_, srvs, err := r.LookupSRV(context.TODO(), "minecraft", "tcp", host)
|
||||
if err == nil && len(srvs) > 0 {
|
||||
host, port = srvs[0].Target, srvs[0].Port
|
||||
}
|
||||
|
||||
return net.JoinHostPort(host, strconv.FormatUint(uint64(port), 10)), nil
|
||||
}
|
||||
|
||||
func (c *Client) join(d *net.Dialer, addr string) error {
|
||||
func (c *Client) join(ctx context.Context, d *mcnet.Dialer, addr string) error {
|
||||
const Handshake = 0x00
|
||||
addrSrv, err := parseAddress(d.Resolver, addr)
|
||||
if err != nil {
|
||||
return LoginErr{"resolved address", err}
|
||||
}
|
||||
|
||||
// Split Host and Port
|
||||
host, portStr, err := net.SplitHostPort(addrSrv)
|
||||
host, portStr, err := net.SplitHostPort(addr)
|
||||
if err != nil {
|
||||
return LoginErr{"split address", err}
|
||||
}
|
||||
@ -76,7 +44,7 @@ func (c *Client) join(d *net.Dialer, addr string) error {
|
||||
}
|
||||
|
||||
// Dial connection
|
||||
c.Conn, err = mcnet.DialMC(addrSrv)
|
||||
c.Conn, err = d.DialMCContext(ctx, addr)
|
||||
if err != nil {
|
||||
return LoginErr{"connect server", err}
|
||||
}
|
||||
|
@ -1,8 +1,11 @@
|
||||
package bot
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
"os"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
@ -16,53 +19,63 @@ import (
|
||||
//
|
||||
// For more information for JSON format, see https://wiki.vg/Server_List_Ping#Response
|
||||
func PingAndList(addr string) ([]byte, time.Duration, error) {
|
||||
addrSrv, err := parseAddress(&net.Resolver{}, addr)
|
||||
if err != nil {
|
||||
return nil, 0, LoginErr{"parse address", err}
|
||||
}
|
||||
|
||||
conn, err := mcnet.DialMC(addrSrv)
|
||||
conn, err := mcnet.DialMC(addr)
|
||||
if err != nil {
|
||||
return nil, 0, LoginErr{"dial connection", err}
|
||||
}
|
||||
return pingAndList(addr, conn)
|
||||
return pingAndList(context.Background(), addr, conn)
|
||||
}
|
||||
|
||||
// PingAndListTimeout is the version of PingAndList with max request time.
|
||||
func PingAndListTimeout(addr string, timeout time.Duration) ([]byte, time.Duration, error) {
|
||||
deadLine := time.Now().Add(timeout)
|
||||
ctx, cancel := context.WithTimeout(context.Background(), timeout)
|
||||
defer cancel()
|
||||
return PingAndListContext(ctx, addr)
|
||||
}
|
||||
|
||||
addrSrv, err := parseAddress(&net.Resolver{}, addr)
|
||||
if err != nil {
|
||||
return nil, 0, LoginErr{"parse address", err}
|
||||
}
|
||||
|
||||
conn, err := mcnet.DialMCTimeout(addrSrv, timeout)
|
||||
func PingAndListContext(ctx context.Context, addr string) ([]byte, time.Duration, error) {
|
||||
conn, err := mcnet.DefaultDialer.DialMCContext(ctx, addr)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
err = conn.Socket.SetDeadline(deadLine)
|
||||
if err != nil {
|
||||
return nil, 0, LoginErr{"set deadline", err}
|
||||
}
|
||||
|
||||
return pingAndList(addr, conn)
|
||||
return pingAndList(ctx, addr, conn)
|
||||
}
|
||||
|
||||
func pingAndList(addr string, conn *mcnet.Conn) ([]byte, time.Duration, error) {
|
||||
addrSrv, err := parseAddress(nil, addr)
|
||||
if err != nil {
|
||||
return nil, 0, LoginErr{"resolved address", err}
|
||||
func pingAndList(ctx context.Context, addr string, conn *mcnet.Conn) (data []byte, delay time.Duration, err error) {
|
||||
if deadline, hasDeadline := ctx.Deadline(); hasDeadline {
|
||||
if err := conn.Socket.SetDeadline(deadline); err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
defer func() {
|
||||
// Reset deadline
|
||||
if err2 := conn.Socket.SetDeadline(time.Time{}); err2 != nil {
|
||||
if err2 == nil {
|
||||
err = err2
|
||||
}
|
||||
return
|
||||
}
|
||||
// Map error type
|
||||
if errors.Is(err, os.ErrDeadlineExceeded) {
|
||||
err = context.DeadlineExceeded
|
||||
}
|
||||
}()
|
||||
}
|
||||
// Split Host and Port
|
||||
host, portStr, err := net.SplitHostPort(addrSrv)
|
||||
host, portStr, err := net.SplitHostPort(addr)
|
||||
var port uint64
|
||||
if err != nil {
|
||||
return nil, 0, LoginErr{"split address", err}
|
||||
}
|
||||
port, err := strconv.ParseUint(portStr, 0, 16)
|
||||
if err != nil {
|
||||
return nil, 0, LoginErr{"parse port", err}
|
||||
var addrErr *net.AddrError
|
||||
const missingPort = "missing port in address"
|
||||
if errors.As(err, &addrErr) && addrErr.Err == missingPort {
|
||||
host, port, err = addr, DefaultPort, nil
|
||||
} else {
|
||||
return nil, 0, LoginErr{"split address", err}
|
||||
}
|
||||
} else {
|
||||
port, err = strconv.ParseUint(portStr, 0, 16)
|
||||
if err != nil {
|
||||
return nil, 0, LoginErr{"parse port", err}
|
||||
}
|
||||
}
|
||||
|
||||
const Handshake = 0x00
|
||||
|
Reference in New Issue
Block a user