diff --git a/connector/handle_game_connection.go b/connector/handle_game_connection.go deleted file mode 100644 index b2dc32e..0000000 --- a/connector/handle_game_connection.go +++ /dev/null @@ -1,16 +0,0 @@ -package connector - -import ( - "io" -) - -// This is the size that Netty apparently uses to read from the incoming -// connection at a time -// -// This is documented in a mod that removes this limit: -// https://www.curseforge.com/minecraft/mc-mods/xl-packets -const nettyMaxPacketSize = 2_097_152 // 16MiB - -func handleGameConnection(conn io.Reader) { - panic("Unhandled game traffic") -} diff --git a/connector/listen_port.go b/connector/listen_port.go index f2a7e7b..d04c711 100644 --- a/connector/listen_port.go +++ b/connector/listen_port.go @@ -1,11 +1,13 @@ package connector import ( - "bytes" + "errors" "fmt" "io" "net" + mcnet "github.com/Tnze/go-mc/net" + pk "github.com/Tnze/go-mc/net/packet" log "github.com/sirupsen/logrus" "github.com/spf13/cobra" @@ -46,32 +48,61 @@ var ProxyPortCommand = &cobra.Command{ }, } -func handleConn(conn net.Conn) { - defer conn.Close() - log.Infof("Received connection from %v", conn.RemoteAddr()) +func handleConn(clientConn net.Conn) { + defer log.Info("Closed all connections") + defer clientConn.Close() + log.Infof("Received connection from %v", clientConn.RemoteAddr()) // Open a connection to the remote server serverConn, err := net.Dial("tcp", fmt.Sprintf(":%d", outputPort)) if err != nil { - panic(err) + log.Errorf("Could not connect to remote server: %v", err) + return } defer serverConn.Close() - var sidecarServerDataStream bytes.Buffer + // Wrap the server's connection into a mc conn to read packets + wrappedServerConn := mcnet.WrapConn(serverConn) + defer wrappedServerConn.Close() - // Divert any data read from the server to be copied into the sidecar data stream - serverReader := io.TeeReader(serverConn, &sidecarServerDataStream) + // Writes to the client connection any data read from the server connection + wrappedServerConn.Reader = io.TeeReader(wrappedServerConn.Reader, clientConn) - go handleGameConnection(&sidecarServerDataStream) - - // Start copying data from the server to the client go func() { - if _, err := io.Copy(conn, serverReader); err != nil { - panic(err) + log.Info("Listening for packets") + var p pk.Packet + for { + if err := wrappedServerConn.ReadPacket(&p); err != nil { + if errors.Is(err, io.EOF) { + return + } + panic(err) + } + + // log.Infof("Received packet with id %.2x", p.ID) + + switch p.ID { + case 0x20: + panic("Incoming chunk") + // From here: https://wiki.vg/Protocol#Chunk_Data_and_Update_Light + case 0x25: + var ( + // chunkX pk.Int + // chunkZ pk.Int + // heightmaps struct { + // MotionBlocking []int64 `nbt:"MOTION_BLOCKING"` + // WorldSurface []pk.NBTField `nbt:"WORLD_SURFACE"` + // } + // heightmaps nbt.RawMessage + // chunkData pk.ByteArray + ) + panic("Chunk upload called") + } } }() - // Copy data from the client to the server - if _, err := io.Copy(serverConn, conn); err != nil { + // Now, we need to copy the network data from the client to the server + log.Info("Copying data from the client to the server") + if _, err := io.Copy(wrappedServerConn, clientConn); err != nil { panic(err) } } diff --git a/docker-compose.yaml b/docker-compose.yaml new file mode 100644 index 0000000..111e96f --- /dev/null +++ b/docker-compose.yaml @@ -0,0 +1,16 @@ +version: "3.8" + +services: + mc: + image: itzg/minecraft-server + tty: true + stdin_open: true + ports: + - "25565:25565" + environment: + EULA: "TRUE" + VERSION: "1.20.2" + ONLINE_MODE: "false" + NETWORK_COMPRESSION_THRESHOLD: "-1" + volumes: + - ./data:/data diff --git a/go.mod b/go.mod index 6559197..169d832 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module git.nicholasnovak.io/nnovak/spatial-db go 1.21.3 require ( + github.com/Tnze/go-mc v1.19.4 github.com/charmbracelet/bubbletea v0.24.2 github.com/charmbracelet/lipgloss v0.9.1 github.com/sirupsen/logrus v1.9.3 @@ -12,6 +13,7 @@ require ( require ( github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81 // indirect + github.com/google/uuid v1.3.0 // indirect github.com/inconshreveable/mousetrap v1.1.0 // indirect github.com/lucasb-eyer/go-colorful v1.2.0 // indirect github.com/mattn/go-isatty v0.0.18 // indirect diff --git a/go.sum b/go.sum index a5930f2..21ba377 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,5 @@ +github.com/Tnze/go-mc v1.19.4 h1:9qtxH+xRJWswOYnlf/dsFY4EI2f5jsFhtqTYOObaGIE= +github.com/Tnze/go-mc v1.19.4/go.mod h1:c1znJQglgqa1Jjs3Dr29woN/msguiJrlNtWXhKedh2U= github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k= github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8= github.com/charmbracelet/bubbletea v0.24.2 h1:uaQIKx9Ai6Gdh5zpTbGiWpytMU+CfsPp06RaW2cx/SY= @@ -7,6 +9,10 @@ github.com/charmbracelet/lipgloss v0.9.1/go.mod h1:1mPmG4cxScwUQALAAnacHaigiiHB9 github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81 h1:q2hJAaP1k2wIvVRd/hEHD7lacgqrCPS+k8g1MndzfWY= github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81/go.mod h1:YynlIjWYF8myEu6sdkwKIvGQq+cOckRm6So2avqoYAk= github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= +github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY= @@ -30,6 +36,7 @@ github.com/muesli/termenv v0.15.1 h1:UzuTb/+hhlBugQz28rpzey4ZuKcZ03MeKsoG7IJZIxs github.com/muesli/termenv v0.15.1/go.mod h1:HeAQPTzpfs016yGtA4g00CsdYnVLJvxsS4ANqrZs2sQ= github.com/muesli/termenv v0.15.2 h1:GohcuySI0QmI3wN8Ok9PtKGkgkFIk7y6Vpb5PvrY+Wo= github.com/muesli/termenv v0.15.2/go.mod h1:Epx+iuz8sNs7mNKhxzH4fWXGNpZwUaJKRS1noLXviQ8= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY= github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= @@ -40,8 +47,11 @@ github.com/spf13/cobra v1.7.0 h1:hyqWnYt1ZQShIddO5kBpj3vu05/++x6tJ6dg8EC572I= github.com/spf13/cobra v1.7.0/go.mod h1:uLxZILRyS/50WlhOIKD7W6V5bgeIt+4sICxh6uRMrb0= github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o= golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= @@ -52,4 +62,5 @@ golang.org/x/term v0.6.0/go.mod h1:m6U89DPEgQRMq3DNkDClhWw02AUbt2daBVO4cn4Hv9U= golang.org/x/text v0.3.8 h1:nAL+RVCQ9uMn3vJZbV+MRnydTJFPf8qqY42YiA6MrqY= golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=