fix: fixed replay time computation using actual dates instead of the assumption of one frame per second

This commit is contained in:
evilsocket 2019-03-30 13:59:08 +01:00
commit 54116f7fbe
No known key found for this signature in database
GPG key ID: 1564D7F30393A456
4 changed files with 45 additions and 2 deletions

View file

@ -8,6 +8,7 @@ import (
"io/ioutil"
"os"
"sync"
"time"
"github.com/bettercap/bettercap/session"
@ -146,6 +147,36 @@ func (e *RecordEntry) Next() []byte {
return e.frames[cur]
}
func (e *RecordEntry) TimeOf(idx int) time.Time {
e.Lock()
defer e.Unlock()
buf := e.frames[idx]
frame := make(map[string]interface{})
if err := json.Unmarshal(buf, &frame); err != nil {
fmt.Printf("%v\n", err)
return time.Time{}
} else if tm, err := time.Parse(time.RFC3339, frame["polled_at"].(string)); err != nil {
fmt.Printf("%v\n", err)
return time.Time{}
} else {
return tm
}
}
func (e *RecordEntry) StartedAt() time.Time {
return e.TimeOf(0)
}
func (e *RecordEntry) StoppedAt() time.Time {
return e.TimeOf(e.NumStates)
}
func (e *RecordEntry) Duration() time.Duration {
return e.StoppedAt().Sub(e.StartedAt())
}
// the Record object represents a recorded session
type Record struct {
sync.Mutex