Compare commits
3 Commits
dev
..
1ca4bd87b8
| Author | SHA1 | Date | |
|---|---|---|---|
| 1ca4bd87b8 | |||
| 15747c6eee | |||
| b4bdb21888 |
@@ -24,8 +24,6 @@ The program will convert IPv4-only wireguard-interfaces to IPv6. It converts and
|
||||
|
||||
IPv6-Adresses are generated based on the IPv4-Adress.
|
||||
|
||||
If not filtered out, then default routes (0.0.0.0/0) are handled specially and are converted to the IPv6 default route (::/0).
|
||||
|
||||
Beware: This program needs `NET_ADMIN` privileges for setting Adresses and to access the wireguard-daemon.
|
||||
|
||||
<br>
|
||||
|
||||
+18
-27
@@ -17,7 +17,6 @@ import (
|
||||
var envRequired = []string{
|
||||
"INTERFACE",
|
||||
}
|
||||
|
||||
var envDefaults = map[string]string{
|
||||
"IPV6_FORMAT": "fc12::%02x%02x:%02x%02x/%d",
|
||||
"FILTER_PREFIX": "100.100",
|
||||
@@ -27,7 +26,7 @@ var envDefaults = map[string]string{
|
||||
func main() {
|
||||
// Environment-vars
|
||||
err := envChecks.HandleRequired(envRequired)
|
||||
if err != nil {
|
||||
if(err != nil){
|
||||
logger.Error.Fatal(err)
|
||||
}
|
||||
envChecks.HandleDefaults(envDefaults)
|
||||
@@ -54,16 +53,7 @@ func main() {
|
||||
logger.Error.Fatalf("Couldn't parse RECHECK_INTERVAL '%s': %s", checkIntervalStr, err)
|
||||
}
|
||||
|
||||
// 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
|
||||
// Get the IPv4 address of the interface
|
||||
addrs, err := netlink.AddrList(netInterface, netlink.FAMILY_V4)
|
||||
if err != nil {
|
||||
logger.Error.Fatal(err)
|
||||
@@ -92,15 +82,24 @@ func main() {
|
||||
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)
|
||||
logger.Warn.Printf("Failed to set address on interface: %v", err)
|
||||
}
|
||||
}
|
||||
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)
|
||||
}
|
||||
|
||||
// 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
|
||||
wgDevice, err := client.Device(iface)
|
||||
if err != nil {
|
||||
@@ -112,7 +111,7 @@ func main() {
|
||||
|
||||
for _, peer := range wgDevice.Peers {
|
||||
// Create slice for 1 expected addition
|
||||
addAllowedIPs := make([]net.IPNet, 0, 1)
|
||||
var addAllowedIPs = make([]net.IPNet, 0, 1)
|
||||
|
||||
// Loop through the allowed-ips and add the ones starting with 100.100
|
||||
for _, allowedIP := range peer.AllowedIPs {
|
||||
@@ -136,7 +135,7 @@ func main() {
|
||||
}
|
||||
}
|
||||
|
||||
if len(addAllowedIPs) > 0 {
|
||||
if(len(addAllowedIPs) > 0){
|
||||
// Create peer-config
|
||||
peerConfig := wgtypes.PeerConfig{
|
||||
PublicKey: peer.PublicKey,
|
||||
@@ -148,11 +147,11 @@ func main() {
|
||||
}
|
||||
}
|
||||
|
||||
if len(wgConfig.Peers) == 0 {
|
||||
if(len(wgConfig.Peers) == 0){
|
||||
logger.Info.Println("No changes, skipping")
|
||||
} else {
|
||||
err = client.ConfigureDevice(iface, wgConfig)
|
||||
if err != nil {
|
||||
if(err != nil){
|
||||
logger.Error.Fatalf("Error configuring wg-device '%s': %s", iface, err)
|
||||
}
|
||||
}
|
||||
@@ -162,15 +161,7 @@ func main() {
|
||||
}
|
||||
}
|
||||
|
||||
func convertIPv4ToIPv6(ipv6Format *string, ipv4 *net.IPNet) *string {
|
||||
// Check if this is a default route (0.0.0.0/0)
|
||||
if ipv4.IP.Equal(net.IPv4zero) {
|
||||
if ones, _ := ipv4.Mask.Size(); ones == 0 {
|
||||
defaultRoute := "::/0"
|
||||
return &defaultRoute
|
||||
}
|
||||
}
|
||||
|
||||
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))
|
||||
|
||||
Reference in New Issue
Block a user