|
| 1 | +package netmc |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "net" |
| 6 | + "testing" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/go-logr/logr" |
| 10 | + |
| 11 | + "go.minekube.com/gate/pkg/edition/java/proto/codec" |
| 12 | + "go.minekube.com/gate/pkg/edition/java/proto/packet" |
| 13 | + "go.minekube.com/gate/pkg/edition/java/proto/state" |
| 14 | + "go.minekube.com/gate/pkg/edition/java/proto/version" |
| 15 | + "go.minekube.com/gate/pkg/gate/proto" |
| 16 | + "go.minekube.com/gate/pkg/internal/packetlimiter" |
| 17 | +) |
| 18 | + |
| 19 | +type noopSessionHandler struct{} |
| 20 | + |
| 21 | +func (noopSessionHandler) HandlePacket(*proto.PacketContext) {} |
| 22 | +func (noopSessionHandler) Disconnected() {} |
| 23 | +func (noopSessionHandler) Activated() {} |
| 24 | +func (noopSessionHandler) Deactivated() {} |
| 25 | + |
| 26 | +// A client that floods serverbound packets past its rate limit must have its |
| 27 | +// connection closed by the read loop. |
| 28 | +func TestReadLoopClosesConnectionOnPacketFlood(t *testing.T) { |
| 29 | + client, server := net.Pipe() |
| 30 | + defer client.Close() |
| 31 | + |
| 32 | + // 1 packet/s over a 1s window: the 2nd packet in the window trips the limit. |
| 33 | + limiter := packetlimiter.New(1, -1, time.Second) |
| 34 | + conn, startReadLoop := NewMinecraftConn( |
| 35 | + context.Background(), server, proto.ServerBound, |
| 36 | + 5*time.Second, 5*time.Second, 0, limiter, |
| 37 | + ) |
| 38 | + conn.SetActiveSessionHandler(state.Handshake, noopSessionHandler{}) |
| 39 | + |
| 40 | + done := make(chan struct{}) |
| 41 | + go func() { startReadLoop(); close(done) }() |
| 42 | + |
| 43 | + // Flood handshake packets from the client side. |
| 44 | + go func() { |
| 45 | + enc := codec.NewEncoder(client, proto.ServerBound, logr.Discard()) |
| 46 | + hs := &packet.Handshake{ |
| 47 | + ProtocolVersion: int(version.Minecraft_1_21.Protocol), |
| 48 | + ServerAddress: "localhost", |
| 49 | + Port: 25565, |
| 50 | + NextStatus: 1, |
| 51 | + } |
| 52 | + for i := 0; i < 10; i++ { |
| 53 | + if _, err := enc.WritePacket(hs); err != nil { |
| 54 | + return // pipe closed once the limiter kicked in |
| 55 | + } |
| 56 | + } |
| 57 | + }() |
| 58 | + |
| 59 | + select { |
| 60 | + case <-done: |
| 61 | + if !Closed(conn) { |
| 62 | + t.Fatal("read loop returned but connection is not closed") |
| 63 | + } |
| 64 | + case <-time.After(3 * time.Second): |
| 65 | + t.Fatal("connection was not closed after packet flood") |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +// Without a limiter, the same packet stream must NOT close the connection — this |
| 70 | +// guards against the flood test passing for an unrelated reason (e.g. a framing |
| 71 | +// or decode error). |
| 72 | +func TestReadLoopKeepsConnectionWithoutLimiter(t *testing.T) { |
| 73 | + client, server := net.Pipe() |
| 74 | + defer client.Close() |
| 75 | + |
| 76 | + conn, startReadLoop := NewMinecraftConn( |
| 77 | + context.Background(), server, proto.ServerBound, |
| 78 | + 5*time.Second, 5*time.Second, 0, nil, // no limiter |
| 79 | + ) |
| 80 | + conn.SetActiveSessionHandler(state.Handshake, noopSessionHandler{}) |
| 81 | + |
| 82 | + done := make(chan struct{}) |
| 83 | + go func() { startReadLoop(); close(done) }() |
| 84 | + |
| 85 | + enc := codec.NewEncoder(client, proto.ServerBound, logr.Discard()) |
| 86 | + hs := &packet.Handshake{ |
| 87 | + ProtocolVersion: int(version.Minecraft_1_21.Protocol), |
| 88 | + ServerAddress: "localhost", |
| 89 | + Port: 25565, |
| 90 | + NextStatus: 1, |
| 91 | + } |
| 92 | + for i := 0; i < 10; i++ { |
| 93 | + if _, err := enc.WritePacket(hs); err != nil { |
| 94 | + t.Fatalf("write %d failed (connection closed unexpectedly): %v", i, err) |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + select { |
| 99 | + case <-done: |
| 100 | + t.Fatal("connection closed without a limiter configured") |
| 101 | + case <-time.After(200 * time.Millisecond): |
| 102 | + // Still open after processing the flood, as expected. |
| 103 | + } |
| 104 | +} |
0 commit comments