Просмотр исходного кода

Add documentation for ipblocklist

tags/v2.0.0-rc1
9seconds 5 лет назад
Родитель
Сommit
0274b3436a
6 измененных файлов: 81 добавлений и 27 удалений
  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 Просмотреть файл

30
 // hardcore math which proves that if you choose this P correctly, you
30
 // hardcore math which proves that if you choose this P correctly, you
31
 // can maintain the same error rate for a stream of elements.
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
 func NewStableBloomFilter(byteSize uint, errorRate float64) mtglib.AntiReplayCache {
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
 	sf := boom.NewDefaultStableBloomFilter(byteSize*8, errorRate) // nolint: gomnd
45
 	sf := boom.NewDefaultStableBloomFilter(byteSize*8, errorRate) // nolint: gomnd
37
 	sf.SetHash(xxhash.New64())
46
 	sf.SetHash(xxhash.New64())
38
 
47
 

+ 1
- 1
internal/cli/proxy.go Просмотреть файл

133
 		return err // nolint: wrapcheck
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
 	opts.IPBlocklist = firehol
138
 	opts.IPBlocklist = firehol
139
 
139
 

+ 49
- 22
ipblocklist/firehol.go Просмотреть файл

27
 
27
 
28
 var fireholRegexpComment = regexp.MustCompile(`\s*#.*?$`)
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
 type Firehol struct {
43
 type Firehol struct {
31
 	ctx       context.Context
44
 	ctx       context.Context
32
 	ctxCancel context.CancelFunc
45
 	ctxCancel context.CancelFunc
44
 	treeV6 *bool_tree.TreeV6
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
 func (f *Firehol) Contains(ip net.IP) bool {
66
 func (f *Firehol) Contains(ip net.IP) bool {
48
 	if ip == nil {
67
 	if ip == nil {
49
 		return true
68
 		return true
61
 	return f.containsIPv6(ip.To16())
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
 	ticker := time.NewTicker(updateEach)
92
 	ticker := time.NewTicker(updateEach)
86
 
93
 
87
 	defer func() {
94
 	defer func() {
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
 func (f *Firehol) update() error { // nolint: funlen, cyclop
143
 func (f *Firehol) update() error { // nolint: funlen, cyclop
302
 	return nil
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
 func NewFirehol(logger mtglib.Logger, network mtglib.Network,
332
 func NewFirehol(logger mtglib.Logger, network mtglib.Network,
306
 	downloadConcurrency uint,
333
 	downloadConcurrency uint,
307
 	remoteURLs []string,
334
 	remoteURLs []string,
326
 	}
353
 	}
327
 
354
 
328
 	if downloadConcurrency == 0 {
355
 	if downloadConcurrency == 0 {
329
-		downloadConcurrency = DefaultDownloadConcurrency
356
+		downloadConcurrency = DefaultFireholDownloadConcurrency
330
 	}
357
 	}
331
 
358
 
332
 	workerPool, _ := ants.NewPool(int(downloadConcurrency))
359
 	workerPool, _ := ants.NewPool(int(downloadConcurrency))

+ 12
- 2
ipblocklist/init.go Просмотреть файл

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
 package ipblocklist
6
 package ipblocklist
2
 
7
 
3
 import "time"
8
 import "time"
4
 
9
 
5
 const (
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 Просмотреть файл

10
 
10
 
11
 func (n noop) Contains(ip net.IP) bool { return false }
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
 func NewNoop() mtglib.IPBlocklist {
15
 func NewNoop() mtglib.IPBlocklist {
14
 	return noop{}
16
 	return noop{}
15
 }
17
 }

+ 6
- 0
mtglib/init.go Просмотреть файл

69
 	SeenBefore(data []byte) bool
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
 type IPBlocklist interface {
78
 type IPBlocklist interface {
73
 	Contains(net.IP) bool
79
 	Contains(net.IP) bool
74
 }
80
 }

Загрузка…
Отмена
Сохранить