Debian Codegraph @

modules/api_rest/api_rest_replay.go

raw symbols SCIP
1 package api_rest
2
3 import (
4 "errors"
5 "fmt"
6 "time"
7
8 "github.com/bettercap/recording"
9
10 "github.com/evilsocket/islazy/fs"
11 )
12
13 var (
14 errNotReplaying = errors.New("not replaying")
15 )
16
17 func (mod *RestAPI) errAlreadyReplaying() error {
18 return fmt.Errorf("the module is already replaying a session from %s", mod.recordFileName)
19 }
20
21 func (mod *RestAPI) startReplay(filename string) (err error) {
22 if mod.replaying {
23 return mod.errAlreadyReplaying()
24 } else if mod.recording {
25 return mod.errAlreadyRecording()
26 } else if mod.recordFileName, err = fs.Expand(filename); err != nil {
27 return err
28 }
29
30 mod.State.Store("load_progress", 0)
31 defer func() {
32 mod.State.Store("load_progress", 100.0)
33 }()
34
35 mod.loading = true
36 defer func() {
37 mod.loading = false
38 }()
39
40 mod.Info("loading %s ...", mod.recordFileName)
41
42 start := time.Now()
43 mod.record, err = recording.Load(mod.recordFileName, func(progress float64, done int, total int) {
44 mod.State.Store("load_progress", progress)
45 })
46 if err != nil {
47 return err
48 }
49 loadedIn := time.Since(start)
50
51 // we need the api itself up and running
52 if !mod.Running() {
53 if err := mod.Start(); err != nil {
54 return err
55 }
56 }
57
58 mod.recStarted = mod.record.Session.StartedAt()
59 mod.recStopped = mod.record.Session.StoppedAt()
60 duration := mod.recStopped.Sub(mod.recStarted)
61 mod.recTime = int(duration.Seconds())
62 mod.replaying = true
63 mod.recording = false
64
65 mod.Info("loaded %s of recording (%d frames) started at %s in %s, started replaying ...",
66 duration,
67 mod.record.Session.Frames(),
68 mod.recStarted,
69 loadedIn)
70
71 return nil
72 }
73
74 func (mod *RestAPI) stopReplay() error {
75 if !mod.replaying {
76 return errNotReplaying
77 }
78
79 mod.replaying = false
80
81 mod.Info("stopped replaying from %s ...", mod.recordFileName)
82
83 mod.recordFileName = ""
84
85 return nil
86 }