Bläddra i källkod

fix #1842

Warn about banning a single IPv6 address
tags/v2.9.0-rc1
Shivaram Lingamneni 2 år sedan
förälder
incheckning
fd45529d94
3 ändrade filer med 54 tillägg och 1 borttagningar
  1. 8
    0
      irc/flatip/flatip.go
  2. 34
    0
      irc/flatip/flatip_test.go
  3. 12
    1
      irc/uban.go

+ 8
- 0
irc/flatip/flatip.go Visa fil

155
 	return cidr.IP == maskedIP
155
 	return cidr.IP == maskedIP
156
 }
156
 }
157
 
157
 
158
+func (cidr IPNet) Size() (ones, bits int) {
159
+	if cidr.IP.IsIPv4() {
160
+		return int(cidr.PrefixLen) - 96, 32
161
+	} else {
162
+		return int(cidr.PrefixLen), 128
163
+	}
164
+}
165
+
158
 // FromNetIPnet converts a net.IPNet into an IPNet.
166
 // FromNetIPnet converts a net.IPNet into an IPNet.
159
 func FromNetIPNet(network net.IPNet) (result IPNet) {
167
 func FromNetIPNet(network net.IPNet) (result IPNet) {
160
 	ones, _ := network.Mask.Size()
168
 	ones, _ := network.Mask.Size()

+ 34
- 0
irc/flatip/flatip_test.go Visa fil

2
 
2
 
3
 import (
3
 import (
4
 	"bytes"
4
 	"bytes"
5
+	"fmt"
5
 	"math/rand"
6
 	"math/rand"
6
 	"net"
7
 	"net"
8
+	"reflect"
7
 	"testing"
9
 	"testing"
8
 	"time"
10
 	"time"
9
 )
11
 )
86
 	}
88
 	}
87
 }
89
 }
88
 
90
 
91
+func assertEqual(found, expected interface{}) {
92
+	if !reflect.DeepEqual(found, expected) {
93
+		panic(fmt.Sprintf("expected %#v, found %#v", expected, found))
94
+	}
95
+}
96
+
97
+func TestSize(t *testing.T) {
98
+	_, net, err := ParseCIDR("8.8.8.8/24")
99
+	if err != nil {
100
+		panic(err)
101
+	}
102
+	ones, bits := net.Size()
103
+	assertEqual(ones, 24)
104
+	assertEqual(bits, 32)
105
+
106
+	_, net, err = ParseCIDR("2001::0db8/64")
107
+	if err != nil {
108
+		panic(err)
109
+	}
110
+	ones, bits = net.Size()
111
+	assertEqual(ones, 64)
112
+	assertEqual(bits, 128)
113
+
114
+	_, net, err = ParseCIDR("2001::0db8/96")
115
+	if err != nil {
116
+		panic(err)
117
+	}
118
+	ones, bits = net.Size()
119
+	assertEqual(ones, 96)
120
+	assertEqual(bits, 128)
121
+}
122
+
89
 func TestMasking(t *testing.T) {
123
 func TestMasking(t *testing.T) {
90
 	for _, ipstr := range testIPStrs {
124
 	for _, ipstr := range testIPStrs {
91
 		doMaskingTest(easyParseIP(ipstr), t)
125
 		doMaskingTest(easyParseIP(ipstr), t)

+ 12
- 1
irc/uban.go Visa fil

366
 }
366
 }
367
 
367
 
368
 func ubanInfoCIDR(client *Client, target ubanTarget, rb *ResponseBuffer) {
368
 func ubanInfoCIDR(client *Client, target ubanTarget, rb *ResponseBuffer) {
369
-	if target.cidr.PrefixLen == 128 {
369
+	config := client.server.Config()
370
+	// show connection limiter/throttler state if this CIDR is entirely
371
+	// contained in a single limiter/throttler bucket:
372
+	ones, bits := target.cidr.Size()
373
+	showLimiter := (bits == 32 && ones >= config.Server.IPLimits.CidrLenIPv4) ||
374
+		(bits == 128 && ones >= config.Server.IPLimits.CidrLenIPv6)
375
+	sendMaskWarning := (bits == 128 && ones > config.Server.IPLimits.CidrLenIPv6)
376
+	if showLimiter {
370
 		netName, status := client.server.connectionLimiter.Status(target.cidr.IP)
377
 		netName, status := client.server.connectionLimiter.Status(target.cidr.IP)
371
 		if status.Exempt {
378
 		if status.Exempt {
372
 			rb.Notice(fmt.Sprintf(client.t("IP %s is exempt from connection limits"), target.cidr.IP.String()))
379
 			rb.Notice(fmt.Sprintf(client.t("IP %s is exempt from connection limits"), target.cidr.IP.String()))
391
 			rb.Notice(line)
398
 			rb.Notice(line)
392
 		}
399
 		}
393
 	}
400
 	}
401
+	if sendMaskWarning {
402
+		rb.Notice(fmt.Sprintf(client.t("Note: try evaluating a wider IPv6 CIDR like %s/%d"),
403
+			target.cidr.IP.String(), config.Server.IPLimits.CidrLenIPv6))
404
+	}
394
 }
405
 }
395
 
406
 
396
 func ubanInfoNickmask(client *Client, target ubanTarget, rb *ResponseBuffer) {
407
 func ubanInfoNickmask(client *Client, target ubanTarget, rb *ResponseBuffer) {

Laddar…
Avbryt
Spara