2022-09-04 14:53:21 +00:00
|
|
|
package subcommands
|
2022-04-03 20:12:21 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
2022-10-14 18:26:09 +00:00
|
|
|
"encoding/binary"
|
2022-04-03 20:12:21 +00:00
|
|
|
"flag"
|
2022-10-14 18:26:09 +00:00
|
|
|
"io"
|
2022-04-03 20:12:21 +00:00
|
|
|
"net"
|
|
|
|
"os"
|
2022-10-14 22:42:43 +00:00
|
|
|
"sync"
|
2022-04-03 20:12:21 +00:00
|
|
|
"time"
|
|
|
|
|
2023-01-23 12:40:12 +00:00
|
|
|
"github.com/bedrock-tool/bedrocktool/locale"
|
2022-09-04 14:53:21 +00:00
|
|
|
"github.com/bedrock-tool/bedrocktool/utils"
|
2022-09-04 14:26:32 +00:00
|
|
|
|
2022-08-14 20:55:52 +00:00
|
|
|
"github.com/google/subcommands"
|
2022-04-03 20:12:21 +00:00
|
|
|
"github.com/sandertv/gophertunnel/minecraft/protocol/packet"
|
2022-09-03 17:32:30 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2022-04-03 20:12:21 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2022-09-04 14:26:32 +00:00
|
|
|
utils.RegisterCommand(&CaptureCMD{})
|
2022-04-03 20:12:21 +00:00
|
|
|
}
|
|
|
|
|
2022-10-14 22:42:43 +00:00
|
|
|
var dump_lock sync.Mutex
|
|
|
|
|
2022-10-14 18:26:09 +00:00
|
|
|
func dump_packet(f io.WriteCloser, toServer bool, payload []byte) {
|
2022-10-14 22:42:43 +00:00
|
|
|
dump_lock.Lock()
|
|
|
|
defer dump_lock.Unlock()
|
|
|
|
f.Write([]byte{0xAA, 0xAA, 0xAA, 0xAA})
|
|
|
|
packet_size := uint32(len(payload))
|
|
|
|
binary.Write(f, binary.LittleEndian, packet_size)
|
2022-10-14 18:26:09 +00:00
|
|
|
binary.Write(f, binary.LittleEndian, toServer)
|
2022-10-14 22:42:43 +00:00
|
|
|
_, err := f.Write(payload)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Error(err)
|
|
|
|
}
|
|
|
|
f.Write([]byte{0xBB, 0xBB, 0xBB, 0xBB})
|
2022-04-03 20:12:21 +00:00
|
|
|
}
|
|
|
|
|
2022-08-14 20:55:52 +00:00
|
|
|
type CaptureCMD struct {
|
|
|
|
server_address string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*CaptureCMD) Name() string { return "capture" }
|
2023-01-23 12:40:12 +00:00
|
|
|
func (*CaptureCMD) Synopsis() string { return locale.Loc("capture_synopsis", nil) }
|
2022-08-14 20:55:52 +00:00
|
|
|
|
|
|
|
func (p *CaptureCMD) SetFlags(f *flag.FlagSet) {
|
|
|
|
f.StringVar(&p.server_address, "address", "", "remote server address")
|
|
|
|
}
|
2022-09-03 17:32:30 +00:00
|
|
|
|
2022-08-14 20:55:52 +00:00
|
|
|
func (c *CaptureCMD) Usage() string {
|
2023-01-23 12:40:12 +00:00
|
|
|
return c.Name() + ": " + c.Synopsis() + "\n" + locale.Loc("server_address_help", nil)
|
2022-08-14 20:55:52 +00:00
|
|
|
}
|
2022-04-03 20:12:21 +00:00
|
|
|
|
2022-08-14 20:55:52 +00:00
|
|
|
func (c *CaptureCMD) Execute(ctx context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus {
|
2022-10-21 01:58:30 +00:00
|
|
|
address, hostname, err := utils.ServerInput(ctx, c.server_address)
|
2022-04-03 20:12:21 +00:00
|
|
|
if err != nil {
|
2022-09-07 12:07:33 +00:00
|
|
|
logrus.Fatal(err)
|
2022-08-14 20:55:52 +00:00
|
|
|
return 1
|
2022-04-03 20:12:21 +00:00
|
|
|
}
|
|
|
|
|
2023-01-25 12:51:00 +00:00
|
|
|
os.Mkdir("captures", 0o775)
|
|
|
|
|
|
|
|
fio, err := os.Create("captures/" + hostname + "-" + time.Now().Format("2006-01-02_15-04-05") + ".pcap2")
|
2022-04-03 20:12:21 +00:00
|
|
|
if err != nil {
|
2022-09-07 12:07:33 +00:00
|
|
|
logrus.Fatal(err)
|
2022-08-14 20:55:52 +00:00
|
|
|
return 1
|
2022-04-03 20:12:21 +00:00
|
|
|
}
|
2022-08-14 20:55:52 +00:00
|
|
|
defer fio.Close()
|
2022-04-03 20:12:21 +00:00
|
|
|
|
2023-01-23 12:40:12 +00:00
|
|
|
proxy := utils.NewProxy()
|
2022-09-04 14:26:32 +00:00
|
|
|
proxy.PacketFunc = func(header packet.Header, payload []byte, src, dst net.Addr) {
|
2023-01-29 15:08:41 +00:00
|
|
|
from_client := dst.String() == proxy.Server.RemoteAddr().String()
|
2022-10-14 18:26:09 +00:00
|
|
|
|
|
|
|
buf := bytes.NewBuffer(nil)
|
|
|
|
header.Write(buf)
|
|
|
|
buf.Write(payload)
|
|
|
|
dump_packet(fio, from_client, buf.Bytes())
|
2022-09-04 13:24:55 +00:00
|
|
|
}
|
2022-08-13 14:30:46 +00:00
|
|
|
|
2022-09-04 13:24:55 +00:00
|
|
|
err = proxy.Run(ctx, address)
|
2022-10-14 22:42:43 +00:00
|
|
|
time.Sleep(2 * time.Second)
|
2022-09-03 17:32:30 +00:00
|
|
|
if err != nil {
|
2022-09-07 12:07:33 +00:00
|
|
|
logrus.Fatal(err)
|
2022-09-03 17:32:30 +00:00
|
|
|
return 1
|
2022-04-03 20:12:21 +00:00
|
|
|
}
|
2022-09-03 17:32:30 +00:00
|
|
|
return 0
|
2022-04-03 20:12:21 +00:00
|
|
|
}
|