bedrocktool/main.go

199 lines
4.4 KiB
Go
Raw Normal View History

2022-02-12 22:27:49 +00:00
package main
import (
"bufio"
2022-02-20 11:39:02 +00:00
"bytes"
"context"
2022-02-12 22:27:49 +00:00
"flag"
"fmt"
2022-02-20 11:39:02 +00:00
"net"
2022-02-12 22:27:49 +00:00
"os"
"os/signal"
2022-02-20 11:39:02 +00:00
"reflect"
2022-02-27 10:08:19 +00:00
"regexp"
"syscall"
2022-02-12 22:27:49 +00:00
"github.com/google/subcommands"
2022-02-12 22:27:49 +00:00
"github.com/sandertv/gophertunnel/minecraft/auth"
2022-02-20 11:39:02 +00:00
"github.com/sandertv/gophertunnel/minecraft/protocol"
"github.com/sandertv/gophertunnel/minecraft/protocol/packet"
2022-08-11 20:22:39 +00:00
"golang.org/x/exp/slices"
2022-02-12 22:27:49 +00:00
"golang.org/x/oauth2"
)
const TOKEN_FILE = "token.json"
var G_src oauth2.TokenSource
2022-04-16 11:02:32 +00:00
var G_xbl_token *auth.XBLToken
2022-02-27 10:08:19 +00:00
var G_debug bool
var G_preload_packs bool
2022-08-11 20:22:39 +00:00
var G_exit []func() = []func(){}
2022-02-20 11:39:02 +00:00
var pool = packet.NewPool()
2022-08-11 20:22:39 +00:00
var muted_packets = []string{
"*packet.UpdateBlock",
"*packet.MoveActorAbsolute",
"*packet.SetActorMotion",
"*packet.SetTime",
"*packet.RemoveActor",
"*packet.AddActor",
"*packet.UpdateAttributes",
"*packet.Interact",
"*packet.LevelEvent",
"*packet.SetActorData",
"*packet.MoveActorDelta",
"*packet.MovePlayer",
"*packet.BlockActorData",
"*packet.PlayerAuthInput",
2022-08-12 02:53:43 +00:00
"*packet.LevelChunk",
2022-08-11 20:22:39 +00:00
"*packet.LevelSoundEvent",
"*packet.ActorEvent",
"*packet.NetworkChunkPublisherUpdate",
2022-08-12 16:54:29 +00:00
"*packet.UpdateSubChunkBlocks",
"*packet.SubChunk",
"*packet.SubChunkRequest",
"*packet.Animate",
2022-08-13 16:09:31 +00:00
"*packet.NetworkStackLatency",
2022-08-11 20:22:39 +00:00
}
2022-02-20 11:39:02 +00:00
func PacketLogger(header packet.Header, payload []byte, src, dst net.Addr) {
var pk packet.Packet
buf := bytes.NewBuffer(payload)
r := protocol.NewReader(buf, 0)
pkFunc, ok := pool[header.PacketID]
if !ok {
pk = &packet.Unknown{PacketID: header.PacketID}
} else {
pk = pkFunc()
2022-02-20 11:39:02 +00:00
}
pk.Unmarshal(r)
2022-08-13 16:09:31 +00:00
dir := "S->C"
src_addr, _, _ := net.SplitHostPort(src.String())
if IPPrivate(net.ParseIP(src_addr)) {
dir = "C->S"
2022-02-20 11:39:02 +00:00
}
2022-08-11 20:22:39 +00:00
2022-08-13 16:09:31 +00:00
pk_name := reflect.TypeOf(pk).String()
2022-08-11 20:22:39 +00:00
if slices.Contains(muted_packets, pk_name) {
2022-07-29 16:12:06 +00:00
return
2022-02-20 11:39:02 +00:00
}
switch pk := pk.(type) {
case *packet.Disconnect:
fmt.Printf("Disconnect: %s", pk.Message)
2022-08-11 20:22:39 +00:00
}
2022-08-13 16:09:31 +00:00
fmt.Printf("%s 0x%x, %s\n", dir, pk.ID(), pk_name)
2022-02-20 11:39:02 +00:00
}
func exit() {
fmt.Printf("\nExiting\n")
for i := len(G_exit) - 1; i >= 0; i-- { // go through cleanup functions reversed
G_exit[i]()
}
os.Exit(0)
}
var valid_cmds = make(map[string]string, 0)
func register_command(sub subcommands.Command) {
subcommands.Register(sub, "")
valid_cmds[sub.Name()] = sub.Synopsis()
}
2022-02-27 10:08:19 +00:00
func main() {
ctx, cancel := context.WithCancel(context.Background())
2022-02-27 10:08:19 +00:00
flag.BoolVar(&G_debug, "debug", false, "debug mode")
flag.BoolVar(&G_preload_packs, "preload", false, "preload resourcepacks for proxy")
enable_dns := flag.Bool("dns", false, "enable dns server for consoles")
subcommands.Register(subcommands.HelpCommand(), "")
subcommands.ImportantFlag("debug")
subcommands.ImportantFlag("dns")
subcommands.ImportantFlag("preload")
2022-08-15 00:29:01 +00:00
subcommands.HelpCommand()
{ // interactive input
if len(os.Args) < 2 {
select {
case <-ctx.Done():
return
default:
fmt.Println("Available commands:")
for name, desc := range valid_cmds {
fmt.Printf("\t%s\t%s\n", name, desc)
}
fmt.Printf("Use '%s <command>' to run a command\n", os.Args[0])
fmt.Printf("Input Command: ")
reader := bufio.NewReader(os.Stdin)
target, _ := reader.ReadString('\n')
r, _ := regexp.Compile(`[\n\r]`)
target = string(r.ReplaceAll([]byte(target), []byte("")))
os.Args = append(os.Args, target)
}
}
}
flag.Parse()
if *enable_dns {
init_dns()
}
2022-08-11 20:22:39 +00:00
// exit cleanup
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
go func() {
<-sigs
cancel()
exit()
}()
2022-02-20 11:39:02 +00:00
{ // authenticate
token := get_token()
G_src = auth.RefreshTokenSource(&token)
{
_token, err := G_src.Token()
if err != nil {
panic(err)
}
G_xbl_token, err = auth.RequestXBLToken(ctx, _token, "https://pocket.realms.minecraft.net/")
if err != nil {
panic(err)
}
2022-02-20 11:39:02 +00:00
}
2022-02-12 22:27:49 +00:00
}
ret := subcommands.Execute(ctx)
exit()
os.Exit(int(ret))
2022-07-29 16:12:06 +00:00
}
type TransCMD struct{}
func (*TransCMD) Name() string { return "trans" }
func (*TransCMD) Synopsis() string { return "" }
func (c *TransCMD) SetFlags(f *flag.FlagSet) {}
func (c *TransCMD) Usage() string {
return c.Name() + ": " + c.Synopsis() + "\n"
}
func (c *TransCMD) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus {
const (
BLACK_FG = "\033[30m"
BOLD = "\033[1m"
BLUE = "\033[46m"
PINK = "\033[45m"
WHITE = "\033[47m"
RESET = "\033[0m"
)
fmt.Println(BLACK_FG + BOLD + BLUE + " Trans " + PINK + " Rights " + WHITE + " Are " + PINK + " Human " + BLUE + " Rights " + RESET)
return 0
}
func init() {
register_command(&TransCMD{})
}