support regex and player name sniff

This commit is contained in:
iceBear67
2026-07-15 21:26:50 +08:00
parent 3a5ad7e318
commit ada07e0e36
11 changed files with 250 additions and 49 deletions
+31
View File
@@ -63,6 +63,37 @@ func TestRoundTrip(t *testing.T) {
}
}
// TestRegexPatternMatch verifies a wildcard regex pattern routes a matching
// player (whole-hostname, case-insensitive) and drops a non-matching one, and
// that the client maps the hub-echoed pattern back to its destination.
func TestRegexPatternMatch(t *testing.T) {
const psk = "e2e-regex"
port := freePort(t)
hubAddr := fmt.Sprintf("127.0.0.1:%d", port)
startHub(t, port, psk)
dest := newMockDest(t, modeEcho)
startClient(t, hubAddr, psk, 2, []client.Mapping{
{Pattern: `mc\d+\.local`, Destination: dest.addr},
})
// Matches the regex (digit wildcard, mixed case); the whole hostname matches.
pc := dialPlayer(t, hubAddr, "MC7.Local")
defer pc.Close()
playerEcho(t, pc, []byte("regex hello"))
ev := dest.waitEvent(t, 5*time.Second)
if ev.handshakeAddr != "MC7.Local" {
t.Fatalf("destination saw handshake address %q, want verbatim %q", ev.handshakeAddr, "MC7.Local")
}
// A host that does not fully match the pattern is dropped ('\d+' needs a digit).
pc2 := dialPlayer(t, hubAddr, "mc.local")
defer pc2.Close()
_ = pc2.SetReadDeadline(time.Now().Add(3 * time.Second))
if _, err := pc2.Read(make([]byte, 16)); err == nil {
t.Fatalf("expected the hub to drop a host that does not match the regex")
}
}
// TestLargeTransfer pushes a multi-megabyte payload both ways to exercise mux
// framing and back-pressure.
func TestLargeTransfer(t *testing.T) {