Allow maxsupermanhd to disable public key sending or to instruct the KeyPair to be used which he got from external. close #227
This commit is contained in:
53
bot/mcbot.go
53
bot/mcbot.go
@ -24,19 +24,46 @@ const (
|
|||||||
DefaultPort = mcnet.DefaultPort
|
DefaultPort = mcnet.DefaultPort
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type JoinOptions struct {
|
||||||
|
Dialer *net.Dialer
|
||||||
|
Context context.Context
|
||||||
|
|
||||||
|
// Indicate not to fetch and sending player's PubKey
|
||||||
|
NoPublicKey bool
|
||||||
|
|
||||||
|
// Specify the player PubKey to use.
|
||||||
|
// If nil, it will be obtained from Mojang when joining
|
||||||
|
KeyPair *user.KeyPairResp
|
||||||
|
}
|
||||||
|
|
||||||
// JoinServer connect a Minecraft server for playing the game.
|
// JoinServer connect a Minecraft server for playing the game.
|
||||||
// Using roughly the same way to parse address as minecraft.
|
// Using roughly the same way to parse address as minecraft.
|
||||||
func (c *Client) JoinServer(addr string) (err error) {
|
func (c *Client) JoinServer(addr string) (err error) {
|
||||||
return c.join(context.Background(), &mcnet.DefaultDialer, addr)
|
return c.join(addr, JoinOptions{
|
||||||
|
Context: context.Background(),
|
||||||
|
Dialer: (*net.Dialer)(&mcnet.DefaultDialer),
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// JoinServerWithDialer is similar to JoinServer but using a Dialer.
|
// JoinServerWithDialer is similar to JoinServer but using a Dialer.
|
||||||
func (c *Client) JoinServerWithDialer(d *net.Dialer, addr string) (err error) {
|
func (c *Client) JoinServerWithDialer(dialer *net.Dialer, addr string) (err error) {
|
||||||
dialer := (*mcnet.Dialer)(d)
|
return c.join(addr, JoinOptions{
|
||||||
return c.join(context.Background(), dialer, addr)
|
Context: context.Background(),
|
||||||
|
Dialer: dialer,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) join(ctx context.Context, d *mcnet.Dialer, addr string) error {
|
func (c *Client) JoinServerWithOptions(addr string, options JoinOptions) (err error) {
|
||||||
|
if options.Dialer == nil {
|
||||||
|
options.Dialer = (*net.Dialer)(&mcnet.DefaultDialer)
|
||||||
|
}
|
||||||
|
if options.Context == nil {
|
||||||
|
options.Context = context.Background()
|
||||||
|
}
|
||||||
|
return c.join(addr, options)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) join(addr string, options JoinOptions) error {
|
||||||
const Handshake = 0x00
|
const Handshake = 0x00
|
||||||
// Split Host and Port
|
// Split Host and Port
|
||||||
host, portStr, err := net.SplitHostPort(addr)
|
host, portStr, err := net.SplitHostPort(addr)
|
||||||
@ -58,6 +85,8 @@ func (c *Client) join(ctx context.Context, d *mcnet.Dialer, addr string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Dial connection
|
// Dial connection
|
||||||
|
d := (*mcnet.Dialer)(options.Dialer)
|
||||||
|
ctx := options.Context
|
||||||
c.Conn, err = d.DialMCContext(ctx, addr)
|
c.Conn, err = d.DialMCContext(ctx, addr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return LoginErr{"connect server", err}
|
return LoginErr{"connect server", err}
|
||||||
@ -75,13 +104,17 @@ func (c *Client) join(ctx context.Context, d *mcnet.Dialer, addr string) error {
|
|||||||
return LoginErr{"handshake", err}
|
return LoginErr{"handshake", err}
|
||||||
}
|
}
|
||||||
// Login Start
|
// Login Start
|
||||||
KeyPairResp, err := user.GetOrFetchKeyPair(c.Auth.AsTk)
|
var KeyPair pk.OptionEncoder[*user.KeyPairResp]
|
||||||
if err == nil {
|
if c.Auth.AsTk != "" && !options.NoPublicKey {
|
||||||
|
if options.KeyPair != nil {
|
||||||
|
KeyPair.Has = true
|
||||||
|
KeyPair.Val = options.KeyPair
|
||||||
|
c.KeyPair = options.KeyPair
|
||||||
|
} else if KeyPairResp, err := user.GetOrFetchKeyPair(c.Auth.AsTk); err == nil {
|
||||||
|
KeyPair.Has = true
|
||||||
|
KeyPair.Val = &KeyPairResp
|
||||||
c.KeyPair = &KeyPairResp
|
c.KeyPair = &KeyPairResp
|
||||||
}
|
}
|
||||||
KeyPair := pk.OptionEncoder[*user.KeyPairResp]{
|
|
||||||
Has: err == nil,
|
|
||||||
Val: &KeyPairResp,
|
|
||||||
}
|
}
|
||||||
c.UUID, err = uuid.Parse(c.Auth.UUID)
|
c.UUID, err = uuid.Parse(c.Auth.UUID)
|
||||||
PlayerUUID := pk.Option[pk.UUID, *pk.UUID]{
|
PlayerUUID := pk.Option[pk.UUID, *pk.UUID]{
|
||||||
|
@ -3,10 +3,9 @@ package server
|
|||||||
import (
|
import (
|
||||||
_ "embed"
|
_ "embed"
|
||||||
|
|
||||||
"github.com/google/uuid"
|
|
||||||
|
|
||||||
"github.com/Tnze/go-mc/net"
|
"github.com/Tnze/go-mc/net"
|
||||||
"github.com/Tnze/go-mc/server/auth"
|
"github.com/Tnze/go-mc/server/auth"
|
||||||
|
"github.com/google/uuid"
|
||||||
)
|
)
|
||||||
|
|
||||||
type GamePlay interface {
|
type GamePlay interface {
|
||||||
|
Reference in New Issue
Block a user