/whois: Hide IP if user isn't admin; display client and time joined. (#192)

Resolves #170.
This commit is contained in:
Dmitri Shuralyov 2016-08-06 15:20:34 -07:00 committed by Andrey Petrov
parent f6de73d420
commit 66adee6f9a
6 changed files with 38 additions and 8 deletions

4
.gitmodules vendored
View File

@ -14,3 +14,7 @@
path = vendor/github.com/shazow/rateio
url = https://github.com/shazow/rateio
branch = master
[submodule "vendor/github.com/dustin/go-humanize"]
path = vendor/github.com/dustin/go-humanize
url = https://github.com/dustin/go-humanize
branch = master

View File

@ -330,7 +330,14 @@ func (h *Host) InitCommands(c *chat.Commands) {
}
id := target.Identifier.(*Identity)
room.Send(message.NewSystemMsg(id.Whois(), msg.From()))
var whois string
switch room.IsOp(msg.From()) {
case true:
whois = id.WhoisAdmin()
case false:
whois = id.Whois()
}
room.Send(message.NewSystemMsg(whois, msg.From()))
return nil
},

View File

@ -26,7 +26,7 @@ func stripPrompt(s string) string {
func TestHostGetPrompt(t *testing.T) {
var expected, actual string
u := message.NewUser(&Identity{nil, "foo"})
u := message.NewUser(&Identity{id: "foo"})
u.SetColorIdx(2)
actual = GetPrompt(u)

View File

@ -1,9 +1,10 @@
package sshchat
import (
"fmt"
"net"
"time"
"github.com/dustin/go-humanize"
"github.com/shazow/ssh-chat/chat"
"github.com/shazow/ssh-chat/chat/message"
"github.com/shazow/ssh-chat/sshd"
@ -12,7 +13,8 @@ import (
// Identity is a container for everything that identifies a client.
type Identity struct {
sshd.Connection
id string
id string
created time.Time
}
// NewIdentity returns a new identity object from an sshd.Connection.
@ -20,6 +22,7 @@ func NewIdentity(conn sshd.Connection) *Identity {
return &Identity{
Connection: conn,
id: chat.SanitizeName(conn.Name()),
created: time.Now(),
}
}
@ -39,14 +42,28 @@ 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: " + chat.SanitizeData(string(i.ClientVersion())) + message.Newline +
" > joined: " + humanize.Time(i.created)
}
// 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())
}
// TODO: Include time joined, client, etc.
return fmt.Sprintf("name: %s"+message.Newline+
" > ip: %s"+message.Newline+
" > fingerprint: %s", i.Name(), ip, fingerprint)
return "name: " + i.Name() + message.Newline +
" > ip: " + ip + message.Newline +
" > fingerprint: " + fingerprint + message.Newline +
" > client: " + chat.SanitizeData(string(i.ClientVersion())) + message.Newline +
" > joined: " + humanize.Time(i.created)
}

View File

@ -22,6 +22,7 @@ type Connection interface {
PublicKey() ssh.PublicKey
RemoteAddr() net.Addr
Name() string
ClientVersion() []byte
Close() error
}

1
vendor/github.com/dustin/go-humanize generated vendored Submodule

@ -0,0 +1 @@
Subproject commit 2fcb5204cdc65b4bec9fd0a87606bb0d0e3c54e8