Przeglądaj źródła

Fix proxied DLINE/throttle/etc error messages

tags/v0.9.2-alpha
Daniel Oaks 6 lat temu
rodzic
commit
ffe7375a68
2 zmienionych plików z 12 dodań i 19 usunięć
  1. 0
    8
      irc/connection_throttling.go
  2. 12
    11
      irc/server.go

+ 0
- 8
irc/connection_throttling.go Wyświetl plik

@@ -7,8 +7,6 @@ import (
7 7
 	"fmt"
8 8
 	"net"
9 9
 	"time"
10
-
11
-	"github.com/goshuirc/irc-go/ircmsg"
12 10
 )
13 11
 
14 12
 // ThrottleDetails holds the connection-throttling details for a subnet/IP.
@@ -115,12 +113,6 @@ func NewConnectionThrottle(config ConnectionThrottleConfig) (*ConnectionThrottle
115 113
 
116 114
 	ct.BanDuration = config.BanDuration
117 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 117
 	// assemble exempted nets
126 118
 	for _, cidr := range config.Exempted {

+ 12
- 11
irc/server.go Wyświetl plik

@@ -31,10 +31,11 @@ import (
31 31
 )
32 32
 
33 33
 var (
34
+	// common error line to sub values into
35
+	errorMsg, _ = (&[]ircmsg.IrcMessage{ircmsg.MakeMessage(nil, "", "ERROR", "%s ")}[0]).Line()
36
+
34 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 41
 const (
@@ -275,7 +276,8 @@ func (server *Server) Run() {
275 276
 
276 277
 			isBanned, banMsg := server.checkBans(ipaddr)
277 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 281
 				conn.Conn.Close()
280 282
 				continue
281 283
 			}
@@ -289,11 +291,11 @@ func (server *Server) Run() {
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 295
 	// check DLINEs
294 296
 	isBanned, info := server.dlines.CheckIP(ipaddr)
295 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 301
 	// check connection limits
@@ -302,8 +304,7 @@ func (server *Server) checkBans(ipaddr net.IP) (banned bool, message []byte) {
302 304
 	server.connectionLimitsMutex.Unlock()
303 305
 	if err != nil {
304 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 310
 	// check connection throttle
@@ -323,10 +324,10 @@ func (server *Server) checkBans(ipaddr net.IP) (banned bool, message []byte) {
323 324
 		server.connectionThrottle.ResetFor(ipaddr)
324 325
 
325 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,7 +2197,7 @@ func proxyHandler(server *Server, client *Client, msg ircmsg.IrcMessage) bool {
2196 2197
 
2197 2198
 			isBanned, banMsg := server.checkBans(parsedProxiedIP)
2198 2199
 			if isBanned {
2199
-				client.Quit(string(banMsg))
2200
+				client.Quit(banMsg)
2200 2201
 				return true
2201 2202
 			}
2202 2203
 

Ładowanie…
Anuluj
Zapisz