@@ -3,7 +3,6 @@ package service
33import (
44 "context"
55 "fmt"
6- "os"
76 "sort"
87 "strconv"
98 "strings"
@@ -14,7 +13,6 @@ import (
1413 "github.com/1Panel-dev/1Panel/agent/buserr"
1514 "github.com/1Panel-dev/1Panel/agent/constant"
1615 "github.com/1Panel-dev/1Panel/agent/global"
17- "github.com/1Panel-dev/1Panel/agent/utils/cmd"
1816 "github.com/1Panel-dev/1Panel/agent/utils/common"
1917 "github.com/1Panel-dev/1Panel/agent/utils/controller"
2018 "github.com/1Panel-dev/1Panel/agent/utils/firewall"
@@ -23,9 +21,6 @@ import (
2321 "github.com/jinzhu/copier"
2422)
2523
26- const confPath = "/etc/sysctl.conf"
27- const panelSysctlPath = "/etc/sysctl.d/98-onepanel.conf"
28-
2924type FirewallService struct {}
3025
3126type IFirewallService interface {
@@ -62,7 +57,7 @@ func (u *FirewallService) LoadBaseInfo(tab string) (dto.FirewallBaseInfo, error)
6257 wg .Add (2 )
6358 go func () {
6459 defer wg .Done ()
65- baseInfo .PingStatus = u . pingStatus ()
60+ baseInfo .PingStatus = firewall . LoadPingStatus ()
6661 baseInfo .Version , _ = client .Version ()
6762 }()
6863 go func () {
@@ -207,9 +202,17 @@ func (u *FirewallService) OperateFirewall(req dto.FirewallOperation) error {
207202 }
208203 needRestartDocker = true
209204 case "disablePing" :
210- return u .updatePingStatus ("0" )
205+ if err := firewall .UpdatePingStatus ("0" ); err != nil {
206+ _ = settingRepo .Update ("BanPing" , constant .StatusDisable )
207+ return err
208+ }
209+ return nil
211210 case "enablePing" :
212- return u .updatePingStatus ("1" )
211+ if err := firewall .UpdatePingStatus ("1" ); err != nil {
212+ _ = settingRepo .Update ("BanPing" , constant .StatusEnable )
213+ return err
214+ }
215+ return nil
213216 default :
214217 return fmt .Errorf ("not supported operation: %s" , req .Operation )
215218 }
@@ -589,105 +592,6 @@ func (u *FirewallService) cleanUnUsedData(client firewall.FirewallClient) {
589592 }
590593}
591594
592- func (u * FirewallService ) pingStatus () string {
593- data , err := os .ReadFile ("/proc/sys/net/ipv4/icmp_echo_ignore_all" )
594- if err != nil {
595- return constant .StatusNone
596- }
597- v6Data , v6err := os .ReadFile ("/proc/sys/net/ipv6/icmp/echo_ignore_all" )
598- if v6err != nil {
599- if strings .TrimSpace (string (data )) == "1" {
600- return constant .StatusEnable
601- }
602- return constant .StatusDisable
603- } else {
604- if strings .TrimSpace (string (data )) == "1" && strings .TrimSpace (string (v6Data )) == "1" {
605- return constant .StatusEnable
606- }
607- return constant .StatusDisable
608- }
609-
610- }
611-
612- func (u * FirewallService ) updatePingStatus (enable string ) error {
613- var targetPath string
614- var applyCmd string
615-
616- if _ , err := os .Stat (confPath ); os .IsNotExist (err ) {
617- // Debian 13
618- targetPath = panelSysctlPath
619- applyCmd = fmt .Sprintf ("%s sysctl --system" , cmd .SudoHandleCmd ())
620- if err := cmd .RunDefaultBashCf ("%s mkdir -p /etc/sysctl.d" , cmd .SudoHandleCmd ()); err != nil {
621- return fmt .Errorf ("failed to create directory /etc/sysctl.d: %v" , err )
622- }
623- } else {
624- targetPath = confPath
625- applyCmd = fmt .Sprintf ("%s sysctl -p" , cmd .SudoHandleCmd ())
626- }
627-
628- lineBytes , err := os .ReadFile (targetPath )
629- if err != nil && ! os .IsNotExist (err ) {
630- return fmt .Errorf ("failed to read %s: %v" , targetPath , err )
631- }
632-
633- if err := cmd .RunDefaultBashCf ("echo %s | %s tee /proc/sys/net/ipv4/icmp_echo_ignore_all > /dev/null" , enable , cmd .SudoHandleCmd ()); err != nil {
634- return fmt .Errorf ("failed to apply ipv4 ping status temporarily: %v" , err )
635- }
636-
637- var hasIpv6 bool
638- if _ , err := os .Stat ("/proc/sys/net/ipv6/icmp/echo_ignore_all" ); err == nil {
639- hasIpv6 = true
640- if err := cmd .RunDefaultBashCf ("echo %s | %s tee /proc/sys/net/ipv6/icmp/echo_ignore_all > /dev/null" , enable , cmd .SudoHandleCmd ()); err != nil {
641- global .LOG .Warnf ("failed to apply ipv6 ping status temporarily: %v" , err )
642- }
643- }
644-
645- var files []string
646- if err == nil {
647- files = strings .Split (string (lineBytes ), "\n " )
648- }
649-
650- var newFiles []string
651- hasIPv4Line , hasIPv6Line := false , false
652-
653- for _ , line := range files {
654- if strings .HasPrefix (strings .TrimSpace (line ), "net.ipv4.icmp_echo_ignore_all" ) {
655- newFiles = append (newFiles , "net.ipv4.icmp_echo_ignore_all=" + enable )
656- hasIPv4Line = true
657- continue
658- }
659- if strings .HasPrefix (strings .TrimSpace (line ), "net.ipv6.icmp.echo_ignore_all" ) {
660- newFiles = append (newFiles , "net.ipv6.icmp.echo_ignore_all=" + enable )
661- hasIPv6Line = true
662- continue
663- }
664- newFiles = append (newFiles , line )
665- }
666-
667- if ! hasIPv4Line {
668- newFiles = append (newFiles , "net.ipv4.icmp_echo_ignore_all=" + enable )
669- }
670- if hasIpv6 && ! hasIPv6Line {
671- newFiles = append (newFiles , "net.ipv6.icmp.echo_ignore_all=" + enable )
672- }
673-
674- file , err := os .OpenFile (targetPath , os .O_WRONLY | os .O_CREATE | os .O_TRUNC , constant .FilePerm )
675- if err != nil {
676- return fmt .Errorf ("failed to open %s: %v" , targetPath , err )
677- }
678- defer file .Close ()
679-
680- if _ , err = file .WriteString (strings .Join (newFiles , "\n " )); err != nil {
681- return fmt .Errorf ("failed to write to %s: %v" , targetPath , err )
682- }
683-
684- if err := cmd .RunDefaultBashC (applyCmd ); err != nil {
685- global .LOG .Warnf ("failed to apply persistent config with '%s': %v" , applyCmd , err )
686- }
687-
688- return nil
689- }
690-
691595func (u * FirewallService ) addPortsBeforeStart (client firewall.FirewallClient ) error {
692596 if ! global .IsMaster {
693597 if err := client .Port (fireClient.FireInfo {Port : global .CONF .Base .Port , Protocol : "tcp" , Strategy : "accept" }, "add" ); err != nil {
0 commit comments