mirror of
https://github.com/shazow/ssh-chat.git
synced 2025-04-15 08:30:36 +03:00
Add commands.
This commit is contained in:
parent
1046af3f4c
commit
6680c7419e
61
client.go
61
client.go
@ -2,6 +2,7 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"golang.org/x/crypto/ssh"
|
"golang.org/x/crypto/ssh"
|
||||||
"golang.org/x/crypto/ssh/terminal"
|
"golang.org/x/crypto/ssh/terminal"
|
||||||
@ -9,6 +10,24 @@ import (
|
|||||||
|
|
||||||
const MSG_BUFFER = 10
|
const MSG_BUFFER = 10
|
||||||
|
|
||||||
|
const HELP_TEXT = `-> Available commands:
|
||||||
|
/about
|
||||||
|
/exit
|
||||||
|
/help
|
||||||
|
/list
|
||||||
|
/nick $NAME
|
||||||
|
`
|
||||||
|
|
||||||
|
const ABOUT_TEXT = `-> ssh-chat is made by @shazow.
|
||||||
|
|
||||||
|
It is a custom ssh server built in Go to serve a chat experience
|
||||||
|
instead of a shell.
|
||||||
|
|
||||||
|
Source: https://github.com/shazow/ssh-chat
|
||||||
|
|
||||||
|
For more, visit shazow.net or follow at twitter.com/shazow
|
||||||
|
`
|
||||||
|
|
||||||
type Client struct {
|
type Client struct {
|
||||||
Server *Server
|
Server *Server
|
||||||
Conn *ssh.ServerConn
|
Conn *ssh.ServerConn
|
||||||
@ -19,15 +38,11 @@ type Client struct {
|
|||||||
termHeight int
|
termHeight int
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewClient(server *Server, conn *ssh.ServerConn, name string) *Client {
|
func NewClient(server *Server, conn *ssh.ServerConn) *Client {
|
||||||
if name == "" {
|
|
||||||
name = "Anonymoose"
|
|
||||||
}
|
|
||||||
|
|
||||||
return &Client{
|
return &Client{
|
||||||
Server: server,
|
Server: server,
|
||||||
Conn: conn,
|
Conn: conn,
|
||||||
Name: name,
|
Name: conn.User(),
|
||||||
Msg: make(chan string, MSG_BUFFER),
|
Msg: make(chan string, MSG_BUFFER),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -42,10 +57,9 @@ func (c *Client) Resize(width int, height int) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) sendWelcome() {
|
func (c *Client) Rename(name string) {
|
||||||
msg := fmt.Sprintf("Welcome to ssh-chat. Enter /help for more.\r\n")
|
c.Name = name
|
||||||
c.Msg <- msg
|
c.term.SetPrompt(fmt.Sprintf("[%s] ", name))
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) handleShell(channel ssh.Channel) {
|
func (c *Client) handleShell(channel ssh.Channel) {
|
||||||
@ -63,12 +77,31 @@ func (c *Client) handleShell(channel ssh.Channel) {
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
switch line {
|
parts := strings.SplitN(line, " ", 2)
|
||||||
case "/exit":
|
isCmd := strings.HasPrefix(parts[0], "/")
|
||||||
channel.Close()
|
|
||||||
|
if isCmd {
|
||||||
|
switch parts[0] {
|
||||||
|
case "/exit":
|
||||||
|
channel.Close()
|
||||||
|
case "/help":
|
||||||
|
c.Msg <- HELP_TEXT
|
||||||
|
case "/about":
|
||||||
|
c.Msg <- ABOUT_TEXT
|
||||||
|
case "/nick":
|
||||||
|
if len(parts) == 2 {
|
||||||
|
c.Server.Rename(c, parts[1])
|
||||||
|
} else {
|
||||||
|
c.Msg <- fmt.Sprintf("-> Missing $NAME from: /nick $NAME\r\n")
|
||||||
|
}
|
||||||
|
case "/list":
|
||||||
|
c.Msg <- fmt.Sprintf("-> Connected: %s\r\n", strings.Join(c.Server.List(nil), ","))
|
||||||
|
default:
|
||||||
|
c.Msg <- fmt.Sprintf("-> Invalid command: %s\r\n", line)
|
||||||
|
}
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
//c.term.Write(c.term.Escape.Reset)
|
|
||||||
msg := fmt.Sprintf("%s: %s\r\n", c.Name, line)
|
msg := fmt.Sprintf("%s: %s\r\n", c.Name, line)
|
||||||
c.Server.Broadcast(msg, c)
|
c.Server.Broadcast(msg, c)
|
||||||
}
|
}
|
||||||
|
92
server.go
92
server.go
@ -9,12 +9,15 @@ import (
|
|||||||
"golang.org/x/crypto/ssh"
|
"golang.org/x/crypto/ssh"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type Clients map[string]*Client
|
||||||
|
|
||||||
type Server struct {
|
type Server struct {
|
||||||
sshConfig *ssh.ServerConfig
|
sshConfig *ssh.ServerConfig
|
||||||
sshSigner *ssh.Signer
|
sshSigner *ssh.Signer
|
||||||
done chan struct{}
|
done chan struct{}
|
||||||
clients map[*Client]struct{}
|
clients Clients
|
||||||
lock sync.Mutex
|
lock sync.Mutex
|
||||||
|
count int
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewServer(privateKey []byte) (*Server, error) {
|
func NewServer(privateKey []byte) (*Server, error) {
|
||||||
@ -39,7 +42,8 @@ func NewServer(privateKey []byte) (*Server, error) {
|
|||||||
sshConfig: &config,
|
sshConfig: &config,
|
||||||
sshSigner: &signer,
|
sshSigner: &signer,
|
||||||
done: make(chan struct{}),
|
done: make(chan struct{}),
|
||||||
clients: map[*Client]struct{}{},
|
clients: Clients{},
|
||||||
|
count: 0,
|
||||||
}
|
}
|
||||||
|
|
||||||
return &server, nil
|
return &server, nil
|
||||||
@ -47,7 +51,8 @@ func NewServer(privateKey []byte) (*Server, error) {
|
|||||||
|
|
||||||
func (s *Server) Broadcast(msg string, except *Client) {
|
func (s *Server) Broadcast(msg string, except *Client) {
|
||||||
logger.Debugf("Broadcast to %d: %s", len(s.clients), strings.TrimRight(msg, "\r\n"))
|
logger.Debugf("Broadcast to %d: %s", len(s.clients), strings.TrimRight(msg, "\r\n"))
|
||||||
for client := range s.clients {
|
|
||||||
|
for _, client := range s.clients {
|
||||||
if except != nil && client == except {
|
if except != nil && client == except {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@ -55,6 +60,65 @@ func (s *Server) Broadcast(msg string, except *Client) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *Server) Add(client *Client) {
|
||||||
|
client.Msg <- fmt.Sprintf("-> Welcome to ssh-chat. Enter /help for more.\r\n")
|
||||||
|
|
||||||
|
s.lock.Lock()
|
||||||
|
s.count++
|
||||||
|
|
||||||
|
_, collision := s.clients[client.Name]
|
||||||
|
if collision {
|
||||||
|
newName := fmt.Sprintf("Guest%d", s.count)
|
||||||
|
client.Msg <- fmt.Sprintf("-> Your name '%s' was taken, renamed to '%s'. Use /nick <name> to change it.\r\n", client.Name, newName)
|
||||||
|
client.Name = newName
|
||||||
|
}
|
||||||
|
|
||||||
|
s.clients[client.Name] = client
|
||||||
|
num := len(s.clients)
|
||||||
|
s.lock.Unlock()
|
||||||
|
|
||||||
|
s.Broadcast(fmt.Sprintf("* %s joined. (Total connected: %d)\r\n", client.Name, num), nil)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Server) Remove(client *Client) {
|
||||||
|
s.lock.Lock()
|
||||||
|
delete(s.clients, client.Name)
|
||||||
|
s.lock.Unlock()
|
||||||
|
|
||||||
|
s.Broadcast(fmt.Sprintf("* %s left.\r\n", client.Name), nil)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Server) Rename(client *Client, newName string) {
|
||||||
|
s.lock.Lock()
|
||||||
|
|
||||||
|
_, collision := s.clients[newName]
|
||||||
|
if collision {
|
||||||
|
client.Msg <- fmt.Sprintf("-> %s is not available.\r\n", newName)
|
||||||
|
s.lock.Unlock()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
delete(s.clients, client.Name)
|
||||||
|
oldName := client.Name
|
||||||
|
client.Rename(newName)
|
||||||
|
s.clients[client.Name] = client
|
||||||
|
s.lock.Unlock()
|
||||||
|
|
||||||
|
s.Broadcast(fmt.Sprintf("* %s is now known as %s.\r\n", oldName, newName), nil)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Server) List(prefix *string) []string {
|
||||||
|
r := []string{}
|
||||||
|
|
||||||
|
for name, _ := range s.clients {
|
||||||
|
if prefix != nil && !strings.HasPrefix(name, *prefix) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
r = append(r, name)
|
||||||
|
}
|
||||||
|
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
func (s *Server) Start(laddr string) error {
|
func (s *Server) Start(laddr string) error {
|
||||||
// Once a ServerConfig has been configured, connections can be
|
// Once a ServerConfig has been configured, connections can be
|
||||||
// accepted.
|
// accepted.
|
||||||
@ -88,25 +152,13 @@ func (s *Server) Start(laddr string) error {
|
|||||||
|
|
||||||
go ssh.DiscardRequests(requests)
|
go ssh.DiscardRequests(requests)
|
||||||
|
|
||||||
client := NewClient(s, sshConn, sshConn.User())
|
client := NewClient(s, sshConn)
|
||||||
// TODO: mutex this
|
s.Add(client)
|
||||||
|
|
||||||
s.lock.Lock()
|
|
||||||
s.clients[client] = struct{}{}
|
|
||||||
num := len(s.clients)
|
|
||||||
s.lock.Unlock()
|
|
||||||
|
|
||||||
client.sendWelcome()
|
|
||||||
|
|
||||||
s.Broadcast(fmt.Sprintf("* %s joined. (Total connected: %d)\r\n", client.Name, num), nil)
|
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
|
// Block until done, then remove.
|
||||||
sshConn.Wait()
|
sshConn.Wait()
|
||||||
s.lock.Lock()
|
s.Remove(client)
|
||||||
delete(s.clients, client)
|
|
||||||
s.lock.Unlock()
|
|
||||||
|
|
||||||
s.Broadcast(fmt.Sprintf("* %s left.\r\n", client.Name), nil)
|
|
||||||
}()
|
}()
|
||||||
|
|
||||||
go client.handleChannels(channels)
|
go client.handleChannels(channels)
|
||||||
@ -123,7 +175,7 @@ func (s *Server) Start(laddr string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) Stop() {
|
func (s *Server) Stop() {
|
||||||
for client := range s.clients {
|
for _, client := range s.clients {
|
||||||
client.Conn.Close()
|
client.Conn.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user