-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrule.go
205 lines (186 loc) · 4.26 KB
/
rule.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
//go:build !windows && !plan9 && !linux
// +build !windows,!plan9,!linux
package pf
import (
"fmt"
"net"
)
// GetRules returns a string array of currently configured firewall rules
func (f *Firewall) GetRules() ([]string, error) {
return f.execPfCtl("-s", "rules")
}
// Rule is the struct that holds all relevant data for a pf firewall anchor rule
type Rule struct {
Action string
AdressFamily string
committed bool
Direction string
Destination *net.IPNet
DestPort uint32
Interface string
Log bool
Protocol string
Source *net.IPNet
SourcePort uint32
}
// SetSourceIP sets a source IP for the current Rule
func (a *Rule) SetSourceIP(i string, m ...string) {
if !a.committed {
a.Source = parseIP(i, m)
}
}
// SetSourceCIDR sets a source IP for the current Rule
func (a *Rule) SetSourceCIDR(c string) error {
if !a.committed {
_, ipNet, err := net.ParseCIDR(c)
if err != nil {
return err
}
a.Source = ipNet
}
return nil
}
// SetDestinationIP sets a destination IP for the current Rule
func (a *Rule) SetDestinationIP(i string, m ...string) {
if !a.committed {
a.Destination = parseIP(i, m)
}
}
// SetDestinationCIDR sets a source IP for the current Rule
func (a *Rule) SetDestinationCIDR(c string) error {
if !a.committed {
_, ipNet, err := net.ParseCIDR(c)
if err != nil {
return err
}
a.Destination = ipNet
}
return nil
}
// SetSourcePort sets the source port for the current Rule
func (a *Rule) SetSourcePort(p uint32) {
if !a.committed {
a.SourcePort = p
}
}
// SetDestinationPort sets the source port for the current Rule
func (a *Rule) SetDestinationPort(p uint32) {
if !a.committed {
a.DestPort = p
}
}
// SetInterface sets the interface for the current Rule
func (a *Rule) SetInterface(i string) {
if !a.committed {
a.Interface = i
}
}
// SetProtocol sets the protocol type for the current Rule
func (a *Rule) SetProtocol(p Protocol) {
if !a.committed {
switch p {
case ProtocolTcp:
a.Protocol = "tcp"
case ProtocolUdp:
a.Protocol = "udp"
case ProtocolIcmp:
a.Protocol = "icmp"
case ProtocolIcmpv6:
a.Protocol = "icmp6"
case ProtocolUnknown:
a.Protocol = ""
default:
a.Protocol = ""
}
}
}
// SetAction sets the action type for the current Rule
func (a *Rule) SetAction(ac Action) {
if !a.committed {
switch ac {
case ActionPass:
a.Action = "pass"
case ActionBlock:
a.Action = "block"
case ActionUnknown:
a.Action = ""
default:
a.Action = "block"
}
}
}
// SetAddrFamily sets the address family for the current Rule
func (a *Rule) SetAddrFamily(f AddrFam) {
if !a.committed {
switch f {
case AdressFamilyInet:
a.AdressFamily = "inet"
case AdressFamilyInetv6:
a.AdressFamily = "inet6"
default:
a.AdressFamily = ""
}
}
}
// SetDirection sets the address family for the current Rule
func (a *Rule) SetDirection(d Direction) {
if !a.committed {
switch d {
case DirectionIn:
a.Direction = "in"
case DirectionOut:
a.Direction = "out"
case DirectionUnknown:
a.Direction = ""
default:
a.Direction = ""
}
}
}
// SetLogging enables logging for the current Rule
func (a *Rule) SetLogging() {
a.Log = true
}
// Commit commits the current Rule so it is immutable
func (a *Rule) Commit() {
a.committed = true
}
// String parses a given Rule and returns the full rule as string
func (a *Rule) String() string {
var fwRule string
if a.Action != "" {
fwRule = a.Action
}
if a.Direction != "" {
fwRule = fmt.Sprintf("%s %s", fwRule, a.Direction)
}
if a.Log {
fwRule = fmt.Sprintf("%s log", fwRule)
}
if a.Interface != "" {
fwRule = fmt.Sprintf("%s on %s", fwRule, a.Interface)
}
if a.AdressFamily != "" {
fwRule = fmt.Sprintf("%s on %s", fwRule, a.Interface)
}
if a.Protocol != "" {
fwRule = fmt.Sprintf("%s proto %s", fwRule, a.Protocol)
}
if a.Source != nil {
fwRule = fmt.Sprintf("%s from %s", fwRule, a.Source.String())
} else {
fwRule = fmt.Sprintf("%s from any", fwRule)
}
if a.SourcePort > 0 {
fwRule = fmt.Sprintf("%s port %d", fwRule, a.SourcePort)
}
if a.Destination != nil {
fwRule = fmt.Sprintf("%s to %s", fwRule, a.Destination.String())
} else {
fwRule = fmt.Sprintf("%s to any", fwRule)
}
if a.DestPort > 0 {
fwRule = fmt.Sprintf("%s port %d", fwRule, a.DestPort)
}
return fwRule
}