bedrocktool/subcommands/debug.go

73 lines
1.7 KiB
Go
Raw Normal View History

2022-09-05 11:13:43 +00:00
package subcommands
import (
"context"
"flag"
"strings"
"github.com/bedrock-tool/bedrocktool/locale"
2022-09-05 11:13:43 +00:00
"github.com/bedrock-tool/bedrocktool/utils"
"github.com/google/subcommands"
"github.com/sirupsen/logrus"
)
type DebugProxyCMD struct {
2023-02-12 21:22:44 +00:00
Address string
filter string
pathCustomUserData string
2022-09-05 11:13:43 +00:00
}
func (*DebugProxyCMD) Name() string { return "debug-proxy" }
func (*DebugProxyCMD) Synopsis() string { return locale.Loc("debug_proxy_synopsis", nil) }
2022-09-05 11:13:43 +00:00
func (c *DebugProxyCMD) SetFlags(f *flag.FlagSet) {
f.StringVar(&c.Address, "address", "", locale.Loc("remote_address", nil))
f.StringVar(&c.filter, "filter", "", locale.Loc("packet_filter", nil))
2023-02-12 21:22:44 +00:00
f.StringVar(&c.pathCustomUserData, "userdata", "", locale.Loc("custom_user_data", nil))
2022-09-05 11:13:43 +00:00
}
func (c *DebugProxyCMD) Usage() string {
return c.Name() + ": " + c.Synopsis() + "\n" + locale.Loc("server_address_help", nil)
2022-09-05 11:13:43 +00:00
}
func (c *DebugProxyCMD) Execute(ctx context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus {
address, _, err := utils.ServerInput(ctx, c.Address)
2022-09-05 11:13:43 +00:00
if err != nil {
logrus.Error(err)
return 1
}
2023-01-29 21:20:13 +00:00
utils.GDebug = true
2022-09-05 11:13:43 +00:00
filters := strings.Split(c.filter, ",")
if len(filters) > 0 {
for _, v := range filters {
2022-09-05 11:35:18 +00:00
if len(v) == 0 {
continue
}
2022-09-05 11:13:43 +00:00
if string(v[0]) == "*" {
v = v[1:]
}
v = strings.TrimPrefix(v, "packet.")
v = "packet." + v
utils.ExtraVerbose = append(utils.ExtraVerbose, v)
}
}
2023-02-12 21:22:44 +00:00
proxy, err := utils.NewProxy(c.pathCustomUserData)
if err != nil {
logrus.Error(err)
return 1
}
2022-09-05 11:13:43 +00:00
if err := proxy.Run(ctx, address); err != nil {
logrus.Error(err)
return 1
}
return 0
}
func init() {
utils.RegisterCommand(&DebugProxyCMD{})
}