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

Function EnableIptablesForward

agent/utils/firewall/client/iptables.go:234–257  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

232}
233
234func EnableIptablesForward() error {
235 if err := cmd.WriteFileWithOptionalSudo("/proc/sys/net/ipv4/ip_forward", []byte("1"), 0644); err != nil {
236 return fmt.Errorf("failed to enable IP forwarding: %w", err)
237 }
238 if data, err := os.ReadFile("/etc/sysctl.conf"); err == nil {
239 if !strings.Contains(string(data), "net.ipv4.ip_forward") {
240 content := strings.TrimRight(string(data), "\n") + "\nnet.ipv4.ip_forward = 1\n"
241 _ = cmd.WriteFileWithOptionalSudo("/etc/sysctl.conf", []byte(content), 0644)
242 }
243 }
244 _ = cmd.NewCommandMgr().RunWithOptionalSudo("sysctl", "-p")
245
246 if err := iptables.AddChainWithAppend(iptables.NatTab, "PREROUTING", iptables.Chain1PanelPreRouting); err != nil {
247 return err
248 }
249 if err := iptables.AddChainWithAppend(iptables.NatTab, "POSTROUTING", iptables.Chain1PanelPostRouting); err != nil {
250 return err
251 }
252 if err := iptables.AddChainWithAppend(iptables.FilterTab, "FORWARD", iptables.Chain1PanelForward); err != nil {
253 return err
254 }
255
256 return nil
257}
258
259func iptablesPortForward(info Forward, operation string) error {
260 if operation != "add" && operation != "remove" {

Callers 2

EnableForwardMethod · 0.85
EnableForwardMethod · 0.85

Calls 1

RunWithOptionalSudoMethod · 0.80

Tested by

no test coverage detected