Skip to content

Commit be75e8e

Browse files
committed
Try to sniff every packet
1 parent 913162f commit be75e8e

File tree

2 files changed

+39
-8
lines changed

2 files changed

+39
-8
lines changed

pkg/mcproto/types.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"errors"
66
"io"
77
"strings"
8+
9+
"github.com/sirupsen/logrus"
810
)
911

1012
type Frame struct {
@@ -38,6 +40,12 @@ func ReadVarInt(reader io.Reader) (int, error) {
3840
for numRead <= 5 {
3941
n, err := reader.Read(b)
4042
if err != nil {
43+
logrus.WithError(err).WithFields(logrus.Fields{
44+
"b": b,
45+
"numRead": numRead,
46+
"result": result,
47+
"n": n,
48+
}).Infof("")
4149
return 0, err
4250
}
4351
if n == 0 {
@@ -131,6 +139,7 @@ func ReadPacket(reader io.Reader) (*Packet, error) {
131139

132140
packet.PacketID, err = ReadVarInt(remainder)
133141
if err != nil {
142+
logrus.WithField("remainder", remainder.String()).Info("Failed to find PacketID")
134143
return nil, err
135144
}
136145

pkg/server/connector.go

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
package server
22

33
import (
4-
"net"
5-
"github.com/sirupsen/logrus"
6-
"github.com/itzg/mc-router/pkg/mcproto"
4+
"bytes"
75
"context"
86
"io"
9-
"bytes"
7+
"net"
8+
9+
"github.com/itzg/mc-router/pkg/mcproto"
10+
"github.com/sirupsen/logrus"
1011
)
1112

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

138139
func pumpFrames(incoming io.Reader, outgoing io.Writer, errors chan<- error, from, to string) {
139-
amount, err := io.Copy(outgoing, incoming)
140-
if err != nil {
141-
errors <- err
140+
for {
141+
inspectionBuffer := new(bytes.Buffer)
142+
143+
inspectionReader := io.TeeReader(incoming, inspectionBuffer)
144+
145+
packet, err := mcproto.ReadPacket(inspectionReader)
146+
if err != nil {
147+
logrus.WithError(err).Error("Failed to read packet")
148+
errors <- err
149+
continue
150+
}
151+
amount, err := io.Copy(outgoing, inspectionBuffer)
152+
if err != nil {
153+
errors <- err
154+
continue
155+
}
156+
logrus.WithFields(logrus.Fields{
157+
"PacketID": packet.PacketID,
158+
"PacketLength": packet.Length,
159+
"from": from,
160+
"to": to,
161+
"amount": amount,
162+
}).Info("Proxied packet")
142163
}
143-
logrus.WithField("amount", amount).Infof("Finished relay %s->%s", from, to)
164+
165+
logrus.Infof("Finished relay %s->%s", from, to)
144166
}

0 commit comments

Comments
 (0)