mirror of
https://github.com/safing/portmaster
synced 2025-09-02 18:49:14 +00:00
Silience "not found" errors in recover-iptables
This commit is contained in:
parent
866f1981e0
commit
071f2a9bd5
3 changed files with 84 additions and 17 deletions
|
@ -1,6 +1,11 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/hashicorp/go-multierror"
|
||||||
"github.com/safing/portmaster/firewall/interception"
|
"github.com/safing/portmaster/firewall/interception"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
@ -9,7 +14,44 @@ var recoverIPTablesCmd = &cobra.Command{
|
||||||
Use: "recover-iptables",
|
Use: "recover-iptables",
|
||||||
Short: "Removes obsolete IP tables rules in case of an unclean shutdown",
|
Short: "Removes obsolete IP tables rules in case of an unclean shutdown",
|
||||||
RunE: func(*cobra.Command, []string) error {
|
RunE: func(*cobra.Command, []string) error {
|
||||||
return interception.DeactivateNfqueueFirewall()
|
// interception.DeactiveNfqueueFirewall uses coreos/go-iptables
|
||||||
|
// which shells out to the /sbin/iptables binary. As a result,
|
||||||
|
// we don't get the errno of the actual error and need to parse the
|
||||||
|
// output instead. Make sure it's always english by setting LC_ALL=C
|
||||||
|
currentLocale := os.Getenv("LC_ALL")
|
||||||
|
os.Setenv("LC_ALL", "C") // nolint:errcheck - we tried at least ...
|
||||||
|
defer os.Setenv("LC_ALL", currentLocale) // nolint:errcheck
|
||||||
|
|
||||||
|
err := interception.DeactivateNfqueueFirewall()
|
||||||
|
if err == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// we don't want to show ErrNotExists to the user
|
||||||
|
// as that only means portmaster did the cleanup itself.
|
||||||
|
mr, ok := err.(*multierror.Error)
|
||||||
|
if !ok {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
var filteredErrors *multierror.Error
|
||||||
|
for _, err := range mr.Errors {
|
||||||
|
// if we have a permission denied error, all errors will be the same
|
||||||
|
if strings.Contains(err.Error(), "Permission denied") {
|
||||||
|
return fmt.Errorf("failed to cleanup iptables: %w", os.ErrPermission)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !strings.Contains(err.Error(), "No such file or directory") {
|
||||||
|
filteredErrors = multierror.Append(filteredErrors, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if filteredErrors != nil {
|
||||||
|
filteredErrors.ErrorFormat = formatNfqErrors
|
||||||
|
return filteredErrors
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
},
|
},
|
||||||
SilenceUsage: true,
|
SilenceUsage: true,
|
||||||
}
|
}
|
||||||
|
@ -17,3 +59,20 @@ var recoverIPTablesCmd = &cobra.Command{
|
||||||
func init() {
|
func init() {
|
||||||
rootCmd.AddCommand(recoverIPTablesCmd)
|
rootCmd.AddCommand(recoverIPTablesCmd)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func formatNfqErrors(es []error) string {
|
||||||
|
if len(es) == 1 {
|
||||||
|
return fmt.Sprintf("1 error occurred:\n\t* %s\n\n", es[0])
|
||||||
|
}
|
||||||
|
|
||||||
|
points := make([]string, len(es))
|
||||||
|
for i, err := range es {
|
||||||
|
// only display the very first line of each error
|
||||||
|
first := strings.Split(err.Error(), "\n")[0]
|
||||||
|
points[i] = fmt.Sprintf("* %s", first)
|
||||||
|
}
|
||||||
|
|
||||||
|
return fmt.Sprintf(
|
||||||
|
"%d errors occurred:\n\t%s\n\n",
|
||||||
|
len(es), strings.Join(points, "\n\t"))
|
||||||
|
}
|
||||||
|
|
|
@ -7,6 +7,7 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/coreos/go-iptables/iptables"
|
"github.com/coreos/go-iptables/iptables"
|
||||||
|
"github.com/hashicorp/go-multierror"
|
||||||
|
|
||||||
"github.com/safing/portbase/log"
|
"github.com/safing/portbase/log"
|
||||||
"github.com/safing/portmaster/firewall/interception/nfqexp"
|
"github.com/safing/portmaster/firewall/interception/nfqexp"
|
||||||
|
@ -139,16 +140,20 @@ func activateNfqueueFirewall() error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// DeactivateNfqueueFirewall drops portmaster related IP tables rules.
|
// DeactivateNfqueueFirewall drops portmaster related IP tables rules.
|
||||||
|
// Any errors encountered accumulated into a *multierror.Error.
|
||||||
func DeactivateNfqueueFirewall() error {
|
func DeactivateNfqueueFirewall() error {
|
||||||
// IPv4
|
// IPv4
|
||||||
errV4 := deactivateIPTables(iptables.ProtocolIPv4, v4once, v4chains)
|
var result *multierror.Error
|
||||||
|
if err := deactivateIPTables(iptables.ProtocolIPv4, v4once, v4chains); err != nil {
|
||||||
// IPv6
|
result = multierror.Append(result, err)
|
||||||
if errV6 := deactivateIPTables(iptables.ProtocolIPv6, v6once, v6chains); errV6 != nil && errV4 == nil {
|
|
||||||
return errV6
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return errV4
|
// IPv6
|
||||||
|
if err := deactivateIPTables(iptables.ProtocolIPv6, v6once, v6chains); err != nil {
|
||||||
|
result = multierror.Append(result, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
func activateIPTables(protocol iptables.Protocol, rules, once, chains []string) error {
|
func activateIPTables(protocol iptables.Protocol, rules, once, chains []string) error {
|
||||||
|
@ -193,31 +198,32 @@ func deactivateIPTables(protocol iptables.Protocol, rules, chains []string) erro
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
var firstErr error
|
var multierr *multierror.Error
|
||||||
|
|
||||||
for _, rule := range rules {
|
for _, rule := range rules {
|
||||||
splittedRule := strings.Split(rule, " ")
|
splittedRule := strings.Split(rule, " ")
|
||||||
ok, err := tbls.Exists(splittedRule[0], splittedRule[1], splittedRule[2:]...)
|
ok, err := tbls.Exists(splittedRule[0], splittedRule[1], splittedRule[2:]...)
|
||||||
if err != nil && firstErr == nil {
|
if err != nil {
|
||||||
firstErr = err
|
multierr = multierror.Append(multierr, err)
|
||||||
}
|
}
|
||||||
if ok {
|
if ok {
|
||||||
if err = tbls.Delete(splittedRule[0], splittedRule[1], splittedRule[2:]...); err != nil && firstErr == nil {
|
if err = tbls.Delete(splittedRule[0], splittedRule[1], splittedRule[2:]...); err != nil {
|
||||||
firstErr = err
|
multierr = multierror.Append(multierr, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, chain := range chains {
|
for _, chain := range chains {
|
||||||
splittedRule := strings.Split(chain, " ")
|
splittedRule := strings.Split(chain, " ")
|
||||||
if err = tbls.ClearChain(splittedRule[0], splittedRule[1]); err != nil && firstErr == nil {
|
if err = tbls.ClearChain(splittedRule[0], splittedRule[1]); err != nil {
|
||||||
firstErr = err
|
multierr = multierror.Append(multierr, err)
|
||||||
}
|
}
|
||||||
if err = tbls.DeleteChain(splittedRule[0], splittedRule[1]); err != nil && firstErr == nil {
|
if err = tbls.DeleteChain(splittedRule[0], splittedRule[1]); err != nil {
|
||||||
firstErr = err
|
multierr = multierror.Append(multierr, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return firstErr
|
return multierr
|
||||||
}
|
}
|
||||||
|
|
||||||
// StartNfqueueInterception starts the nfqueue interception.
|
// StartNfqueueInterception starts the nfqueue interception.
|
||||||
|
|
|
@ -58,6 +58,7 @@ func parseUDP(packet gopacket.Packet, info *Info) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
func parseUDPLite(packet gopacket.Packet, info *Info) error {
|
func parseUDPLite(packet gopacket.Packet, info *Info) error {
|
||||||
if udpLite, ok := packet.TransportLayer().(*layers.UDPLite); ok {
|
if udpLite, ok := packet.TransportLayer().(*layers.UDPLite); ok {
|
||||||
info.Protocol = UDPLite
|
info.Protocol = UDPLite
|
||||||
|
@ -66,6 +67,7 @@ func parseUDPLite(packet gopacket.Packet, info *Info) error {
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
func parseICMPv4(packet gopacket.Packet, info *Info) error {
|
func parseICMPv4(packet gopacket.Packet, info *Info) error {
|
||||||
if icmp, ok := packet.Layer(layers.LayerTypeICMPv4).(*layers.ICMPv4); ok {
|
if icmp, ok := packet.Layer(layers.LayerTypeICMPv4).(*layers.ICMPv4); ok {
|
||||||
|
|
Loading…
Add table
Reference in a new issue