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
parent 50d01429cd
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

@ -35,6 +35,8 @@ type RestAPI struct {
recordFileName string
recordWait *sync.WaitGroup
record *Record
recStarted time.Time
recStopped time.Time
}
func NewRestAPI(s *session.Session) *RestAPI {
@ -65,6 +67,8 @@ func NewRestAPI(s *session.Session) *RestAPI {
mod.State.Store("rec_filename", &mod.recordFileName)
mod.State.Store("rec_frames", 0)
mod.State.Store("rec_cur_frame", 0)
mod.State.Store("rec_started", &mod.recStarted)
mod.State.Store("rec_stopped", &mod.recStopped)
mod.AddParam(session.NewStringParameter("api.rest.address",
"127.0.0.1",

View file

@ -36,7 +36,7 @@ func (mod *RestAPI) setAuthFailed(w http.ResponseWriter, r *http.Request) {
func (mod *RestAPI) toJSON(w http.ResponseWriter, o interface{}) {
w.Header().Set("Content-Type", "application/json")
if err := json.NewEncoder(w).Encode(o); err != nil {
fmt.Printf("error while encoding object to JSON: %v\n", err)
mod.Warning("error while encoding object to JSON: %v", err)
}
}

View file

@ -50,10 +50,18 @@ func (mod *RestAPI) startReplay(filename string) (err error) {
}
}
mod.recStarted = mod.record.Session.StartedAt()
mod.recStopped = mod.record.Session.StoppedAt()
duration := mod.recStopped.Sub(mod.recStarted)
mod.recTime = int(duration.Seconds())
mod.replaying = true
mod.recording = false
mod.Info("loaded %d frames in %s, started replaying ...", mod.record.Session.Frames(), loadedIn)
mod.Info("loaded %s of recording (%d frames) started at %s in %s, started replaying ...",
duration,
mod.record.Session.Frames(),
mod.recStarted,
loadedIn)
return nil
}

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