Skip to content

Commit

Permalink
code tidy / cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
BuckarooBanzay committed Aug 26, 2024
1 parent b334fca commit 3762249
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 20 deletions.
2 changes: 1 addition & 1 deletion cmd/client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/client/ping.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:]),
}

Expand Down
4 changes: 4 additions & 0 deletions commandclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand Down
5 changes: 0 additions & 5 deletions commandclient/clientready.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package commandclient

import (
"errors"
"fmt"

"github.com/minetest-go/minetest_client/commands"
)
Expand All @@ -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
Expand Down
4 changes: 1 addition & 3 deletions commandclient/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package commandclient

import (
"errors"
"fmt"
"time"

"github.com/minetest-go/minetest_client/commands"
Expand All @@ -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
Expand Down
14 changes: 7 additions & 7 deletions commandclient/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ package commandclient

import (
"errors"
"fmt"

"github.com/minetest-go/minetest_client/commands"
"github.com/minetest-go/minetest_client/packet"
"github.com/minetest-go/minetest_client/srp"
)

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)
Expand All @@ -31,28 +31,30 @@ 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
}
}

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:
Expand All @@ -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
}
Expand Down
3 changes: 0 additions & 3 deletions commands/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package commands

import (
"encoding/binary"
"fmt"
)

func Parse(payload []byte) (Command, error) {
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 3762249

Please sign in to comment.