-
Notifications
You must be signed in to change notification settings - Fork 0
/
dnsdb.go
288 lines (235 loc) · 5.7 KB
/
dnsdb.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
package block
import (
"encoding/json"
"errors"
"fmt"
"os"
"time"
bolt "go.etcd.io/bbolt"
)
//import "fmt"
var (
ErrBucketList = errors.New("error listing buckets")
ErrBucketGet = errors.New("error retrieving bucket")
ErrBucketMissing = errors.New("bucket doesn't exist")
ErrBucketCreate = errors.New("error creating bucket")
ErrBucketDelete = errors.New("error deleting bucket")
ErrBucketDecodeName = errors.New("error reading bucket name")
ErrBucketInvalidName = errors.New("invalid bucket name")
ErrBucketItemDecode = errors.New("error reading bucket item")
ErrBucketItemEncode = errors.New("error encoding bucket item")
ErrBucketItemGet = errors.New("error getting bucket item")
ErrBucketItemCreate = errors.New("error creating bucket item")
ErrBucketItemUpdate = errors.New("error updating bucket item")
ErrBucketItemDelete = errors.New("error deleting bucket item")
)
type BucketItem struct {
Key string
Value DomainValue
}
func (item *BucketItem) EncodeKey() []byte {
return []byte(item.Key)
}
func (item *BucketItem) EncodeValue() ([]byte, error) {
buf, err := json.Marshal(item.Value)
if err != nil {
return nil, ErrBucketItemEncode
}
return buf, nil
}
func (item *BucketItem) DecodeValue(rawValue []byte) error {
if err := json.Unmarshal(rawValue, &item.Value); err != nil {
return err
}
return nil
}
func getItems(db *bolt.DB, bucket string) (error, []BucketItem) {
items := []BucketItem{}
err := db.View(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte(bucket))
if b == nil {
return ErrBucketMissing
}
b.ForEach(func(k, v []byte) error {
bucketItem := BucketItem{Key: string(k)}
bucketItem.DecodeValue(v)
items = append(items, bucketItem)
return nil
})
return nil
})
return err, items
}
func putItem(db *bolt.DB, bucket string, item BucketItem) error {
itemKey := item.EncodeKey()
itemValue, err := item.EncodeValue()
if err != nil {
return err
}
err = db.Update(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte(bucket))
err = b.Put(itemKey, itemValue)
return err
})
if err == nil {
db.Sync()
}
return err
}
func cleanBucket(db *bolt.DB, bucket string) error {
err := db.Update(func(tx *bolt.Tx) error {
return tx.DeleteBucket([]byte(bucket))
})
if err == nil {
db.Sync()
}
return err
}
func (b *Block) compcatDb() error {
dst, err := bolt.Open(b.DbPath+".tmp", 0664, nil)
defer dst.Close()
if err != nil {
return err
}
err = bolt.Compact(dst, b.Db, 0)
if err != nil {
return err
}
err = os.Rename(b.DbPath+".tmp", b.DbPath)
if err != nil {
return err
}
//close and re-open db
b.Db.Close()
options := &bolt.Options{Timeout: 1 * time.Second}
db, err := bolt.Open(b.DbPath, 0664, options)
if err != nil {
log.Fatal("Failed to open", b.DbPath, err)
}
b.Db = db
return nil
}
func getCount(db *bolt.DB, bucket string) int64 {
keyN := int64(0)
db.View(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte(bucket))
if b != nil {
stats := b.Stats()
keyN = int64(stats.KeyN)
}
return nil
})
return keyN
}
func getItemBucket(b *bolt.Bucket, key string) (error, BucketItem) {
bucketItem := BucketItem{}
v := b.Get([]byte(key))
if v == nil {
return ErrBucketItemGet, bucketItem
}
bucketItem.DecodeValue(v)
return nil, bucketItem
}
func getItem(db *bolt.DB, bucket string, key string) (error, BucketItem) {
bucketItem := BucketItem{}
err := db.View(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte(bucket))
if b == nil {
return ErrBucketMissing
}
v := b.Get([]byte(key))
if v == nil {
return ErrBucketItemGet
}
return bucketItem.DecodeValue(v)
})
return err, bucketItem
}
func BoltOpen(filename string) *bolt.DB {
options := &bolt.Options{Timeout: 1 * time.Second, NoSync: true}
db, err := bolt.Open(filename, 0664, options)
if err != nil {
log.Fatal("Failed to open ", filename, err)
}
err = db.Update(func(tx *bolt.Tx) error {
_, err := tx.CreateBucketIfNotExists([]byte(gDomainBucket))
if err != nil {
log.Fatal("could not create bucket", err)
}
return nil
})
if err != nil {
log.Fatal("Failed to make bucket", err)
} else {
db.Sync()
}
return db
}
func storeBatch(db *bolt.DB, domains []string, idx int, list_id int) error {
err := db.Batch(func(tx *bolt.Tx) error {
bucket := tx.Bucket([]byte(gDomainBucket))
i := 0
for i < idx {
domain := domains[i]
i++
value := DomainValue{[]int{list_id}, false}
//see if bucket already has it
err, item := getItemBucket(bucket, domain)
if err != nil {
//add this current list_id to it.
value.List_ids = append(item.Value.List_ids, list_id)
}
item = BucketItem{domain, value}
itemValue, err := item.EncodeValue()
if err == nil {
err = bucket.Put(item.EncodeKey(), itemValue)
}
if err != nil {
fmt.Println("putItem failed", domain)
return err
}
}
return nil
})
if err == nil {
db.Sync()
}
return err
}
func (b *Block) transferStagingDB() error {
Stagemtx.Lock()
defer Stagemtx.Unlock()
b.Db.Close()
err := os.Rename(b.DbPath+"-staging", b.DbPath)
if err != nil {
return err
}
b.Db = BoltOpen(b.DbPath)
gMetrics.BlockedDomains = getCount(b.Db, gDomainBucket)
return nil
}
func (b *Block) UpdateDomains(update map[string]DomainValue) error {
err := b.Db.Update(func(tx *bolt.Tx) error {
bucket := tx.Bucket([]byte(gDomainBucket))
for entry, v := range b.update {
if entry == "" {
continue
}
item := BucketItem{entry, v}
itemValue, err := item.EncodeValue()
if err == nil {
err = bucket.Put(item.EncodeKey(), itemValue)
}
if err != nil {
return err
}
}
return nil
})
if err != nil {
return err
}
b.Db.Sync()
gMetrics.BlockedDomains = getCount(b.Db, gDomainBucket)
return nil
}