()
| 232 | } |
| 233 | |
| 234 | func 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 | |
| 259 | func iptablesPortForward(info Forward, operation string) error { |
| 260 | if operation != "add" && operation != "remove" { |
no test coverage detected