|
@@ -25,17 +25,17 @@ import (
|
25
|
25
|
)
|
26
|
26
|
|
27
|
27
|
const (
|
28
|
|
- // IdleTimeout is how long without traffic before a client's considered idle.
|
|
28
|
+ // RegisterTimeout is how long clients have to register before we disconnect them
|
|
29
|
+ RegisterTimeout = time.Minute
|
|
30
|
+ // IdleTimeout is how long without traffic before a registered client is considered idle.
|
29
|
31
|
IdleTimeout = time.Minute + time.Second*30
|
30
|
|
- // QuitTimeout is how long without traffic (after they're considered idle) that clients are killed.
|
|
32
|
+ // QuitTimeout is how long without traffic before an idle client is disconnected
|
31
|
33
|
QuitTimeout = time.Minute
|
32
|
34
|
// IdentTimeoutSeconds is how many seconds before our ident (username) check times out.
|
33
|
35
|
IdentTimeoutSeconds = 1.5
|
34
|
36
|
)
|
35
|
37
|
|
36
|
38
|
var (
|
37
|
|
- // TimeoutStatedSeconds is how many seconds before clients are timed out (IdleTimeout plus QuitTimeout).
|
38
|
|
- TimeoutStatedSeconds = strconv.Itoa(int((IdleTimeout + QuitTimeout).Seconds()))
|
39
|
39
|
// ErrNickAlreadySet is a weird error that's sent when the server's consistency has been compromised.
|
40
|
40
|
ErrNickAlreadySet = errors.New("Nickname is already set")
|
41
|
41
|
)
|
|
@@ -58,7 +58,7 @@ type Client struct {
|
58
|
58
|
hasQuit bool
|
59
|
59
|
hops int
|
60
|
60
|
hostname string
|
61
|
|
- idleTimer *time.Timer
|
|
61
|
+ idletimer *IdleTimer
|
62
|
62
|
isDestroyed bool
|
63
|
63
|
isQuitting bool
|
64
|
64
|
nick string
|
|
@@ -68,8 +68,6 @@ type Client struct {
|
68
|
68
|
operName string
|
69
|
69
|
proxiedIP string // actual remote IP if using the PROXY protocol
|
70
|
70
|
quitMessage string
|
71
|
|
- quitMessageSent bool
|
72
|
|
- quitTimer *time.Timer
|
73
|
71
|
rawHostname string
|
74
|
72
|
realname string
|
75
|
73
|
registered bool
|
|
@@ -79,7 +77,6 @@ type Client struct {
|
79
|
77
|
server *Server
|
80
|
78
|
socket *Socket
|
81
|
79
|
stateMutex sync.RWMutex // generic protection for mutable state
|
82
|
|
- timerMutex sync.Mutex
|
83
|
80
|
username string
|
84
|
81
|
vhost string
|
85
|
82
|
whoisLine string
|
|
@@ -140,7 +137,6 @@ func NewClient(server *Server, conn net.Conn, isTLS bool) *Client {
|
140
|
137
|
client.Notice("*** Could not find your username")
|
141
|
138
|
}
|
142
|
139
|
}
|
143
|
|
- client.Touch()
|
144
|
140
|
go client.run()
|
145
|
141
|
|
146
|
142
|
return client
|
|
@@ -194,6 +190,9 @@ func (client *Client) run() {
|
194
|
190
|
var line string
|
195
|
191
|
var msg ircmsg.IrcMessage
|
196
|
192
|
|
|
193
|
+ client.idletimer = NewIdleTimer(client)
|
|
194
|
+ client.idletimer.Start()
|
|
195
|
+
|
197
|
196
|
// Set the hostname for this client
|
198
|
197
|
// (may be overridden by a later PROXY command from stunnel)
|
199
|
198
|
client.rawHostname = utils.AddrLookupHostname(client.socket.conn.RemoteAddr())
|
|
@@ -247,44 +246,15 @@ func (client *Client) Active() {
|
247
|
246
|
}
|
248
|
247
|
|
249
|
248
|
// Touch marks the client as alive (as it it has a connection to us and we
|
250
|
|
-// can receive messages from it), and resets when we'll send the client a
|
251
|
|
-// keepalive PING.
|
|
249
|
+// can receive messages from it).
|
252
|
250
|
func (client *Client) Touch() {
|
253
|
|
- client.timerMutex.Lock()
|
254
|
|
- defer client.timerMutex.Unlock()
|
255
|
|
-
|
256
|
|
- if client.quitTimer != nil {
|
257
|
|
- client.quitTimer.Stop()
|
258
|
|
- }
|
259
|
|
-
|
260
|
|
- if client.idleTimer == nil {
|
261
|
|
- client.idleTimer = time.AfterFunc(IdleTimeout, client.connectionIdle)
|
262
|
|
- } else {
|
263
|
|
- client.idleTimer.Reset(IdleTimeout)
|
264
|
|
- }
|
|
251
|
+ client.idletimer.Touch()
|
265
|
252
|
}
|
266
|
253
|
|
267
|
|
-// connectionIdle is run when the client has not sent us any data for a while,
|
268
|
|
-// sends the client a PING and starts the quit timeout.
|
269
|
|
-func (client *Client) connectionIdle() {
|
270
|
|
- client.timerMutex.Lock()
|
271
|
|
- defer client.timerMutex.Unlock()
|
272
|
|
-
|
|
254
|
+// Ping sends the client a PING message.
|
|
255
|
+func (client *Client) Ping() {
|
273
|
256
|
client.Send(nil, "", "PING", client.nick)
|
274
|
257
|
|
275
|
|
- if client.quitTimer == nil {
|
276
|
|
- client.quitTimer = time.AfterFunc(QuitTimeout, client.connectionTimeout)
|
277
|
|
- } else {
|
278
|
|
- client.quitTimer.Reset(QuitTimeout)
|
279
|
|
- }
|
280
|
|
-}
|
281
|
|
-
|
282
|
|
-// connectionTimeout runs after connectionIdle has been run, if we do not receive a
|
283
|
|
-// ping or any other activity back from the client. When this happens we assume the
|
284
|
|
-// connection has died and remove the client from the network.
|
285
|
|
-func (client *Client) connectionTimeout() {
|
286
|
|
- client.Quit(fmt.Sprintf("Ping timeout: %s seconds", TimeoutStatedSeconds))
|
287
|
|
- client.isQuitting = true
|
288
|
258
|
}
|
289
|
259
|
|
290
|
260
|
//
|
|
@@ -293,12 +263,16 @@ func (client *Client) connectionTimeout() {
|
293
|
263
|
|
294
|
264
|
// Register sets the client details as appropriate when entering the network.
|
295
|
265
|
func (client *Client) Register() {
|
296
|
|
- if client.registered {
|
|
266
|
+ client.stateMutex.Lock()
|
|
267
|
+ alreadyRegistered := client.registered
|
|
268
|
+ client.registered = true
|
|
269
|
+ client.stateMutex.Unlock()
|
|
270
|
+
|
|
271
|
+ if alreadyRegistered {
|
297
|
272
|
return
|
298
|
273
|
}
|
299
|
|
- client.registered = true
|
300
|
|
- client.Touch()
|
301
|
274
|
|
|
275
|
+ client.Touch()
|
302
|
276
|
client.updateNickMask("")
|
303
|
277
|
client.server.monitorManager.AlertAbout(client, true)
|
304
|
278
|
}
|
|
@@ -504,9 +478,9 @@ func (client *Client) RplISupport() {
|
504
|
478
|
// Quit sends the given quit message to the client (but does not destroy them).
|
505
|
479
|
func (client *Client) Quit(message string) {
|
506
|
480
|
client.stateMutex.Lock()
|
507
|
|
- alreadyQuit := client.quitMessageSent
|
|
481
|
+ alreadyQuit := client.isQuitting
|
508
|
482
|
if !alreadyQuit {
|
509
|
|
- client.quitMessageSent = true
|
|
483
|
+ client.isQuitting = true
|
510
|
484
|
client.quitMessage = message
|
511
|
485
|
}
|
512
|
486
|
client.stateMutex.Unlock()
|
|
@@ -567,11 +541,8 @@ func (client *Client) destroy() {
|
567
|
541
|
client.server.clients.Remove(client)
|
568
|
542
|
|
569
|
543
|
// clean up self
|
570
|
|
- if client.idleTimer != nil {
|
571
|
|
- client.idleTimer.Stop()
|
572
|
|
- }
|
573
|
|
- if client.quitTimer != nil {
|
574
|
|
- client.quitTimer.Stop()
|
|
544
|
+ if client.idletimer != nil {
|
|
545
|
+ client.idletimer.Stop()
|
575
|
546
|
}
|
576
|
547
|
|
577
|
548
|
client.socket.Close()
|