-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreplay.go
85 lines (67 loc) · 1.4 KB
/
replay.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
package main
import (
"bytes"
"fmt"
"golang.org/x/net/websocket"
"sync"
"webshell/ttyrec"
)
func replayHandler(ws *websocket.Conn) {
logger.Info("Replaying session")
var err error
replayer, err := ttyrec.NewReplayer(config.ReplayFile)
if err != nil {
logger.Error(fmt.Sprintf("failed to load audit file: %v", err))
return
}
defer func() {
logger.Info("Stopping terminal")
replayer.Close()
}()
var wg sync.WaitGroup
wg.Add(1)
wsWriter := WsWriter{ws: ws}
// temp
go func() {
replayer.Play(wsWriter)
}()
go func() {
buffer := make([]byte, maxBufferSizeBytes)
for {
if err = websocket.Message.Receive(ws, &buffer); err != nil {
logger.Warn(fmt.Sprintf("Websocket closed: %s", err))
break
}
b := bytes.Trim(buffer, "\x00")
// Handle resize message from the terminal.
if b[0] == 1 {
specialPayload := bytes.Trim(b[1:], " \n\r\t\x00\x01")
logger.Info(string(specialPayload))
if len(specialPayload) == 0 {
continue
}
if string(specialPayload) == "PING" {
logger.Debug("PING")
continue
}
if string(specialPayload) == "PLAY" {
go func() {
replayer.Play(wsWriter)
}()
continue
}
}
}
}()
wg.Wait()
}
type WsWriter struct {
ws *websocket.Conn
}
func (w WsWriter) Write(b []byte) (int, error) {
err := websocket.Message.Send(w.ws, b)
if err != nil {
return 0, err
}
return len(b), err
}