@ -4,7 +4,6 @@ import (
"fmt"
"fmt"
"net"
"net"
"os"
"os"
"strconv"
"time"
"time"
envChecks "git.ruekov.eu/ruakij/routingtabletowg/lib/environmentchecks"
envChecks "git.ruekov.eu/ruakij/routingtabletowg/lib/environmentchecks"
@ -21,7 +20,7 @@ var envRequired = []string{
var envDefaults = map [ string ] string {
var envDefaults = map [ string ] string {
"IPV6_FORMAT" : "fc12::%02x%02x:%02x%02x/%d" ,
"IPV6_FORMAT" : "fc12::%02x%02x:%02x%02x/%d" ,
"FILTER_PREFIX" : "100.100" ,
"FILTER_PREFIX" : "100.100" ,
"RECHECK_INTERVAL" : " 300 ",
"RECHECK_INTERVAL" : " 5m ",
}
}
func main ( ) {
func main ( ) {
@ -49,36 +48,47 @@ func main() {
filterPrefix := os . Getenv ( "FILTER_PREFIX" )
filterPrefix := os . Getenv ( "FILTER_PREFIX" )
checkIntervalStr := os . Getenv ( "RECHECK_INTERVAL" )
checkIntervalStr := os . Getenv ( "RECHECK_INTERVAL" )
checkInterval Sec, err := strconv . Atoi ( checkIntervalStr )
checkInterval , err := time . ParseDuration ( checkIntervalStr )
if err != nil {
if err != nil {
logger . Error . Fatalf ( "Couldn't read RECHECK_INTERVAL '%s': %s", checkIntervalStr , err )
logger . Error . Fatalf ( "Couldn't pa rs e RECHECK_INTERVAL '%s': %s", checkIntervalStr , err )
}
}
checkInterval := time . Second * time . Duration ( checkIntervalSec )
// Get the IPv4 address of the interface
// Get the IPv4 address of the interface
addrs , err := netlink . AddrList ( netInterface , netlink . FAMILY_V4 )
addrs , err := netlink . AddrList ( netInterface , netlink . FAMILY_V4 )
if err != nil {
if err != nil {
logger . Error . Fatal ( err )
logger . Error . Fatal ( err )
}
}
if ( len ( addrs ) == 0 ) {
processedCount := 0
logger . Error . Fatal ( "Interface doesnt have IPv4-Adresses" )
filteredCount := 0
for _ , addr := range addrs {
// Check filter
if addr . String ( ) [ : len ( filterPrefix ) ] != filterPrefix {
filteredCount ++
continue
}
}
// Add the IPv6 address to the interface
// Add the IPv6 address to the interface
ipv6Str := * convertIPv4ToIPv6 ( & ipv6Format , addrs [ 0 ] . IPNet )
ipv6Str := * convertIPv4ToIPv6 ( & ipv6Format , addr . IPNet )
ipv6 , err := netlink . ParseAddr ( ipv6Str )
ipv6 , err := netlink . ParseAddr ( ipv6Str )
if err != nil {
if err != nil {
logger . Error . Fatal ( err )
logger . Warn . Printf ( "failed parsing converted %s -> %s : %s" , addr . IPNet . String ( ) , ipv6Str , err )
continue
}
}
logger . Info . Printf ( "Adding converted %s -> %s to interface" , addrs [ 0 ] . IPNet . String ( ) , ipv6Str )
logger . Info . Printf ( "Adding converted %s -> %s to interface" , addr . IPNet . String ( ) , ipv6Str )
err = netlink . AddrAdd ( netInterface , ipv6 )
err = netlink . AddrAdd ( netInterface , ipv6 )
if err != nil {
if err != nil {
switch {
switch {
case os . IsExist ( err ) :
case os . IsExist ( err ) :
logger . Warn . Println ( "Address is already set on interface" )
logger . Warn . Println ( "Address is already set on interface" )
default :
default :
logger . Error . Fatalf ( "Failed to set address on interface: %v" , err )
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 )
}
}
// Create a WireGuard client
// Create a WireGuard client