ssh-chat/identity.go
Andrey Petrov 903d6c9420
/ban query support (#286)
For #285 

Turns out there were some bugs in Set, and I was using it incorrectly too.

The query syntax is a little awkward but couldn't find a nicer easy to parse format that worked with quoted string values.
2018-12-25 14:29:19 -05:00

69 lines
1.7 KiB
Go

package sshchat
import (
"net"
"time"
"github.com/shazow/ssh-chat/chat/message"
"github.com/shazow/ssh-chat/internal/sanitize"
"github.com/shazow/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: sanitize.Name(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 "name: " + i.Name() + message.Newline +
" > fingerprint: " + fingerprint + message.Newline +
" > client: " + sanitize.Data(string(i.ClientVersion()), 64) + 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 "name: " + i.Name() + message.Newline +
" > ip: " + ip + message.Newline +
" > fingerprint: " + fingerprint + message.Newline +
" > client: " + sanitize.Data(string(i.ClientVersion()), 64) + message.Newline +
" > joined: " + humanSince(time.Since(i.created)) + " ago"
}