This repository has been archived by the owner on Feb 17, 2024. It is now read-only.
forked from shazow/ssh-chat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
identity.go
100 lines (88 loc) · 2.64 KB
/
identity.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
package sshchat
import (
"fmt"
"net"
"time"
"github.com/gostones/ssh-chat/chat"
"github.com/gostones/ssh-chat/chat/message"
"github.com/gostones/ssh-chat/sshd"
)
// Identity is a container for everything that identifies a client.
type Identity struct {
sshd.Connection
id string
created time.Time
}
// NewIdentity returns a new identity object from an sshd.Connection.
func NewIdentity(conn sshd.Connection) *Identity {
return &Identity{
Connection: conn,
id: chat.SanitizeName(conn.Name()),
created: time.Now(),
}
}
func (i Identity) ID() string {
return i.id
}
func (i *Identity) SetID(id string) {
i.id = id
}
func (i *Identity) SetName(name string) {
i.SetID(name)
}
func (i Identity) Name() string {
return i.id
}
// Whois returns a whois description for non-admin users.
func (i Identity) Whois() string {
fingerprint := "(no public key)"
if i.PublicKey() != nil {
fingerprint = sshd.Fingerprint(i.PublicKey())
}
return fmt.Sprintf(`{"name":"%v", "fingerprint":"%v", "client":"%v", "joined":"%v"}`,
i.Name(),
fingerprint,
chat.SanitizeData(string(i.ClientVersion())),
humanSince(time.Since(i.created)),
)
// return "name: " + i.Name() + message.Newline +
// " > fingerprint: " + fingerprint + message.Newline +
// " > client: " + chat.SanitizeData(string(i.ClientVersion())) + message.Newline +
// " > joined: " + humanSince(time.Since(i.created)) + " ago"
}
// WhoisAdmin returns a whois description for admin users.
func (i Identity) WhoisAdmin() string {
ip, _, _ := net.SplitHostPort(i.RemoteAddr().String())
fingerprint := "(no public key)"
if i.PublicKey() != nil {
fingerprint = sshd.Fingerprint(i.PublicKey())
}
return fmt.Sprintf(`{"name":"%v", "ip":"%v", "fingerprint":"%v", "client":"%v", "joined":"%v"}`,
i.Name(),
ip,
fingerprint,
chat.SanitizeData(string(i.ClientVersion())),
humanSince(time.Since(i.created)),
)
// return "name: " + i.Name() + message.Newline +
// " > ip: " + ip + message.Newline +
// " > fingerprint: " + fingerprint + message.Newline +
// " > client: " + chat.SanitizeData(string(i.ClientVersion())) + message.Newline +
// " > joined: " + humanSince(time.Since(i.created)) + " ago"
}
func Whois(u *message.User) string {
i := u.Identifier.(*Identity)
fingerprint := "(no public key)"
if i.PublicKey() != nil {
fingerprint = sshd.Fingerprint(i.PublicKey())
}
return fmt.Sprintf(`{"name":"%v", "fingerprint":"%v", "client":"%v", "joined":"%v", "user_type":"%v", "host_port":"%v", "remote_port":"%v"}`,
i.Name(),
fingerprint,
chat.SanitizeData(string(i.ClientVersion())),
humanSince(time.Since(i.created)),
u.UserType,
u.HostPort,
u.RemotePort,
)
}