From 56b47e8fb6dded235206fb1431da0a39afac031e Mon Sep 17 00:00:00 2001 From: Ruakij Date: Thu, 30 Mar 2023 12:31:22 +0200 Subject: [PATCH 1/4] Move setting all routes to own func --- cmd/app/main.go | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/cmd/app/main.go b/cmd/app/main.go index 14c9eda..aad25af 100644 --- a/cmd/app/main.go +++ b/cmd/app/main.go @@ -91,22 +91,27 @@ func main() { if err != nil { logger.Error.Fatalf("Couldn't get route-entries: %s", err) } - + logger.Info.Printf("Initially setting all current routes") + syncCurrentRoutesToHandler(routeSubChan, routeList) + + select {} +} + +func syncCurrentRoutesToHandler(routeSubChan chan netlink.RouteUpdate, routeList []netlink.Route){ + for _, route := range routeList { // Ignore routes with empty gateway if(route.Gw == nil){ continue } - + // Send current routes to handler routeSubChan <- netlink.RouteUpdate{ Type: unix.RTM_NEWROUTE, Route: route, } } - - select {} } var routeUpdateTypeMapFromId = map[uint16]string{ From 74bc32fc2ce8b31ba574895d31a77bf261129230 Mon Sep 17 00:00:00 2001 From: Ruakij Date: Thu, 30 Mar 2023 12:34:18 +0200 Subject: [PATCH 2/4] Add env-var and check for periodic-sync --- cmd/app/main.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/cmd/app/main.go b/cmd/app/main.go index aad25af..f74bed9 100644 --- a/cmd/app/main.go +++ b/cmd/app/main.go @@ -3,6 +3,7 @@ package main import ( "net" "os" + "strconv" envChecks "git.ruekov.eu/ruakij/routingtabletowg/lib/environmentchecks" ip2Map "git.ruekov.eu/ruakij/routingtabletowg/lib/iproute2mapping" @@ -24,6 +25,8 @@ var envDefaults = map[string]string{ "FILTER_PROTOCOL": "-1", "FILTER_TABLE": "-1", + + "PERIODIC_SYNC": "-1", } func main() { @@ -55,6 +58,12 @@ func main() { logger.Error.Fatalf("Couldn't read FILTER_TABLE '%s': %s", filterTableStr, err) } + periodicSyncStr := os.Getenv("PERIODIC_SYNC") + periodicSync, err := strconv.Atoi(periodicSyncStr) + if err != nil { + logger.Error.Fatalf("Couldn't read PERIODIC_SYNC '%s': %s", periodicSyncStr, err) + } + // Create filter filterOptions := FilterOptions{ Table: filterTable, From add4e8dbcf4826fd5f5b62bee9f1b7b21b45eea7 Mon Sep 17 00:00:00 2001 From: Ruakij Date: Thu, 30 Mar 2023 12:39:38 +0200 Subject: [PATCH 3/4] Add function to run periodic checks --- cmd/app/main.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/cmd/app/main.go b/cmd/app/main.go index f74bed9..78798b0 100644 --- a/cmd/app/main.go +++ b/cmd/app/main.go @@ -4,6 +4,7 @@ import ( "net" "os" "strconv" + "time" envChecks "git.ruekov.eu/ruakij/routingtabletowg/lib/environmentchecks" ip2Map "git.ruekov.eu/ruakij/routingtabletowg/lib/iproute2mapping" @@ -104,9 +105,29 @@ func main() { logger.Info.Printf("Initially setting all current routes") syncCurrentRoutesToHandler(routeSubChan, routeList) + if(periodicSync > 0){ + go runPeriodicSync(periodicSync, link, routeSubChan) + } + select {} } +func runPeriodicSync(seconds int, link netlink.Link, routeSubChan chan netlink.RouteUpdate){ + interval := time.Duration(seconds) * time.Second + for { + time.Sleep(interval) + + // Get routing-table entries from device + routeList, err := netlink.RouteList(link, netlink.FAMILY_ALL) + if err != nil { + logger.Error.Fatalf("Couldn't get route-entries: %s", err) + } + + logger.Info.Printf("Periodically syncing all routes") + syncCurrentRoutesToHandler(routeSubChan, routeList) + } +} + func syncCurrentRoutesToHandler(routeSubChan chan netlink.RouteUpdate, routeList []netlink.Route){ for _, route := range routeList { From 9a700a117c1726638c81902a1606cd433c0907f2 Mon Sep 17 00:00:00 2001 From: Ruakij Date: Thu, 30 Mar 2023 12:44:09 +0200 Subject: [PATCH 4/4] Add documentation for PERIODIC_SYNC --- README.md | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index e9ac395..d3fdc87 100644 --- a/README.md +++ b/README.md @@ -38,11 +38,12 @@ In case routes clash or cant be added to Wireguard, Warnings will be logged. ### 1.2.1. Environment -Variable|Description|Default --|-|- -`INTERFACE`* | Wireguard-Interface Name | -`FILTER_PROTOCOL` | Protocol to react on | All -`FILTER_TABLE` | Table to react on | All +Variable|Description|Type|Default +-|-|-|- +`INTERFACE`* | Wireguard-Interface Name | String | +`FILTER_PROTOCOL` | Protocol to react on | Number / iproute2-name | All +`FILTER_TABLE` | Table to react on | Number / iproute2-name | All +`PERIODIC_SYNC` | Reguarly sync the routing-table
Useful when the wg-interface is changed/updated without us knowing | Seconds | -1 *\* Required*