mirror of
https://github.com/ergochat/ergo.git
synced 2025-01-03 08:32:43 +01:00
adjust authorization and add more hang logging
This commit is contained in:
parent
ce931b23a2
commit
b6a7d98b64
@ -3,7 +3,6 @@ package irc
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"net"
|
||||
"strings"
|
||||
@ -11,6 +10,7 @@ import (
|
||||
)
|
||||
|
||||
type Client struct {
|
||||
atime time.Time
|
||||
away bool
|
||||
awayMessage string
|
||||
channels ChannelSet
|
||||
@ -27,7 +27,7 @@ type Client struct {
|
||||
registered bool
|
||||
replies chan Reply
|
||||
server *Server
|
||||
serverPass bool
|
||||
authorized bool
|
||||
username string
|
||||
}
|
||||
|
||||
@ -48,9 +48,12 @@ func NewClient(server *Server, conn net.Conn) *Client {
|
||||
}
|
||||
|
||||
func (client *Client) Touch() {
|
||||
client.atime = time.Now()
|
||||
|
||||
if client.quitTimer != nil {
|
||||
client.quitTimer.Stop()
|
||||
}
|
||||
|
||||
if client.idleTimer == nil {
|
||||
client.idleTimer = time.AfterFunc(IDLE_TIMEOUT, client.Idle)
|
||||
} else {
|
||||
@ -64,6 +67,7 @@ func (client *Client) Idle() {
|
||||
} else {
|
||||
client.quitTimer.Reset(QUIT_TIMEOUT)
|
||||
}
|
||||
|
||||
client.Reply(RplPing(client.server, client))
|
||||
}
|
||||
|
||||
@ -90,12 +94,8 @@ func (c *Client) readConn() {
|
||||
line, err := recv.ReadString('\n')
|
||||
if err != nil {
|
||||
if DEBUG_NET {
|
||||
if err == io.EOF {
|
||||
log.Printf("%s → closed", c.conn.RemoteAddr())
|
||||
} else {
|
||||
log.Printf("%s → error: %s", c.conn.RemoteAddr(), err)
|
||||
}
|
||||
}
|
||||
break
|
||||
}
|
||||
|
||||
@ -124,12 +124,8 @@ func (c *Client) readConn() {
|
||||
func (client *Client) maybeLogWriteError(err error) bool {
|
||||
if err != nil {
|
||||
if DEBUG_NET {
|
||||
if err == io.EOF {
|
||||
log.Printf("%s ← closed", client.conn.RemoteAddr())
|
||||
} else {
|
||||
log.Printf("%s ← error: %s", client.conn.RemoteAddr(), err)
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
return false
|
||||
@ -181,6 +177,8 @@ func (client *Client) Destroy() {
|
||||
// clear channel list
|
||||
client.channels = make(ChannelSet)
|
||||
|
||||
client.server.clients.Remove(client)
|
||||
|
||||
client.destroyed = true
|
||||
}
|
||||
|
||||
|
@ -55,26 +55,39 @@ func (server *Server) receiveCommands(commands <-chan Command) {
|
||||
log.Printf("%s → %s %s", command.Client(), server, command)
|
||||
}
|
||||
client := command.Client()
|
||||
|
||||
if !server.Authorize(client, command) {
|
||||
client.Destroy()
|
||||
return
|
||||
}
|
||||
|
||||
client.Touch()
|
||||
command.HandleServer(server)
|
||||
|
||||
if DEBUG_SERVER {
|
||||
log.Printf("%s → %s %s processed", command.Client(), server, command)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (server *Server) Authorize(client *Client, command Command) bool {
|
||||
if client.authorized {
|
||||
return true
|
||||
}
|
||||
|
||||
if !client.serverPass {
|
||||
if server.password == "" {
|
||||
client.serverPass = true
|
||||
client.authorized = true
|
||||
return true
|
||||
}
|
||||
|
||||
} else {
|
||||
switch command.(type) {
|
||||
case *PassCommand, *CapCommand, *ProxyCommand:
|
||||
// no-op
|
||||
default:
|
||||
client.Reply(ErrPasswdMismatch(server))
|
||||
server.clients.Remove(client)
|
||||
client.Destroy()
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
command.HandleServer(server)
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func newListener(config ListenerConfig) (net.Listener, error) {
|
||||
@ -222,14 +235,20 @@ func (m *PongCommand) HandleServer(s *Server) {
|
||||
}
|
||||
|
||||
func (m *PassCommand) HandleServer(s *Server) {
|
||||
if s.password != m.password {
|
||||
m.Client().Reply(ErrPasswdMismatch(s))
|
||||
m.Client().Destroy()
|
||||
client := m.Client()
|
||||
|
||||
if client.registered || client.authorized {
|
||||
client.Reply(ErrAlreadyRegistered(s))
|
||||
return
|
||||
}
|
||||
|
||||
m.Client().serverPass = true
|
||||
// no reply?
|
||||
if s.password != m.password {
|
||||
client.Reply(ErrPasswdMismatch(s))
|
||||
client.Destroy()
|
||||
return
|
||||
}
|
||||
|
||||
client.authorized = true
|
||||
}
|
||||
|
||||
func (m *NickCommand) HandleServer(s *Server) {
|
||||
@ -449,9 +468,7 @@ func (msg *CapCommand) HandleServer(server *Server) {
|
||||
}
|
||||
|
||||
func (msg *ProxyCommand) HandleServer(server *Server) {
|
||||
go func() {
|
||||
msg.Client().hostname = LookupHostname(msg.sourceIP)
|
||||
}()
|
||||
}
|
||||
|
||||
func (msg *AwayCommand) HandleServer(server *Server) {
|
||||
|
Loading…
Reference in New Issue
Block a user