forked from jumpserver/koko
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wrappersession.go
126 lines (107 loc) · 2.1 KB
/
wrappersession.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
114
115
116
117
118
119
120
121
122
123
124
125
126
package handler
import (
"io"
"net"
"sync"
"github.com/gliderlabs/ssh"
"github.com/eeeeethan2333/koko/pkg/logger"
uuid "github.com/satori/go.uuid"
)
type WrapperSession struct {
Uuid string
Sess ssh.Session
inWriter io.WriteCloser
outReader io.ReadCloser
mux *sync.RWMutex
closed chan struct{}
}
func (w *WrapperSession) initial() {
w.Uuid = uuid.NewV4().String()
w.closed = make(chan struct{})
w.initReadPip()
go w.readLoop()
}
func (w *WrapperSession) readLoop() {
buf := make([]byte, 1024*8)
for {
nr, err := w.Sess.Read(buf)
if nr > 0 {
w.mux.RLock()
_, _ = w.inWriter.Write(buf[:nr])
w.mux.RUnlock()
}
if err != nil {
break
}
}
w.mux.RLock()
_ = w.inWriter.Close()
_ = w.outReader.Close()
w.mux.RUnlock()
close(w.closed)
logger.Infof("Request %s: Read loop break", w.Uuid)
}
func (w *WrapperSession) Read(p []byte) (int, error) {
select {
case <-w.closed:
return 0, io.EOF
default:
}
w.mux.RLock()
defer w.mux.RUnlock()
return w.outReader.Read(p)
}
func (w *WrapperSession) Close() error {
select {
case <-w.closed:
return nil
default:
}
_ = w.inWriter.Close()
err := w.outReader.Close()
w.initReadPip()
return err
}
func (w *WrapperSession) Write(p []byte) (int, error) {
return w.Sess.Write(p)
}
func (w *WrapperSession) initReadPip() {
w.mux.Lock()
defer w.mux.Unlock()
w.outReader, w.inWriter = io.Pipe()
}
func (w *WrapperSession) Protocol() string {
return "ssh"
}
func (w *WrapperSession) User() string {
return w.Sess.User()
}
func (w *WrapperSession) WinCh() (winch <-chan ssh.Window) {
_, winch, ok := w.Sess.Pty()
if ok {
return
}
return nil
}
func (w *WrapperSession) LoginFrom() string {
return "ST"
}
func (w *WrapperSession) RemoteAddr() string {
host, _, _ := net.SplitHostPort(w.Sess.RemoteAddr().String())
return host
}
func (w *WrapperSession) Pty() ssh.Pty {
pty, _, _ := w.Sess.Pty()
return pty
}
func (w *WrapperSession) ID() string {
return w.Uuid
}
func NewWrapperSession(sess ssh.Session) *WrapperSession {
w := &WrapperSession{
Sess: sess,
mux: new(sync.RWMutex),
}
w.initial()
return w
}