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
package httpd
import (
"sync"
"github.com/gliderlabs/ssh"
socketio "github.com/googollee/go-socket.io"
"github.com/jumpserver/koko/pkg/model"
)
var conns = &connections{container: make(map[string]*WebConn), mu: new(sync.RWMutex)}
type connections struct {
container map[string]*WebConn
mu *sync.RWMutex
}
func (c *connections) GetWebConn(conID string) (conn *WebConn) {
c.mu.RLock()
defer c.mu.RUnlock()
conn = c.container[conID]
return
}
func (c *connections) DeleteWebConn(conID string) {
c.mu.RLock()
webC, ok := c.container[conID]
c.mu.RUnlock()
if !ok {
return
}
webC.Close()
c.mu.Lock()
defer c.mu.Unlock()
delete(c.container, conID)
}
func (c *connections) AddWebConn(conID string, conn *WebConn) {
c.mu.Lock()
defer c.mu.Unlock()
c.container[conID] = conn
}
func newWebConn(id string, sock socketio.Conn, addr string, user *model.User) *WebConn {
conn := &WebConn{Cid: id, Sock: sock, Addr: addr, User: user, mu: new(sync.RWMutex), Clients: make(map[string]*Client)}
return conn
}
type WebConn struct {
Cid string
Sock socketio.Conn
Addr string
User *model.User
Clients map[string]*Client
mu *sync.RWMutex
}
func (w *WebConn) GetClient(clientID string) (conn *Client) {
w.mu.RLock()
defer w.mu.RUnlock()
return w.Clients[clientID]
}
func (w *WebConn) DeleteClient(clientID string) {
w.mu.Lock()
defer w.mu.Unlock()
delete(w.Clients, clientID)
}
func (w *WebConn) AddClient(clientID string, conn *Client) {
w.mu.Lock()
defer w.mu.Unlock()
w.Clients[clientID] = conn
}
func (w *WebConn) GetAllClients() (clients []string) {
clients = make([]string, 0)
w.mu.RLock()
defer w.mu.RUnlock()
for k := range w.Clients {
clients = append(clients, k)
}
return clients
}
func (w *WebConn) SetWinSize(winSize ssh.Window) {
w.mu.RLock()
defer w.mu.RUnlock()
for _, client := range w.Clients {
client.WinChan <- winSize
}
}
func (w *WebConn) Close() {
w.mu.Lock()
defer w.mu.Unlock()
clientsCopy := make(map[string]*Client)
for k, v := range w.Clients {
clientsCopy[k] = v
}
for k, client := range clientsCopy {
_ = client.Close()
delete(w.Clients, k)
}
}