Merge branch 'dev'

main 0.3
Ruakij 2 years ago
commit 80ee221226

@ -26,7 +26,7 @@ var envDefaults = map[string]string{
func main() { func main() {
// Environment-vars // Environment-vars
err := envChecks.HandleRequired(envRequired) err := envChecks.HandleRequired(envRequired)
if(err != nil){ if err != nil {
logger.Error.Fatal(err) logger.Error.Fatal(err)
} }
envChecks.HandleDefaults(envDefaults) envChecks.HandleDefaults(envDefaults)
@ -39,7 +39,7 @@ func main() {
} }
ipv6Format := os.Getenv("IPV6_FORMAT") 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) _, err = netlink.ParseIPNet(ipv6TestStr)
if err != nil { if err != nil {
logger.Error.Fatalf("IPV6_FORMAT is invalid: %s", err) logger.Error.Fatalf("IPV6_FORMAT is invalid: %s", err)
@ -53,7 +53,16 @@ func main() {
logger.Error.Fatalf("Couldn't parse RECHECK_INTERVAL '%s': %s", checkIntervalStr, err) logger.Error.Fatalf("Couldn't parse RECHECK_INTERVAL '%s': %s", checkIntervalStr, err)
} }
// Get the IPv4 address of the interface // Create a WireGuard client
client, err := wgctrl.New()
if err != nil {
logger.Error.Fatal(err)
}
defer client.Close()
// Loop indefinitely
for {
// Get the IPv4 addresses of the interface
addrs, err := netlink.AddrList(netInterface, netlink.FAMILY_V4) addrs, err := netlink.AddrList(netInterface, netlink.FAMILY_V4)
if err != nil { if err != nil {
logger.Error.Fatal(err) logger.Error.Fatal(err)
@ -82,24 +91,15 @@ func main() {
case os.IsExist(err): case os.IsExist(err):
logger.Warn.Println("Address is already set on interface") logger.Warn.Println("Address is already set on interface")
default: default:
logger.Warn.Printf("Failed to set address on interface: %v", err) logger.Error.Fatalf("Failed to set address on interface: %v", err)
} }
} }
processedCount++ processedCount++
} }
if(processedCount != len(addrs)) { 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) logger.Warn.Printf("Not all Interface-Addresses were processed. Summary: %d processed, %d filtered, %d failed", processedCount, filteredCount, len(addrs)-processedCount-filteredCount)
} }
// Create a WireGuard client
client, err := wgctrl.New()
if err != nil {
logger.Error.Fatal(err)
}
defer client.Close()
// Loop indefinitely
for {
// Get the WireGuard peers on the interface // Get the WireGuard peers on the interface
wgDevice, err := client.Device(iface) wgDevice, err := client.Device(iface)
if err != nil { if err != nil {
@ -135,7 +135,7 @@ func main() {
} }
} }
if(len(addAllowedIPs) > 0){ if len(addAllowedIPs) > 0 {
// Create peer-config // Create peer-config
peerConfig := wgtypes.PeerConfig{ peerConfig := wgtypes.PeerConfig{
PublicKey: peer.PublicKey, PublicKey: peer.PublicKey,
@ -147,11 +147,11 @@ func main() {
} }
} }
if(len(wgConfig.Peers) == 0){ if len(wgConfig.Peers) == 0 {
logger.Info.Println("No changes, skipping") logger.Info.Println("No changes, skipping")
} else { } else {
err = client.ConfigureDevice(iface, wgConfig) err = client.ConfigureDevice(iface, wgConfig)
if(err != nil){ if err != nil {
logger.Error.Fatalf("Error configuring wg-device '%s': %s", iface, err) logger.Error.Fatalf("Error configuring wg-device '%s': %s", iface, err)
} }
} }
@ -161,7 +161,7 @@ func main() {
} }
} }
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[3], net.IPv6len*8-(net.IPv4len*8-CIDR))

Loading…
Cancel
Save