Skip to content

Commit fa15b1e

Browse files
committed
2021-12-31 14:29:37 : 修改文本å分隔符为空格 支持 ipv6
1 parent f6f563a commit fa15b1e

File tree

8 files changed

+99
-136
lines changed

8 files changed

+99
-136
lines changed

data/domain_cn.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
cn:full:www.baidu.com
1+
cn full www.baidu.com

data/ecs_table.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
global:127.0.0.1:114.114.114.114
1+
global 127.0.0.1 114.114.114.114

data/keyword_cn.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
cn:baidu
1+
cn baidu

data/netlist_cn.txt

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
cn:114.114.114.114
2-
cn:223.5.5.5
3-
cn:223.6.6.6
1+
cn 114.114.114.114
2+
cn 223.5.5.5
3+
cn 223.6.6.6

plugin/pkg/datatable/domain.go

+24-39
Original file line numberDiff line numberDiff line change
@@ -27,26 +27,7 @@ func (d *DomainData) ParseFile(r io.Reader) error {
2727
scanner := bufio.NewScanner(r)
2828
for scanner.Scan() {
2929
line := scanner.Text()
30-
if i := strings.IndexByte(line, '#'); i >= 0 {
31-
line = line[:i]
32-
}
33-
34-
if strings.Index(line, ":") != -1 {
35-
attrs := strings.Split(line, ":")
36-
if len(attrs) != 3 {
37-
continue
38-
}
39-
if attrs[1] != netutils.MatchFullType &&
40-
attrs[1] != netutils.MatchDomainType &&
41-
attrs[1] != netutils.MatchRegexType {
42-
continue
43-
}
44-
if d.tag == strings.ToUpper(attrs[0]) {
45-
d.data.Add(attrs[1], attrs[2])
46-
}
47-
} else {
48-
d.data.Add(netutils.MatchFullType, line)
49-
}
30+
d.parseline(line)
5031
}
5132
return nil
5233
}
@@ -56,25 +37,29 @@ func (d *DomainData) ParseLines(lines []string, reset bool) {
5637
d.data.Clear()
5738
}
5839
for _, line := range lines {
59-
if i := strings.IndexByte(line, '#'); i >= 0 {
60-
line = line[:i]
61-
}
62-
if strings.Index(line, ":") != -1 {
63-
attrs := strings.Split(line, ":")
64-
if len(attrs) != 3 {
65-
continue
66-
}
67-
if attrs[1] != netutils.MatchFullType &&
68-
attrs[1] != netutils.MatchDomainType &&
69-
attrs[1] != netutils.MatchRegexType {
70-
continue
71-
}
72-
if d.tag == strings.ToUpper(attrs[0]) {
73-
d.data.Add(attrs[1], attrs[2])
74-
}
75-
} else {
76-
d.data.Add(netutils.MatchFullType, line)
77-
}
40+
d.parseline(line)
41+
}
42+
}
43+
44+
func (d *DomainData) parseline(line string) {
45+
if i := strings.IndexByte(line, '#'); i >= 0 {
46+
line = line[:i]
47+
}
48+
attrs := strings.Fields(line)
49+
if len(attrs) == 1 {
50+
d.data.Add(netutils.MatchFullType, line)
51+
return
52+
}
53+
if len(attrs) < 3 {
54+
return
55+
}
56+
if attrs[1] != netutils.MatchFullType &&
57+
attrs[1] != netutils.MatchDomainType &&
58+
attrs[1] != netutils.MatchRegexType {
59+
return
60+
}
61+
if d.tag == strings.ToUpper(attrs[0]) {
62+
d.data.Add(attrs[1], attrs[2])
7863
}
7964
}
8065

plugin/pkg/datatable/ecs.go

