-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathselfPlaySession.go
113 lines (94 loc) · 2.85 KB
/
selfPlaySession.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package cmd
import (
"fmt"
"os"
"time"
"github.com/inpher/sb/internal/commands"
"github.com/inpher/sb/internal/config"
"github.com/inpher/sb/internal/helpers"
"github.com/inpher/sb/internal/models"
"github.com/inpher/sb/internal/storage"
"github.com/pkg/errors"
"maze.io/x/ttyrec"
)
// SelfPlaySession describes the selfListAccesses command
type SelfPlaySession struct{}
func init() {
commands.RegisterCommand("self session replay", func() (c commands.Command, r models.Right, h helpers.Helper, args map[string]commands.Argument) {
return new(SelfPlaySession), models.Public, helpers.Helper{
Header: "watch a recording of an SSH session",
Usage: "self session replay",
Description: "watch a recording of an SSH session",
Aliases: []string{"selfPlaySession"},
}, map[string]commands.Argument{
"session-id": {
Required: true,
Description: "The session recording ID to watch",
},
}
})
}
// Checks checks whether or not the user can execute this method
func (c *SelfPlaySession) Checks(ct *commands.Context) error {
// No specific rights needed but a sb account
return nil
}
// Execute executes the command
func (c *SelfPlaySession) Execute(ct *commands.Context) (repl models.ReplicationData, cmdError error, err error) {
filename := fmt.Sprintf("%s.ttyrec", ct.FormattedArguments["session-id"])
localFilepath := fmt.Sprintf("%s/%s", ct.User.GetTtyrecDirectory(), filename)
// If TTYRecs offloading is enabled, we start by getting the ttyrec file from a storage
ttyRecsOffloadingConfig := config.GetTTYRecsOffloadingConfig()
if ttyRecsOffloadingConfig.Enabled {
var rs storage.Storage
rs, err = storage.GetStorage(ttyRecsOffloadingConfig)
if err != nil {
return
}
err = rs.GetFromStorage(fmt.Sprintf("%s.bin", filename), fmt.Sprintf("%s.bin", localFilepath))
if err != nil {
return
}
err = helpers.DecryptFile(fmt.Sprintf("%s.bin", localFilepath), localFilepath, config.GetEncryptionKey())
if err != nil {
return
}
err = os.Remove(fmt.Sprintf("%s.bin", localFilepath))
if err != nil {
return
}
}
r, err := os.Open(localFilepath)
if err != nil {
err = errors.Wrap(err, "file not found")
return
}
d := ttyrec.NewDecoder(r)
frames, stop := d.DecodeStream()
defer stop()
var previous *ttyrec.Frame
for frame := range frames {
if _, errFrame := os.Stdout.Write(frame.Data); err != nil {
err = errors.Wrap(errFrame, "error writing frame")
return
}
if previous != nil {
d := frame.Time.Sub(previous.Time)
time.Sleep(time.Duration(float64(d)))
}
previous = frame
}
if ttyRecsOffloadingConfig.Enabled {
err = os.Remove(localFilepath)
if err != nil {
return
}
}
return
}
func (c *SelfPlaySession) PostExecute(repl models.ReplicationData) (err error) {
return
}
func (c *SelfPlaySession) Replicate(repl models.ReplicationData) (err error) {
return
}