Bläddra i källkod

Add documentation for ipblocklist

tags/v2.0.0-rc1
9seconds 5 år sedan
förälder
incheckning
0274b3436a
6 ändrade filer med 81 tillägg och 27 borttagningar
  1. 11
    2
      antireplay/stable_bloom_filter.go
  2. 1
    1
      internal/cli/proxy.go
  3. 49
    22
      ipblocklist/firehol.go
  4. 12
    2
      ipblocklist/init.go
  5. 2
    0
      ipblocklist/noop.go
  6. 6
    0
      mtglib/init.go

+ 11
- 2
antireplay/stable_bloom_filter.go Visa fil

@@ -30,9 +30,18 @@ func (s *stableBloomFilter) SeenBefore(digest []byte) bool {
30 30
 // hardcore math which proves that if you choose this P correctly, you
31 31
 // can maintain the same error rate for a stream of elements.
32 32
 //
33
-// byteSize is the number of bytes you want to give to a bloom filter  .
34
-// errorRate is desired false-positive error rate                      .
33
+// byteSize is the number of bytes you want to give to a bloom filter.
34
+// errorRate is desired false-positive error rate. If you want to use
35
+// default values, please pass 0 for byteSize and <0 for errorRate.
35 36
 func NewStableBloomFilter(byteSize uint, errorRate float64) mtglib.AntiReplayCache {
37
+	if byteSize == 0 {
38
+		byteSize = DefaultStableBloomFilterMaxSize
39
+	}
40
+
41
+	if errorRate < 0 {
42
+		errorRate = DefaultStableBloomFilterErrorRate
43
+	}
44
+
36 45
 	sf := boom.NewDefaultStableBloomFilter(byteSize*8, errorRate) // nolint: gomnd
37 46
 	sf.SetHash(xxhash.New64())
38 47
 

+ 1
- 1
internal/cli/proxy.go Visa fil

@@ -133,7 +133,7 @@ func (c *Proxy) setupIPBlocklist(opts *mtglib.ProxyOpts) error {
133 133
 		return err // nolint: wrapcheck
134 134
 	}
135 135
 
136
-	go firehol.Run(c.Config.Defense.Blocklist.UpdateEach.Value(ipblocklist.DefaultUpdateEach))
136
+	go firehol.Run(c.Config.Defense.Blocklist.UpdateEach.Value(ipblocklist.DefaultFireholUpdateEach))
137 137
 
138 138
 	opts.IPBlocklist = firehol
139 139
 

+ 49
- 22
ipblocklist/firehol.go Visa fil

@@ -27,6 +27,19 @@ const (
27 27
 
28 28
 var fireholRegexpComment = regexp.MustCompile(`\s*#.*?$`)
29 29
 
30
+// Firehol is IPBlocklist which uses lists from FireHOL:
31
+// https://iplists.firehol.org/
32
+//
33
+// It can use both local files and remote URLs. This is not necessary
34
+// that blocklists should be taken from this website, we expect only
35
+// compatible formats here.
36
+//
37
+// Example of the format:
38
+//
39
+//     # this is a comment
40
+//     # to ignore
41
+//     127.0.0.1   # you can specify an IP
42
+//     10.0.0.0/8  # or cidr
30 43
 type Firehol struct {
31 44
 	ctx       context.Context
32 45
 	ctxCancel context.CancelFunc
@@ -44,6 +57,12 @@ type Firehol struct {
44 57
 	treeV6 *bool_tree.TreeV6
45 58
 }
46 59
 
60
+// Shutdown stop a background update process.
61
+func (f *Firehol) Shutdown() {
62
+	f.ctxCancel()
63
+}
64
+
65
+// Contains is given IP list can be found in FireHOL blocklists.
47 66
 func (f *Firehol) Contains(ip net.IP) bool {
48 67
 	if ip == nil {
49 68
 		return true
@@ -61,27 +80,15 @@ func (f *Firehol) Contains(ip net.IP) bool {
61 80
 	return f.containsIPv6(ip.To16())
62 81
 }
63 82
 
64
-func (f *Firehol) containsIPv4(addr net.IP) bool {
65
-	ip := patricia.NewIPv4AddressFromBytes(addr, 32)
66
-
67
-	if ok, _, err := f.treeV4.FindDeepestTag(ip); ok && err == nil {
68
-		return true
69
-	}
70
-
71
-	return false
72
-}
73
-
74
-func (f *Firehol) containsIPv6(addr net.IP) bool {
75
-	ip := patricia.NewIPv6Address(addr, 128)
76
-
77
-	if ok, _, err := f.treeV6.FindDeepestTag(ip); ok && err == nil {
78
-		return true
83
+// Run starts a background update process.
84
+//
85
+// This is a blocking method so you probably want to run it in a
86
+// goroutine.
87
+func (f *Firehol) Run(updateEach time.Duration) {
88
+	if updateEach == 0 {
89
+		updateEach = DefaultFireholUpdateEach
79 90
 	}
80 91
 
81
-	return false
82
-}
83
-
84
-func (f *Firehol) Run(updateEach time.Duration) {
85 92
 	ticker := time.NewTicker(updateEach)
86 93
 
87 94
 	defer func() {
@@ -113,8 +120,24 @@ func (f *Firehol) Run(updateEach time.Duration) {
113 120
 	}
114 121
 }
115 122
 
116
-func (f *Firehol) Shutdown() {
117
-	f.ctxCancel()
123
+func (f *Firehol) containsIPv4(addr net.IP) bool {
124
+	ip := patricia.NewIPv4AddressFromBytes(addr, 32)
125
+
126
+	if ok, _, err := f.treeV4.FindDeepestTag(ip); ok && err == nil {
127
+		return true
128
+	}
129
+
130
+	return false
131
+}
132
+
133
+func (f *Firehol) containsIPv6(addr net.IP) bool {
134
+	ip := patricia.NewIPv6Address(addr, 128)
135
+
136
+	if ok, _, err := f.treeV6.FindDeepestTag(ip); ok && err == nil {
137
+		return true
138
+	}
139
+
140
+	return false
118 141
 }
119 142
 
120 143
 func (f *Firehol) update() error { // nolint: funlen, cyclop
@@ -302,6 +325,10 @@ func (f *Firehol) updateAddToTrees(ip net.IP, cidr uint,
302 325
 	return nil
303 326
 }
304 327
 
328
+// NewFirehol creates a new instance of FireHOL IP blocklist.
329
+//
330
+// This method does not start an update process so please execute Run
331
+// when it is necessary.
305 332
 func NewFirehol(logger mtglib.Logger, network mtglib.Network,
306 333
 	downloadConcurrency uint,
307 334
 	remoteURLs []string,
@@ -326,7 +353,7 @@ func NewFirehol(logger mtglib.Logger, network mtglib.Network,
326 353
 	}
327 354
 
328 355
 	if downloadConcurrency == 0 {
329
-		downloadConcurrency = DefaultDownloadConcurrency
356
+		downloadConcurrency = DefaultFireholDownloadConcurrency
330 357
 	}
331 358
 
332 359
 	workerPool, _ := ants.NewPool(int(downloadConcurrency))

+ 12
- 2
ipblocklist/init.go Visa fil

@@ -1,8 +1,18 @@
1
+// Package ipblocklist contains default implementation of the
2
+// IPBlocklist for mtg.
3
+//
4
+// Please check documentation for mtglib.IPBlocklist interface to get an
5
+// idea of this abstraction.
1 6
 package ipblocklist
2 7
 
3 8
 import "time"
4 9
 
5 10
 const (
6
-	DefaultDownloadConcurrency = 1
7
-	DefaultUpdateEach          = 12 * time.Hour
11
+	// DefaultFireholDownloadConcurrency defines a default max number of
12
+	// concurrent downloads of ip blocklists for Firehol.
13
+	DefaultFireholDownloadConcurrency = 1
14
+
15
+	// DefaultFireholUpdateEach defines a default time period when
16
+	// Firehol requests updates of the blocklists.
17
+	DefaultFireholUpdateEach = 6 * time.Hour
8 18
 )

+ 2
- 0
ipblocklist/noop.go Visa fil

@@ -10,6 +10,8 @@ type noop struct{}
10 10
 
11 11
 func (n noop) Contains(ip net.IP) bool { return false }
12 12
 
13
+// NewNoop returns a dummy ipblocklist which allows all incoming
14
+// connections.
13 15
 func NewNoop() mtglib.IPBlocklist {
14 16
 	return noop{}
15 17
 }

+ 6
- 0
mtglib/init.go Visa fil

@@ -69,6 +69,12 @@ type AntiReplayCache interface {
69 69
 	SeenBefore(data []byte) bool
70 70
 }
71 71
 
72
+// IPBlocklist filters requests based on IP address.
73
+//
74
+// If this filter has an IP address, then mtg closes a request without
75
+// reading anything from a socket. It also does not give such request to
76
+// a worker pool, so in worst cases you can expect that you invoke this
77
+// object more frequent than defined proxy concurrency.
72 78
 type IPBlocklist interface {
73 79
 	Contains(net.IP) bool
74 80
 }

Laddar…
Avbryt
Spara