@@ -26,80 +26,106 @@ import (
26
26
)
27
27
28
28
const (
29
+ // Destination represents the destination address flag
29
30
Destination = "-d "
30
- Source = "-s "
31
- DPort = "--dport "
32
- Protocol = "-p "
33
- Jump = "-j "
34
- Reject = "REJECT"
35
- ToDest = "--to-destination "
36
- Recent = "recent "
37
- MatchSet = "--match-set "
38
- SrcType = "--src-type "
39
- Masquerade = "MASQUERADE "
31
+ // Source represents the source address flag
32
+ Source = "-s "
33
+ // DPort represents the destination port
34
+ DPort = "--dport "
35
+ // Protocol represents the protocol flag which takes input by number of name
36
+ Protocol = "-p "
37
+ // Jump represents jump flag specifies the jump target
38
+ Jump = "-j "
39
+ // Reject specifies the reject target
40
+ Reject = "REJECT"
41
+ // ToDest represents the --to-destination flag used to specify the destination address in DNAT
42
+ ToDest = "--to-destination "
43
+ // Recent represents the sub-command recent that allows to dynamically create list of IP address to match against
44
+ Recent = "recent "
45
+ // MatchSet represents the --match-set flag which match packets against the specified set
46
+ MatchSet = "--match-set "
47
+ // SrcType represents the --src-type flag which matches if the source address is of given type
48
+ SrcType = "--src-type "
49
+ // Masquerade represents the target that is used in nat table.
50
+ Masquerade = "MASQUERADE "
40
51
)
41
52
53
+ // Rule holds a map of rules.
42
54
type Rule map [string ]string
43
55
44
- // no-op implementation of iptables Interface
56
+ // FakeIPTables no-op implementation of iptables Interface.
45
57
type FakeIPTables struct {
46
58
hasRandomFully bool
47
59
Lines []byte
48
60
}
49
61
62
+ // NewFake returns a pointer for no-op implementation of iptables Interface.
50
63
func NewFake () * FakeIPTables {
51
64
return & FakeIPTables {}
52
65
}
53
66
67
+ // SetHasRandomFully will enable the port maping fully randomized in the no-op implementation of iptables Interface.
54
68
func (f * FakeIPTables ) SetHasRandomFully (can bool ) * FakeIPTables {
55
69
f .hasRandomFully = can
56
70
return f
57
71
}
58
72
73
+ // EnsureChain will returns true and states the specified chain exists for testing.
59
74
func (* FakeIPTables ) EnsureChain (table iptables.Table , chain iptables.Chain ) (bool , error ) {
60
75
return true , nil
61
76
}
62
77
78
+ // FlushChain returns nil and states that the specified chain is cleared.
63
79
func (* FakeIPTables ) FlushChain (table iptables.Table , chain iptables.Chain ) error {
64
80
return nil
65
81
}
66
82
83
+ // DeleteChain returns nil and states that the specified chain exists and it is deleted.
67
84
func (* FakeIPTables ) DeleteChain (table iptables.Table , chain iptables.Chain ) error {
68
85
return nil
69
86
}
70
87
88
+ // EnsureRule return true and states that the specified rule is present.
71
89
func (* FakeIPTables ) EnsureRule (position iptables.RulePosition , table iptables.Table , chain iptables.Chain , args ... string ) (bool , error ) {
72
90
return true , nil
73
91
}
74
92
93
+ // DeleteRule returns nil and states that the specified rule is present and is deleted.
75
94
func (* FakeIPTables ) DeleteRule (table iptables.Table , chain iptables.Chain , args ... string ) error {
76
95
return nil
77
96
}
78
97
98
+ // IsIpv6 returns false and states that it is managing only ipv4 tables.
79
99
func (* FakeIPTables ) IsIpv6 () bool {
80
100
return false
81
101
}
82
102
103
+ // Save returns a copy of the iptables lines byte array.
83
104
func (f * FakeIPTables ) Save (table iptables.Table ) ([]byte , error ) {
84
105
lines := make ([]byte , len (f .Lines ))
85
106
copy (lines , f .Lines )
86
107
return lines , nil
87
108
}
88
109
110
+ // SaveInto calls `iptables-save` command for table and stores result in a given buffer.
89
111
func (f * FakeIPTables ) SaveInto (table iptables.Table , buffer * bytes.Buffer ) error {
90
112
buffer .Write (f .Lines )
91
113
return nil
92
114
}
93
115
116
+ // Restore returns null and states that it ran `iptables-restore` successfully.
94
117
func (* FakeIPTables ) Restore (table iptables.Table , data []byte , flush iptables.FlushFlag , counters iptables.RestoreCountersFlag ) error {
95
118
return nil
96
119
}
97
120
121
+ // RestoreAll is the same as Restore except that no table is specified.
98
122
func (f * FakeIPTables ) RestoreAll (data []byte , flush iptables.FlushFlag , counters iptables.RestoreCountersFlag ) error {
99
123
f .Lines = data
100
124
return nil
101
125
}
102
126
127
+ // Monitor detects when the given iptables tables have been flushed by an external
128
+ // tool (e.g. a firewall reload) by creating canary chains and polling to see if they have been deleted.
103
129
func (f * FakeIPTables ) Monitor (canary iptables.Chain , tables []iptables.Table , reloadFunc func (), interval time.Duration , stopCh <- chan struct {}) {
104
130
}
105
131
@@ -111,7 +137,7 @@ func getToken(line, separator string) string {
111
137
return ""
112
138
}
113
139
114
- // GetChain returns a list of rules for the given chain.
140
+ // GetRules returns a list of rules for the given chain.
115
141
// The chain name must match exactly.
116
142
// The matching is pretty dumb, don't rely on it for anything but testing.
117
143
func (f * FakeIPTables ) GetRules (chainName string ) (rules []Rule ) {
@@ -130,6 +156,7 @@ func (f *FakeIPTables) GetRules(chainName string) (rules []Rule) {
130
156
return
131
157
}
132
158
159
+ // HasRandomFully returns the value of the flag --random-fully
133
160
func (f * FakeIPTables ) HasRandomFully () bool {
134
161
return f .hasRandomFully
135
162
}
0 commit comments