Browse Source

Fix proxied DLINE/throttle/etc error messages

tags/v0.9.2-alpha
Daniel Oaks 6 years ago
parent
commit
ffe7375a68
2 changed files with 12 additions and 19 deletions
  1. 0
    8
      irc/connection_throttling.go
  2. 12
    11
      irc/server.go

+ 0
- 8
irc/connection_throttling.go View File

7
 	"fmt"
7
 	"fmt"
8
 	"net"
8
 	"net"
9
 	"time"
9
 	"time"
10
-
11
-	"github.com/goshuirc/irc-go/ircmsg"
12
 )
10
 )
13
 
11
 
14
 // ThrottleDetails holds the connection-throttling details for a subnet/IP.
12
 // ThrottleDetails holds the connection-throttling details for a subnet/IP.
115
 
113
 
116
 	ct.BanDuration = config.BanDuration
114
 	ct.BanDuration = config.BanDuration
117
 	ct.BanMessage = config.BanMessage
115
 	ct.BanMessage = config.BanMessage
118
-	ircmsgOutput := ircmsg.MakeMessage(nil, "", "ERROR", ct.BanMessage)
119
-	msg, err := ircmsgOutput.Line()
120
-	if err != nil {
121
-		return nil, fmt.Errorf("Could not make error message: %s", err.Error())
122
-	}
123
-	ct.BanMessageBytes = []byte(msg)
124
 
116
 
125
 	// assemble exempted nets
117
 	// assemble exempted nets
126
 	for _, cidr := range config.Exempted {
118
 	for _, cidr := range config.Exempted {

+ 12
- 11
irc/server.go View File

31
 )
31
 )
32
 
32
 
33
 var (
33
 var (
34
+	// common error line to sub values into
35
+	errorMsg, _ = (&[]ircmsg.IrcMessage{ircmsg.MakeMessage(nil, "", "ERROR", "%s ")}[0]).Line()
36
+
34
 	// common error responses
37
 	// common error responses
35
-	tooManyClientsMsg, _   = (&[]ircmsg.IrcMessage{ircmsg.MakeMessage(nil, "", "ERROR", "Too many clients from your network")}[0]).Line()
36
-	couldNotParseIPMsg, _  = (&[]ircmsg.IrcMessage{ircmsg.MakeMessage(nil, "", "ERROR", "Unable to parse your IP address")}[0]).Line()
37
-	bannedFromServerMsg, _ = (&[]ircmsg.IrcMessage{ircmsg.MakeMessage(nil, "", "ERROR", "You are banned from this server (%s)")}[0]).Line()
38
+	couldNotParseIPMsg, _ = (&[]ircmsg.IrcMessage{ircmsg.MakeMessage(nil, "", "ERROR", "Unable to parse your IP address")}[0]).Line()
38
 )
39
 )
39
 
40
 
40
 const (
41
 const (
275
 
276
 
276
 			isBanned, banMsg := server.checkBans(ipaddr)
277
 			isBanned, banMsg := server.checkBans(ipaddr)
277
 			if isBanned {
278
 			if isBanned {
278
-				conn.Conn.Write(banMsg)
279
+				// this might not show up properly on some clients, but our objective here is just to close the connection out before it has a load impact on us
280
+				conn.Conn.Write([]byte(fmt.Sprintf(errorMsg, banMsg)))
279
 				conn.Conn.Close()
281
 				conn.Conn.Close()
280
 				continue
282
 				continue
281
 			}
283
 			}
289
 	}
291
 	}
290
 }
292
 }
291
 
293
 
292
-func (server *Server) checkBans(ipaddr net.IP) (banned bool, message []byte) {
294
+func (server *Server) checkBans(ipaddr net.IP) (banned bool, message string) {
293
 	// check DLINEs
295
 	// check DLINEs
294
 	isBanned, info := server.dlines.CheckIP(ipaddr)
296
 	isBanned, info := server.dlines.CheckIP(ipaddr)
295
 	if isBanned {
297
 	if isBanned {
296
-		return true, []byte(info.BanMessage(bannedFromServerMsg))
298
+		return true, info.BanMessage("You are banned from this server (%s)")
297
 	}
299
 	}
298
 
300
 
299
 	// check connection limits
301
 	// check connection limits
302
 	server.connectionLimitsMutex.Unlock()
304
 	server.connectionLimitsMutex.Unlock()
303
 	if err != nil {
305
 	if err != nil {
304
 		// too many connections from one client, tell the client and close the connection
306
 		// too many connections from one client, tell the client and close the connection
305
-		// this might not show up properly on some clients, but our objective here is just to close it out before it has a load impact on us
306
-		return true, []byte(tooManyClientsMsg)
307
+		return true, "Too many clients from your network"
307
 	}
308
 	}
308
 
309
 
309
 	// check connection throttle
310
 	// check connection throttle
323
 		server.connectionThrottle.ResetFor(ipaddr)
324
 		server.connectionThrottle.ResetFor(ipaddr)
324
 
325
 
325
 		// this might not show up properly on some clients, but our objective here is just to close it out before it has a load impact on us
326
 		// this might not show up properly on some clients, but our objective here is just to close it out before it has a load impact on us
326
-		return true, []byte(server.connectionThrottle.BanMessageBytes)
327
+		return true, server.connectionThrottle.BanMessage
327
 	}
328
 	}
328
 
329
 
329
-	return false, nil
330
+	return false, ""
330
 }
331
 }
331
 
332
 
332
 //
333
 //
2196
 
2197
 
2197
 			isBanned, banMsg := server.checkBans(parsedProxiedIP)
2198
 			isBanned, banMsg := server.checkBans(parsedProxiedIP)
2198
 			if isBanned {
2199
 			if isBanned {
2199
-				client.Quit(string(banMsg))
2200
+				client.Quit(banMsg)
2200
 				return true
2201
 				return true
2201
 			}
2202
 			}
2202
 
2203
 

Loading…
Cancel
Save