From a0e1c30e22bff63c9015fc738cd81441a4da0dea Mon Sep 17 00:00:00 2001 From: ssongliu <73214554+ssongliu@users.noreply.github.com> Date: Wed, 22 Nov 2023 14:00:09 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E7=A6=81=20Ping=20=E5=85=BC=E5=AE=B9=20?= =?UTF-8?q?ipv6=20(#3024)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/app/service/firewall.go | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/backend/app/service/firewall.go b/backend/app/service/firewall.go index 28541f418..fad43b2fb 100644 --- a/backend/app/service/firewall.go +++ b/backend/app/service/firewall.go @@ -485,20 +485,31 @@ func (u *FirewallService) updatePingStatus(enable string) error { if err != nil { return err } + hasV4Line, hasV6Line := false, false + if _, err := os.Stat("/proc/sys/net/ipv6/icmp_echo_ignore_all"); err != nil { + hasV6Line = true + } files := strings.Split(string(lineBytes), "\n") var newFiles []string - hasLine := false for _, line := range files { - if strings.Contains(line, "net/ipv4/icmp_echo_ignore_all") || strings.HasPrefix(line, "net/ipv4/icmp_echo_ignore_all") { + if strings.HasPrefix(strings.ReplaceAll(line, " ", ""), "net/ipv4/icmp_echo_ignore_all") && !hasV4Line { newFiles = append(newFiles, "net/ipv4/icmp_echo_ignore_all="+enable) - hasLine = true - } else { - newFiles = append(newFiles, line) + hasV4Line = true + continue } + if strings.HasPrefix(strings.ReplaceAll(line, " ", ""), "net/ipv6/icmp_echo_ignore_all") && !hasV6Line { + newFiles = append(newFiles, "net/ipv6/icmp_echo_ignore_all="+enable) + hasV6Line = true + continue + } + newFiles = append(newFiles, line) } - if !hasLine { + if !hasV4Line { newFiles = append(newFiles, "net/ipv4/icmp_echo_ignore_all="+enable) } + if !hasV6Line { + newFiles = append(newFiles, "net/ipv6/icmp_echo_ignore_all="+enable) + } file, err := os.OpenFile(confPath, os.O_WRONLY|os.O_TRUNC, 0666) if err != nil { return err