|
1 | 1 | package server |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "net" |
5 | | - "github.com/sirupsen/logrus" |
6 | | - "github.com/itzg/mc-router/pkg/mcproto" |
| 4 | + "bytes" |
7 | 5 | "context" |
8 | 6 | "io" |
9 | | - "bytes" |
| 7 | + "net" |
| 8 | + |
| 9 | + "github.com/itzg/mc-router/pkg/mcproto" |
| 10 | + "github.com/sirupsen/logrus" |
10 | 11 | ) |
11 | 12 |
|
12 | 13 | type IConnector interface { |
@@ -136,9 +137,30 @@ func pumpConnections(ctx context.Context, frontendConn, backendConn net.Conn) { |
136 | 137 | } |
137 | 138 |
|
138 | 139 | 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") |
142 | 163 | } |
143 | | - logrus.WithField("amount", amount).Infof("Finished relay %s->%s", from, to) |
| 164 | + |
| 165 | + logrus.Infof("Finished relay %s->%s", from, to) |
144 | 166 | } |
0 commit comments