@@ -566,10 +566,8 @@ func (u *FirewallService) pingStatus() string {
566566 if _ , err := os .Stat ("/etc/sysctl.conf" ); err != nil {
567567 return constant .StatusNone
568568 }
569- sudo := cmd .SudoHandleCmd ()
570- command := fmt .Sprintf ("%s cat /etc/sysctl.conf | grep net/ipv4/icmp_echo_ignore_all= " , sudo )
571- stdout , _ := cmd .Exec (command )
572- if stdout == "net/ipv4/icmp_echo_ignore_all=1\n " {
569+ stdout , _ := cmd .Execf ("%s sysctl -a 2>/dev/null | grep 'net.ipv4.icmp.echo_ignore_all'" , cmd .SudoHandleCmd ())
570+ if stdout == "net.ipv4.icmp_echo_ignore_all = 1\n " {
573571 return constant .StatusEnable
574572 }
575573 return constant .StatusDisable
@@ -582,17 +580,30 @@ func (u *FirewallService) updatePingStatus(enable string) error {
582580 }
583581 files := strings .Split (string (lineBytes ), "\n " )
584582 var newFiles []string
585- hasLine := false
583+ var hasIpv6 bool
584+ ipv6Status , _ := cmd .Exec ("sysctl -a 2>/dev/null | grep 'net.ipv6.icmp.echo_ignore_all'" )
585+ if len (strings .ReplaceAll (ipv6Status , "\n " , "" )) != 0 {
586+ hasIpv6 = true
587+ }
588+ hasIPv4Line , hasIPv6Line := false , false
586589 for _ , line := range files {
587- if strings .Contains (line , "net/ipv4/icmp_echo_ignore_all" ) || strings .HasPrefix (line , "net/ipv4/icmp_echo_ignore_all" ) {
588- newFiles = append (newFiles , "net/ipv4/icmp_echo_ignore_all=" + enable )
589- hasLine = true
590- } else {
591- newFiles = append (newFiles , line )
590+ if strings .Contains (line , "net/ipv4/icmp_echo_ignore_all" ) || strings .Contains (line , "net.ipv4.icmp_echo_ignore_all" ) {
591+ newFiles = append (newFiles , "net.ipv4.icmp_echo_ignore_all=" + enable )
592+ hasIPv4Line = true
593+ continue
592594 }
595+ if hasIpv6 && strings .Contains (line , "net/ipv6/icmp/echo_ignore_all" ) || strings .Contains (line , "net.ipv6.icmp.echo_ignore_all" ) {
596+ newFiles = append (newFiles , "net.ipv6.icmp.echo_ignore_all=" + enable )
597+ hasIPv6Line = true
598+ continue
599+ }
600+ newFiles = append (newFiles , line )
601+ }
602+ if ! hasIPv4Line {
603+ newFiles = append (newFiles , "net.ipv4.icmp_echo_ignore_all=" + enable )
593604 }
594- if ! hasLine {
595- newFiles = append (newFiles , "net/ipv4/icmp_echo_ignore_all =" + enable )
605+ if ! hasIPv6Line {
606+ newFiles = append (newFiles , "net.ipv6.icmp.echo_ignore_all =" + enable )
596607 }
597608 file , err := os .OpenFile (confPath , os .O_WRONLY | os .O_TRUNC , constant .FilePerm )
598609 if err != nil {
0 commit comments