+31-33
Original file line numberDiff line numberDiff line change
@@ -89,41 +89,39 @@ func (e *EcsData) parseLine(line string) {
8989
if i := strings.IndexByte(line, '#'); i >= 0 {
9090
line = line[:i]
9191
}
92-
if strings.Index(line, ":") != -1 {
93-
attrs := strings.Split(line, ":")
94-
if len(attrs) != 3 {
95-
return
96-
}
97-
if e.tag != strings.ToUpper(attrs[0]) {
98-
return
99-
}
100-
var ecsip = net.ParseIP(attrs[2])
101-
if ecsip == nil {
102-
return
103-
}
92+
attrs := strings.Fields(line)
93+
if len(attrs) < 3 {
94+
return
95+
}
96+
if e.tag != strings.ToUpper(attrs[0]) {
97+
return
98+
}
99+
var ecsip = net.ParseIP(attrs[2])
100+
if ecsip == nil {
101+
return
102+
}
104103

105-
var addrs = attrs[1]
106-
var clist []string
107-
if strings.Index(addrs, ",") != -1 {
108-
clist = append(clist, strings.Split(addrs, ",")...)
109-
} else {
110-
clist = append(clist, addrs)
111-
}
112-
for _, c := range clist {
113-
if strings.Index(c, "/") != -1 {
114-
inet, err := netutils.ParseIpNet(c)
115-
if err != nil {
116-
continue
117-
}
118-
err = e.data.Set(inet.String(), ecsip)
119-
if err != nil {
120-
fmt.Println("add ecsip error" + err.Error())
121-
continue
122-
}
123-
e.netBindings.Add(inet)
124-
} else {
125-
_ = e.data.Set(c, ecsip)
104+
var addrs = attrs[1]
105+
var clist []string
106+
if strings.Index(addrs, ",") != -1 {
107+
clist = append(clist, strings.Split(addrs, ",")...)
108+
} else {
109+
clist = append(clist, addrs)
110+
}
111+
for _, c := range clist {
112+
if strings.Index(c, "/") != -1 {
113+
inet, err := netutils.ParseIpNet(c)
114+
if err != nil {
115+
continue
126116
}
117+
err = e.data.Set(inet.String(), ecsip)
118+
if err != nil {
119+
fmt.Println("add ecsip error" + err.Error())
120+
continue
121+
}
122+
e.netBindings.Add(inet)
123+
} else {
124+
_ = e.data.Set(c, ecsip)
127125
}
128126
}
129127
}

plugin/pkg/datatable/keyword.go

+19-29
Original file line numberDiff line numberDiff line change
@@ -33,21 +33,7 @@ func (k *keywordData) ParseFile(r io.Reader) error {
3333
scanner := bufio.NewScanner(r)
3434
for scanner.Scan() {
3535
line := scanner.Text()
36-
if i := strings.IndexByte(line, '#'); i >= 0 {
37-
line = line[:i]
38-
}
39-
40-
if strings.Index(line, ":") != -1 {
41-
attrs := strings.Split(line, ":")
42-
if len(attrs) != 2 {
43-
continue
44-
}
45-
if k.tag == strings.ToUpper(attrs[0]) {
46-
k.data = append(k.data, attrs[1])
47-
}
48-
} else {
49-
k.data = append(k.data, line)
50-
}
36+
k.parseline(line)
5137
}
5238
return nil
5339
}
@@ -59,20 +45,24 @@ func (k *keywordData) ParseLines(lines []string, reset bool) {
5945
k.data = make([]string, 0)
6046
}
6147
for _, line := range lines {
62-
if i := strings.IndexByte(line, '#'); i >= 0 {
63-
line = line[:i]
64-
}
65-
if strings.Index(line, ":") != -1 {
66-
attrs := strings.Split(line, ":")
67-
if len(attrs) != 2 {
68-
continue
69-
}
70-
if k.tag == strings.ToUpper(attrs[0]) {
71-
k.data = append(k.data, attrs[1])
72-
}
73-
} else {
74-
k.data = append(k.data, line)
75-
}
48+
k.parseline(line)
49+
}
50+
}
51+
52+
func (k *keywordData) parseline(line string) {
53+
if i := strings.IndexByte(line, '#'); i >= 0 {
54+
line = line[:i]
55+
}
56+
attrs := strings.Fields(line)
57+
if len(attrs) == 1 {
58+
k.data = append(k.data, line)
59+
return
60+
}
61+
if len(attrs) < 2 {
62+
return
63+
}
64+
if k.tag == strings.ToUpper(attrs[0]) {
65+
k.data = append(k.data, attrs[1])
7666
}
7767
}
7868

plugin/pkg/datatable/netlist.go

+19-29
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,7 @@ func (n *NetlistData) ParseFile(r io.Reader) error {
2828
scanner := bufio.NewScanner(r)
2929
for scanner.Scan() {
3030
line := scanner.Text()
31-
if i := strings.IndexByte(line, '#'); i >= 0 {
32-
line = line[:i]
33-
}
34-
35-
if strings.Index(line, ":") != -1 {
36-
attrs := strings.Split(line, ":")
37-
if len(attrs) != 2 {
38-
continue
39-
}
40-
if n.tag == strings.ToUpper(attrs[0]) {
41-
n.data.AddByString(attrs[1])
42-
}
43-
} else {
44-
n.data.AddByString(line)
45-
}
31+
n.parseline(line)
4632
}
4733
return nil
4834
}
@@ -52,20 +38,24 @@ func (n *NetlistData) ParseLines(lines []string, reset bool) {
5238
n.data.Clear()
5339
}
5440
for _, line := range lines {
55-
if i := strings.IndexByte(line, '#'); i >= 0 {
56-
line = line[:i]
57-
}
58-
if strings.Index(line, ":") != -1 {
59-
attrs := strings.Split(line, ":")
60-
if len(attrs) != 2 {
61-
continue
62-
}
63-
if n.tag == strings.ToUpper(attrs[0]) {
64-
n.data.AddByString(attrs[1])
65-
}
66-
} else {
67-
n.data.AddByString(line)
68-
}
41+
n.parseline(line)
42+
}
43+
}
44+
45+
func (n *NetlistData) parseline(line string) {
46+
if i := strings.IndexByte(line, '#'); i >= 0 {
47+
line = line[:i]
48+
}
49+
attrs := strings.Fields(line)
50+
if len(attrs) == 1 {
51+
n.data.AddByString(line)
52+
return
53+
}
54+
if len(attrs) < 2 {
55+
return
56+
}
57+
if n.tag == strings.ToUpper(attrs[0]) {
58+
n.data.AddByString(attrs[1])
6959
}
7060
}
7161

0 commit comments

Comments
 (0)