bedrocktool/subcommands/chat_log.go

68 lines
1.6 KiB
Go
Raw Normal View History

2022-09-09 18:11:45 +00:00
package subcommands
import (
"context"
"flag"
"fmt"
"os"
"time"
"github.com/bedrock-tool/bedrocktool/locale"
2022-09-09 18:11:45 +00:00
"github.com/bedrock-tool/bedrocktool/utils"
"github.com/sandertv/gophertunnel/minecraft/protocol/packet"
"github.com/sirupsen/logrus"
)
type ChatLogCMD struct {
2023-03-06 01:03:31 +00:00
ServerAddress string
Verbose bool
2022-09-09 18:11:45 +00:00
}
func (*ChatLogCMD) Name() string { return "chat-log" }
func (*ChatLogCMD) Synopsis() string { return locale.Loc("chat_log_synopsis", nil) }
2022-09-09 18:11:45 +00:00
func (c *ChatLogCMD) SetFlags(f *flag.FlagSet) {
2023-03-06 01:03:31 +00:00
f.StringVar(&c.ServerAddress, "address", "", "remote server address")
f.BoolVar(&c.Verbose, "v", false, "verbose")
2022-09-09 18:11:45 +00:00
}
func (c *ChatLogCMD) Execute(ctx context.Context, ui utils.UI) error {
2023-03-06 01:03:31 +00:00
address, hostname, err := utils.ServerInput(ctx, c.ServerAddress)
2022-09-09 18:11:45 +00:00
if err != nil {
return err
2022-09-09 18:11:45 +00:00
}
filename := fmt.Sprintf("%s_%s_chat.log", hostname, time.Now().Format("2006-01-02_15-04-05_Z07"))
f, err := os.Create(filename)
if err != nil {
return err
2022-09-09 18:11:45 +00:00
}
defer f.Close()
2023-03-06 01:03:31 +00:00
proxy, err := utils.NewProxy()
2023-02-12 21:22:44 +00:00
if err != nil {
return err
2023-02-12 21:22:44 +00:00
}
2023-03-23 19:39:47 +00:00
proxy.PacketCB = func(pk packet.Packet, toServer bool, t time.Time) (packet.Packet, error) {
2022-09-09 18:11:45 +00:00
if text, ok := pk.(*packet.Text); ok {
logLine := text.Message
2023-03-06 01:03:31 +00:00
if c.Verbose {
2022-09-09 18:11:45 +00:00
logLine += fmt.Sprintf(" (TextType: %d | XUID: %s | PlatformChatID: %s)", text.TextType, text.XUID, text.PlatformChatID)
}
2023-03-23 19:39:47 +00:00
f.WriteString(fmt.Sprintf("[%s] ", t.Format(time.RFC3339)))
2022-09-09 18:11:45 +00:00
logrus.Info(logLine)
if toServer {
f.WriteString("SENT: ")
}
f.WriteString(logLine + "\n")
}
return pk, nil
}
2023-04-01 22:22:50 +00:00
return proxy.Run(ctx, address)
2022-09-09 18:11:45 +00:00
}
func init() {
utils.RegisterCommand(&ChatLogCMD{})
}