@@ -87,8 +87,14 @@ type IPTableEntry struct {
87
87
Params string
88
88
}
89
89
90
+ type Client struct {}
91
+
92
+ func NewClient () * Client {
93
+ return & Client {}
94
+ }
95
+
90
96
// Run iptables command
91
- func RunCmd (version , params string ) error {
97
+ func ( c * Client ) RunCmd (version , params string ) error {
92
98
var cmd string
93
99
94
100
p := platform .NewExecClient (logger )
@@ -111,29 +117,29 @@ func RunCmd(version, params string) error {
111
117
}
112
118
113
119
// check if iptable chain alreay exists
114
- func ChainExists (version , tableName , chainName string ) bool {
120
+ func ( c * Client ) ChainExists (version , tableName , chainName string ) bool {
115
121
params := fmt .Sprintf ("-t %s -L %s" , tableName , chainName )
116
- if err := RunCmd (version , params ); err != nil {
122
+ if err := c . RunCmd (version , params ); err != nil {
117
123
return false
118
124
}
119
125
120
126
return true
121
127
}
122
128
123
- func GetCreateChainCmd (version , tableName , chainName string ) IPTableEntry {
129
+ func ( c * Client ) GetCreateChainCmd (version , tableName , chainName string ) IPTableEntry {
124
130
return IPTableEntry {
125
131
Version : version ,
126
132
Params : fmt .Sprintf ("-t %s -N %s" , tableName , chainName ),
127
133
}
128
134
}
129
135
130
136
// create new iptable chain under specified table name
131
- func CreateChain (version , tableName , chainName string ) error {
137
+ func ( c * Client ) CreateChain (version , tableName , chainName string ) error {
132
138
var err error
133
139
134
- if ! ChainExists (version , tableName , chainName ) {
135
- cmd := GetCreateChainCmd (version , tableName , chainName )
136
- err = RunCmd (version , cmd .Params )
140
+ if ! c . ChainExists (version , tableName , chainName ) {
141
+ cmd := c . GetCreateChainCmd (version , tableName , chainName )
142
+ err = c . RunCmd (version , cmd .Params )
137
143
} else {
138
144
logger .Info ("Chain exists in table" , zap .String ("chainName" , chainName ), zap .String ("tableName" , tableName ))
139
145
}
@@ -142,52 +148,52 @@ func CreateChain(version, tableName, chainName string) error {
142
148
}
143
149
144
150
// check if iptable rule alreay exists
145
- func RuleExists (version , tableName , chainName , match , target string ) bool {
151
+ func ( c * Client ) RuleExists (version , tableName , chainName , match , target string ) bool {
146
152
params := fmt .Sprintf ("-t %s -C %s %s -j %s" , tableName , chainName , match , target )
147
- if err := RunCmd (version , params ); err != nil {
153
+ if err := c . RunCmd (version , params ); err != nil {
148
154
return false
149
155
}
150
156
return true
151
157
}
152
158
153
- func GetInsertIptableRuleCmd (version , tableName , chainName , match , target string ) IPTableEntry {
159
+ func ( c * Client ) GetInsertIptableRuleCmd (version , tableName , chainName , match , target string ) IPTableEntry {
154
160
return IPTableEntry {
155
161
Version : version ,
156
162
Params : fmt .Sprintf ("-t %s -I %s 1 %s -j %s" , tableName , chainName , match , target ),
157
163
}
158
164
}
159
165
160
166
// Insert iptable rule at beginning of iptable chain
161
- func InsertIptableRule (version , tableName , chainName , match , target string ) error {
162
- if RuleExists (version , tableName , chainName , match , target ) {
167
+ func ( c * Client ) InsertIptableRule (version , tableName , chainName , match , target string ) error {
168
+ if c . RuleExists (version , tableName , chainName , match , target ) {
163
169
logger .Info ("Rule already exists" )
164
170
return nil
165
171
}
166
172
167
- cmd := GetInsertIptableRuleCmd (version , tableName , chainName , match , target )
168
- return RunCmd (version , cmd .Params )
173
+ cmd := c . GetInsertIptableRuleCmd (version , tableName , chainName , match , target )
174
+ return c . RunCmd (version , cmd .Params )
169
175
}
170
176
171
- func GetAppendIptableRuleCmd (version , tableName , chainName , match , target string ) IPTableEntry {
177
+ func ( c * Client ) GetAppendIptableRuleCmd (version , tableName , chainName , match , target string ) IPTableEntry {
172
178
return IPTableEntry {
173
179
Version : version ,
174
180
Params : fmt .Sprintf ("-t %s -A %s %s -j %s" , tableName , chainName , match , target ),
175
181
}
176
182
}
177
183
178
184
// Append iptable rule at end of iptable chain
179
- func AppendIptableRule (version , tableName , chainName , match , target string ) error {
180
- if RuleExists (version , tableName , chainName , match , target ) {
185
+ func ( c * Client ) AppendIptableRule (version , tableName , chainName , match , target string ) error {
186
+ if c . RuleExists (version , tableName , chainName , match , target ) {
181
187
logger .Info ("Rule already exists" )
182
188
return nil
183
189
}
184
190
185
- cmd := GetAppendIptableRuleCmd (version , tableName , chainName , match , target )
186
- return RunCmd (version , cmd .Params )
191
+ cmd := c . GetAppendIptableRuleCmd (version , tableName , chainName , match , target )
192
+ return c . RunCmd (version , cmd .Params )
187
193
}
188
194
189
195
// Delete matched iptable rule
190
- func DeleteIptableRule (version , tableName , chainName , match , target string ) error {
196
+ func ( c * Client ) DeleteIptableRule (version , tableName , chainName , match , target string ) error {
191
197
params := fmt .Sprintf ("-t %s -D %s %s -j %s" , tableName , chainName , match , target )
192
- return RunCmd (version , params )
198
+ return c . RunCmd (version , params )
193
199
}
0 commit comments