f-25: vms: add iptables gestion
Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
This commit is contained in:
parent
a5bf748bdf
commit
504a2a723b
3 changed files with 89 additions and 12 deletions
42
internal/iptables/iptables.go
Normal file
42
internal/iptables/iptables.go
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
package iptables
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os/exec"
|
||||
)
|
||||
|
||||
func addRule(args ...string) error {
|
||||
return exec.Command("iptables", append([]string{"-t", "nat", "-A"}, args...)...).Run()
|
||||
}
|
||||
|
||||
func deleteRule(args ...string) error {
|
||||
return exec.Command("iptables", append([]string{"-t", "nat", "-D"}, args...)...).Run()
|
||||
}
|
||||
|
||||
func AddMetadataRedirect(vmIP, gatewayIP, metadataPort string) error {
|
||||
if err := addRule("PREROUTING",
|
||||
"-s", vmIP+"/32",
|
||||
"-d", "169.254.169.254/32",
|
||||
"-p", "tcp", "-m", "tcp",
|
||||
"--dport", "80",
|
||||
"-j", "DNAT",
|
||||
"--to-destination", gatewayIP+":"+metadataPort,
|
||||
); err != nil {
|
||||
return fmt.Errorf("iptables metadata redirect: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func DeleteMetadataRedirect(vmIP, gatewayIP, metadataPort string) error {
|
||||
if err := deleteRule("PREROUTING",
|
||||
"-s", vmIP+"/32",
|
||||
"-d", "169.254.169.254/32",
|
||||
"-p", "tcp", "-m", "tcp",
|
||||
"--dport", "80",
|
||||
"-j", "DNAT",
|
||||
"--to-destination", gatewayIP+":"+metadataPort,
|
||||
); err != nil {
|
||||
return fmt.Errorf("iptables delete metadata redirect: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue