mirror of
https://github.com/shazow/ssh-chat.git
synced 2025-06-07 10:53:07 +03:00
Embedded lock in to server struct
This commit is contained in:
parent
356e89c241
commit
79eb6b4388
32
server.go
32
server.go
@ -28,7 +28,6 @@ type Server struct {
|
|||||||
sshConfig *ssh.ServerConfig
|
sshConfig *ssh.ServerConfig
|
||||||
done chan struct{}
|
done chan struct{}
|
||||||
clients Clients
|
clients Clients
|
||||||
lock sync.Mutex
|
|
||||||
count int
|
count int
|
||||||
history *History
|
history *History
|
||||||
motd string
|
motd string
|
||||||
@ -36,6 +35,7 @@ type Server struct {
|
|||||||
bannedPk map[string]*time.Time // fingerprint lookup
|
bannedPk map[string]*time.Time // fingerprint lookup
|
||||||
bannedIp map[net.Addr]*time.Time
|
bannedIp map[net.Addr]*time.Time
|
||||||
started time.Time
|
started time.Time
|
||||||
|
sync.Mutex
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewServer(privateKey []byte) (*Server, error) {
|
func NewServer(privateKey []byte) (*Server, error) {
|
||||||
@ -119,9 +119,9 @@ func (s *Server) Privmsg(nick, message string, sender *Client) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) SetMotd(client *Client, motd string) {
|
func (s *Server) SetMotd(client *Client, motd string) {
|
||||||
s.lock.Lock()
|
s.Lock()
|
||||||
s.motd = motd
|
s.motd = motd
|
||||||
s.lock.Unlock()
|
s.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) MotdUnicast(client *Client) {
|
func (s *Server) MotdUnicast(client *Client) {
|
||||||
@ -142,7 +142,7 @@ func (s *Server) Add(client *Client) {
|
|||||||
client.SysMsg("Welcome to ssh-chat. Enter /help for more.")
|
client.SysMsg("Welcome to ssh-chat. Enter /help for more.")
|
||||||
}()
|
}()
|
||||||
|
|
||||||
s.lock.Lock()
|
s.Lock()
|
||||||
s.count++
|
s.count++
|
||||||
|
|
||||||
newName, err := s.proposeName(client.Name)
|
newName, err := s.proposeName(client.Name)
|
||||||
@ -153,15 +153,15 @@ func (s *Server) Add(client *Client) {
|
|||||||
client.Rename(newName)
|
client.Rename(newName)
|
||||||
s.clients[client.Name] = client
|
s.clients[client.Name] = client
|
||||||
num := len(s.clients)
|
num := len(s.clients)
|
||||||
s.lock.Unlock()
|
s.Unlock()
|
||||||
|
|
||||||
s.Broadcast(ContinuousFormat(SYSTEM_MESSAGE_FORMAT, fmt.Sprintf(" * %s joined. (Total connected: %d)", client.ColoredName(), num)), client)
|
s.Broadcast(ContinuousFormat(SYSTEM_MESSAGE_FORMAT, fmt.Sprintf(" * %s joined. (Total connected: %d)", client.ColoredName(), num)), client)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) Remove(client *Client) {
|
func (s *Server) Remove(client *Client) {
|
||||||
s.lock.Lock()
|
s.Lock()
|
||||||
delete(s.clients, client.Name)
|
delete(s.clients, client.Name)
|
||||||
s.lock.Unlock()
|
s.Unlock()
|
||||||
|
|
||||||
s.SysMsg("%s left.", client.ColoredName())
|
s.SysMsg("%s left.", client.ColoredName())
|
||||||
}
|
}
|
||||||
@ -187,12 +187,12 @@ func (s *Server) proposeName(name string) (string, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) Rename(client *Client, newName string) {
|
func (s *Server) Rename(client *Client, newName string) {
|
||||||
s.lock.Lock()
|
s.Lock()
|
||||||
|
|
||||||
newName, err := s.proposeName(newName)
|
newName, err := s.proposeName(newName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
client.SysMsg("%s", err)
|
client.SysMsg("%s", err)
|
||||||
s.lock.Unlock()
|
s.Unlock()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -201,7 +201,7 @@ func (s *Server) Rename(client *Client, newName string) {
|
|||||||
oldName := client.Name
|
oldName := client.Name
|
||||||
client.Rename(newName)
|
client.Rename(newName)
|
||||||
s.clients[client.Name] = client
|
s.clients[client.Name] = client
|
||||||
s.lock.Unlock()
|
s.Unlock()
|
||||||
|
|
||||||
s.SysMsg("%s is now known as %s.", ColorString(client.Color, oldName), ColorString(client.Color, newName))
|
s.SysMsg("%s is now known as %s.", ColorString(client.Color, oldName), ColorString(client.Color, newName))
|
||||||
}
|
}
|
||||||
@ -225,9 +225,9 @@ func (s *Server) Who(name string) *Client {
|
|||||||
|
|
||||||
func (s *Server) Op(fingerprint string) {
|
func (s *Server) Op(fingerprint string) {
|
||||||
logger.Infof("Adding admin: %s", fingerprint)
|
logger.Infof("Adding admin: %s", fingerprint)
|
||||||
s.lock.Lock()
|
s.Lock()
|
||||||
s.admins[fingerprint] = struct{}{}
|
s.admins[fingerprint] = struct{}{}
|
||||||
s.lock.Unlock()
|
s.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) Uptime() string {
|
func (s *Server) Uptime() string {
|
||||||
@ -256,19 +256,19 @@ func (s *Server) IsBanned(fingerprint string) bool {
|
|||||||
|
|
||||||
func (s *Server) Ban(fingerprint string, duration *time.Duration) {
|
func (s *Server) Ban(fingerprint string, duration *time.Duration) {
|
||||||
var until *time.Time
|
var until *time.Time
|
||||||
s.lock.Lock()
|
s.Lock()
|
||||||
if duration != nil {
|
if duration != nil {
|
||||||
when := time.Now().Add(*duration)
|
when := time.Now().Add(*duration)
|
||||||
until = &when
|
until = &when
|
||||||
}
|
}
|
||||||
s.bannedPk[fingerprint] = until
|
s.bannedPk[fingerprint] = until
|
||||||
s.lock.Unlock()
|
s.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) Unban(fingerprint string) {
|
func (s *Server) Unban(fingerprint string) {
|
||||||
s.lock.Lock()
|
s.Lock()
|
||||||
delete(s.bannedPk, fingerprint)
|
delete(s.bannedPk, fingerprint)
|
||||||
s.lock.Unlock()
|
s.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) Start(laddr string) error {
|
func (s *Server) Start(laddr string) error {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user