-
Notifications
You must be signed in to change notification settings - Fork 12
/
pf_darwin.go
170 lines (154 loc) · 3.63 KB
/
pf_darwin.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
// +build darwin
package dnsredir
import (
"fmt"
"github.com/coredns/caddy"
"github.com/leiless/dnsredir/pf"
"github.com/miekg/dns"
"net"
"os"
"strings"
)
type pfHandle struct {
set pf.TableSet
dev int // File descriptor to the /dev/pf
}
// arg in format of NAME[:ANCHOR]
func splitNameAnchor(arg string) (string, string) {
i := strings.IndexByte(arg, ':')
if i < 0 {
return arg, ""
}
return arg[:i], arg[i+1:]
}
func parseFlags(dir string, args *[]string) (pf.Flags, error) {
var flags pf.Flags
var i int
var arg string
for i, arg = range *args {
if strings.HasPrefix(arg, "+") {
switch arg {
case "+create":
flags.TurnOnCreateIfNotExist()
case "+v4_only":
flags.TurnOnV4Only()
case "+v6_only":
flags.TurnOnV6Only()
default:
return 0, fmt.Errorf("%v: unrecognizable option: %q", dir, arg)
}
} else {
break
}
}
if strings.HasPrefix(arg, "+") {
return 0, fmt.Errorf("%v: name[:anchor]... not found in %q", dir, strings.Join(*args, " "))
}
if !flags.IsValid() {
return 0, fmt.Errorf("%v: met invalid/conflict options in %q", dir, strings.Join(*args, " "))
}
*args = (*args)[i:]
return flags, nil
}
func pfParse(c *caddy.Controller, u *reloadableUpstream) error {
dir := c.Val()
args := c.RemainingArgs()
if len(args) == 0 {
return c.ArgErr()
}
if u.pf == nil {
u.pf = &pfHandle{
set: make(pf.TableSet),
dev: -1,
}
}
flags, err := parseFlags(dir, &args)
if err != nil {
return err
}
handle := u.pf.(*pfHandle)
for _, arg := range args {
name, anchor := splitNameAnchor(arg)
if err := handle.set.Add(name, anchor, flags); err != nil && !os.IsExist(err) {
return err
}
}
log.Infof("%v: %v", dir, handle.set.String())
return nil
}
var pfOnce Once
func pfSetup(u *reloadableUpstream) error {
if u.pf == nil {
return nil
}
if os.Geteuid() != 0 {
log.Warningf("pf needs root user privilege to work")
}
handle := u.pf.(*pfHandle)
if dev, err := pf.Open(os.O_WRONLY); err != nil {
return err
} else {
pfOnce.Do(func() {
if enabled, err := pf.IsEnabled(dev); err != nil {
log.Errorf("Cannot check if pf enabled: %v", err)
} else if !enabled {
log.Warningf("pf is not enabled, changes to pf won't take effect immediately.")
}
})
handle.dev = dev
// Try to create tables at pf setup stage.
for tbl, flags := range handle.set {
if !flags.IsCreateIfNotExist() {
continue
}
if created, err := pf.AddTable(handle.dev, tbl.Name, tbl.Anchor); err != nil {
return err
} else {
log.Debugf("pf: %v created: %v", tbl.String(), created)
}
}
return nil
}
}
func pfShutdown(u *reloadableUpstream) error {
if u.pf == nil {
return nil
}
handle := u.pf.(*pfHandle)
return pf.Close(handle.dev)
}
func pfAddIP(u *reloadableUpstream, reply *dns.Msg) {
if u.pf == nil || reply.Rcode != dns.RcodeSuccess {
return
}
handle := u.pf.(*pfHandle)
for _, rr := range reply.Answer {
rrt := rr.Header().Rrtype
if rrt != dns.TypeA && rrt != dns.TypeAAAA {
continue
}
ss := strings.Split(rr.String(), "\t")
if len(ss) != 5 {
log.Warningf("Expected 5 entries, got %v: %q", len(ss), rr.String())
continue
}
ip := net.ParseIP(ss[4])
if ip == nil {
log.Warningf("ipsetAddIP(): %q is not a valid IP address", ss[4])
continue
}
for tbl, flags := range handle.set {
if flags.IsV4Only() && rrt != dns.TypeA {
continue
}
if flags.IsV6Only() && rrt != dns.TypeAAAA {
continue
}
if added, err := pf.AddAddr(handle.dev, tbl.Name, tbl.Anchor, ip); err != nil {
log.Errorf("pf.AddIP(): cannot add %v to %v: %v", ip, tbl, err)
} else {
log.Debugf("pf: %v added: %v", ip.String(), added)
}
}
}
}