mirror of
https://github.com/bettercap/bettercap
synced 2025-08-13 18:26:57 -07:00
misc: decoupled session record reader from the modules
This commit is contained in:
parent
36e5fe8bdb
commit
b743b26dde
3 changed files with 27 additions and 26 deletions
|
@ -45,7 +45,7 @@ func (mod *RestAPI) recorder() {
|
||||||
mod.recTime = 0
|
mod.recTime = 0
|
||||||
mod.recording = true
|
mod.recording = true
|
||||||
mod.replaying = false
|
mod.replaying = false
|
||||||
mod.record = NewRecord(mod.recordFileName, &mod.SessionModule)
|
mod.record = NewRecord(mod.recordFileName, nil)
|
||||||
|
|
||||||
mod.Info("started recording to %s (clock %s) ...", mod.recordFileName, clock)
|
mod.Info("started recording to %s (clock %s) ...", mod.recordFileName, clock)
|
||||||
|
|
||||||
|
|
|
@ -38,7 +38,10 @@ func (mod *RestAPI) startReplay(filename string) (err error) {
|
||||||
mod.Info("loading %s ...", mod.recordFileName)
|
mod.Info("loading %s ...", mod.recordFileName)
|
||||||
|
|
||||||
start := time.Now()
|
start := time.Now()
|
||||||
if mod.record, err = LoadRecord(mod.recordFileName, &mod.SessionModule); err != nil {
|
mod.record, err = LoadRecord(mod.recordFileName, func(progress float64) {
|
||||||
|
mod.State.Store("load_progress", progress)
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
loadedIn := time.Since(start)
|
loadedIn := time.Since(start)
|
||||||
|
|
|
@ -10,8 +10,6 @@ import (
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/bettercap/bettercap/session"
|
|
||||||
|
|
||||||
"github.com/evilsocket/islazy/fs"
|
"github.com/evilsocket/islazy/fs"
|
||||||
"github.com/kr/binarydist"
|
"github.com/kr/binarydist"
|
||||||
)
|
)
|
||||||
|
@ -177,38 +175,40 @@ func (e *RecordEntry) Duration() time.Duration {
|
||||||
return e.StoppedAt().Sub(e.StartedAt())
|
return e.StoppedAt().Sub(e.StartedAt())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type RecordLoadProgress func(p float64)
|
||||||
|
|
||||||
// the Record object represents a recorded session
|
// the Record object represents a recorded session
|
||||||
type Record struct {
|
type Record struct {
|
||||||
sync.Mutex
|
sync.Mutex
|
||||||
|
|
||||||
mod *session.SessionModule `json:"-"`
|
fileName string `json:"-"`
|
||||||
fileName string `json:"-"`
|
done int `json:"-"`
|
||||||
done int `json:"-"`
|
total int `json:"-"`
|
||||||
total int `json:"-"`
|
progress float64 `json:"-"`
|
||||||
progress float64 `json:"-"`
|
onProgress RecordLoadProgress `json:"-"`
|
||||||
Session *RecordEntry `json:"session"`
|
Session *RecordEntry `json:"session"`
|
||||||
Events *RecordEntry `json:"events"`
|
Events *RecordEntry `json:"events"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewRecord(fileName string, mod *session.SessionModule) *Record {
|
func NewRecord(fileName string, cb RecordLoadProgress) *Record {
|
||||||
r := &Record{
|
r := &Record{
|
||||||
fileName: fileName,
|
fileName: fileName,
|
||||||
mod: mod,
|
onProgress: cb,
|
||||||
}
|
}
|
||||||
|
|
||||||
r.Session = NewRecordEntry(r.onProgress)
|
r.Session = NewRecordEntry(r.onPartialProgress)
|
||||||
r.Events = NewRecordEntry(r.onProgress)
|
r.Events = NewRecordEntry(r.onPartialProgress)
|
||||||
|
|
||||||
return r
|
return r
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Record) onProgress(done int) {
|
func (r *Record) onPartialProgress(done int) {
|
||||||
r.done += done
|
r.done += done
|
||||||
r.progress = float64(r.done) / float64(r.total) * 100.0
|
r.progress = float64(r.done) / float64(r.total) * 100.0
|
||||||
r.mod.State.Store("load_progress", r.progress)
|
r.onProgress(r.progress)
|
||||||
}
|
}
|
||||||
|
|
||||||
func LoadRecord(fileName string, mod *session.SessionModule) (*Record, error) {
|
func LoadRecord(fileName string, cb RecordLoadProgress) (*Record, error) {
|
||||||
if !fs.Exists(fileName) {
|
if !fs.Exists(fileName) {
|
||||||
return nil, fmt.Errorf("%s does not exist", fileName)
|
return nil, fmt.Errorf("%s does not exist", fileName)
|
||||||
}
|
}
|
||||||
|
@ -236,17 +236,15 @@ func LoadRecord(fileName string, mod *session.SessionModule) (*Record, error) {
|
||||||
return nil, fmt.Errorf("error while parsing %s: %s", fileName, err)
|
return nil, fmt.Errorf("error while parsing %s: %s", fileName, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
rec.fileName = fileName
|
|
||||||
rec.mod = mod
|
|
||||||
|
|
||||||
rec.Session.NumStates = len(rec.Session.States)
|
rec.Session.NumStates = len(rec.Session.States)
|
||||||
rec.Session.progress = rec.onProgress
|
rec.Session.progress = rec.onPartialProgress
|
||||||
rec.Events.NumStates = len(rec.Events.States)
|
rec.Events.NumStates = len(rec.Events.States)
|
||||||
rec.Events.progress = rec.onProgress
|
rec.Events.progress = rec.onPartialProgress
|
||||||
|
rec.fileName = fileName
|
||||||
rec.done = 0
|
|
||||||
rec.total = rec.Session.NumStates + rec.Events.NumStates + 2
|
rec.total = rec.Session.NumStates + rec.Events.NumStates + 2
|
||||||
rec.progress = 0.0
|
rec.progress = 0.0
|
||||||
|
rec.done = 0
|
||||||
|
rec.onProgress = cb
|
||||||
|
|
||||||
// reset state and precompute frames
|
// reset state and precompute frames
|
||||||
if err = rec.Session.Compile(); err != nil {
|
if err = rec.Session.Compile(); err != nil {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue