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

Loading…
Cancel
Save