-
-
Notifications
You must be signed in to change notification settings - Fork 122
/
users.go
75 lines (55 loc) · 1.48 KB
/
users.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
package internal
import (
"errors"
"fmt"
"sort"
"sync"
"golang.org/x/crypto/ssh"
)
var ErrNilServerConnection = errors.New("The server connection was nil for the client")
var lUsers sync.RWMutex
var users map[string]bool = make(map[string]bool)
type User struct {
// This is the users connection to the server itself, creates new channels and whatnot. NOT to get io.Copy'd
ServerConnection ssh.Conn
Pty *PtyReq
ShellRequests <-chan *ssh.Request
// Remote forwards sent by user
SupportedRemoteForwards map[RemoteForwardRequest]bool //(set)
// So we can capture details about who is currently using the rssh server
ConnectionDetails string
}
func CreateUser(ServerConnection ssh.Conn) (us *User, err error) {
if ServerConnection == nil {
err = ErrNilServerConnection
return
}
us = &User{
ServerConnection: ServerConnection,
SupportedRemoteForwards: make(map[RemoteForwardRequest]bool),
ConnectionDetails: fmt.Sprintf("%s@%s", ServerConnection.User(), ServerConnection.RemoteAddr().String()),
}
lUsers.Lock()
defer lUsers.Unlock()
users[us.ConnectionDetails] = true
return
}
func ListUsers() (userList []string) {
lUsers.RLock()
defer lUsers.RUnlock()
for user := range users {
userList = append(userList, user)
}
sort.Strings(userList)
return
}
func DeleteUser(us *User) {
if us != nil {
lUsers.Lock()
delete(users, us.ConnectionDetails)
lUsers.Unlock()
if us.ServerConnection != nil {
defer us.ServerConnection.Close()
}
}
}