Skip to content

Commit 504a04b

Browse files
committed
Try to sniff every packet
1 parent 9c02368 commit 504a04b

File tree

2 files changed

+42
-8
lines changed

2 files changed

+42
-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: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
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"
11+
12+
"github.com/itzg/mc-router/mcproto"
13+
"github.com/sirupsen/logrus"
1014
)
1115

1216
type IConnector interface {
@@ -136,9 +140,30 @@ func pumpConnections(ctx context.Context, frontendConn, backendConn net.Conn) {
136140
}
137141

138142
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
143+
for {
144+
inspectionBuffer := new(bytes.Buffer)
145+
146+
inspectionReader := io.TeeReader(incoming, inspectionBuffer)
147+
148+
packet, err := mcproto.ReadPacket(inspectionReader)
149+
if err != nil {
150+
logrus.WithError(err).Error("Failed to read packet")
151+
errors <- err
152+
continue
153+
}
154+
amount, err := io.Copy(outgoing, inspectionBuffer)
155+
if err != nil {
156+
errors <- err
157+
continue
158+
}
159+
logrus.WithFields(logrus.Fields{
160+
"PacketID": packet.PacketID,
161+
"PacketLength": packet.Length,
162+
"from": from,
163+
"to": to,
164+
"amount": amount,
165+
}).Info("Proxied packet")
142166
}
143-
logrus.WithField("amount", amount).Infof("Finished relay %s->%s", from, to)
167+
168+
logrus.Infof("Finished relay %s->%s", from, to)
144169
}

0 commit comments

Comments
 (0)