Move IP-conversion into loop

dev
Ruakij 2 years ago
parent 631b1bf12e
commit ba6342a71f

@ -53,53 +53,54 @@ func main() {
logger.Error.Fatalf("Couldn't parse RECHECK_INTERVAL '%s': %s", checkIntervalStr, err)
}
// Get the IPv4 addresses of the interface
addrs, err := netlink.AddrList(netInterface, netlink.FAMILY_V4)
// Create a WireGuard client
client, err := wgctrl.New()
if err != nil {
logger.Error.Fatal(err)
}
processedCount := 0
filteredCount := 0
for _, addr := range addrs {
// Check filter
if addr.String()[:len(filterPrefix)] != filterPrefix {
filteredCount++
continue
}
defer client.Close()
// Add the IPv6 address to the interface
ipv6Str := *convertIPv4ToIPv6(&ipv6Format, addr.IPNet)
ipv6, err := netlink.ParseAddr(ipv6Str)
// Loop indefinitely
for {
// Get the IPv4 addresses of the interface
addrs, err := netlink.AddrList(netInterface, netlink.FAMILY_V4)
if err != nil {
logger.Warn.Printf("failed parsing converted %s -> %s : %s", addr.IPNet.String(), ipv6Str, err)
continue
logger.Error.Fatal(err)
}
processedCount := 0
filteredCount := 0
for _, addr := range addrs {
// Check filter
if addr.String()[:len(filterPrefix)] != filterPrefix {
filteredCount++
continue
}
logger.Info.Printf("Adding converted %s -> %s to interface", addr.IPNet.String(), ipv6Str)
err = netlink.AddrAdd(netInterface, ipv6)
if err != nil {
switch {
case os.IsExist(err):
logger.Warn.Println("Address is already set on interface")
default:
logger.Warn.Printf("Failed to set address on interface: %v", err)
// Add the IPv6 address to the interface
ipv6Str := *convertIPv4ToIPv6(&ipv6Format, addr.IPNet)
ipv6, err := netlink.ParseAddr(ipv6Str)
if err != nil {
logger.Warn.Printf("failed parsing converted %s -> %s : %s", addr.IPNet.String(), ipv6Str, err)
continue
}
}
processedCount++
}
if(processedCount != len(addrs)) {
logger.Warn.Printf("Not all Interface-Addresses were processed. Summary: %d processed, %d filtered, %d failed", processedCount, filteredCount, len(addrs)-processedCount-filteredCount)
}
// Create a WireGuard client
client, err := wgctrl.New()
if err != nil {
logger.Error.Fatal(err)
}
defer client.Close()
logger.Info.Printf("Adding converted %s -> %s to interface", addr.IPNet.String(), ipv6Str)
err = netlink.AddrAdd(netInterface, ipv6)
if err != nil {
switch {
case os.IsExist(err):
logger.Warn.Println("Address is already set on interface")
default:
logger.Warn.Printf("Failed to set address on interface: %v", err)
}
}
processedCount++
}
if processedCount != len(addrs) {
logger.Warn.Printf("Not all Interface-Addresses were processed. Summary: %d processed, %d filtered, %d failed", processedCount, filteredCount, len(addrs)-processedCount-filteredCount)
}
// Loop indefinitely
for {
// Get the WireGuard peers on the interface
wgDevice, err := client.Device(iface)
if err != nil {

Loading…
Cancel
Save