Browse Source

adjust authorization and add more hang logging

tags/v0.1.0
Jeremy Latt 10 years ago
parent
commit
b6a7d98b64
2 changed files with 50 additions and 35 deletions
  1. 10
    12
      irc/client.go
  2. 40
    23
      irc/server.go

+ 10
- 12
irc/client.go View File

@@ -3,7 +3,6 @@ package irc
3 3
 import (
4 4
 	"bufio"
5 5
 	"fmt"
6
-	"io"
7 6
 	"log"
8 7
 	"net"
9 8
 	"strings"
@@ -11,6 +10,7 @@ import (
11 10
 )
12 11
 
13 12
 type Client struct {
13
+	atime       time.Time
14 14
 	away        bool
15 15
 	awayMessage string
16 16
 	channels    ChannelSet
@@ -27,7 +27,7 @@ type Client struct {
27 27
 	registered  bool
28 28
 	replies     chan Reply
29 29
 	server      *Server
30
-	serverPass  bool
30
+	authorized  bool
31 31
 	username    string
32 32
 }
33 33
 
@@ -48,9 +48,12 @@ func NewClient(server *Server, conn net.Conn) *Client {
48 48
 }
49 49
 
50 50
 func (client *Client) Touch() {
51
+	client.atime = time.Now()
52
+
51 53
 	if client.quitTimer != nil {
52 54
 		client.quitTimer.Stop()
53 55
 	}
56
+
54 57
 	if client.idleTimer == nil {
55 58
 		client.idleTimer = time.AfterFunc(IDLE_TIMEOUT, client.Idle)
56 59
 	} else {
@@ -64,6 +67,7 @@ func (client *Client) Idle() {
64 67
 	} else {
65 68
 		client.quitTimer.Reset(QUIT_TIMEOUT)
66 69
 	}
70
+
67 71
 	client.Reply(RplPing(client.server, client))
68 72
 }
69 73
 
@@ -90,11 +94,7 @@ func (c *Client) readConn() {
90 94
 		line, err := recv.ReadString('\n')
91 95
 		if err != nil {
92 96
 			if DEBUG_NET {
93
-				if err == io.EOF {
94
-					log.Printf("%s → closed", c.conn.RemoteAddr())
95
-				} else {
96
-					log.Printf("%s → error: %s", c.conn.RemoteAddr(), err)
97
-				}
97
+				log.Printf("%s → error: %s", c.conn.RemoteAddr(), err)
98 98
 			}
99 99
 			break
100 100
 		}
@@ -124,11 +124,7 @@ func (c *Client) readConn() {
124 124
 func (client *Client) maybeLogWriteError(err error) bool {
125 125
 	if err != nil {
126 126
 		if DEBUG_NET {
127
-			if err == io.EOF {
128
-				log.Printf("%s ← closed", client.conn.RemoteAddr())
129
-			} else {
130
-				log.Printf("%s ← error: %s", client.conn.RemoteAddr(), err)
131
-			}
127
+			log.Printf("%s ← error: %s", client.conn.RemoteAddr(), err)
132 128
 		}
133 129
 		return true
134 130
 	}
@@ -181,6 +177,8 @@ func (client *Client) Destroy() {
181 177
 	// clear channel list
182 178
 	client.channels = make(ChannelSet)
183 179
 
180
+	client.server.clients.Remove(client)
181
+
184 182
 	client.destroyed = true
185 183
 }
186 184
 

+ 40
- 23
irc/server.go View File

@@ -55,26 +55,39 @@ func (server *Server) receiveCommands(commands <-chan Command) {
55 55
 			log.Printf("%s → %s %s", command.Client(), server, command)
56 56
 		}
57 57
 		client := command.Client()
58
-		client.Touch()
59 58
 
60
-		if !client.serverPass {
61
-			if server.password == "" {
62
-				client.serverPass = true
63
-
64
-			} else {
65
-				switch command.(type) {
66
-				case *PassCommand, *CapCommand, *ProxyCommand:
67
-					// no-op
68
-				default:
69
-					client.Reply(ErrPasswdMismatch(server))
70
-					server.clients.Remove(client)
71
-					client.Destroy()
72
-					return
73
-				}
74
-			}
59
+		if !server.Authorize(client, command) {
60
+			client.Destroy()
61
+			return
75 62
 		}
63
+
64
+		client.Touch()
76 65
 		command.HandleServer(server)
66
+
67
+		if DEBUG_SERVER {
68
+			log.Printf("%s → %s %s processed", command.Client(), server, command)
69
+		}
70
+	}
71
+}
72
+
73
+func (server *Server) Authorize(client *Client, command Command) bool {
74
+	if client.authorized {
75
+		return true
76
+	}
77
+
78
+	if server.password == "" {
79
+		client.authorized = true
80
+		return true
77 81
 	}
82
+
83
+	switch command.(type) {
84
+	case *PassCommand, *CapCommand, *ProxyCommand:
85
+		// no-op
86
+	default:
87
+		return false
88
+	}
89
+
90
+	return true
78 91
 }
79 92
 
80 93
 func newListener(config ListenerConfig) (net.Listener, error) {
@@ -222,14 +235,20 @@ func (m *PongCommand) HandleServer(s *Server) {
222 235
 }
223 236
 
224 237
 func (m *PassCommand) HandleServer(s *Server) {
238
+	client := m.Client()
239
+
240
+	if client.registered || client.authorized {
241
+		client.Reply(ErrAlreadyRegistered(s))
242
+		return
243
+	}
244
+
225 245
 	if s.password != m.password {
226
-		m.Client().Reply(ErrPasswdMismatch(s))
227
-		m.Client().Destroy()
246
+		client.Reply(ErrPasswdMismatch(s))
247
+		client.Destroy()
228 248
 		return
229 249
 	}
230 250
 
231
-	m.Client().serverPass = true
232
-	// no reply?
251
+	client.authorized = true
233 252
 }
234 253
 
235 254
 func (m *NickCommand) HandleServer(s *Server) {
@@ -449,9 +468,7 @@ func (msg *CapCommand) HandleServer(server *Server) {
449 468
 }
450 469
 
451 470
 func (msg *ProxyCommand) HandleServer(server *Server) {
452
-	go func() {
453
-		msg.Client().hostname = LookupHostname(msg.sourceIP)
454
-	}()
471
+	msg.Client().hostname = LookupHostname(msg.sourceIP)
455 472
 }
456 473
 
457 474
 func (msg *AwayCommand) HandleServer(server *Server) {

Loading…
Cancel
Save