Compare commits

..

No commits in common. "df1708152c6bff232badc9fa3b8ba415024912c7" and "1d48e2ac4edc66e8508d2b52d01b818cc0cbcdfa" have entirely different histories.

2 changed files with 8 additions and 19 deletions

View File

@ -71,17 +71,16 @@ Or using a systemd-service based on the example:
[Unit]
Description=WireGuard IPv6 converter for netbird
BindsTo=netbird.service
After=netbird.service
[Service]
Type=simple
ExecStartPre=/bin/sleep 10
ExecStart=/usr/local/bin/wg-ipv6-converter
Restart=always
RestartSec=30
RestartSec=60
StandardOutput=syslog
StandardError=syslog
Environment="INTERFACE=wt0"
Environment="RECHECK_INTERVAL=60"
[Install]
WantedBy=multi-user.target

View File

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