104 lines
2.2 KiB
Go
Raw Normal View History

2024-09-06 14:24:55 +03:00
package netfilterHelper
import (
"fmt"
2024-10-22 23:23:07 +03:00
"net"
2024-09-06 14:24:55 +03:00
"strconv"
2024-10-22 23:23:07 +03:00
"github.com/coreos/go-iptables/iptables"
"github.com/vishvananda/netlink"
2024-09-06 14:24:55 +03:00
)
type PortRemap struct {
IPTables *iptables.IPTables
ChainName string
2024-10-22 23:23:07 +03:00
Addresses []netlink.Addr
2024-09-06 14:24:55 +03:00
From uint16
To uint16
Enabled bool
}
2024-09-06 16:50:56 +03:00
func (r *PortRemap) PutIPTable(table string) error {
if table == "all" || table == "nat" {
err := r.IPTables.ClearChain("nat", r.ChainName)
if err != nil {
return fmt.Errorf("failed to clear chain: %w", err)
}
2024-09-06 14:24:55 +03:00
2024-10-22 23:23:07 +03:00
for _, addr := range r.Addresses {
var addrIP net.IP
iptablesProtocol := r.IPTables.Proto()
if (iptablesProtocol == iptables.ProtocolIPv4 && len(addr.IP) == net.IPv4len) || (iptablesProtocol == iptables.ProtocolIPv6 && len(addr.IP) == net.IPv6len) {
addrIP = addr.IP
}
if addrIP == nil {
continue
}
err = r.IPTables.AppendUnique("nat", r.ChainName, "-p", "udp", "-d", addrIP.String(), "--dport", strconv.Itoa(int(r.From)), "-j", "DNAT", "--to-destination", fmt.Sprintf(":%d", r.To))
if err != nil {
return fmt.Errorf("failed to create rule: %w", err)
}
2024-09-06 16:50:56 +03:00
}
err = r.IPTables.InsertUnique("nat", "PREROUTING", 1, "-j", r.ChainName)
if err != nil {
return fmt.Errorf("failed to linking chain: %w", err)
}
2024-09-06 14:24:55 +03:00
}
2024-09-06 16:50:56 +03:00
return nil
}
func (r *PortRemap) ForceEnable() error {
err := r.PutIPTable("all")
2024-09-06 14:24:55 +03:00
if err != nil {
2024-09-06 16:50:56 +03:00
return err
2024-09-06 14:24:55 +03:00
}
r.Enabled = true
return nil
}
func (r *PortRemap) Disable() []error {
var errs []error
err := r.IPTables.DeleteIfExists("nat", "PREROUTING", "-j", r.ChainName)
if err != nil {
errs = append(errs, fmt.Errorf("failed to unlinking chain: %w", err))
}
err = r.IPTables.ClearAndDeleteChain("nat", r.ChainName)
if err != nil {
errs = append(errs, fmt.Errorf("failed to delete chain: %w", err))
}
r.Enabled = false
return errs
}
func (r *PortRemap) Enable() error {
if r.Enabled {
return nil
}
err := r.ForceEnable()
if err != nil {
r.Disable()
return err
}
return nil
}
2024-10-22 23:23:07 +03:00
func (nh *NetfilterHelper) PortRemap(name string, from, to uint16, addr []netlink.Addr) *PortRemap {
2024-09-06 14:24:55 +03:00
return &PortRemap{
IPTables: nh.IPTables,
ChainName: name,
2024-10-22 23:23:07 +03:00
Addresses: addr,
2024-09-06 14:24:55 +03:00
From: from,
To: to,
}
}