Merge iptables.probe() into iptables.detectIptables()

The former was doing some checks and logging warnings, whereas
the latter was doing the same checks but to set some internal variables.
As both are called only once and from the same place, there're now
merged together.

Signed-off-by: Albin Kerouanton <albinker@gmail.com>
(cherry picked from commit 205e5278c6)
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Albin Kerouanton 2021-12-06 01:32:42 +01:00 committed by Sebastiaan van Stijn
parent 2bf66f725c
commit 91f2d963c6
No known key found for this signature in database
GPG key ID: 76698F39D527CE8C

View file

@ -89,19 +89,32 @@ func (e ChainError) Error() string {
return fmt.Sprintf("Error iptables %s: %s", e.Chain, string(e.Output))
}
func probe() {
func detectIptables() {
path, err := exec.LookPath("iptables")
if err != nil {
logrus.Warnf("Failed to find iptables: %v", err)
logrus.WithError(err).Warnf("failed to find iptables")
return
}
if out, err := exec.Command(path, "--wait", "-t", "nat", "-L", "-n").CombinedOutput(); err != nil {
logrus.Warnf("Running iptables --wait -t nat -L -n failed with message: `%s`, error: %v", strings.TrimSpace(string(out)), err)
iptablesPath = path
if out, err := exec.Command(path, "--wait", "-L", "-n").CombinedOutput(); err != nil {
logrus.WithError(err).Infof("unable to detect if iptables supports xlock: 'iptables --wait -L -n': `%s`", strings.TrimSpace(string(out)))
} else {
supportsXlock = true
}
_, err = exec.LookPath("ip6tables")
mj, mn, mc, err := GetVersion()
if err != nil {
logrus.Warnf("Failed to find ip6tables: %v", err)
return
logrus.Warnf("Failed to read iptables version: %v", err)
} else {
supportsCOpt = supportsCOption(mj, mn, mc)
}
path, err = exec.LookPath("ip6tables")
if err != nil {
logrus.WithError(err).Warnf("unable to find ip6tables")
} else {
ip6tablesPath = path
}
}
@ -113,35 +126,11 @@ func initFirewalld() {
return
}
if err := FirewalldInit(); err != nil {
logrus.Debugf("Fail to initialize firewalld: %v, using raw iptables instead", err)
}
}
func detectIptables() {
path, err := exec.LookPath("iptables")
if err != nil {
return
}
iptablesPath = path
supportsXlock = exec.Command(iptablesPath, "--wait", "-L", "-n").Run() == nil
mj, mn, mc, err := GetVersion()
if err != nil {
logrus.Warnf("Failed to read iptables version: %v", err)
return
}
supportsCOpt = supportsCOption(mj, mn, mc)
path, err = exec.LookPath("ip6tables")
if err != nil {
return
} else {
ip6tablesPath = path
logrus.WithError(err).Debugf("unable to initialize firewalld; using raw iptables instead")
}
}
func initDependencies() {
probe()
initFirewalld()
detectIptables()
}
@ -554,6 +543,9 @@ func (iptable IPTable) raw(args ...string) ([]byte, error) {
path := iptablesPath
commandName := "iptables"
if iptable.Version == IPv6 {
if ip6tablesPath == "" {
return nil, fmt.Errorf("ip6tables is missing")
}
path = ip6tablesPath
commandName = "ip6tables"
}