| 1 | package api_rest |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "encoding/json" |
| 6 | "errors" |
| 7 | "fmt" |
| 8 | "time" |
| 9 | |
| 10 | "github.com/bettercap/recording" |
| 11 | |
| 12 | "github.com/evilsocket/islazy/fs" |
| 13 | ) |
| 14 | |
| 15 | var ( |
| 16 | errNotRecording = errors.New("not recording") |
| 17 | ) |
| 18 | |
| 19 | func (mod *RestAPI) errAlreadyRecording() error { |
| 20 | return fmt.Errorf("the module is already recording to %s", mod.recordFileName) |
| 21 | } |
| 22 | |
| 23 | func (mod *RestAPI) recordState() error { |
| 24 | mod.Session.Lock() |
| 25 | defer mod.Session.Unlock() |
| 26 | |
| 27 | session := new(bytes.Buffer) |
| 28 | encoder := json.NewEncoder(session) |
| 29 | |
| 30 | if err := encoder.Encode(mod.Session); err != nil { |
| 31 | return err |
| 32 | } |
| 33 | |
| 34 | events := new(bytes.Buffer) |
| 35 | encoder = json.NewEncoder(events) |
| 36 | |
| 37 | if err := encoder.Encode(mod.getEvents(0)); err != nil { |
| 38 | return err |
| 39 | } |
| 40 | |
| 41 | return mod.record.NewState(session.Bytes(), events.Bytes()) |
| 42 | } |
| 43 | |
| 44 | func (mod *RestAPI) recorder() { |
| 45 | clock := time.Duration(mod.recClock) * time.Second |
| 46 | |
| 47 | mod.recTime = 0 |
| 48 | mod.recording = true |
| 49 | mod.replaying = false |
| 50 | mod.record = recording.New(mod.recordFileName) |
| 51 | |
| 52 | mod.Info("started recording to %s (clock %s) ...", mod.recordFileName, clock) |
| 53 | |
| 54 | mod.recordWait.Add(1) |
| 55 | defer mod.recordWait.Done() |
| 56 | |
| 57 | tick := time.NewTicker(1 * time.Second) |
| 58 | lastSampled := time.Time{} |
| 59 | |
| 60 | for range tick.C { |
| 61 | if !mod.recording { |
| 62 | break |
| 63 | } |
| 64 | |
| 65 | mod.recTime++ |
| 66 | |
| 67 | if time.Since(lastSampled) >= clock { |
| 68 | lastSampled = time.Now() |
| 69 | if err := mod.recordState(); err != nil { |
| 70 | mod.Error("error while recording: %s", err) |
| 71 | mod.recording = false |
| 72 | break |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | mod.Info("stopped recording to %s ...", mod.recordFileName) |
| 78 | } |
| 79 | |
| 80 | func (mod *RestAPI) startRecording(filename string) (err error) { |
| 81 | if mod.recording { |
| 82 | return mod.errAlreadyRecording() |
| 83 | } else if mod.replaying { |
| 84 | return mod.errAlreadyReplaying() |
| 85 | } else if err, mod.recClock = mod.IntParam("api.rest.record.clock"); err != nil { |
| 86 | return err |
| 87 | } else if mod.recordFileName, err = fs.Expand(filename); err != nil { |
| 88 | return err |
| 89 | } |
| 90 | |
| 91 | // we need the api itself up and running |
| 92 | if !mod.Running() { |
| 93 | if err = mod.Start(); err != nil { |
| 94 | return err |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | go mod.recorder() |
| 99 | |
| 100 | return nil |
| 101 | } |
| 102 | |
| 103 | func (mod *RestAPI) stopRecording() error { |
| 104 | if !mod.recording { |
| 105 | return errNotRecording |
| 106 | } |
| 107 | |
| 108 | mod.recording = false |
| 109 | |
| 110 | mod.recordWait.Wait() |
| 111 | |
| 112 | err := mod.record.Flush() |
| 113 | |
| 114 | mod.recordFileName = "" |
| 115 | mod.record = nil |
| 116 | |
| 117 | return err |
| 118 | } |