@@ -21,8 +21,6 @@ const (
2121 // IPTablesModePlain signals the usage of the iptables commands, which
2222 // can be either legacy or nft
2323 IPTablesModePlain = "plain"
24- // IPTablesModeAuto signals automatic detection of the iptables backend
25- IPTablesModeAuto = "auto"
2624
2725 cmdLegacy = "iptables-legacy"
2826 cmdLegacySave = "iptables-legacy-save"
@@ -167,11 +165,21 @@ func NewRootCmd() *cobra.Command {
167165
168166// BuildFirewallConfiguration returns an iptables FirewallConfiguration suitable to use to configure iptables.
169167func BuildFirewallConfiguration (options * RootOptions ) (* iptables.FirewallConfiguration , error ) {
170- if options .IPTablesMode != "" &&
171- options .IPTablesMode != IPTablesModeLegacy &&
172- options .IPTablesMode != IPTablesModeNFT &&
173- options .IPTablesMode != IPTablesModePlain {
174- return nil , fmt .Errorf ("--iptables-mode valid values are only \" %s\" , \" %s\" , \" %s\" , and \" %s\" " , IPTablesModeLegacy , IPTablesModeNFT , IPTablesModeAuto , IPTablesModePlain )
168+ if options .IPTablesMode != "" && options .IPTablesMode != IPTablesModeLegacy && options .IPTablesMode != IPTablesModeNFT && options .IPTablesMode != IPTablesModePlain {
169+ return nil , fmt .Errorf ("--iptables-mode valid values are only \" %s\" , \" %s\" and \" %s\" " , IPTablesModeLegacy , IPTablesModeNFT , IPTablesModePlain )
170+ }
171+
172+ if options .IPTablesMode == "" {
173+ switch options .FirewallBinPath {
174+ case "" , cmdLegacy :
175+ options .IPTablesMode = IPTablesModeLegacy
176+ case cmdNFT :
177+ options .IPTablesMode = IPTablesModeNFT
178+ case cmdPlain :
179+ options .IPTablesMode = IPTablesModePlain
180+ default :
181+ return nil , fmt .Errorf ("--firewall-bin-path valid values are only \" %s\" , \" %s\" and \" %s\" " , cmdLegacy , cmdNFT , cmdPlain )
182+ }
175183 }
176184
177185 if ! util .IsValidPort (options .IncomingProxyPort ) {
@@ -182,6 +190,8 @@ func BuildFirewallConfiguration(options *RootOptions) (*iptables.FirewallConfigu
182190 return nil , fmt .Errorf ("--outgoing-proxy-port must be a valid TCP port number" )
183191 }
184192
193+ cmd , cmdSave := getCommands (options )
194+
185195 sanitizedSubnets := []string {}
186196 for _ , subnet := range options .SubnetsToIgnore {
187197 subnet := strings .TrimSpace (subnet )
@@ -205,6 +215,8 @@ func BuildFirewallConfiguration(options *RootOptions) (*iptables.FirewallConfigu
205215 SimulateOnly : options .SimulateOnly ,
206216 NetNs : options .NetNs ,
207217 UseWaitFlag : options .UseWaitFlag ,
218+ BinPath : cmd ,
219+ SaveBinPath : cmdSave ,
208220 }
209221
210222 if len (options .PortsToRedirect ) > 0 {
@@ -213,16 +225,6 @@ func BuildFirewallConfiguration(options *RootOptions) (*iptables.FirewallConfigu
213225 firewallConfiguration .Mode = iptables .RedirectAllMode
214226 }
215227
216- // For backwards-compatibility, if IPTablesMode is not set, use the FirewallBinPath
217- // explicitly set by the user.
218- if options .IPTablesMode == "" {
219- firewallConfiguration .BinPath = options .FirewallBinPath
220- firewallConfiguration .SaveBinPath = options .FirewallSaveBinPath
221- } else {
222- // Otherwise, detect and set the appropriate backend.
223- iptables .DetectBackend (firewallConfiguration , exec .LookPath , options .IPv6 , options .IPTablesMode )
224- }
225-
226228 return firewallConfiguration , nil
227229}
228230
@@ -235,6 +237,26 @@ func getFormatter(format string) log.Formatter {
235237 }
236238}
237239
240+ func getCommands (options * RootOptions ) (string , string ) {
241+ switch options .IPTablesMode {
242+ case IPTablesModeLegacy :
243+ if options .IPv6 {
244+ return cmdLegacyIPv6 , cmdLegacyIPv6Save
245+ }
246+ return cmdLegacy , cmdLegacySave
247+ case IPTablesModeNFT :
248+ if options .IPv6 {
249+ return cmdNFTIPv6 , cmdNFTIPv6Save
250+ }
251+ return cmdNFT , cmdNFTSave
252+ default :
253+ if options .IPv6 {
254+ return cmdPlainIPv6 , cmdPlainIPv6Save
255+ }
256+ return cmdPlain , cmdPlainSave
257+ }
258+ }
259+
238260func setLogLevel (logLevel string ) error {
239261 level , err := log .ParseLevel (logLevel )
240262 if err != nil {
0 commit comments