Properly setup storange handling with slices
This commit is contained in:
		
							parent
							
								
									e6edb6ae2b
								
							
						
					
					
						commit
						e980f520e2
					
				@ -59,7 +59,12 @@ func main() {
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
    err = netlink.AddrAdd(netInterface, ipv6Addr)
 | 
					    err = netlink.AddrAdd(netInterface, ipv6Addr)
 | 
				
			||||||
    if err != nil {
 | 
					    if err != nil {
 | 
				
			||||||
        logger.Error.Fatal(err)
 | 
					        switch {
 | 
				
			||||||
 | 
					        case os.IsExist(err):
 | 
				
			||||||
 | 
					            logger.Warn.Println("Address is already set on interface")
 | 
				
			||||||
 | 
					        default:
 | 
				
			||||||
 | 
					            logger.Error.Fatalf("Failed to set address on interface: %v", err)
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // Create a WireGuard client
 | 
					    // Create a WireGuard client
 | 
				
			||||||
@ -78,13 +83,11 @@ func main() {
 | 
				
			|||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		var wgConfig wgtypes.Config
 | 
							var wgConfig wgtypes.Config
 | 
				
			||||||
        wgConfig.Peers = make([]wgtypes.PeerConfig, len(wgDevice.Peers))
 | 
					        wgConfig.Peers = make([]wgtypes.PeerConfig, 0, len(wgDevice.Peers))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        for i, peer := range wgDevice.Peers {
 | 
					        for _, peer := range wgDevice.Peers {
 | 
				
			||||||
            // Create slice with initial size of 2xAllowedIPs as the max we expect
 | 
					            // Create slice for 1 expected addition
 | 
				
			||||||
            var allowedIPs = make([]net.IPNet, len(peer.AllowedIPs)*2)
 | 
					            var addAllowedIPs = make([]net.IPNet, 0, 1)
 | 
				
			||||||
            // Copy in all old entries
 | 
					 | 
				
			||||||
            copy(allowedIPs, peer.AllowedIPs)
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
            // Loop through the allowed-ips and add the ones starting with 100.100
 | 
					            // Loop through the allowed-ips and add the ones starting with 100.100
 | 
				
			||||||
            for _, allowedIP := range peer.AllowedIPs {
 | 
					            for _, allowedIP := range peer.AllowedIPs {
 | 
				
			||||||
@ -104,17 +107,26 @@ func main() {
 | 
				
			|||||||
                    }
 | 
					                    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                    // Add the IPv6 allowed-ip to the peer
 | 
					                    // Add the IPv6 allowed-ip to the peer
 | 
				
			||||||
                    allowedIPs = append(allowedIPs, *ipv6)
 | 
					                    addAllowedIPs = append(addAllowedIPs, *ipv6)
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            wgConfig.Peers[i] = wgtypes.PeerConfig{AllowedIPs: allowedIPs}
 | 
					            if(len(addAllowedIPs) > 0){
 | 
				
			||||||
 | 
					                // Create peer-config
 | 
				
			||||||
 | 
					                peerConfig := wgtypes.PeerConfig{
 | 
				
			||||||
 | 
					                    PublicKey: peer.PublicKey,
 | 
				
			||||||
 | 
					                    AllowedIPs: append(peer.AllowedIPs, addAllowedIPs...),
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                // Add entry
 | 
				
			||||||
 | 
					                wgConfig.Peers = append(wgConfig.Peers, peerConfig)
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        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)
 | 
				
			||||||
		}
 | 
					        }
 | 
				
			||||||
        
 | 
					        
 | 
				
			||||||
        // Sleep for 300 seconds before running the loop again
 | 
					        // Sleep for 300 seconds before running the loop again
 | 
				
			||||||
        time.Sleep(time.Second * 300)
 | 
					        time.Sleep(time.Second * 300)
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user