f-88: nft: traduction 1:1 sans état de l'IP flottante, table ip two #88
Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
This commit is contained in:
parent
19eeb0c281
commit
f1f4c2b629
2 changed files with 111 additions and 0 deletions
58
internal/nft/fip.go
Normal file
58
internal/nft/fip.go
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
package nft
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type FloatingIP struct {
|
||||
Iface string
|
||||
Public string
|
||||
Private string
|
||||
}
|
||||
|
||||
var fipChains = []natChain{
|
||||
{"pre-", "filter", "prerouting", "raw"},
|
||||
{"post-", "filter", "postrouting", "srcnat"},
|
||||
}
|
||||
|
||||
func (f FloatingIP) rule(c natChain) string {
|
||||
if c.hook == "prerouting" {
|
||||
return fmt.Sprintf("iifname %q ip daddr %s ip daddr set %s", f.Iface, f.Public, f.Private)
|
||||
}
|
||||
return fmt.Sprintf("oifname %q ip saddr %s ip saddr set %s", f.Iface, f.Private, f.Public)
|
||||
}
|
||||
|
||||
func fipSetScript(f FloatingIP) string {
|
||||
var b strings.Builder
|
||||
fmt.Fprintf(&b, "add table ip %s\n", Table)
|
||||
for _, c := range fipChains {
|
||||
name := c.declare(&b, f.Iface)
|
||||
fmt.Fprintf(&b, "add rule ip %s %s %s\n", Table, name, f.rule(c))
|
||||
}
|
||||
return b.String()
|
||||
}
|
||||
|
||||
func fipDeleteScript(iface string) string {
|
||||
var b strings.Builder
|
||||
fmt.Fprintf(&b, "add table ip %s\n", Table)
|
||||
for _, c := range fipChains {
|
||||
name := c.declare(&b, iface)
|
||||
fmt.Fprintf(&b, "delete chain ip %s %s\n", Table, name)
|
||||
}
|
||||
return b.String()
|
||||
}
|
||||
|
||||
func SetFloatingIP(f FloatingIP) error {
|
||||
if err := apply(fipSetScript(f)); err != nil {
|
||||
return fmt.Errorf("set floating ip %s: %w", f.Iface, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func DeleteFloatingIP(iface string) error {
|
||||
if err := apply(fipDeleteScript(iface)); err != nil {
|
||||
return fmt.Errorf("delete floating ip %s: %w", iface, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue