/
replay.go
453 lines (390 loc) · 10.4 KB
/
replay.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
package proxy
import (
"archive/zip"
"compress/flate"
"context"
"encoding/binary"
"errors"
"fmt"
"io"
"maps"
"net"
"os"
"sync"
"sync/atomic"
"time"
"github.com/sandertv/gophertunnel/minecraft"
"github.com/sandertv/gophertunnel/minecraft/protocol/login"
"github.com/sandertv/gophertunnel/minecraft/protocol/packet"
"github.com/sandertv/gophertunnel/minecraft/resource"
"github.com/sirupsen/logrus"
)
type replayConnector struct {
f *os.File
packetF io.Reader
ver uint32
packets chan packet.Packet
err error
spawn chan struct{}
close chan struct{}
closed atomic.Bool
once sync.Once
pool packet.Pool
proto minecraft.Protocol
clientData login.ClientData
gameData minecraft.GameData
packetFunc PacketFunc
resourcePackHandler *rpHandler
}
func (r *replayConnector) readPacket() (payload []byte, toServer bool, err error) {
var magic uint32 = 0
var packetLength uint32 = 0
timeReceived := time.Now()
err = binary.Read(r.packetF, binary.LittleEndian, &magic)
if err != nil {
if err == io.ErrUnexpectedEOF {
err = io.EOF
}
if errors.Is(err, io.EOF) {
logrus.Info("Reached End")
return nil, false, nil
}
return nil, false, err
}
if magic != 0xAAAAAAAA {
return nil, toServer, fmt.Errorf("wrong Magic")
}
binary.Read(r.packetF, binary.LittleEndian, &packetLength)
binary.Read(r.packetF, binary.LittleEndian, &toServer)
if r.ver >= 2 {
var timeMs int64
binary.Read(r.packetF, binary.LittleEndian, &timeMs)
timeReceived = time.UnixMilli(timeMs)
}
payload = make([]byte, packetLength)
n, err := io.ReadFull(r.packetF, payload)
if err != nil {
return nil, toServer, err
}
if n != int(packetLength) {
return nil, toServer, fmt.Errorf("truncated")
}
var magic2 uint32
binary.Read(r.packetF, binary.LittleEndian, &magic2)
if magic2 != 0xBBBBBBBB {
return nil, toServer, fmt.Errorf("wrong Magic2")
}
_ = timeReceived
return payload, toServer, nil
}
func (r *replayConnector) handleLoginSequence(pk packet.Packet) (bool, error) {
switch pk := pk.(type) {
case *packet.StartGame:
r.SetGameData(minecraft.GameData{
WorldName: pk.WorldName,
WorldSeed: pk.WorldSeed,
Difficulty: pk.Difficulty,
EntityUniqueID: pk.EntityUniqueID,
EntityRuntimeID: pk.EntityRuntimeID,
PlayerGameMode: pk.PlayerGameMode,
PersonaDisabled: pk.PersonaDisabled,
CustomSkinsDisabled: pk.CustomSkinsDisabled,
BaseGameVersion: pk.BaseGameVersion,
PlayerPosition: pk.PlayerPosition,
Pitch: pk.Pitch,
Yaw: pk.Yaw,
Dimension: pk.Dimension,
WorldSpawn: pk.WorldSpawn,
EditorWorldType: pk.EditorWorldType,
WorldGameMode: pk.WorldGameMode,
GameRules: pk.GameRules,
Time: pk.Time,
ServerBlockStateChecksum: pk.ServerBlockStateChecksum,
CustomBlocks: pk.Blocks,
Items: pk.Items,
PlayerMovementSettings: pk.PlayerMovementSettings,
ServerAuthoritativeInventory: pk.ServerAuthoritativeInventory,
Experiments: pk.Experiments,
ClientSideGeneration: pk.ClientSideGeneration,
ChatRestrictionLevel: pk.ChatRestrictionLevel,
DisablePlayerInteractions: pk.DisablePlayerInteractions,
UseBlockNetworkIDHashes: pk.UseBlockNetworkIDHashes,
})
case *packet.ResourcePacksInfo:
return false, r.resourcePackHandler.OnResourcePacksInfo(pk)
case *packet.ResourcePackDataInfo:
return false, r.resourcePackHandler.OnResourcePackDataInfo(pk)
case *packet.ResourcePackChunkData:
return false, r.resourcePackHandler.OnResourcePackChunkData(pk)
case *packet.ResourcePackStack:
return false, r.resourcePackHandler.OnResourcePackStack(pk)
case *packet.SetLocalPlayerAsInitialised:
if pk.EntityRuntimeID != r.gameData.EntityRuntimeID {
return false, fmt.Errorf("entity runtime ID mismatch: entity runtime ID in StartGame and SetLocalPlayerAsInitialised packets should be equal")
}
close(r.spawn)
return true, nil
}
return false, nil
}
func (r *replayConnector) loop() {
gameStarted := false
defer r.Close()
for {
payload, toServer, err := r.readPacket()
if err != nil {
r.err = err
r.Close()
return
}
if payload == nil {
return
}
var src, dst = r.RemoteAddr(), r.LocalAddr()
if toServer {
src, dst = r.LocalAddr(), r.RemoteAddr()
}
pkData, err := minecraft.ParseData(payload, r, src, dst)
if err != nil {
r.err = err
r.Close()
return
}
pks, err := pkData.Decode(r)
if err != nil {
logrus.Error(err)
continue
}
for _, pk := range pks {
if !gameStarted {
gameStarted, err = r.handleLoginSequence(pk)
if err != nil {
r.err = err
r.Close()
return
}
} else {
if r.closed.Load() {
return
}
r.packets <- pk
}
}
}
}
func CreateReplayConnector(ctx context.Context, filename string, packetFunc PacketFunc, onResourcePackInfo func(), OnFinishedPack func(*resource.Pack)) (r *replayConnector, err error) {
pool := minecraft.DefaultProtocol.Packets(true)
maps.Copy(pool, minecraft.DefaultProtocol.Packets(false))
r = &replayConnector{
pool: pool,
proto: minecraft.DefaultProtocol,
packetFunc: packetFunc,
spawn: make(chan struct{}),
close: make(chan struct{}),
packets: make(chan packet.Packet),
}
r.resourcePackHandler = newRpHandler(ctx, nil)
r.resourcePackHandler.OnResourcePacksInfoCB = onResourcePackInfo
r.resourcePackHandler.OnFinishedPack = OnFinishedPack
r.resourcePackHandler.SetServer(r)
cache := &replayCache{}
r.resourcePackHandler.cache = cache
logrus.Infof("Reading replay %s", filename)
r.f, err = os.Open(filename)
if err != nil {
return nil, err
}
var head = make([]byte, 16)
r.f.Read(head)
magic := string(head[0:4])
var z *zip.Reader
var zipSize uint64
if magic != "BTCP" {
logrus.Warn("capture is old format")
stat, _ := r.f.Stat()
z, err = zip.NewReader(r.f, stat.Size())
if err != nil {
return nil, err
}
f, err := z.Open("version")
if err != nil {
return nil, err
}
binary.Read(f, binary.LittleEndian, &r.ver)
f.Close()
} else {
r.ver = binary.LittleEndian.Uint32(head[4:8])
zipSize = binary.LittleEndian.Uint64(head[8:16])
z, err = zip.NewReader(io.NewSectionReader(r.f, 16, int64(zipSize)), int64(zipSize))
if err != nil {
return nil, err
}
}
// read all packs
err = cache.ReadFrom(z)
if err != nil {
return nil, err
}
if r.ver < 4 {
// open packets bin
r.packetF, err = z.Open("packets.bin")
if err != nil {
return nil, err
}
} else {
r.f.Seek(int64(zipSize+16), 0)
fr := flate.NewReader(r.f)
r.packetF = fr
}
go r.loop()
return r, nil
}
func (r *replayConnector) DisconnectOnInvalidPacket() bool {
return false
}
func (r *replayConnector) DisconnectOnUnknownPacket() bool {
return false
}
func (r *replayConnector) OnDisconnect() <-chan struct{} {
return r.close
}
func (r *replayConnector) Expect(...uint32) {
}
func (r *replayConnector) SetLoggedIn() {
}
func (r *replayConnector) Close() error {
r.once.Do(func() {
r.closed.Store(true)
close(r.close)
select {
case <-r.packets:
default:
}
close(r.packets)
})
return nil
}
func (r *replayConnector) Authenticated() bool {
return true
}
func (r *replayConnector) ChunkRadius() int {
return 80
}
func (r *replayConnector) ClientCacheEnabled() bool {
return false
}
func (r *replayConnector) ClientData() login.ClientData {
return r.clientData
}
func (r *replayConnector) DoSpawn() error {
return r.DoSpawnContext(context.Background())
}
func (r *replayConnector) DoSpawnContext(ctx context.Context) error {
select {
case <-r.close:
case <-ctx.Done():
case <-r.spawn:
// Conn was spawned successfully.
return nil
}
if r.err != nil {
return r.err
}
if ctx.Err() != nil {
return ctx.Err()
}
return errors.New("do spawn")
}
func (r *replayConnector) DoSpawnTimeout(timeout time.Duration) error {
c, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
return r.DoSpawnContext(c)
}
func (r *replayConnector) Flush() error {
return nil
}
func (r *replayConnector) GameData() minecraft.GameData {
return r.gameData
}
func (r *replayConnector) IdentityData() login.IdentityData {
return login.IdentityData{}
}
func (r *replayConnector) Latency() time.Duration {
return 0
}
func (r *replayConnector) LocalAddr() net.Addr {
return &net.UDPAddr{
IP: net.IPv4(1, 1, 1, 1),
}
}
func (r *replayConnector) Read(b []byte) (n int, err error) {
return 0, errors.New("not Implemented")
}
func (r *replayConnector) ReadPacket() (pk packet.Packet, err error) {
select {
case <-r.close:
if r.err != nil {
return nil, r.err
}
return nil, net.ErrClosed
case p, ok := <-r.packets:
if !ok {
err = net.ErrClosed
if r.err != nil {
err = r.err
}
}
return p, err
}
}
func (r *replayConnector) Write(b []byte) (n int, err error) {
panic("unimplemented")
}
func (r *replayConnector) WritePacket(pk packet.Packet) error {
return nil
}
func (r *replayConnector) RemoteAddr() net.Addr {
return &net.UDPAddr{
IP: net.IPv4(2, 2, 2, 2),
}
}
func (r *replayConnector) ResourcePacks() []*resource.Pack {
return r.resourcePackHandler.ResourcePacks()
}
func (r *replayConnector) SetGameData(data minecraft.GameData) {
r.gameData = data
}
func (r *replayConnector) StartGame(data minecraft.GameData) error {
return r.StartGameContext(context.Background(), data)
}
func (r *replayConnector) StartGameContext(ctx context.Context, data minecraft.GameData) error {
return nil
}
func (r *replayConnector) StartGameTimeout(data minecraft.GameData, timeout time.Duration) error {
c, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
return r.StartGameContext(c, data)
}
func (r *replayConnector) SetDeadline(t time.Time) error {
return nil
}
func (r *replayConnector) SetReadDeadline(t time.Time) error {
return nil
}
func (r *replayConnector) SetWriteDeadline(time.Time) error {
return nil
}
func (r *replayConnector) Pool() packet.Pool {
return r.pool
}
func (r *replayConnector) ShieldID() int32 {
return 0
}
func (r *replayConnector) Proto() minecraft.Protocol {
return r.proto
}
func (r *replayConnector) PacketFunc(header packet.Header, payload []byte, src, dst net.Addr) {
if r.packetFunc != nil {
r.packetFunc(header, payload, src, dst)
}
}