-
Notifications
You must be signed in to change notification settings - Fork 0
/
cidr.go
61 lines (48 loc) · 1.1 KB
/
cidr.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
package collector
import (
"errors"
"github.com/blueberryorg/public/source/rule/rules"
"net/http"
"os"
"strings"
"time"
"github.com/elliotchance/pie/v2"
"github.com/ice-cream-heaven/log"
"github.com/ice-cream-heaven/utils/xtime"
)
type Cidr struct {
}
func (p *Cidr) Download(path string) ([]byte, error) {
resp, err := client.R().Get(path)
if err != nil {
log.Errorf("err:%v", err)
return nil, err
}
if resp.StatusCode() != http.StatusOK {
log.Errorf("err:%v", err)
return nil, errors.New(resp.Status())
}
return resp.Body(), nil
}
func (p *Cidr) ParseBody(tag string, body []byte) (rules []rules.Rule) {
pie.Each(strings.Split(string(body), "\n"), func(line string) {
line = strings.ReplaceAll(line, "\r", "")
if line == "" {
return
}
r, err := ParseRules("IP-CIDR," + line + "," + tag)
if err != nil {
log.Errorf("parse rule err:%s", line)
log.Errorf("err:%v", err)
return
}
rules = append(rules, r)
})
return
}
func NewCidr() *Cidr {
return &Cidr{}
}
func (p *Cidr) NeedUpdate(info os.FileInfo) bool {
return time.Since(info.ModTime()) > xtime.Week
}