|
|
|
|
@@ -4,6 +4,7 @@ import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"net"
|
|
|
|
|
"os"
|
|
|
|
|
"strconv"
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
envChecks "git.ruekov.eu/ruakij/routingtabletowg/lib/environmentchecks"
|
|
|
|
|
@@ -20,13 +21,13 @@ var envRequired = []string{
|
|
|
|
|
var envDefaults = map[string]string{
|
|
|
|
|
"IPV6_FORMAT": "fc12::%02x%02x:%02x%02x/%d",
|
|
|
|
|
"FILTER_PREFIX": "100.100",
|
|
|
|
|
"RECHECK_INTERVAL": "5m",
|
|
|
|
|
"RECHECK_INTERVAL": "300",
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
|
// Environment-vars
|
|
|
|
|
err := envChecks.HandleRequired(envRequired)
|
|
|
|
|
if err != nil {
|
|
|
|
|
if(err != nil){
|
|
|
|
|
logger.Error.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
envChecks.HandleDefaults(envDefaults)
|
|
|
|
|
@@ -39,7 +40,7 @@ func main() {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ipv6Format := os.Getenv("IPV6_FORMAT")
|
|
|
|
|
ipv6TestStr := *convertIPv4ToIPv6(&ipv6Format, &net.IPNet{IP: net.IPv4(1, 1, 1, 1), Mask: net.CIDRMask(24, net.IPv4len)})
|
|
|
|
|
ipv6TestStr := *convertIPv4ToIPv6(&ipv6Format, &net.IPNet{IP: net.IPv4(1,1,1,1), Mask: net.CIDRMask(24, net.IPv4len)})
|
|
|
|
|
_, err = netlink.ParseIPNet(ipv6TestStr)
|
|
|
|
|
if err != nil {
|
|
|
|
|
logger.Error.Fatalf("IPV6_FORMAT is invalid: %s", err)
|
|
|
|
|
@@ -48,9 +49,36 @@ func main() {
|
|
|
|
|
filterPrefix := os.Getenv("FILTER_PREFIX")
|
|
|
|
|
|
|
|
|
|
checkIntervalStr := os.Getenv("RECHECK_INTERVAL")
|
|
|
|
|
checkInterval, err := time.ParseDuration(checkIntervalStr)
|
|
|
|
|
checkIntervalSec, err := strconv.Atoi(checkIntervalStr)
|
|
|
|
|
if err != nil {
|
|
|
|
|
logger.Error.Fatalf("Couldn't parse RECHECK_INTERVAL '%s': %s", checkIntervalStr, err)
|
|
|
|
|
logger.Error.Fatalf("Couldn't read RECHECK_INTERVAL '%s': %s", checkIntervalStr, err)
|
|
|
|
|
}
|
|
|
|
|
checkInterval := time.Second * time.Duration(checkIntervalSec)
|
|
|
|
|
|
|
|
|
|
// Get the IPv4 address of the interface
|
|
|
|
|
addrs, err := netlink.AddrList(netInterface, netlink.FAMILY_V4)
|
|
|
|
|
if err != nil {
|
|
|
|
|
logger.Error.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
if(len(addrs) == 0){
|
|
|
|
|
logger.Error.Fatal("Interface doesnt have IPv4-Adresses")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Add the IPv6 address to the interface
|
|
|
|
|
ipv6Str := *convertIPv4ToIPv6(&ipv6Format, addrs[0].IPNet)
|
|
|
|
|
ipv6, err := netlink.ParseAddr(ipv6Str)
|
|
|
|
|
if err != nil {
|
|
|
|
|
logger.Error.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
logger.Info.Printf("Adding converted %s -> %s to interface", addrs[0].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.Error.Fatalf("Failed to set address on interface: %v", err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Create a WireGuard client
|
|
|
|
|
@@ -62,44 +90,6 @@ func main() {
|
|
|
|
|
|
|
|
|
|
// Loop indefinitely
|
|
|
|
|
for {
|
|
|
|
|
// Get the IPv4 addresses of the interface
|
|
|
|
|
addrs, err := netlink.AddrList(netInterface, netlink.FAMILY_V4)
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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.Error.Fatalf("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)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get the WireGuard peers on the interface
|
|
|
|
|
wgDevice, err := client.Device(iface)
|
|
|
|
|
if err != nil {
|
|
|
|
|
@@ -135,7 +125,7 @@ func main() {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(addAllowedIPs) > 0 {
|
|
|
|
|
if(len(addAllowedIPs) > 0){
|
|
|
|
|
// Create peer-config
|
|
|
|
|
peerConfig := wgtypes.PeerConfig{
|
|
|
|
|
PublicKey: peer.PublicKey,
|
|
|
|
|
@@ -147,11 +137,11 @@ func main() {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(wgConfig.Peers) == 0 {
|
|
|
|
|
if(len(wgConfig.Peers) == 0){
|
|
|
|
|
logger.Info.Println("No changes, skipping")
|
|
|
|
|
} else {
|
|
|
|
|
err = client.ConfigureDevice(iface, wgConfig)
|
|
|
|
|
if err != nil {
|
|
|
|
|
if(err != nil){
|
|
|
|
|
logger.Error.Fatalf("Error configuring wg-device '%s': %s", iface, err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
@@ -161,7 +151,7 @@ func main() {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func convertIPv4ToIPv6(ipv6Format *string, ipv4 *net.IPNet) *string {
|
|
|
|
|
func convertIPv4ToIPv6(ipv6Format *string, ipv4 *net.IPNet) (*string) {
|
|
|
|
|
CIDR, _ := ipv4.Mask.Size()
|
|
|
|
|
// Run format
|
|
|
|
|
ipv6Str := fmt.Sprintf(*ipv6Format, (*ipv4).IP[0], (*ipv4).IP[1], (*ipv4).IP[2], (*ipv4).IP[3], net.IPv6len*8-(net.IPv4len*8-CIDR))
|
|
|
|
|
|