-
Notifications
You must be signed in to change notification settings - Fork 3
/
logchannel.go
54 lines (41 loc) · 1.01 KB
/
logchannel.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
package logchannel
import (
"encoding/binary"
"io"
"syscall"
"time"
"golang.org/x/crypto/ssh"
)
type logChannel struct {
channel ssh.Channel
writer io.WriteCloser
}
func writeTTYRecHeader(fd io.Writer, length int) {
t := time.Now()
tv := syscall.NsecToTimeval(t.UnixNano())
binary.Write(fd, binary.LittleEndian, int32(tv.Sec))
binary.Write(fd, binary.LittleEndian, int32(tv.Usec))
binary.Write(fd, binary.LittleEndian, int32(length))
}
func New(channel ssh.Channel, writer io.WriteCloser) *logChannel {
return &logChannel{
channel: channel,
writer: writer,
}
}
func (l *logChannel) Read(data []byte) (int, error) {
return l.Read(data)
}
func (l *logChannel) Write(data []byte) (int, error) {
writeTTYRecHeader(l.writer, len(data))
l.writer.Write(data)
return l.channel.Write(data)
}
func (l *logChannel) LogWrite(data []byte) (int, error) {
writeTTYRecHeader(l.writer, len(data))
return l.writer.Write(data)
}
func (l *logChannel) Close() error {
l.writer.Close()
return l.channel.Close()
}