Skip to content

Commit

Permalink
Try to sniff every packet
Browse files Browse the repository at this point in the history
  • Loading branch information
fabianvf committed May 6, 2019
1 parent 9c02368 commit 30d0969
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 8 deletions.
9 changes: 9 additions & 0 deletions pkg/mcproto/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"errors"
"io"
"strings"

"github.com/sirupsen/logrus"
)

type Frame struct {
Expand Down Expand Up @@ -38,6 +40,12 @@ func ReadVarInt(reader io.Reader) (int, error) {
for numRead <= 5 {
n, err := reader.Read(b)
if err != nil {
logrus.WithError(err).WithFields(logrus.Fields{
"b": b,
"numRead": numRead,
"result": result,
"n": n,
}).Infof("")
return 0, err
}
if n == 0 {
Expand Down Expand Up @@ -131,6 +139,7 @@ func ReadPacket(reader io.Reader) (*Packet, error) {

packet.PacketID, err = ReadVarInt(remainder)
if err != nil {
logrus.WithField("remainder", remainder.String()).Info("Failed to find PacketID")
return nil, err
}

Expand Down
38 changes: 30 additions & 8 deletions pkg/server/connector.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package server

import (
"net"
"github.com/sirupsen/logrus"
"github.com/itzg/mc-router/pkg/mcproto"
"bytes"
"context"
"io"
"bytes"
"net"

"github.com/itzg/mc-router/pkg/mcproto"
"github.com/sirupsen/logrus"
)

type IConnector interface {
Expand Down Expand Up @@ -136,9 +137,30 @@ func pumpConnections(ctx context.Context, frontendConn, backendConn net.Conn) {
}

func pumpFrames(incoming io.Reader, outgoing io.Writer, errors chan<- error, from, to string) {
amount, err := io.Copy(outgoing, incoming)
if err != nil {
errors <- err
for {
inspectionBuffer := new(bytes.Buffer)

inspectionReader := io.TeeReader(incoming, inspectionBuffer)

packet, err := mcproto.ReadPacket(inspectionReader)
if err != nil {
logrus.WithError(err).Error("Failed to read packet")
errors <- err
continue
}
amount, err := io.Copy(outgoing, inspectionBuffer)
if err != nil {
errors <- err
continue
}
logrus.WithFields(logrus.Fields{
"PacketID": packet.PacketID,
"PacketLength": packet.Length,
"from": from,
"to": to,
"amount": amount,
}).Info("Proxied packet")
}
logrus.WithField("amount", amount).Infof("Finished relay %s->%s", from, to)

logrus.Infof("Finished relay %s->%s", from, to)
}

0 comments on commit 30d0969

Please sign in to comment.