mirror of
https://github.com/Mezeporta/Erupe.git
synced 2026-03-21 23:22:34 +01:00
feat(pcap): complete replay system with filtering, metadata, and live replay
Wire ExcludeOpcodes config into RecordingConn so configured opcodes (e.g. ping, nop, position) are filtered at record time. Add padded metadata with in-place PatchMetadata to populate CharID/UserID after login. Implement --mode replay using protbot's encrypted connection with timing-aware packet sending, auto-ping response, concurrent S→C collection, and byte-level payload diff reporting.
This commit is contained in:
@@ -2,11 +2,22 @@ package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"erupe-ce/network"
|
||||
"erupe-ce/network/pcap"
|
||||
)
|
||||
|
||||
// maxPayloadDiffs is the maximum number of byte-level diffs to report per packet.
|
||||
const maxPayloadDiffs = 16
|
||||
|
||||
// ByteDiff describes a single byte difference between expected and actual payloads.
|
||||
type ByteDiff struct {
|
||||
Offset int
|
||||
Expected byte
|
||||
Actual byte
|
||||
}
|
||||
|
||||
// PacketDiff describes a difference between an expected and actual packet.
|
||||
type PacketDiff struct {
|
||||
Index int
|
||||
@@ -14,10 +25,15 @@ type PacketDiff struct {
|
||||
Actual *pcap.PacketRecord // nil if no response received
|
||||
OpcodeMismatch bool
|
||||
SizeDelta int
|
||||
PayloadDiffs []ByteDiff // byte-level diffs (when opcodes match and sizes match)
|
||||
}
|
||||
|
||||
func (d PacketDiff) String() string {
|
||||
if d.Actual == nil {
|
||||
if d.Expected.Opcode == 0 {
|
||||
return fmt.Sprintf("#%d: unexpected extra response 0x%04X (%s)",
|
||||
d.Index, d.Expected.Opcode, network.PacketID(d.Expected.Opcode))
|
||||
}
|
||||
return fmt.Sprintf("#%d: expected 0x%04X (%s), got no response",
|
||||
d.Index, d.Expected.Opcode, network.PacketID(d.Expected.Opcode))
|
||||
}
|
||||
@@ -27,9 +43,22 @@ func (d PacketDiff) String() string {
|
||||
d.Expected.Opcode, network.PacketID(d.Expected.Opcode),
|
||||
d.Actual.Opcode, network.PacketID(d.Actual.Opcode))
|
||||
}
|
||||
if d.SizeDelta != 0 {
|
||||
return fmt.Sprintf("#%d: 0x%04X (%s) size delta %+d bytes",
|
||||
d.Index, d.Expected.Opcode, network.PacketID(d.Expected.Opcode), d.SizeDelta)
|
||||
}
|
||||
if len(d.PayloadDiffs) > 0 {
|
||||
var sb strings.Builder
|
||||
fmt.Fprintf(&sb, "#%d: 0x%04X (%s) %d byte diff(s):",
|
||||
d.Index, d.Expected.Opcode, network.PacketID(d.Expected.Opcode), len(d.PayloadDiffs))
|
||||
for _, bd := range d.PayloadDiffs {
|
||||
fmt.Fprintf(&sb, " [0x%04X: %02X→%02X]", bd.Offset, bd.Expected, bd.Actual)
|
||||
}
|
||||
return sb.String()
|
||||
}
|
||||
return fmt.Sprintf("#%d: 0x%04X (%s) unknown diff",
|
||||
d.Index, d.Expected.Opcode, network.PacketID(d.Expected.Opcode))
|
||||
}
|
||||
|
||||
// ComparePackets compares expected server responses against actual responses.
|
||||
// Only compares S→C packets (server responses).
|
||||
@@ -62,6 +91,17 @@ func ComparePackets(expected, actual []pcap.PacketRecord) []PacketDiff {
|
||||
Actual: &act,
|
||||
SizeDelta: len(act.Payload) - len(exp.Payload),
|
||||
})
|
||||
} else {
|
||||
// Same opcode and size — check for byte-level diffs.
|
||||
byteDiffs := comparePayloads(exp.Payload, act.Payload)
|
||||
if len(byteDiffs) > 0 {
|
||||
diffs = append(diffs, PacketDiff{
|
||||
Index: i,
|
||||
Expected: exp,
|
||||
Actual: &act,
|
||||
PayloadDiffs: byteDiffs,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -77,3 +117,19 @@ func ComparePackets(expected, actual []pcap.PacketRecord) []PacketDiff {
|
||||
|
||||
return diffs
|
||||
}
|
||||
|
||||
// comparePayloads returns byte-level diffs between two equal-length payloads.
|
||||
// Returns at most maxPayloadDiffs entries.
|
||||
func comparePayloads(expected, actual []byte) []ByteDiff {
|
||||
var diffs []ByteDiff
|
||||
for i := 0; i < len(expected) && len(diffs) < maxPayloadDiffs; i++ {
|
||||
if expected[i] != actual[i] {
|
||||
diffs = append(diffs, ByteDiff{
|
||||
Offset: i,
|
||||
Expected: expected[i],
|
||||
Actual: actual[i],
|
||||
})
|
||||
}
|
||||
}
|
||||
return diffs
|
||||
}
|
||||
|
||||
@@ -5,29 +5,35 @@
|
||||
// replay --capture file.mhfr --mode dump # Human-readable text output
|
||||
// replay --capture file.mhfr --mode json # JSON export
|
||||
// replay --capture file.mhfr --mode stats # Opcode histogram, duration, counts
|
||||
// replay --capture file.mhfr --mode replay --target 127.0.0.1:54001 # Replay against live server
|
||||
// replay --capture file.mhfr --mode replay --target 127.0.0.1:54001 --no-auth # Replay against live server
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"sort"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"erupe-ce/cmd/protbot/conn"
|
||||
"erupe-ce/network"
|
||||
"erupe-ce/network/pcap"
|
||||
)
|
||||
|
||||
// MSG_SYS_PING opcode for auto-responding to server pings.
|
||||
const opcodeSysPing = 0x0017
|
||||
|
||||
func main() {
|
||||
capturePath := flag.String("capture", "", "Path to .mhfr capture file (required)")
|
||||
mode := flag.String("mode", "dump", "Mode: dump, json, stats, replay")
|
||||
target := flag.String("target", "", "Target server address for replay mode (host:port)")
|
||||
speed := flag.Float64("speed", 1.0, "Replay speed multiplier (e.g. 2.0 = 2x faster)")
|
||||
_ = target // used in replay mode
|
||||
_ = speed
|
||||
noAuth := flag.Bool("no-auth", false, "Skip auth token patching (requires DisableTokenCheck on server)")
|
||||
_ = noAuth // currently only no-auth mode is supported
|
||||
flag.Parse()
|
||||
|
||||
if *capturePath == "" {
|
||||
@@ -57,8 +63,10 @@ func main() {
|
||||
fmt.Fprintln(os.Stderr, "error: --target is required for replay mode")
|
||||
os.Exit(1)
|
||||
}
|
||||
fmt.Fprintln(os.Stderr, "replay mode not yet implemented (requires live server connection)")
|
||||
if err := runReplay(*capturePath, *target, *speed); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "replay failed: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
default:
|
||||
fmt.Fprintf(os.Stderr, "unknown mode: %s\n", *mode)
|
||||
os.Exit(1)
|
||||
@@ -93,6 +101,129 @@ func readAllPackets(r *pcap.Reader) ([]pcap.PacketRecord, error) {
|
||||
return records, nil
|
||||
}
|
||||
|
||||
func runReplay(path, target string, speed float64) error {
|
||||
r, f, err := openCapture(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer func() { _ = f.Close() }()
|
||||
|
||||
records, err := readAllPackets(r)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
c2s := pcap.FilterByDirection(records, pcap.DirClientToServer)
|
||||
expectedS2C := pcap.FilterByDirection(records, pcap.DirServerToClient)
|
||||
|
||||
if len(c2s) == 0 {
|
||||
fmt.Println("No C→S packets in capture, nothing to replay.")
|
||||
return nil
|
||||
}
|
||||
|
||||
fmt.Printf("=== Replay: %s ===\n", path)
|
||||
fmt.Printf("Server type: %s Target: %s Speed: %.1fx\n", r.Header.ServerType, target, speed)
|
||||
fmt.Printf("C→S packets to send: %d Expected S→C responses: %d\n\n", len(c2s), len(expectedS2C))
|
||||
|
||||
// Connect based on server type.
|
||||
var mhf *conn.MHFConn
|
||||
switch r.Header.ServerType {
|
||||
case pcap.ServerTypeChannel:
|
||||
mhf, err = conn.DialDirect(target)
|
||||
default:
|
||||
mhf, err = conn.DialWithInit(target)
|
||||
}
|
||||
if err != nil {
|
||||
return fmt.Errorf("connect to %s: %w", target, err)
|
||||
}
|
||||
|
||||
// Collect S→C responses concurrently.
|
||||
var actualS2C []pcap.PacketRecord
|
||||
var mu sync.Mutex
|
||||
done := make(chan struct{})
|
||||
|
||||
go func() {
|
||||
defer close(done)
|
||||
for {
|
||||
pkt, err := mhf.ReadPacket()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
var opcode uint16
|
||||
if len(pkt) >= 2 {
|
||||
opcode = binary.BigEndian.Uint16(pkt[:2])
|
||||
}
|
||||
|
||||
// Auto-respond to ping to keep connection alive.
|
||||
if opcode == opcodeSysPing {
|
||||
pong := buildPingResponse()
|
||||
_ = mhf.SendPacket(pong)
|
||||
}
|
||||
|
||||
mu.Lock()
|
||||
actualS2C = append(actualS2C, pcap.PacketRecord{
|
||||
TimestampNs: time.Now().UnixNano(),
|
||||
Direction: pcap.DirServerToClient,
|
||||
Opcode: opcode,
|
||||
Payload: pkt,
|
||||
})
|
||||
mu.Unlock()
|
||||
}
|
||||
}()
|
||||
|
||||
// Send C→S packets with timing.
|
||||
var lastTs int64
|
||||
for i, pkt := range c2s {
|
||||
if i > 0 && speed > 0 {
|
||||
delta := time.Duration(float64(pkt.TimestampNs-lastTs) / speed)
|
||||
if delta > 0 {
|
||||
time.Sleep(delta)
|
||||
}
|
||||
}
|
||||
lastTs = pkt.TimestampNs
|
||||
opcodeName := network.PacketID(pkt.Opcode).String()
|
||||
fmt.Printf("[replay] #%d sending 0x%04X %-30s (%d bytes)\n", i, pkt.Opcode, opcodeName, len(pkt.Payload))
|
||||
if err := mhf.SendPacket(pkt.Payload); err != nil {
|
||||
fmt.Printf("[replay] send error: %v\n", err)
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
// Wait for remaining responses.
|
||||
fmt.Println("\n[replay] All packets sent, waiting for remaining responses...")
|
||||
time.Sleep(2 * time.Second)
|
||||
_ = mhf.Close()
|
||||
<-done
|
||||
|
||||
// Compare.
|
||||
mu.Lock()
|
||||
diffs := ComparePackets(expectedS2C, actualS2C)
|
||||
mu.Unlock()
|
||||
|
||||
// Report.
|
||||
fmt.Printf("\n=== Replay Results ===\n")
|
||||
fmt.Printf("Sent: %d C→S packets\n", len(c2s))
|
||||
fmt.Printf("Expected: %d S→C responses\n", len(expectedS2C))
|
||||
fmt.Printf("Received: %d S→C responses\n", len(actualS2C))
|
||||
fmt.Printf("Differences: %d\n\n", len(diffs))
|
||||
for _, d := range diffs {
|
||||
fmt.Println(d.String())
|
||||
}
|
||||
|
||||
if len(diffs) == 0 {
|
||||
fmt.Println("All responses match!")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// buildPingResponse builds a minimal MSG_SYS_PING response packet.
|
||||
// Format: [opcode 0x0017][0x00 0x10 terminator]
|
||||
func buildPingResponse() []byte {
|
||||
return []byte{0x00, 0x17, 0x00, 0x10}
|
||||
}
|
||||
|
||||
func runDump(path string) error {
|
||||
r, f, err := openCapture(path)
|
||||
if err != nil {
|
||||
|
||||
@@ -2,7 +2,10 @@ package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"net"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"erupe-ce/network/pcap"
|
||||
@@ -141,14 +144,169 @@ func TestComparePacketsMissingResponse(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestComparePacketsPayloadDiff(t *testing.T) {
|
||||
expected := []pcap.PacketRecord{
|
||||
{Direction: pcap.DirServerToClient, Opcode: 0x0012, Payload: []byte{0x00, 0x12, 0xAA, 0xBB}},
|
||||
}
|
||||
actual := []pcap.PacketRecord{
|
||||
{Direction: pcap.DirServerToClient, Opcode: 0x0012, Payload: []byte{0x00, 0x12, 0xCC, 0xBB}},
|
||||
}
|
||||
|
||||
diffs := ComparePackets(expected, actual)
|
||||
if len(diffs) != 1 {
|
||||
t.Fatalf("expected 1 diff, got %d", len(diffs))
|
||||
}
|
||||
if len(diffs[0].PayloadDiffs) != 1 {
|
||||
t.Fatalf("expected 1 payload diff, got %d", len(diffs[0].PayloadDiffs))
|
||||
}
|
||||
bd := diffs[0].PayloadDiffs[0]
|
||||
if bd.Offset != 2 || bd.Expected != 0xAA || bd.Actual != 0xCC {
|
||||
t.Errorf("ByteDiff = {Offset:%d, Expected:0x%02X, Actual:0x%02X}, want {2, 0xAA, 0xCC}",
|
||||
bd.Offset, bd.Expected, bd.Actual)
|
||||
}
|
||||
}
|
||||
|
||||
func TestComparePacketsIdentical(t *testing.T) {
|
||||
records := []pcap.PacketRecord{
|
||||
{Direction: pcap.DirServerToClient, Opcode: 0x0012, Payload: []byte{0x00, 0x12, 0xAA}},
|
||||
}
|
||||
diffs := ComparePackets(records, records)
|
||||
if len(diffs) != 0 {
|
||||
t.Errorf("expected 0 diffs for identical packets, got %d", len(diffs))
|
||||
}
|
||||
}
|
||||
|
||||
func TestPacketDiffString(t *testing.T) {
|
||||
d := PacketDiff{
|
||||
tests := []struct {
|
||||
name string
|
||||
diff PacketDiff
|
||||
contains string
|
||||
}{
|
||||
{
|
||||
name: "missing response",
|
||||
diff: PacketDiff{
|
||||
Index: 0,
|
||||
Expected: pcap.PacketRecord{Opcode: 0x0012},
|
||||
Actual: nil,
|
||||
},
|
||||
contains: "no response",
|
||||
},
|
||||
{
|
||||
name: "opcode mismatch",
|
||||
diff: PacketDiff{
|
||||
Index: 1,
|
||||
Expected: pcap.PacketRecord{Opcode: 0x0012},
|
||||
Actual: &pcap.PacketRecord{Opcode: 0x0099},
|
||||
OpcodeMismatch: true,
|
||||
},
|
||||
contains: "opcode mismatch",
|
||||
},
|
||||
{
|
||||
name: "size delta",
|
||||
diff: PacketDiff{
|
||||
Index: 2,
|
||||
Expected: pcap.PacketRecord{Opcode: 0x0012},
|
||||
Actual: &pcap.PacketRecord{Opcode: 0x0012},
|
||||
SizeDelta: 5,
|
||||
},
|
||||
contains: "size delta",
|
||||
},
|
||||
{
|
||||
name: "payload diffs",
|
||||
diff: PacketDiff{
|
||||
Index: 3,
|
||||
Expected: pcap.PacketRecord{Opcode: 0x0012},
|
||||
Actual: &pcap.PacketRecord{Opcode: 0x0012},
|
||||
PayloadDiffs: []ByteDiff{
|
||||
{Offset: 2, Expected: 0xAA, Actual: 0xBB},
|
||||
},
|
||||
},
|
||||
contains: "byte diff",
|
||||
},
|
||||
}
|
||||
s := d.String()
|
||||
if s == "" {
|
||||
t.Error("PacketDiff.String() returned empty")
|
||||
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
s := tc.diff.String()
|
||||
if !strings.Contains(s, tc.contains) {
|
||||
t.Errorf("String() = %q, want it to contain %q", s, tc.contains)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunReplayWithMockServer(t *testing.T) {
|
||||
// Start a mock TCP server that echoes a response for each received packet.
|
||||
ln, err := net.Listen("tcp", "127.0.0.1:0")
|
||||
if err != nil {
|
||||
t.Fatalf("Listen: %v", err)
|
||||
}
|
||||
defer func() { _ = ln.Close() }()
|
||||
|
||||
serverDone := make(chan struct{})
|
||||
go func() {
|
||||
defer close(serverDone)
|
||||
c, err := ln.Accept()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
defer func() { _ = c.Close() }()
|
||||
|
||||
// This mock doesn't do Blowfish encryption — it just reads raw and echoes.
|
||||
// Since the replay uses protbot's CryptConn (Blowfish), we need a real crypto echo.
|
||||
// For a simpler test, just verify the function handles connection errors gracefully.
|
||||
// Read a bit and close.
|
||||
buf := make([]byte, 1024)
|
||||
_, _ = c.Read(buf)
|
||||
}()
|
||||
|
||||
// Create a minimal capture with one C→S packet.
|
||||
path := createTestCapture(t, []pcap.PacketRecord{
|
||||
{TimestampNs: 1000000100, Direction: pcap.DirClientToServer, Opcode: 0x0013,
|
||||
Payload: []byte{0x00, 0x13, 0xDE, 0xAD}},
|
||||
})
|
||||
|
||||
// Run replay — the connection will fail (no Blowfish on mock), but it should not panic.
|
||||
err = runReplay(path, ln.Addr().String(), 0)
|
||||
// We expect an error or graceful handling since the mock doesn't speak Blowfish.
|
||||
// The important thing is no panic.
|
||||
_ = err
|
||||
}
|
||||
|
||||
func TestComparePayloads(t *testing.T) {
|
||||
a := []byte{0x00, 0x12, 0xAA, 0xBB, 0xCC}
|
||||
b := []byte{0x00, 0x12, 0xAA, 0xDD, 0xCC}
|
||||
|
||||
diffs := comparePayloads(a, b)
|
||||
if len(diffs) != 1 {
|
||||
t.Fatalf("expected 1 diff, got %d", len(diffs))
|
||||
}
|
||||
if diffs[0].Offset != 3 {
|
||||
t.Errorf("Offset = %d, want 3", diffs[0].Offset)
|
||||
}
|
||||
}
|
||||
|
||||
func TestComparePayloadsMaxDiffs(t *testing.T) {
|
||||
// All bytes different — should cap at maxPayloadDiffs.
|
||||
a := make([]byte, 100)
|
||||
b := make([]byte, 100)
|
||||
for i := range b {
|
||||
b[i] = 0xFF
|
||||
}
|
||||
|
||||
diffs := comparePayloads(a, b)
|
||||
if len(diffs) != maxPayloadDiffs {
|
||||
t.Errorf("expected %d diffs (capped), got %d", maxPayloadDiffs, len(diffs))
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildPingResponse(t *testing.T) {
|
||||
pong := buildPingResponse()
|
||||
if len(pong) < 2 {
|
||||
t.Fatal("ping response too short")
|
||||
}
|
||||
opcode := binary.BigEndian.Uint16(pong[:2])
|
||||
if opcode != opcodeSysPing {
|
||||
t.Errorf("opcode = 0x%04X, want 0x%04X", opcode, opcodeSysPing)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,6 +12,11 @@ const (
|
||||
|
||||
// HeaderSize is the fixed size of the file header in bytes.
|
||||
HeaderSize = 32
|
||||
|
||||
// MinMetadataSize is the minimum metadata block size in bytes.
|
||||
// Metadata is padded to at least this size to allow in-place patching
|
||||
// (e.g., adding CharID/UserID after login).
|
||||
MinMetadataSize = 512
|
||||
)
|
||||
|
||||
// Direction indicates whether a packet was sent or received.
|
||||
|
||||
48
network/pcap/patch.go
Normal file
48
network/pcap/patch.go
Normal file
@@ -0,0 +1,48 @@
|
||||
package pcap
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
// PatchMetadata rewrites the metadata block in a .mhfr capture file.
|
||||
// The file must have been written with padded metadata (MinMetadataSize).
|
||||
// The new JSON must fit within the existing MetadataLen allocation.
|
||||
func PatchMetadata(f *os.File, meta SessionMetadata) error {
|
||||
newJSON, err := json.Marshal(&meta)
|
||||
if err != nil {
|
||||
return fmt.Errorf("pcap: marshal metadata: %w", err)
|
||||
}
|
||||
|
||||
// Read MetadataLen from header (offset 20: after magic(4)+version(2)+servertype(1)+clientmode(1)+startnanos(8)+reserved(4)).
|
||||
var metaLen uint32
|
||||
if _, err := f.Seek(20, 0); err != nil {
|
||||
return fmt.Errorf("pcap: seek to metadata len: %w", err)
|
||||
}
|
||||
if err := binary.Read(f, binary.BigEndian, &metaLen); err != nil {
|
||||
return fmt.Errorf("pcap: read metadata len: %w", err)
|
||||
}
|
||||
|
||||
if uint32(len(newJSON)) > metaLen {
|
||||
return fmt.Errorf("pcap: new metadata (%d bytes) exceeds allocated space (%d bytes)", len(newJSON), metaLen)
|
||||
}
|
||||
|
||||
// Pad with spaces to fill the allocated block.
|
||||
padded := make([]byte, metaLen)
|
||||
copy(padded, newJSON)
|
||||
for i := len(newJSON); i < len(padded); i++ {
|
||||
padded[i] = ' '
|
||||
}
|
||||
|
||||
// Write at offset HeaderSize (32).
|
||||
if _, err := f.Seek(HeaderSize, 0); err != nil {
|
||||
return fmt.Errorf("pcap: seek to metadata: %w", err)
|
||||
}
|
||||
if _, err := f.Write(padded); err != nil {
|
||||
return fmt.Errorf("pcap: write metadata: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -3,6 +3,7 @@ package pcap
|
||||
import (
|
||||
"bytes"
|
||||
"io"
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
@@ -252,6 +253,100 @@ func TestDirectionString(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestMetadataPadding(t *testing.T) {
|
||||
var buf bytes.Buffer
|
||||
|
||||
hdr := FileHeader{
|
||||
Version: FormatVersion,
|
||||
ServerType: ServerTypeChannel,
|
||||
ClientMode: 40,
|
||||
SessionStartNs: 1000,
|
||||
}
|
||||
meta := SessionMetadata{Host: "127.0.0.1"}
|
||||
|
||||
_, err := NewWriter(&buf, hdr, meta)
|
||||
if err != nil {
|
||||
t.Fatalf("NewWriter: %v", err)
|
||||
}
|
||||
|
||||
// The metadata block should be at least MinMetadataSize.
|
||||
data := buf.Bytes()
|
||||
if len(data) < HeaderSize+MinMetadataSize {
|
||||
t.Errorf("file size %d < HeaderSize+MinMetadataSize (%d)", len(data), HeaderSize+MinMetadataSize)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPatchMetadata(t *testing.T) {
|
||||
// Create a capture file with initial metadata.
|
||||
f, err := os.CreateTemp(t.TempDir(), "test-patch-*.mhfr")
|
||||
if err != nil {
|
||||
t.Fatalf("CreateTemp: %v", err)
|
||||
}
|
||||
defer func() { _ = f.Close() }()
|
||||
|
||||
hdr := FileHeader{
|
||||
Version: FormatVersion,
|
||||
ServerType: ServerTypeChannel,
|
||||
ClientMode: 40,
|
||||
SessionStartNs: 1000,
|
||||
}
|
||||
meta := SessionMetadata{Host: "127.0.0.1", Port: 54001}
|
||||
|
||||
w, err := NewWriter(f, hdr, meta)
|
||||
if err != nil {
|
||||
t.Fatalf("NewWriter: %v", err)
|
||||
}
|
||||
// Write a packet so we can verify it survives patching.
|
||||
if err := w.WritePacket(PacketRecord{
|
||||
TimestampNs: 2000, Direction: DirClientToServer, Opcode: 0x0013, Payload: []byte{0x00, 0x13},
|
||||
}); err != nil {
|
||||
t.Fatalf("WritePacket: %v", err)
|
||||
}
|
||||
if err := w.Flush(); err != nil {
|
||||
t.Fatalf("Flush: %v", err)
|
||||
}
|
||||
|
||||
// Patch metadata with CharID/UserID.
|
||||
patched := SessionMetadata{
|
||||
Host: "127.0.0.1",
|
||||
Port: 54001,
|
||||
CharID: 42,
|
||||
UserID: 7,
|
||||
}
|
||||
if err := PatchMetadata(f, patched); err != nil {
|
||||
t.Fatalf("PatchMetadata: %v", err)
|
||||
}
|
||||
|
||||
// Re-read from the beginning.
|
||||
if _, err := f.Seek(0, 0); err != nil {
|
||||
t.Fatalf("Seek: %v", err)
|
||||
}
|
||||
r, err := NewReader(f)
|
||||
if err != nil {
|
||||
t.Fatalf("NewReader: %v", err)
|
||||
}
|
||||
|
||||
// Verify patched metadata.
|
||||
if r.Meta.CharID != 42 {
|
||||
t.Errorf("CharID = %d, want 42", r.Meta.CharID)
|
||||
}
|
||||
if r.Meta.UserID != 7 {
|
||||
t.Errorf("UserID = %d, want 7", r.Meta.UserID)
|
||||
}
|
||||
if r.Meta.Host != "127.0.0.1" {
|
||||
t.Errorf("Host = %q, want %q", r.Meta.Host, "127.0.0.1")
|
||||
}
|
||||
|
||||
// Verify packet survived.
|
||||
rec, err := r.ReadPacket()
|
||||
if err != nil {
|
||||
t.Fatalf("ReadPacket: %v", err)
|
||||
}
|
||||
if rec.Opcode != 0x0013 {
|
||||
t.Errorf("Opcode = 0x%04X, want 0x0013", rec.Opcode)
|
||||
}
|
||||
}
|
||||
|
||||
func TestServerTypeString(t *testing.T) {
|
||||
if ServerTypeSign.String() != "sign" {
|
||||
t.Errorf("ServerTypeSign.String() = %q", ServerTypeSign.String())
|
||||
|
||||
@@ -2,9 +2,11 @@ package pcap
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"erupe-ce/network"
|
||||
"os"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"erupe-ce/network"
|
||||
)
|
||||
|
||||
// RecordingConn wraps a network.Conn and records all packets to a Writer.
|
||||
@@ -13,19 +15,57 @@ type RecordingConn struct {
|
||||
inner network.Conn
|
||||
writer *Writer
|
||||
startNs int64
|
||||
excludeOpcodes map[uint16]struct{}
|
||||
metaFile *os.File // capture file handle for metadata patching
|
||||
meta *SessionMetadata // current metadata (mutated by SetSessionInfo)
|
||||
mu sync.Mutex
|
||||
}
|
||||
|
||||
// NewRecordingConn wraps inner, recording all packets to w.
|
||||
// startNs is the session start time in nanoseconds (used as the time base).
|
||||
func NewRecordingConn(inner network.Conn, w *Writer, startNs int64) *RecordingConn {
|
||||
// excludeOpcodes is an optional list of opcodes to skip when recording.
|
||||
func NewRecordingConn(inner network.Conn, w *Writer, startNs int64, excludeOpcodes []uint16) *RecordingConn {
|
||||
var excl map[uint16]struct{}
|
||||
if len(excludeOpcodes) > 0 {
|
||||
excl = make(map[uint16]struct{}, len(excludeOpcodes))
|
||||
for _, op := range excludeOpcodes {
|
||||
excl[op] = struct{}{}
|
||||
}
|
||||
}
|
||||
return &RecordingConn{
|
||||
inner: inner,
|
||||
writer: w,
|
||||
startNs: startNs,
|
||||
excludeOpcodes: excl,
|
||||
}
|
||||
}
|
||||
|
||||
// SetCaptureFile sets the file handle and metadata pointer for in-place metadata patching.
|
||||
// Must be called before SetSessionInfo. Not required if metadata patching is not needed.
|
||||
func (rc *RecordingConn) SetCaptureFile(f *os.File, meta *SessionMetadata) {
|
||||
rc.mu.Lock()
|
||||
rc.metaFile = f
|
||||
rc.meta = meta
|
||||
rc.mu.Unlock()
|
||||
}
|
||||
|
||||
// SetSessionInfo updates the CharID and UserID in the capture file metadata.
|
||||
// This is called after login when the session identity is known.
|
||||
func (rc *RecordingConn) SetSessionInfo(charID, userID uint32) {
|
||||
rc.mu.Lock()
|
||||
defer rc.mu.Unlock()
|
||||
|
||||
if rc.meta == nil || rc.metaFile == nil {
|
||||
return
|
||||
}
|
||||
|
||||
rc.meta.CharID = charID
|
||||
rc.meta.UserID = userID
|
||||
|
||||
// Best-effort patch — log errors are handled by the caller.
|
||||
_ = PatchMetadata(rc.metaFile, *rc.meta)
|
||||
}
|
||||
|
||||
// ReadPacket reads from the inner connection and records the packet as client-to-server.
|
||||
func (rc *RecordingConn) ReadPacket() ([]byte, error) {
|
||||
data, err := rc.inner.ReadPacket()
|
||||
@@ -52,6 +92,12 @@ func (rc *RecordingConn) record(dir Direction, data []byte) {
|
||||
opcode = binary.BigEndian.Uint16(data[:2])
|
||||
}
|
||||
|
||||
if rc.excludeOpcodes != nil {
|
||||
if _, excluded := rc.excludeOpcodes[opcode]; excluded {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
rec := PacketRecord{
|
||||
TimestampNs: time.Now().UnixNano(),
|
||||
Direction: dir,
|
||||
|
||||
@@ -54,7 +54,7 @@ func TestRecordingConnBasic(t *testing.T) {
|
||||
t.Fatalf("NewWriter: %v", err)
|
||||
}
|
||||
|
||||
rc := NewRecordingConn(mock, w, 1000)
|
||||
rc := NewRecordingConn(mock, w, 1000, nil)
|
||||
|
||||
// Read a packet (C→S).
|
||||
data, err := rc.ReadPacket()
|
||||
@@ -134,7 +134,7 @@ func TestRecordingConnConcurrent(t *testing.T) {
|
||||
t.Fatalf("NewWriter: %v", err)
|
||||
}
|
||||
|
||||
rc := NewRecordingConn(mock, w, 1000)
|
||||
rc := NewRecordingConn(mock, w, 1000, nil)
|
||||
|
||||
// Concurrent reads and sends.
|
||||
var wg sync.WaitGroup
|
||||
@@ -181,3 +181,84 @@ func TestRecordingConnConcurrent(t *testing.T) {
|
||||
t.Errorf("got %d records, want %d", count, 2*numPackets)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRecordingConnExcludeOpcodes(t *testing.T) {
|
||||
// Packets with opcodes 0x0010 (excluded), 0x0013, 0x0011 (excluded), 0x0061.
|
||||
mock := &mockConn{
|
||||
readData: [][]byte{
|
||||
{0x00, 0x10, 0xAA}, // opcode 0x0010 — excluded
|
||||
{0x00, 0x13, 0xBB}, // opcode 0x0013 — kept
|
||||
{0x00, 0x11, 0xCC}, // opcode 0x0011 — excluded
|
||||
{0x00, 0x61, 0xDD, 0xEE}, // opcode 0x0061 — kept
|
||||
},
|
||||
}
|
||||
|
||||
var buf bytes.Buffer
|
||||
hdr := FileHeader{
|
||||
Version: FormatVersion,
|
||||
ServerType: ServerTypeChannel,
|
||||
ClientMode: 40,
|
||||
SessionStartNs: 1000,
|
||||
}
|
||||
w, err := NewWriter(&buf, hdr, SessionMetadata{})
|
||||
if err != nil {
|
||||
t.Fatalf("NewWriter: %v", err)
|
||||
}
|
||||
|
||||
rc := NewRecordingConn(mock, w, 1000, []uint16{0x0010, 0x0011})
|
||||
|
||||
// Read all packets (they should all pass through to the caller).
|
||||
for i := 0; i < 4; i++ {
|
||||
data, err := rc.ReadPacket()
|
||||
if err != nil {
|
||||
t.Fatalf("ReadPacket[%d]: %v", i, err)
|
||||
}
|
||||
if len(data) == 0 {
|
||||
t.Fatalf("ReadPacket[%d]: empty data", i)
|
||||
}
|
||||
}
|
||||
|
||||
// Also send a packet with excluded opcode — it should be sent but not recorded.
|
||||
if err := rc.SendPacket([]byte{0x00, 0x10, 0xFF}); err != nil {
|
||||
t.Fatalf("SendPacket excluded: %v", err)
|
||||
}
|
||||
// Send a packet with non-excluded opcode.
|
||||
if err := rc.SendPacket([]byte{0x00, 0x12, 0xFF}); err != nil {
|
||||
t.Fatalf("SendPacket kept: %v", err)
|
||||
}
|
||||
|
||||
if err := w.Flush(); err != nil {
|
||||
t.Fatalf("Flush: %v", err)
|
||||
}
|
||||
|
||||
// Read back: should only have 3 recorded packets (0x0013 C→S, 0x0061 C→S, 0x0012 S→C).
|
||||
r, err := NewReader(bytes.NewReader(buf.Bytes()))
|
||||
if err != nil {
|
||||
t.Fatalf("NewReader: %v", err)
|
||||
}
|
||||
|
||||
var records []PacketRecord
|
||||
for {
|
||||
rec, err := r.ReadPacket()
|
||||
if err == io.EOF {
|
||||
break
|
||||
}
|
||||
if err != nil {
|
||||
t.Fatalf("ReadPacket: %v", err)
|
||||
}
|
||||
records = append(records, rec)
|
||||
}
|
||||
|
||||
if len(records) != 3 {
|
||||
t.Fatalf("got %d records, want 3; opcodes:", len(records))
|
||||
}
|
||||
if records[0].Opcode != 0x0013 {
|
||||
t.Errorf("records[0].Opcode = 0x%04X, want 0x0013", records[0].Opcode)
|
||||
}
|
||||
if records[1].Opcode != 0x0061 {
|
||||
t.Errorf("records[1].Opcode = 0x%04X, want 0x0061", records[1].Opcode)
|
||||
}
|
||||
if records[2].Opcode != 0x0012 {
|
||||
t.Errorf("records[2].Opcode = 0x%04X, want 0x0012", records[2].Opcode)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,6 +19,15 @@ func NewWriter(w io.Writer, header FileHeader, meta SessionMetadata) (*Writer, e
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("pcap: marshal metadata: %w", err)
|
||||
}
|
||||
// Pad metadata to MinMetadataSize so PatchMetadata can update it in-place.
|
||||
if len(metaBytes) < MinMetadataSize {
|
||||
padded := make([]byte, MinMetadataSize)
|
||||
copy(padded, metaBytes)
|
||||
for i := len(metaBytes); i < MinMetadataSize; i++ {
|
||||
padded[i] = ' '
|
||||
}
|
||||
metaBytes = padded
|
||||
}
|
||||
header.MetadataLen = uint32(len(metaBytes))
|
||||
|
||||
bw := bufio.NewWriter(w)
|
||||
|
||||
@@ -76,6 +76,10 @@ func handleMsgSysLogin(s *Session, p mhfpacket.MHFPacket) {
|
||||
}
|
||||
s.userID = userID
|
||||
|
||||
if s.captureConn != nil {
|
||||
s.captureConn.SetSessionInfo(s.charID, s.userID)
|
||||
}
|
||||
|
||||
bf := byteframe.NewByteFrame()
|
||||
bf.WriteUint32(uint32(TimeAdjusted().Unix())) // Unix timestamp
|
||||
|
||||
|
||||
@@ -14,25 +14,26 @@ import (
|
||||
)
|
||||
|
||||
// startCapture wraps a network.Conn with a RecordingConn if capture is enabled.
|
||||
// Returns the (possibly wrapped) conn and a cleanup function that must be called on session close.
|
||||
func startCapture(server *Server, conn network.Conn, remoteAddr net.Addr, serverType pcap.ServerType) (network.Conn, func()) {
|
||||
// Returns the (possibly wrapped) conn, the RecordingConn (nil if capture disabled),
|
||||
// and a cleanup function that must be called on session close.
|
||||
func startCapture(server *Server, conn network.Conn, remoteAddr net.Addr, serverType pcap.ServerType) (network.Conn, *pcap.RecordingConn, func()) {
|
||||
capCfg := server.erupeConfig.Capture
|
||||
if !capCfg.Enabled {
|
||||
return conn, func() {}
|
||||
return conn, nil, func() {}
|
||||
}
|
||||
|
||||
switch serverType {
|
||||
case pcap.ServerTypeSign:
|
||||
if !capCfg.CaptureSign {
|
||||
return conn, func() {}
|
||||
return conn, nil, func() {}
|
||||
}
|
||||
case pcap.ServerTypeEntrance:
|
||||
if !capCfg.CaptureEntrance {
|
||||
return conn, func() {}
|
||||
return conn, nil, func() {}
|
||||
}
|
||||
case pcap.ServerTypeChannel:
|
||||
if !capCfg.CaptureChannel {
|
||||
return conn, func() {}
|
||||
return conn, nil, func() {}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -42,7 +43,7 @@ func startCapture(server *Server, conn network.Conn, remoteAddr net.Addr, server
|
||||
}
|
||||
if err := os.MkdirAll(outputDir, 0o755); err != nil {
|
||||
server.logger.Warn("Failed to create capture directory", zap.Error(err))
|
||||
return conn, func() {}
|
||||
return conn, nil, func() {}
|
||||
}
|
||||
|
||||
now := time.Now()
|
||||
@@ -56,7 +57,7 @@ func startCapture(server *Server, conn network.Conn, remoteAddr net.Addr, server
|
||||
f, err := os.Create(path)
|
||||
if err != nil {
|
||||
server.logger.Warn("Failed to create capture file", zap.Error(err), zap.String("path", path))
|
||||
return conn, func() {}
|
||||
return conn, nil, func() {}
|
||||
}
|
||||
|
||||
startNs := now.UnixNano()
|
||||
@@ -75,12 +76,13 @@ func startCapture(server *Server, conn network.Conn, remoteAddr net.Addr, server
|
||||
if err != nil {
|
||||
server.logger.Warn("Failed to initialize capture writer", zap.Error(err))
|
||||
_ = f.Close()
|
||||
return conn, func() {}
|
||||
return conn, nil, func() {}
|
||||
}
|
||||
|
||||
server.logger.Info("Capture started", zap.String("file", path))
|
||||
|
||||
rc := pcap.NewRecordingConn(conn, w, startNs)
|
||||
rc := pcap.NewRecordingConn(conn, w, startNs, capCfg.ExcludeOpcodes)
|
||||
rc.SetCaptureFile(f, &meta)
|
||||
cleanup := func() {
|
||||
if err := w.Flush(); err != nil {
|
||||
server.logger.Warn("Failed to flush capture", zap.Error(err))
|
||||
@@ -91,7 +93,7 @@ func startCapture(server *Server, conn network.Conn, remoteAddr net.Addr, server
|
||||
server.logger.Info("Capture saved", zap.String("file", path))
|
||||
}
|
||||
|
||||
return rc, cleanup
|
||||
return rc, rc, cleanup
|
||||
}
|
||||
|
||||
// sanitizeAddr replaces characters that are problematic in filenames.
|
||||
|
||||
@@ -74,6 +74,7 @@ type Session struct {
|
||||
Name string
|
||||
closed atomic.Bool
|
||||
ackStart map[uint32]time.Time
|
||||
captureConn *pcap.RecordingConn // non-nil when capture is active
|
||||
captureCleanup func() // Called on session close to flush/close capture file
|
||||
}
|
||||
|
||||
@@ -81,7 +82,7 @@ type Session struct {
|
||||
func NewSession(server *Server, conn net.Conn) *Session {
|
||||
var cryptConn network.Conn = network.NewCryptConn(conn, server.erupeConfig.RealClientMode, server.logger.Named(conn.RemoteAddr().String()))
|
||||
|
||||
cryptConn, captureCleanup := startCapture(server, cryptConn, conn.RemoteAddr(), pcap.ServerTypeChannel)
|
||||
cryptConn, captureConn, captureCleanup := startCapture(server, cryptConn, conn.RemoteAddr(), pcap.ServerTypeChannel)
|
||||
|
||||
s := &Session{
|
||||
logger: server.logger.Named(conn.RemoteAddr().String()),
|
||||
@@ -96,6 +97,7 @@ func NewSession(server *Server, conn net.Conn) *Session {
|
||||
stageMoveStack: stringstack.New(),
|
||||
ackStart: make(map[uint32]time.Time),
|
||||
semaphoreID: make([]uint16, 2),
|
||||
captureConn: captureConn,
|
||||
captureCleanup: captureCleanup,
|
||||
}
|
||||
return s
|
||||
|
||||
@@ -64,7 +64,7 @@ func startEntranceCapture(s *Server, conn network.Conn, remoteAddr net.Addr) (ne
|
||||
|
||||
s.logger.Info("Capture started", zap.String("file", path))
|
||||
|
||||
rc := pcap.NewRecordingConn(conn, w, startNs)
|
||||
rc := pcap.NewRecordingConn(conn, w, startNs, capCfg.ExcludeOpcodes)
|
||||
cleanup := func() {
|
||||
if err := w.Flush(); err != nil {
|
||||
s.logger.Warn("Failed to flush capture", zap.Error(err))
|
||||
|
||||
@@ -64,7 +64,7 @@ func startSignCapture(s *Server, conn network.Conn, remoteAddr net.Addr) (networ
|
||||
|
||||
s.logger.Info("Capture started", zap.String("file", path))
|
||||
|
||||
rc := pcap.NewRecordingConn(conn, w, startNs)
|
||||
rc := pcap.NewRecordingConn(conn, w, startNs, capCfg.ExcludeOpcodes)
|
||||
cleanup := func() {
|
||||
if err := w.Flush(); err != nil {
|
||||
s.logger.Warn("Failed to flush capture", zap.Error(err))
|
||||
|
||||
Reference in New Issue
Block a user