Compare commits

..

6 Commits

Author SHA1 Message Date
df1708152c Update systemd-example 2023-04-01 12:36:58 +02:00
f06f9f03ff Add RECHECK_INTERVAL env-var 2023-04-01 12:16:56 +02:00
a626ee3c6f Change log-msg for allowedip 2023-04-01 12:16:45 +02:00
f34522ef98 Add log-message for actions 2023-04-01 12:10:23 +02:00
2f8cfa6afa Fix conversion between IPv4 -> IPv6 CIDR 2023-04-01 12:10:09 +02:00
fb2a57288e Fix default env ipv6-format 2023-04-01 12:09:46 +02:00
2 changed files with 19 additions and 8 deletions

View File

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

View File

@ -4,6 +4,7 @@ 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"
@ -18,8 +19,9 @@ var envRequired = []string{
"INTERFACE", "INTERFACE",
} }
var envDefaults = map[string]string{ 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", "FILTER_PREFIX": "100.100",
"RECHECK_INTERVAL": "300",
} }
func main() { func main() {
@ -46,6 +48,12 @@ func main() {
filterPrefix := os.Getenv("FILTER_PREFIX") 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 // Get the IPv4 address of the interface
addrs, err := netlink.AddrList(netInterface, netlink.FAMILY_V4) addrs, err := netlink.AddrList(netInterface, netlink.FAMILY_V4)
@ -62,6 +70,7 @@ func main() {
if err != nil { if err != nil {
logger.Error.Fatal(err) logger.Error.Fatal(err)
} }
logger.Info.Printf("Adding converted %s -> %s to interface", addrs[0].IPNet.String(), ipv6Str)
err = netlink.AddrAdd(netInterface, ipv6) err = netlink.AddrAdd(netInterface, ipv6)
if err != nil { if err != nil {
switch { switch {
@ -98,7 +107,8 @@ func main() {
for _, allowedIP := range peer.AllowedIPs { for _, allowedIP := range peer.AllowedIPs {
if allowedIP.String()[:len(filterPrefix)] == filterPrefix { if allowedIP.String()[:len(filterPrefix)] == filterPrefix {
// Convert the IPv4 allowed-ip to an IPv6 address // 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) ipv6, err := netlink.ParseIPNet(ipv6Str)
if err != nil { if err != nil {
logger.Warn.Printf("Couldnt parse IPv6 address %s of peer %s: %s", ipv6Str, peer.PublicKey, err) 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 // Sleep for x seconds before running the loop again
time.Sleep(time.Second * 300) time.Sleep(checkInterval)
} }
} }
func convertIPv4ToIPv6(ipv6Format *string, ipv4 *net.IPNet) (*string) { func convertIPv4ToIPv6(ipv6Format *string, ipv4 *net.IPNet) (*string) {
CIDR, _ := ipv4.Mask.Size() CIDR, _ := ipv4.Mask.Size()
// Run format // 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 return &ipv6Str
} }