From 376224996e0943d1d08c62d06e00f55adb2c58a7 Mon Sep 17 00:00:00 2001 From: BuckarooBanzay Date: Mon, 26 Aug 2024 10:12:23 +0200 Subject: [PATCH] code tidy / cleanups --- cmd/client/main.go | 2 +- cmd/client/ping.go | 2 +- commandclient/client.go | 4 ++++ commandclient/clientready.go | 5 ----- commandclient/init.go | 4 +--- commandclient/login.go | 14 +++++++------- commands/parse.go | 3 --- 7 files changed, 14 insertions(+), 20 deletions(-) diff --git a/cmd/client/main.go b/cmd/client/main.go index a5bfbb0..0fc7dbf 100644 --- a/cmd/client/main.go +++ b/cmd/client/main.go @@ -56,7 +56,7 @@ func main() { } if !stalk { - err = commandclient.Login(client, username, password) + err = commandclient.Login(client, username, password, true) if err != nil { panic(err) } diff --git a/cmd/client/ping.go b/cmd/client/ping.go index 8fd94ed..695cc57 100644 --- a/cmd/client/ping.go +++ b/cmd/client/ping.go @@ -47,7 +47,7 @@ func Ping(host string, port int) (*PingResult, error) { } res := &PingResult{ - Delay: time.Now().Sub(start), + Delay: time.Since(start), PeerID: binary.BigEndian.Uint16(buf[12:]), } diff --git a/commandclient/client.go b/commandclient/client.go index b678fc5..6a75ec3 100644 --- a/commandclient/client.go +++ b/commandclient/client.go @@ -52,6 +52,10 @@ func (c *CommandClient) Disconnect() error { return err } close(c.netrx) + for _, l := range c.listeners { + close(l) + } + return c.conn.Close() } diff --git a/commandclient/clientready.go b/commandclient/clientready.go index 249a4f4..f48e707 100644 --- a/commandclient/clientready.go +++ b/commandclient/clientready.go @@ -2,7 +2,6 @@ package commandclient import ( "errors" - "fmt" "github.com/minetest-go/minetest_client/commands" ) @@ -15,15 +14,11 @@ func ClientReady(cc *CommandClient) error { for o := range ch { switch o.(type) { case *commands.ServerCSMRestrictionFlags: - fmt.Println("Server sends csm restriction flags") - - fmt.Println("Sending CLIENT_READY") err := cc.SendCommand(commands.NewClientReady(5, 5, 5, "mt-bot", 4)) if err != nil { return err } - fmt.Println("Sending PLAYERPOS") ppos := commands.NewClientPlayerPos() err = cc.SendCommand(ppos) return err diff --git a/commandclient/init.go b/commandclient/init.go index d0226dd..433bd26 100644 --- a/commandclient/init.go +++ b/commandclient/init.go @@ -2,7 +2,6 @@ package commandclient import ( "errors" - "fmt" "time" "github.com/minetest-go/minetest_client/commands" @@ -14,9 +13,8 @@ func Init(cc *CommandClient, username string) error { defer cc.RemoveListener(ch) for o := range ch { - switch cmd := o.(type) { + switch o.(type) { case *commands.ServerSetPeer: - fmt.Printf("Received set_peerid: %d\n", cmd.PeerID) time.Sleep(1 * time.Second) err := cc.SendOriginalCommand(commands.NewClientInit(username)) return err diff --git a/commandclient/login.go b/commandclient/login.go index cbb7858..668051a 100644 --- a/commandclient/login.go +++ b/commandclient/login.go @@ -2,7 +2,6 @@ package commandclient import ( "errors" - "fmt" "github.com/minetest-go/minetest_client/commands" "github.com/minetest-go/minetest_client/packet" @@ -10,8 +9,9 @@ import ( ) var ErrAccessDenied = errors.New("access denied") +var ErrNotRegistered = errors.New("username not registered") -func Login(cc *CommandClient, username, password string) error { +func Login(cc *CommandClient, username, password string, enable_registration bool) error { ch := make(chan commands.Command, 100) cc.AddListener(ch) defer cc.RemoveListener(ch) @@ -31,7 +31,6 @@ func Login(cc *CommandClient, username, password string) error { return err } - fmt.Printf("Sending SRP bytes A, len=%d\n", len(srppub)) err = cc.SendCommand(commands.NewClientSRPBytesA(srppub)) if err != nil { return err @@ -39,20 +38,23 @@ func Login(cc *CommandClient, username, password string) error { } if cmd.AuthMechanismFirstSRP { + if !enable_registration { + // registration not enabled, fail + return ErrNotRegistered + } + // new client salt, verifier, err := srp.NewClient([]byte(username), []byte(password)) if err != nil { return err } - fmt.Printf("Sending first SRP, salt-len=%d, verifier-len=%d\n", len(salt), len(verifier)) err = cc.SendCommand(commands.NewClientFirstSRP(salt, verifier)) if err != nil { return err } } case *commands.ServerAccessDenied: - fmt.Println("Access denied") return ErrAccessDenied case *commands.ServerSRPBytesSB: @@ -66,14 +68,12 @@ func Login(cc *CommandClient, username, password string) error { proof := srp.ClientProof(identifier, cmd.BytesS, srppub, cmd.BytesB, clientK) - fmt.Printf("Sending SRP bytes M, len=%d\n", len(proof)) err = cc.SendCommand(commands.NewClientSRPBytesM(proof)) if err != nil { return err } case *commands.ServerAuthAccept: - fmt.Println("Sending INIT2") err := cc.SendCommand(commands.NewClientInit2()) return err } diff --git a/commands/parse.go b/commands/parse.go index 667f94a..37f8d5e 100644 --- a/commands/parse.go +++ b/commands/parse.go @@ -2,7 +2,6 @@ package commands import ( "encoding/binary" - "fmt" ) func Parse(payload []byte) (Command, error) { @@ -48,8 +47,6 @@ func Parse(payload []byte) (Command, error) { cmd = &ServerAccessDenied{} case ServerCommandNodeDefinitions: cmd = &ServerNodeDefinitions{} - default: - fmt.Printf("Unknown command received: %d\n", commandId) } if cmd != nil {