MCPcopy Create free account
hub / github.com/1Panel-dev/1Panel / UpdatePingStatus

Function UpdatePingStatus

agent/utils/firewall/client.go:75–147  ·  view source on GitHub ↗
(enable string)

Source from the content-addressed store, hash-verified

73}
74
75func UpdatePingStatus(enable string) error {
76 const confPath = "/etc/sysctl.conf"
77 const panelSysctlPath = "/etc/sysctl.d/98-onepanel.conf"
78
79 var targetPath string
80 applyArgs := []string{"-p"}
81
82 if _, err := os.Stat(confPath); os.IsNotExist(err) {
83 targetPath = panelSysctlPath
84 applyArgs = []string{"--system"}
85 if err := cmd.NewCommandMgr().RunWithOptionalSudo("mkdir", "-p", "/etc/sysctl.d"); err != nil {
86 return fmt.Errorf("failed to create directory /etc/sysctl.d: %v", err)
87 }
88 } else {
89 targetPath = confPath
90 }
91
92 lineBytes, err := os.ReadFile(targetPath)
93 if err != nil && !os.IsNotExist(err) {
94 return fmt.Errorf("failed to read %s: %v", targetPath, err)
95 }
96
97 if err := cmd.WriteFileWithOptionalSudo("/proc/sys/net/ipv4/icmp_echo_ignore_all", []byte(enable), constant.FilePerm); err != nil {
98 return fmt.Errorf("failed to apply ipv4 ping status temporarily: %v", err)
99 }
100
101 var hasIpv6 bool
102 if _, err := os.Stat("/proc/sys/net/ipv6/icmp/echo_ignore_all"); err == nil {
103 hasIpv6 = true
104 if err := cmd.WriteFileWithOptionalSudo("/proc/sys/net/ipv6/icmp/echo_ignore_all", []byte(enable), constant.FilePerm); err != nil {
105 global.LOG.Warnf("failed to apply ipv6 ping status temporarily: %v", err)
106 }
107 }
108
109 var files []string
110 if err == nil {
111 files = strings.Split(string(lineBytes), "\n")
112 }
113
114 var newFiles []string
115 hasIPv4Line, hasIPv6Line := false, false
116
117 for _, line := range files {
118 if strings.HasPrefix(strings.TrimSpace(line), "net.ipv4.icmp_echo_ignore_all") {
119 newFiles = append(newFiles, "net.ipv4.icmp_echo_ignore_all="+enable)
120 hasIPv4Line = true
121 continue
122 }
123 if strings.HasPrefix(strings.TrimSpace(line), "net.ipv6.icmp.echo_ignore_all") {
124 newFiles = append(newFiles, "net.ipv6.icmp.echo_ignore_all="+enable)
125 hasIPv6Line = true
126 continue
127 }
128 newFiles = append(newFiles, line)
129 }
130
131 if !hasIPv4Line {
132 newFiles = append(newFiles, "net.ipv4.icmp_echo_ignore_all="+enable)

Callers

nothing calls this directly

Calls 2

RunWithOptionalSudoMethod · 0.80
StatMethod · 0.45

Tested by

no test coverage detected