Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
Ruakij committed Apr 11, 2023
2 parents b4bdb21 + 34bfe19 commit 15747c6
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 23 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ Variable|Description|Default
-|-|-
`INTERFACE`* | Wireguard-Interface Name |
`IPV6_FORMAT` | Format to use for converting v4 to v6 <br> The CIDR-Mask gets translated using 128 - 24 - Mask <br> e.g. `10.0.100.5/16` -> `fc12::0a00:6405/96` | `fc12::%02x%02x:%02x%02x/%d`
`RECHECK_INTERVAL` | Interval in seconds to recheck AllowedIPs entries in case something changed | 300
`FILTER_PREFIX` | Prefix to filter for IP-Networks | `100.100`
`RECHECK_INTERVAL` | Interval in go-time-format to recheck AllowedIPs entries in case something changed | 5m

*\* Required*

Expand Down
13 changes: 13 additions & 0 deletions build/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# ---- Build ----
FROM golang:1.19-alpine AS build
WORKDIR /build
# Copy sources
ADD . .
# Get dependencies
RUN go get ./cmd/app
# Compile
RUN CGO_ENABLED=0 go build -a -o app ./cmd/app

# ---- Output ----
FROM scratch AS export-stage
COPY --from=build /build/app .
9 changes: 9 additions & 0 deletions build/build-multiarch.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
PLATFORM="linux/amd64,linux/arm64/v8,linux/arm/v7"
EXTRA_ARGS="$@"

docker buildx build \
--platform $PLATFORM \
-f $(dirname $0)/Dockerfile \
--output out \
$EXTRA_ARGS \
.
7 changes: 7 additions & 0 deletions build/build-ownarch.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
EXTRA_ARGS="$@"

docker build \
-f $(dirname $0)/Dockerfile \
--output out \
$EXTRA_ARGS \
.
54 changes: 32 additions & 22 deletions cmd/app/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"
"net"
"os"
"strconv"
"time"

envChecks "git.ruekov.eu/ruakij/routingtabletowg/lib/environmentchecks"
Expand All @@ -21,7 +20,7 @@ var envRequired = []string{
var envDefaults = map[string]string{
"IPV6_FORMAT": "fc12::%02x%02x:%02x%02x/%d",
"FILTER_PREFIX": "100.100",
"RECHECK_INTERVAL": "300",
"RECHECK_INTERVAL": "5m",
}

func main() {
Expand Down Expand Up @@ -49,36 +48,47 @@ func main() {
filterPrefix := os.Getenv("FILTER_PREFIX")

checkIntervalStr := os.Getenv("RECHECK_INTERVAL")
checkIntervalSec, err := strconv.Atoi(checkIntervalStr)
checkInterval, err := time.ParseDuration(checkIntervalStr)
if err != nil {
logger.Error.Fatalf("Couldn't read RECHECK_INTERVAL '%s': %s", checkIntervalStr, err)
logger.Error.Fatalf("Couldn't parse RECHECK_INTERVAL '%s': %s", checkIntervalStr, err)
}
checkInterval := time.Second * time.Duration(checkIntervalSec)

// Get the IPv4 address of the interface
addrs, err := netlink.AddrList(netInterface, netlink.FAMILY_V4)
if err != nil {
logger.Error.Fatal(err)
}
if(len(addrs) == 0){
logger.Error.Fatal("Interface doesnt have IPv4-Adresses")
}
processedCount := 0
filteredCount := 0
for _, addr := range addrs {
// Check filter
if addr.String()[:len(filterPrefix)] != filterPrefix {
filteredCount++
continue
}

// Add the IPv6 address to the interface
ipv6Str := *convertIPv4ToIPv6(&ipv6Format, addrs[0].IPNet)
ipv6, err := netlink.ParseAddr(ipv6Str)
if err != nil {
logger.Error.Fatal(err)
}
logger.Info.Printf("Adding converted %s -> %s to interface", addrs[0].IPNet.String(), ipv6Str)
err = netlink.AddrAdd(netInterface, ipv6)
if err != nil {
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)
// Add the IPv6 address to the interface
ipv6Str := *convertIPv4ToIPv6(&ipv6Format, addr.IPNet)
ipv6, err := netlink.ParseAddr(ipv6Str)
if err != nil {
logger.Warn.Printf("failed parsing converted %s -> %s : %s", addr.IPNet.String(), ipv6Str, err)
continue
}

logger.Info.Printf("Adding converted %s -> %s to interface", addr.IPNet.String(), ipv6Str)
err = netlink.AddrAdd(netInterface, ipv6)
if err != nil {
switch {
case os.IsExist(err):
logger.Warn.Println("Address is already set on interface")
default:
logger.Warn.Printf("Failed to set address on interface: %v", err)
}
}
processedCount++
}
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
Expand Down

0 comments on commit 15747c6

Please sign in to comment.