fix: improve error handling in recoverIPTables by filtering chain not exist errors

https://github.com/safing/portmaster/issues/1949
This commit is contained in:
Alexandr Stelnykovych
2025-10-03 12:43:04 +03:00
parent c085ae05ea
commit af6a1623cc

View File

@@ -4,6 +4,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"os" "os"
"regexp"
"strings" "strings"
"github.com/hashicorp/go-multierror" "github.com/hashicorp/go-multierror"
@@ -45,6 +46,8 @@ func recoverIPTables(cmd *cobra.Command, args []string) error {
return err return err
} }
chainNotExistPattern := regexp.MustCompile(`(?i)chain\s+\S+\s+does not exist`) // "Chain ... does not exist"
var filteredErrors *multierror.Error var filteredErrors *multierror.Error
for _, err := range mr.Errors { for _, err := range mr.Errors {
// if we have a permission denied error, all errors will be the same // if we have a permission denied error, all errors will be the same
@@ -52,7 +55,9 @@ func recoverIPTables(cmd *cobra.Command, args []string) error {
return fmt.Errorf("failed to cleanup iptables: %w", os.ErrPermission) return fmt.Errorf("failed to cleanup iptables: %w", os.ErrPermission)
} }
if !strings.Contains(err.Error(), "No such file or directory") { if !strings.Contains(err.Error(), "No such file or directory") &&
!chainNotExistPattern.MatchString(err.Error()) {
filteredErrors = multierror.Append(filteredErrors, err) filteredErrors = multierror.Append(filteredErrors, err)
} }
} }