Skip to content

Commit 2998d4a

Browse files
committed
fix
1 parent 5a03751 commit 2998d4a

File tree

1 file changed

+114
-114
lines changed

1 file changed

+114
-114
lines changed

plugin/dnssrc/ipset_linux.go

+114-114
Original file line numberDiff line numberDiff line change
@@ -1,117 +1,117 @@
11
// +build linux
22

33
package dnssrc
4-
//
5-
// import (
6-
// "net"
7-
// "os"
8-
// "strings"
9-
//
10-
// "github.com/coredns/caddy"
11-
// goipset "github.com/digineo/go-ipset/v2"
12-
// "github.com/miekg/dns"
13-
// )
14-
//
15-
// const (
16-
// nfProtoUnspec = 0
17-
// nfProtoIpv4 = 2
18-
// nfProtoIpv6 = 10
19-
// )
20-
//
21-
// type ipsetHandle struct {
22-
// set StringSet
23-
// conn *goipset.Conn
24-
// }
25-
//
26-
// func ipsetParse(c *caddy.Controller, u *reloadableUpstream) error {
27-
// dir := c.Val()
28-
// names := c.RemainingArgs()
29-
// if len(names) == 0 {
30-
// return c.ArgErr()
31-
// }
32-
// if u.ipset == nil {
33-
// u.ipset = &ipsetHandle{
34-
// set: make(StringSet),
35-
// }
36-
// }
37-
// h := u.ipset.(*ipsetHandle)
38-
// for _, name := range names {
39-
// h.set.Add(name)
40-
// }
41-
// log.Infof("%v: %v", dir, names)
42-
// return nil
43-
// }
44-
//
45-
// func ipsetSetup(u *reloadableUpstream) (err error) {
46-
// // In case of plugin block doesn't have ipset option, which u.ipset is nil
47-
// // panic: interface conversion: interface {} is nil, not *dnssrc.ipsetHandle
48-
// if u.ipset == nil {
49-
// return nil
50-
// }
51-
// if os.Geteuid() != 0 {
52-
// log.Warningf("ipset needs root user privilege to work")
53-
// }
54-
// ipset := u.ipset.(*ipsetHandle)
55-
// ipset.conn, err = goipset.Dial(nfProtoUnspec, nil)
56-
// if err != nil {
57-
// return err
58-
// }
59-
// return nil
60-
// }
61-
//
62-
// func ipsetShutdown(u *reloadableUpstream) (err error) {
63-
// if u.ipset == nil {
64-
// return nil
65-
// }
66-
// return u.ipset.(*ipsetHandle).conn.Close()
67-
// }
68-
//
69-
// // Taken from https://github.com/missdeer/ipset/blob/master/reverter.go#L32 with modification
70-
// func ipsetAddIP(u *reloadableUpstream, reply *dns.Msg) {
71-
// if u.ipset == nil || reply.Rcode != dns.RcodeSuccess {
72-
// return
73-
// }
74-
//
75-
// ipset := u.ipset.(*ipsetHandle)
76-
// for _, rr := range reply.Answer {
77-
// rrType := rr.Header().Rrtype
78-
// if rrType != dns.TypeA && rrType != dns.TypeAAAA {
79-
// continue
80-
// }
81-
//
82-
// ss := strings.Split(rr.String(), "\t")
83-
// if len(ss) != 5 {
84-
// log.Warningf("Expected 5 entries, got %v: %q", len(ss), rr.String())
85-
// continue
86-
// }
87-
//
88-
// ip := net.ParseIP(ss[4])
89-
// if ip == nil {
90-
// log.Warningf("ipsetAddIP(): %q isn't a valid IP address", ss[4])
91-
// continue
92-
// }
93-
//
94-
// for name := range ipset.set {
95-
// p, err := ipset.conn.Header(name)
96-
// if err != nil {
97-
// log.Errorf("ipsetAddIP(): cannot get ipset %q header: %v", name, err)
98-
// continue
99-
// }
100-
//
101-
// var typeMatch bool
102-
// if uint(p.Family.Value) == uint(nfProtoIpv4) {
103-
// typeMatch = rrType == dns.TypeA
104-
// } else if uint(p.Family.Value) == uint(nfProtoIpv6) {
105-
// typeMatch = rrType == dns.TypeAAAA
106-
// }
107-
// if !typeMatch {
108-
// continue
109-
// }
110-
//
111-
// err = ipset.conn.Add(name, goipset.NewEntry(goipset.EntryIP(ip)))
112-
// if err != nil {
113-
// log.Errorf("ipsetAddIP(): cannot add %q to ipset %q: %v", ip, name, err)
114-
// }
115-
// }
116-
// }
117-
// }
4+
5+
import (
6+
"net"
7+
"os"
8+
"strings"
9+
10+
"github.com/coredns/caddy"
11+
goipset "github.com/digineo/go-ipset/v2"
12+
"github.com/miekg/dns"
13+
)
14+
15+
const (
16+
nfProtoUnspec = 0
17+
nfProtoIpv4 = 2
18+
nfProtoIpv6 = 10
19+
)
20+
21+
type ipsetHandle struct {
22+
set StringSet
23+
conn *goipset.Conn
24+
}
25+
26+
func ipsetParse(c *caddy.Controller, u *reloadableUpstream) error {
27+
dir := c.Val()
28+
names := c.RemainingArgs()
29+
if len(names) == 0 {
30+
return c.ArgErr()
31+
}
32+
if u.ipset == nil {
33+
u.ipset = &ipsetHandle{
34+
set: make(StringSet),
35+
}
36+
}
37+
h := u.ipset.(*ipsetHandle)
38+
for _, name := range names {
39+
h.set.Add(name)
40+
}
41+
log.Infof("%v: %v", dir, names)
42+
return nil
43+
}
44+
45+
func ipsetSetup(u *reloadableUpstream) (err error) {
46+
// In case of plugin block doesn't have ipset option, which u.ipset is nil
47+
// panic: interface conversion: interface {} is nil, not *dnssrc.ipsetHandle
48+
if u.ipset == nil {
49+
return nil
50+
}
51+
if os.Geteuid() != 0 {
52+
log.Warningf("ipset needs root user privilege to work")
53+
}
54+
ipset := u.ipset.(*ipsetHandle)
55+
ipset.conn, err = goipset.Dial(nfProtoUnspec, nil)
56+
if err != nil {
57+
return err
58+
}
59+
return nil
60+
}
61+
62+
func ipsetShutdown(u *reloadableUpstream) (err error) {
63+
if u.ipset == nil {
64+
return nil
65+
}
66+
return u.ipset.(*ipsetHandle).conn.Close()
67+
}
68+
69+
// Taken from https://github.com/missdeer/ipset/blob/master/reverter.go#L32 with modification
70+
func ipsetAddIP(u *reloadableUpstream, reply *dns.Msg) {
71+
if u.ipset == nil || reply.Rcode != dns.RcodeSuccess {
72+
return
73+
}
74+
75+
ipset := u.ipset.(*ipsetHandle)
76+
for _, rr := range reply.Answer {
77+
rrType := rr.Header().Rrtype
78+
if rrType != dns.TypeA && rrType != dns.TypeAAAA {
79+
continue
80+
}
81+
82+
ss := strings.Split(rr.String(), "\t")
83+
if len(ss) != 5 {
84+
log.Warningf("Expected 5 entries, got %v: %q", len(ss), rr.String())
85+
continue
86+
}
87+
88+
ip := net.ParseIP(ss[4])
89+
if ip == nil {
90+
log.Warningf("ipsetAddIP(): %q isn't a valid IP address", ss[4])
91+
continue
92+
}
93+
94+
for name := range ipset.set {
95+
p, err := ipset.conn.Header(name)
96+
if err != nil {
97+
log.Errorf("ipsetAddIP(): cannot get ipset %q header: %v", name, err)
98+
continue
99+
}
100+
101+
var typeMatch bool
102+
if uint(p.Family.Value) == uint(nfProtoIpv4) {
103+
typeMatch = rrType == dns.TypeA
104+
} else if uint(p.Family.Value) == uint(nfProtoIpv6) {
105+
typeMatch = rrType == dns.TypeAAAA
106+
}
107+
if !typeMatch {
108+
continue
109+
}
110+
111+
err = ipset.conn.Add(name, goipset.NewEntry(goipset.EntryIP(ip)))
112+
if err != nil {
113+
log.Errorf("ipsetAddIP(): cannot add %q to ipset %q: %v", ip, name, err)
114+
}
115+
}
116+
}
117+
}

0 commit comments

Comments
 (0)