mirror of
https://github.com/bettercap/bettercap
synced 2025-07-13 16:43:49 -07:00
new: api.rest.record.clock parameter to decide delay per sample
This commit is contained in:
parent
0113286b4f
commit
460bd9b159
2 changed files with 23 additions and 7 deletions
|
@ -28,6 +28,7 @@ type RestAPI struct {
|
||||||
upgrader websocket.Upgrader
|
upgrader websocket.Upgrader
|
||||||
quit chan bool
|
quit chan bool
|
||||||
|
|
||||||
|
recClock int
|
||||||
recording bool
|
recording bool
|
||||||
recTime int
|
recTime int
|
||||||
loading bool
|
loading bool
|
||||||
|
@ -50,6 +51,7 @@ func NewRestAPI(s *session.Session) *RestAPI {
|
||||||
ReadBufferSize: 1024,
|
ReadBufferSize: 1024,
|
||||||
WriteBufferSize: 1024,
|
WriteBufferSize: 1024,
|
||||||
},
|
},
|
||||||
|
recClock: 1,
|
||||||
recording: false,
|
recording: false,
|
||||||
recTime: 0,
|
recTime: 0,
|
||||||
loading: false,
|
loading: false,
|
||||||
|
@ -60,6 +62,7 @@ func NewRestAPI(s *session.Session) *RestAPI {
|
||||||
}
|
}
|
||||||
|
|
||||||
mod.State.Store("recording", &mod.recording)
|
mod.State.Store("recording", &mod.recording)
|
||||||
|
mod.State.Store("rec_clock", &mod.recClock)
|
||||||
mod.State.Store("replaying", &mod.replaying)
|
mod.State.Store("replaying", &mod.replaying)
|
||||||
mod.State.Store("loading", &mod.loading)
|
mod.State.Store("loading", &mod.loading)
|
||||||
mod.State.Store("load_progress", 0)
|
mod.State.Store("load_progress", 0)
|
||||||
|
@ -122,6 +125,10 @@ func NewRestAPI(s *session.Session) *RestAPI {
|
||||||
return mod.Stop()
|
return mod.Stop()
|
||||||
}))
|
}))
|
||||||
|
|
||||||
|
mod.AddParam(session.NewIntParameter("api.rest.record.clock",
|
||||||
|
"1",
|
||||||
|
"Number of seconds to wait while recording with api.rest.record between one sample and the next one."))
|
||||||
|
|
||||||
mod.AddHandler(session.NewModuleHandler("api.rest.record off", "",
|
mod.AddHandler(session.NewModuleHandler("api.rest.record off", "",
|
||||||
"Stop recording the session.",
|
"Stop recording the session.",
|
||||||
func(args []string) error {
|
func(args []string) error {
|
||||||
|
@ -129,7 +136,7 @@ func NewRestAPI(s *session.Session) *RestAPI {
|
||||||
}))
|
}))
|
||||||
|
|
||||||
mod.AddHandler(session.NewModuleHandler("api.rest.record FILENAME", `api\.rest\.record (.+)`,
|
mod.AddHandler(session.NewModuleHandler("api.rest.record FILENAME", `api\.rest\.record (.+)`,
|
||||||
"Start polling the rest API every second recording each sample as a session file that can be replayed.",
|
"Start polling the rest API periodically recording each sample in a compressed file that can be later replayed.",
|
||||||
func(args []string) error {
|
func(args []string) error {
|
||||||
return mod.startRecording(args[0])
|
return mod.startRecording(args[0])
|
||||||
}))
|
}))
|
||||||
|
@ -141,7 +148,7 @@ func NewRestAPI(s *session.Session) *RestAPI {
|
||||||
}))
|
}))
|
||||||
|
|
||||||
mod.AddHandler(session.NewModuleHandler("api.rest.replay FILENAME", `api\.rest\.replay (.+)`,
|
mod.AddHandler(session.NewModuleHandler("api.rest.replay FILENAME", `api\.rest\.replay (.+)`,
|
||||||
"Start the rest API module in replay mode using FILENAME as the recorded session file.",
|
"Start the rest API module in replay mode using FILENAME as the recorded session file, will revert to normal mode once the replay is over.",
|
||||||
func(args []string) error {
|
func(args []string) error {
|
||||||
return mod.startReplay(args[0])
|
return mod.startReplay(args[0])
|
||||||
}))
|
}))
|
||||||
|
|
|
@ -40,17 +40,21 @@ func (mod *RestAPI) recordState() error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (mod *RestAPI) recorder() {
|
func (mod *RestAPI) recorder() {
|
||||||
|
clock := time.Duration(mod.recClock) * time.Second
|
||||||
|
|
||||||
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, &mod.SessionModule)
|
||||||
|
|
||||||
mod.Info("started recording to %s ...", mod.recordFileName)
|
mod.Info("started recording to %s (clock %s) ...", mod.recordFileName, clock)
|
||||||
|
|
||||||
mod.recordWait.Add(1)
|
mod.recordWait.Add(1)
|
||||||
defer mod.recordWait.Done()
|
defer mod.recordWait.Done()
|
||||||
|
|
||||||
tick := time.NewTicker(1 * time.Second)
|
tick := time.NewTicker(1 * time.Second)
|
||||||
|
lastSampled := time.Time{}
|
||||||
|
|
||||||
for range tick.C {
|
for range tick.C {
|
||||||
if !mod.recording {
|
if !mod.recording {
|
||||||
break
|
break
|
||||||
|
@ -58,10 +62,13 @@ func (mod *RestAPI) recorder() {
|
||||||
|
|
||||||
mod.recTime++
|
mod.recTime++
|
||||||
|
|
||||||
if err := mod.recordState(); err != nil {
|
if time.Since(lastSampled) >= clock {
|
||||||
mod.Error("error while recording: %s", err)
|
lastSampled = time.Now()
|
||||||
mod.recording = false
|
if err := mod.recordState(); err != nil {
|
||||||
break
|
mod.Error("error while recording: %s", err)
|
||||||
|
mod.recording = false
|
||||||
|
break
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -73,6 +80,8 @@ func (mod *RestAPI) startRecording(filename string) (err error) {
|
||||||
return mod.errAlreadyRecording()
|
return mod.errAlreadyRecording()
|
||||||
} else if mod.replaying {
|
} else if mod.replaying {
|
||||||
return mod.errAlreadyReplaying()
|
return mod.errAlreadyReplaying()
|
||||||
|
} else if err, mod.recClock = mod.IntParam("api.rest.record.clock"); err != nil {
|
||||||
|
return err
|
||||||
} else if mod.recordFileName, err = fs.Expand(filename); err != nil {
|
} else if mod.recordFileName, err = fs.Expand(filename); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue