mirror of
https://github.com/shazow/ssh-chat.git
synced 2025-06-07 02:43:05 +03:00
Hopefully fix hanging?
This commit is contained in:
parent
820372a975
commit
35569887c0
40
client.go
40
client.go
@ -9,18 +9,19 @@ import (
|
|||||||
"golang.org/x/crypto/ssh/terminal"
|
"golang.org/x/crypto/ssh/terminal"
|
||||||
)
|
)
|
||||||
|
|
||||||
const MSG_BUFFER int = 10
|
const MSG_BUFFER int = 50
|
||||||
|
const MAX_MSG_LENGTH int = 512
|
||||||
|
|
||||||
const HELP_TEXT string = SYSTEM_MESSAGE_FORMAT + `-> Available commands:
|
const HELP_TEXT string = SYSTEM_MESSAGE_FORMAT + `-> Available commands:
|
||||||
/about - About this chat
|
/about - About this chat
|
||||||
/exit - Exit the chat
|
/exit - Exit the chat
|
||||||
/help - Show this help text
|
/help - Show this help text
|
||||||
/list - List the users that are currently connected
|
/list - List the users that are currently connected
|
||||||
|
/beep - Enable BEL notifications on mention.
|
||||||
/me $ACTION - Show yourself doing an action
|
/me $ACTION - Show yourself doing an action
|
||||||
/nick $NAME - Rename yourself to a new name
|
/nick $NAME - Rename yourself to a new name
|
||||||
/whois $NAME - Display information about another connected user
|
/whois $NAME - Display information about another connected user
|
||||||
/msg $NAME $MESSAGE
|
/msg $NAME $MESSAGE
|
||||||
/beep - Enable BEL notifications on mention.
|
|
||||||
` + RESET
|
` + RESET
|
||||||
|
|
||||||
const OP_HELP_TEXT string = SYSTEM_MESSAGE_FORMAT + `-> Available operator commands:
|
const OP_HELP_TEXT string = SYSTEM_MESSAGE_FORMAT + `-> Available operator commands:
|
||||||
@ -30,8 +31,7 @@ const OP_HELP_TEXT string = SYSTEM_MESSAGE_FORMAT + `-> Available operator comma
|
|||||||
/silence $NAME - Revoke a user's ability to speak
|
/silence $NAME - Revoke a user's ability to speak
|
||||||
`
|
`
|
||||||
|
|
||||||
const ABOUT_TEXT string = SYSTEM_MESSAGE_FORMAT + `
|
const ABOUT_TEXT string = SYSTEM_MESSAGE_FORMAT + `-> ssh-chat is made by @shazow.
|
||||||
-> ssh-chat is made by @shazow.
|
|
||||||
|
|
||||||
It is a custom ssh server built in Go to serve a chat experience
|
It is a custom ssh server built in Go to serve a chat experience
|
||||||
instead of a shell.
|
instead of a shell.
|
||||||
@ -79,10 +79,6 @@ func (c *Client) SysMsg(msg string, args ...interface{}) {
|
|||||||
c.Msg <- ContinuousFormat(SYSTEM_MESSAGE_FORMAT, "-> "+fmt.Sprintf(msg, args...))
|
c.Msg <- ContinuousFormat(SYSTEM_MESSAGE_FORMAT, "-> "+fmt.Sprintf(msg, args...))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) SysMsg2(msg string, args ...interface{}) {
|
|
||||||
c.Write(ContinuousFormat(SYSTEM_MESSAGE_FORMAT, "-> "+fmt.Sprintf(msg, args...)))
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *Client) Write(msg string) {
|
func (c *Client) Write(msg string) {
|
||||||
c.term.Write([]byte(msg + "\r\n"))
|
c.term.Write([]byte(msg + "\r\n"))
|
||||||
}
|
}
|
||||||
@ -93,6 +89,24 @@ func (c *Client) WriteLines(msg []string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *Client) Send(msg string) {
|
||||||
|
if len(msg) > MAX_MSG_LENGTH {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
select {
|
||||||
|
case c.Msg <- msg:
|
||||||
|
default:
|
||||||
|
logger.Errorf("Msg buffer full, dropping: %s (%s)", c.Name, c.Conn.RemoteAddr())
|
||||||
|
c.Conn.Close()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) SendLines(msg []string) {
|
||||||
|
for _, line := range msg {
|
||||||
|
c.Send(line)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (c *Client) IsSilenced() bool {
|
func (c *Client) IsSilenced() bool {
|
||||||
return c.silencedUntil.After(time.Now())
|
return c.silencedUntil.After(time.Now())
|
||||||
}
|
}
|
||||||
@ -216,7 +230,7 @@ func (c *Client) handleShell(channel ssh.Channel) {
|
|||||||
c.SysMsg("No such name: %s", parts[1])
|
c.SysMsg("No such name: %s", parts[1])
|
||||||
} else {
|
} else {
|
||||||
fingerprint := client.Fingerprint()
|
fingerprint := client.Fingerprint()
|
||||||
client.SysMsg2("Banned by %s.", c.ColoredName())
|
client.SysMsg("Banned by %s.", c.ColoredName())
|
||||||
c.Server.Ban(fingerprint, nil)
|
c.Server.Ban(fingerprint, nil)
|
||||||
client.Conn.Close()
|
client.Conn.Close()
|
||||||
c.Server.Broadcast(fmt.Sprintf("* %s was banned by %s", parts[1], c.ColoredName()), nil)
|
c.Server.Broadcast(fmt.Sprintf("* %s was banned by %s", parts[1], c.ColoredName()), nil)
|
||||||
@ -233,7 +247,7 @@ func (c *Client) handleShell(channel ssh.Channel) {
|
|||||||
c.SysMsg("No such name: %s", parts[1])
|
c.SysMsg("No such name: %s", parts[1])
|
||||||
} else {
|
} else {
|
||||||
fingerprint := client.Fingerprint()
|
fingerprint := client.Fingerprint()
|
||||||
client.SysMsg2("Made op by %s.", c.ColoredName())
|
client.SysMsg("Made op by %s.", c.ColoredName())
|
||||||
c.Server.Op(fingerprint)
|
c.Server.Op(fingerprint)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -247,7 +261,7 @@ func (c *Client) handleShell(channel ssh.Channel) {
|
|||||||
if client == nil {
|
if client == nil {
|
||||||
c.SysMsg("No such name: %s", parts[1])
|
c.SysMsg("No such name: %s", parts[1])
|
||||||
} else {
|
} else {
|
||||||
client.SysMsg2("Kicked by %s.", c.ColoredName())
|
client.SysMsg("Kicked by %s.", c.ColoredName())
|
||||||
client.Conn.Close()
|
client.Conn.Close()
|
||||||
c.Server.Broadcast(fmt.Sprintf("* %s was kicked by %s", parts[1], c.ColoredName()), nil)
|
c.Server.Broadcast(fmt.Sprintf("* %s was kicked by %s", parts[1], c.ColoredName()), nil)
|
||||||
}
|
}
|
||||||
@ -270,7 +284,7 @@ func (c *Client) handleShell(channel ssh.Channel) {
|
|||||||
c.SysMsg("No such name: %s", parts[1])
|
c.SysMsg("No such name: %s", parts[1])
|
||||||
} else {
|
} else {
|
||||||
client.Silence(duration)
|
client.Silence(duration)
|
||||||
client.SysMsg2("Silenced for %s by %s.", duration, c.ColoredName())
|
client.SysMsg("Silenced for %s by %s.", duration, c.ColoredName())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
case "/msg": /* Send a PM */
|
case "/msg": /* Send a PM */
|
||||||
@ -299,7 +313,7 @@ func (c *Client) handleShell(channel ssh.Channel) {
|
|||||||
c.Msg <- fmt.Sprintf("-> Rate limiting in effect.")
|
c.Msg <- fmt.Sprintf("-> Rate limiting in effect.")
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if c.IsSilenced() || len(msg) > 1000 {
|
if c.IsSilenced() || len(msg) > 1000 || len(msg) == 0 {
|
||||||
c.SysMsg("Message rejected.")
|
c.SysMsg("Message rejected.")
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
17
server.go
17
server.go
@ -61,11 +61,6 @@ func NewServer(privateKey []byte) (*Server, error) {
|
|||||||
if server.IsBanned(fingerprint) {
|
if server.IsBanned(fingerprint) {
|
||||||
return nil, fmt.Errorf("Banned.")
|
return nil, fmt.Errorf("Banned.")
|
||||||
}
|
}
|
||||||
ip := strings.Split(conn.RemoteAddr().String(), ":")[0]
|
|
||||||
if ip == "73.3.250.197" {
|
|
||||||
// Can't believe I'm doing this...
|
|
||||||
return nil, fmt.Errorf("Banned.")
|
|
||||||
}
|
|
||||||
perm := &ssh.Permissions{Extensions: map[string]string{"fingerprint": fingerprint}}
|
perm := &ssh.Permissions{Extensions: map[string]string{"fingerprint": fingerprint}}
|
||||||
return perm, nil
|
return perm, nil
|
||||||
},
|
},
|
||||||
@ -93,11 +88,11 @@ func (s *Server) Broadcast(msg string, except *Client) {
|
|||||||
if except != nil && client == except {
|
if except != nil && client == except {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
/* Add an ascii BEL to ding clients when they're mentioned */
|
|
||||||
|
client.Send(msg)
|
||||||
if client.beepMe && strings.Contains(msg, client.Name) {
|
if client.beepMe && strings.Contains(msg, client.Name) {
|
||||||
client.Msg <- msg + BEEP
|
/* Add an ascii BEL to ding clients when they're mentioned */
|
||||||
} else {
|
client.Send(BEEP)
|
||||||
client.Msg <- msg
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -117,8 +112,8 @@ func (s *Server) Privmsg(nick, message string, sender *Client) error {
|
|||||||
|
|
||||||
func (s *Server) Add(client *Client) {
|
func (s *Server) Add(client *Client) {
|
||||||
go func() {
|
go func() {
|
||||||
client.WriteLines(s.history.Get(10))
|
client.SendLines(s.history.Get(10))
|
||||||
client.SysMsg2("Welcome to ssh-chat. Enter /help for more.")
|
client.SysMsg("Welcome to ssh-chat. Enter /help for more.")
|
||||||
}()
|
}()
|
||||||
|
|
||||||
s.lock.Lock()
|
s.lock.Lock()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user