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 httpd
import (
"encoding/json"
"io"
"sync"
"github.com/gliderlabs/ssh"
"github.com/kataras/neffos"
"github.com/jumpserver/koko/pkg/logger"
"github.com/jumpserver/koko/pkg/model"
)
type Client struct {
Uuid string
Cid string
user *model.User
addr string
WinChan chan ssh.Window
UserRead io.Reader
UserWrite io.WriteCloser
Conn *neffos.NSConn
Closed bool
pty ssh.Pty
mu *sync.RWMutex
}
func (c *Client) WinCh() <-chan ssh.Window {
return c.WinChan
}
func (c *Client) LoginFrom() string {
return "WT"
}
func (c *Client) RemoteAddr() string {
return c.addr
}
func (c *Client) Read(p []byte) (n int, err error) {
return c.UserRead.Read(p)
}
func (c *Client) Write(p []byte) (n int, err error) {
c.mu.RLock()
defer c.mu.RUnlock()
if c.Closed {
return
}
data := DataMsg{Data: string(p)}
msg, err := json.Marshal(data)
if err != nil {
return
}
n = len(p)
room := c.Conn.Room(c.Uuid)
if room == nil {
logger.Error("room not found: ", c.Uuid)
return
}
room.Emit("data", msg)
return
}
func (c *Client) Pty() ssh.Pty {
return c.pty
}
func (c *Client) Close() (err error) {
c.mu.Lock()
defer c.mu.Unlock()
if c.Closed {
return
}
c.Closed = true
return c.UserWrite.Close()
}
func (c *Client) SetWinSize(size ssh.Window) {
c.mu.RLock()
defer c.mu.RUnlock()
c.WinChan <- size
}