Compare commits
	
		
			No commits in common. "df1708152c6bff232badc9fa3b8ba415024912c7" and "1d48e2ac4edc66e8508d2b52d01b818cc0cbcdfa" have entirely different histories.
		
	
	
		
			df1708152c
			...
			1d48e2ac4e
		
	
		
@ -71,17 +71,16 @@ 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=30
 | 
					RestartSec=60
 | 
				
			||||||
 | 
					StandardOutput=syslog
 | 
				
			||||||
 | 
					StandardError=syslog
 | 
				
			||||||
 | 
					
 | 
				
			||||||
Environment="INTERFACE=wt0"
 | 
					Environment="INTERFACE=wt0"
 | 
				
			||||||
Environment="RECHECK_INTERVAL=60"
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
[Install]
 | 
					[Install]
 | 
				
			||||||
WantedBy=multi-user.target
 | 
					WantedBy=multi-user.target
 | 
				
			||||||
 | 
				
			|||||||
@ -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"
 | 
				
			||||||
@ -19,9 +18,8 @@ var envRequired = []string{
 | 
				
			|||||||
	"INTERFACE",
 | 
						"INTERFACE",
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
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/%s",
 | 
				
			||||||
    "FILTER_PREFIX": "100.100",
 | 
					    "FILTER_PREFIX": "100.100",
 | 
				
			||||||
    "RECHECK_INTERVAL": "300",
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func main() {
 | 
					func main() {
 | 
				
			||||||
@ -48,12 +46,6 @@ 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)
 | 
				
			||||||
@ -70,7 +62,6 @@ 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 {
 | 
				
			||||||
@ -107,8 +98,7 @@ 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, &allowedIP)
 | 
					                    ipv6Str := *convertIPv4ToIPv6(&ipv6Format, &net.IPNet{IP: allowedIP.IP, Mask: allowedIP.Mask})
 | 
				
			||||||
                    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)
 | 
				
			||||||
@ -146,14 +136,14 @@ func main() {
 | 
				
			|||||||
            }
 | 
					            }
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        
 | 
					        
 | 
				
			||||||
        // Sleep for x seconds before running the loop again
 | 
					        // Sleep for 300 seconds before running the loop again
 | 
				
			||||||
        time.Sleep(checkInterval)
 | 
					        time.Sleep(time.Second * 300)
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
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[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
 | 
					    return &ipv6Str
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user