3
0
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:
Jeremy Latt 2014-02-13 18:39:33 -08:00
parent ce931b23a2
commit b6a7d98b64
2 changed files with 51 additions and 36 deletions

View File

@ -3,7 +3,6 @@ package irc
import ( import (
"bufio" "bufio"
"fmt" "fmt"
"io"
"log" "log"
"net" "net"
"strings" "strings"
@ -11,6 +10,7 @@ import (
) )
type Client struct { type Client struct {
atime time.Time
away bool away bool
awayMessage string awayMessage string
channels ChannelSet channels ChannelSet
@ -27,7 +27,7 @@ type Client struct {
registered bool registered bool
replies chan Reply replies chan Reply
server *Server server *Server
serverPass bool authorized bool
username string username string
} }
@ -48,9 +48,12 @@ func NewClient(server *Server, conn net.Conn) *Client {
} }
func (client *Client) Touch() { func (client *Client) Touch() {
client.atime = time.Now()
if client.quitTimer != nil { if client.quitTimer != nil {
client.quitTimer.Stop() client.quitTimer.Stop()
} }
if client.idleTimer == nil { if client.idleTimer == nil {
client.idleTimer = time.AfterFunc(IDLE_TIMEOUT, client.Idle) client.idleTimer = time.AfterFunc(IDLE_TIMEOUT, client.Idle)
} else { } else {
@ -64,6 +67,7 @@ func (client *Client) Idle() {
} else { } else {
client.quitTimer.Reset(QUIT_TIMEOUT) client.quitTimer.Reset(QUIT_TIMEOUT)
} }
client.Reply(RplPing(client.server, client)) client.Reply(RplPing(client.server, client))
} }
@ -90,11 +94,7 @@ func (c *Client) readConn() {
line, err := recv.ReadString('\n') line, err := recv.ReadString('\n')
if err != nil { if err != nil {
if DEBUG_NET { if DEBUG_NET {
if err == io.EOF { log.Printf("%s → error: %s", c.conn.RemoteAddr(), err)
log.Printf("%s → closed", c.conn.RemoteAddr())
} else {
log.Printf("%s → error: %s", c.conn.RemoteAddr(), err)
}
} }
break break
} }
@ -124,11 +124,7 @@ func (c *Client) readConn() {
func (client *Client) maybeLogWriteError(err error) bool { func (client *Client) maybeLogWriteError(err error) bool {
if err != nil { if err != nil {
if DEBUG_NET { if DEBUG_NET {
if err == io.EOF { log.Printf("%s ← error: %s", client.conn.RemoteAddr(), err)
log.Printf("%s ← closed", client.conn.RemoteAddr())
} else {
log.Printf("%s ← error: %s", client.conn.RemoteAddr(), err)
}
} }
return true return true
} }
@ -181,6 +177,8 @@ func (client *Client) Destroy() {
// clear channel list // clear channel list
client.channels = make(ChannelSet) client.channels = make(ChannelSet)
client.server.clients.Remove(client)
client.destroyed = true client.destroyed = true
} }

View File

@ -55,28 +55,41 @@ func (server *Server) receiveCommands(commands <-chan Command) {
log.Printf("%s → %s %s", command.Client(), server, command) log.Printf("%s → %s %s", command.Client(), server, command)
} }
client := command.Client() client := command.Client()
client.Touch()
if !client.serverPass { if !server.Authorize(client, command) {
if server.password == "" { client.Destroy()
client.serverPass = true return
} else {
switch command.(type) {
case *PassCommand, *CapCommand, *ProxyCommand:
// no-op
default:
client.Reply(ErrPasswdMismatch(server))
server.clients.Remove(client)
client.Destroy()
return
}
}
} }
client.Touch()
command.HandleServer(server) 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 server.password == "" {
client.authorized = true
return true
}
switch command.(type) {
case *PassCommand, *CapCommand, *ProxyCommand:
// no-op
default:
return false
}
return true
}
func newListener(config ListenerConfig) (net.Listener, error) { func newListener(config ListenerConfig) (net.Listener, error) {
if config.IsTLS() { if config.IsTLS() {
certificate, err := tls.LoadX509KeyPair(config.Certificate, config.Key) certificate, err := tls.LoadX509KeyPair(config.Certificate, config.Key)
@ -222,14 +235,20 @@ func (m *PongCommand) HandleServer(s *Server) {
} }
func (m *PassCommand) HandleServer(s *Server) { func (m *PassCommand) HandleServer(s *Server) {
if s.password != m.password { client := m.Client()
m.Client().Reply(ErrPasswdMismatch(s))
m.Client().Destroy() if client.registered || client.authorized {
client.Reply(ErrAlreadyRegistered(s))
return return
} }
m.Client().serverPass = true if s.password != m.password {
// no reply? client.Reply(ErrPasswdMismatch(s))
client.Destroy()
return
}
client.authorized = true
} }
func (m *NickCommand) HandleServer(s *Server) { func (m *NickCommand) HandleServer(s *Server) {
@ -449,9 +468,7 @@ func (msg *CapCommand) HandleServer(server *Server) {
} }
func (msg *ProxyCommand) HandleServer(server *Server) { func (msg *ProxyCommand) HandleServer(server *Server) {
go func() { msg.Client().hostname = LookupHostname(msg.sourceIP)
msg.Client().hostname = LookupHostname(msg.sourceIP)
}()
} }
func (msg *AwayCommand) HandleServer(server *Server) { func (msg *AwayCommand) HandleServer(server *Server) {