|
|
|
|
@@ -4,6 +4,7 @@ import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"net"
|
|
|
|
|
"os"
|
|
|
|
|
"strconv"
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
envChecks "git.ruekov.eu/ruakij/routingtabletowg/lib/environmentchecks"
|
|
|
|
|
@@ -18,8 +19,9 @@ var envRequired = []string{
|
|
|
|
|
"INTERFACE",
|
|
|
|
|
}
|
|
|
|
|
var envDefaults = map[string]string{
|
|
|
|
|
"IPV6_FORMAT": "fc12::%02x%02x:%02x%02x/%s",
|
|
|
|
|
"IPV6_FORMAT": "fc12::%02x%02x:%02x%02x/%d",
|
|
|
|
|
"FILTER_PREFIX": "100.100",
|
|
|
|
|
"RECHECK_INTERVAL": "300",
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
|
@@ -46,6 +48,12 @@ func main() {
|
|
|
|
|
|
|
|
|
|
filterPrefix := os.Getenv("FILTER_PREFIX")
|
|
|
|
|
|
|
|
|
|
checkIntervalStr := os.Getenv("RECHECK_INTERVAL")
|
|
|
|
|
checkIntervalSec, err := strconv.Atoi(checkIntervalStr)
|
|
|
|
|
if err != nil {
|
|
|
|
|
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)
|
|
|
|
|
@@ -62,6 +70,7 @@ func main() {
|
|
|
|
|
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 {
|
|
|
|
|
@@ -98,7 +107,8 @@ func main() {
|
|
|
|
|
for _, allowedIP := range peer.AllowedIPs {
|
|
|
|
|
if allowedIP.String()[:len(filterPrefix)] == filterPrefix {
|
|
|
|
|
// Convert the IPv4 allowed-ip to an IPv6 address
|
|
|
|
|
ipv6Str := *convertIPv4ToIPv6(&ipv6Format, &net.IPNet{IP: allowedIP.IP, Mask: allowedIP.Mask})
|
|
|
|
|
ipv6Str := *convertIPv4ToIPv6(&ipv6Format, &allowedIP)
|
|
|
|
|
logger.Info.Printf("AllowedIP %s -> %s to peer %s", allowedIP.String(), ipv6Str, peer.PublicKey)
|
|
|
|
|
ipv6, err := netlink.ParseIPNet(ipv6Str)
|
|
|
|
|
if err != nil {
|
|
|
|
|
logger.Warn.Printf("Couldnt parse IPv6 address %s of peer %s: %s", ipv6Str, peer.PublicKey, err)
|
|
|
|
|
@@ -136,14 +146,14 @@ func main() {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Sleep for 300 seconds before running the loop again
|
|
|
|
|
time.Sleep(time.Second * 300)
|
|
|
|
|
// Sleep for x seconds before running the loop again
|
|
|
|
|
time.Sleep(checkInterval)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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[4], 128-CIDR)
|
|
|
|
|
ipv6Str := fmt.Sprintf(*ipv6Format, (*ipv4).IP[0], (*ipv4).IP[1], (*ipv4).IP[2], (*ipv4).IP[3], net.IPv6len*8-(net.IPv4len*8-CIDR))
|
|
|
|
|
return &ipv6Str
|
|
|
|
|
}
|
|
|
|
|
|