From 56b47e8fb6dded235206fb1431da0a39afac031e Mon Sep 17 00:00:00 2001 From: Ruakij Date: Thu, 30 Mar 2023 12:31:22 +0200 Subject: [PATCH 1/7] 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/7] 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/7] 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/7] 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* From 05bab9dd606e37852f4b800241be65b04d47746a Mon Sep 17 00:00:00 2001 From: Ruakij Date: Thu, 30 Mar 2023 14:25:59 +0200 Subject: [PATCH 5/7] Fix compose-example --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index d3fdc87..c22b112 100644 --- a/README.md +++ b/README.md @@ -68,7 +68,7 @@ services: cap_add: - NET_ADMIN environment: - - INTERFACE="" + - INTERFACE= ``` From 0c85d8ae1adc563e7d1ce157cc6cfe3678e012c2 Mon Sep 17 00:00:00 2001 From: Ruakij Date: Thu, 30 Mar 2023 14:28:05 +0200 Subject: [PATCH 6/7] Update dependencies --- go.mod | 20 +++++++++--------- go.sum | 67 ++++++++++++++++++---------------------------------------- 2 files changed, 30 insertions(+), 57 deletions(-) diff --git a/go.mod b/go.mod index e548eb9..1b251bb 100644 --- a/go.mod +++ b/go.mod @@ -4,19 +4,19 @@ go 1.19 require ( github.com/vishvananda/netlink v1.1.0 - golang.org/x/sys v0.1.0 - golang.zx2c4.com/wireguard/wgctrl v0.0.0-20221104135756-97bc4ad4a1cb + golang.org/x/sys v0.6.0 + golang.zx2c4.com/wireguard/wgctrl v0.0.0-20230215201556-9c5414ab4bde ) require ( github.com/google/go-cmp v0.5.9 // indirect - github.com/josharian/native v1.0.0 // indirect - github.com/mdlayher/genetlink v1.2.0 // indirect - github.com/mdlayher/netlink v1.6.2 // indirect - github.com/mdlayher/socket v0.2.3 // indirect - github.com/vishvananda/netns v0.0.0-20191106174202-0a2b9b5464df // indirect - golang.org/x/crypto v0.1.0 // indirect - golang.org/x/net v0.1.0 // indirect + github.com/josharian/native v1.1.0 // indirect + github.com/mdlayher/genetlink v1.3.1 // indirect + github.com/mdlayher/netlink v1.7.1 // indirect + github.com/mdlayher/socket v0.4.0 // indirect + github.com/vishvananda/netns v0.0.4 // indirect + golang.org/x/crypto v0.7.0 // indirect + golang.org/x/net v0.8.0 // indirect golang.org/x/sync v0.1.0 // indirect - golang.zx2c4.com/wireguard v0.0.0-20220920152132-bb719d3a6e2c // indirect + golang.zx2c4.com/wireguard v0.0.0-20230325221338-052af4a8072b // indirect ) diff --git a/go.sum b/go.sum index dddec04..33c1be1 100644 --- a/go.sum +++ b/go.sum @@ -1,56 +1,29 @@ -github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= -github.com/google/go-cmp v0.5.7/go.mod h1:n+brtR0CgQNWTVd5ZUFpTBC8YFBDLK/h/bpaJ8/DtOE= github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= -github.com/josharian/native v1.0.0 h1:Ts/E8zCSEsG17dUqv7joXJFybuMLjQfWE04tsBODTxk= -github.com/josharian/native v1.0.0/go.mod h1:7X/raswPFr05uY3HiLlYeyQntB6OO7E/d2Cu7qoaN2w= -github.com/mdlayher/genetlink v1.2.0 h1:4yrIkRV5Wfk1WfpWTcoOlGmsWgQj3OtQN9ZsbrE+XtU= -github.com/mdlayher/genetlink v1.2.0/go.mod h1:ra5LDov2KrUCZJiAtEvXXZBxGMInICMXIwshlJ+qRxQ= -github.com/mdlayher/netlink v1.6.0/go.mod h1:0o3PlBmGst1xve7wQ7j/hwpNaFaH4qCRyWCdcZk8/vA= -github.com/mdlayher/netlink v1.6.2 h1:D2zGSkvYsJ6NreeED3JiVTu1lj2sIYATqSaZlhPzUgQ= -github.com/mdlayher/netlink v1.6.2/go.mod h1:O1HXX2sIWSMJ3Qn1BYZk1yZM+7iMki/uYGGiwGyq/iU= -github.com/mdlayher/socket v0.1.1/go.mod h1:mYV5YIZAfHh4dzDVzI8x8tWLWCliuX8Mon5Awbj+qDs= -github.com/mdlayher/socket v0.2.3 h1:XZA2X2TjdOwNoNPVPclRCURoX/hokBY8nkTmRZFEheM= -github.com/mdlayher/socket v0.2.3/go.mod h1:bz12/FozYNH/VbvC3q7TRIK/Y6dH1kCKsXaUeXi/FmY= +github.com/josharian/native v1.1.0 h1:uuaP0hAbW7Y4l0ZRQ6C9zfb7Mg1mbFKry/xzDAfmtLA= +github.com/josharian/native v1.1.0/go.mod h1:7X/raswPFr05uY3HiLlYeyQntB6OO7E/d2Cu7qoaN2w= +github.com/mdlayher/genetlink v1.3.1 h1:roBiPnual+eqtRkKX2Jb8UQN5ZPWnhDCGj/wR6Jlz2w= +github.com/mdlayher/genetlink v1.3.1/go.mod h1:uaIPxkWmGk753VVIzDtROxQ8+T+dkHqOI0vB1NA9S/Q= +github.com/mdlayher/netlink v1.7.1 h1:FdUaT/e33HjEXagwELR8R3/KL1Fq5x3G5jgHLp/BTmg= +github.com/mdlayher/netlink v1.7.1/go.mod h1:nKO5CSjE/DJjVhk/TNp6vCE1ktVxEA8VEh8drhZzxsQ= +github.com/mdlayher/socket v0.4.0 h1:280wsy40IC9M9q1uPGcLBwXpcTQDtoGwVt+BNoITxIw= +github.com/mdlayher/socket v0.4.0/go.mod h1:xxFqz5GRCUN3UEOm9CZqEJsAbe1C8OwSK46NlmWuVoc= github.com/mikioh/ipaddr v0.0.0-20190404000644-d465c8ab6721 h1:RlZweED6sbSArvlE924+mUcZuXKLBHA35U7LN621Bws= github.com/vishvananda/netlink v1.1.0 h1:1iyaYNBLmP6L0220aDnYQpo1QEV4t4hJ+xEEhhJH8j0= github.com/vishvananda/netlink v1.1.0/go.mod h1:cTgwzPIzzgDAYoQrMm0EdrjRUBkTqKYppBueQtXaqoE= -github.com/vishvananda/netns v0.0.0-20191106174202-0a2b9b5464df h1:OviZH7qLw/7ZovXvuNyL3XQl8UFofeikI1NW1Gypu7k= github.com/vishvananda/netns v0.0.0-20191106174202-0a2b9b5464df/go.mod h1:JP3t17pCcGlemwknint6hfoeCVQrEMVwxRLRjXpq+BU= -golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= -golang.org/x/crypto v0.1.0 h1:MDRAIl0xIo9Io2xV565hzXHw3zVseKrJKodhohM5CjU= -golang.org/x/crypto v0.1.0/go.mod h1:RecgLatLF4+eUMCP1PoPZQb+cVrJcOPbHkTkbkB9sbw= -golang.org/x/net v0.0.0-20190503192946-f4e77d36d62c/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= -golang.org/x/net v0.0.0-20210928044308-7d9f5e0b762b/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= -golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk= -golang.org/x/net v0.0.0-20220923203811-8be639271d50/go.mod h1:YDH+HFinaLZZlnHAfSS6ZXJJ9M9t4Dl22yv3iI2vPwk= -golang.org/x/net v0.1.0 h1:hZ/3BUoy5aId7sCpA/Tc5lt8DkFgdVS2onTpJsZ/fl0= -golang.org/x/net v0.1.0/go.mod h1:Cx3nUiGt4eDBEyega/BKRp+/AlGL8hYe7U9odMt2Cco= -golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.0.0-20220923202941-7f9b1623fab7/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +github.com/vishvananda/netns v0.0.4 h1:Oeaw1EM2JMxD51g9uhtC0D7erkIjgmj8+JZc26m1YX8= +github.com/vishvananda/netns v0.0.4/go.mod h1:SpkAiCQRtJ6TvvxPnOSyH3BMl6unz3xZlaprSwhNNJM= +golang.org/x/crypto v0.7.0 h1:AvwMYaRytfdeVt3u6mLaxYtErKYjxA2OXjJ1HHq6t3A= +golang.org/x/crypto v0.7.0/go.mod h1:pYwdfH91IfpZVANVyUOhSIPZaFoJGxTFbZhFTx+dXZU= +golang.org/x/net v0.8.0 h1:Zrh2ngAOFYneWTAIAPethzeaQLuHwhuBkuV6ZiRnUaQ= +golang.org/x/net v0.8.0/go.mod h1:QVkue5JL9kW//ek3r6jTKnTFis1tRmNAW2P1shuFdJc= golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o= golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190606203320-7fc4e5ec1444/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20220128215802-99c3d69c2c27/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20220319134239-a9b59b0215f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20220728004956-3c1f35247d10/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.1.0 h1:kunALQeHf1/185U1i0GOB/fy1IPRDDpuoOOqRReG57U= -golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= -golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= -golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= -golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= -golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= -golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= -golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -golang.zx2c4.com/wireguard v0.0.0-20220920152132-bb719d3a6e2c h1:Okh6a1xpnJslG9Mn84pId1Mn+Q8cvpo4HCeeFWHo0cA= -golang.zx2c4.com/wireguard v0.0.0-20220920152132-bb719d3a6e2c/go.mod h1:enML0deDxY1ux+B6ANGiwtg0yAJi1rctkTpcHNAVPyg= -golang.zx2c4.com/wireguard/wgctrl v0.0.0-20221104135756-97bc4ad4a1cb h1:9aqVcYEDHmSNb0uOWukxV5lHV09WqiSiCuhEgWNETLY= -golang.zx2c4.com/wireguard/wgctrl v0.0.0-20221104135756-97bc4ad4a1cb/go.mod h1:mQqgjkW8GQQcJQsbBvK890TKqUK1DfKWkuBGbOkuMHQ= +golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ= +golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.zx2c4.com/wireguard v0.0.0-20230325221338-052af4a8072b h1:J1CaxgLerRR5lgx3wnr6L04cJFbWoceSK9JWBdglINo= +golang.zx2c4.com/wireguard v0.0.0-20230325221338-052af4a8072b/go.mod h1:tqur9LnfstdR9ep2LaJT4lFUl0EjlHtge+gAjmsHUG4= +golang.zx2c4.com/wireguard/wgctrl v0.0.0-20230215201556-9c5414ab4bde h1:ybF7AMzIUikL9x4LgwEmzhXtzRpKNqngme1VGDWz+Nk= +golang.zx2c4.com/wireguard/wgctrl v0.0.0-20230215201556-9c5414ab4bde/go.mod h1:mQqgjkW8GQQcJQsbBvK890TKqUK1DfKWkuBGbOkuMHQ= From 8488a9e4cdcd146e93db7fe56e8b38aafd282a3b Mon Sep 17 00:00:00 2001 From: Ruakij Date: Thu, 30 Mar 2023 14:28:19 +0200 Subject: [PATCH 7/7] move netchecks to subfolder of wgchecks --- lib/{ => wgchecks}/netchecks/netchecks.go | 0 lib/wgchecks/wgchecks.go | 2 +- 2 files changed, 1 insertion(+), 1 deletion(-) rename lib/{ => wgchecks}/netchecks/netchecks.go (100%) diff --git a/lib/netchecks/netchecks.go b/lib/wgchecks/netchecks/netchecks.go similarity index 100% rename from lib/netchecks/netchecks.go rename to lib/wgchecks/netchecks/netchecks.go diff --git a/lib/wgchecks/wgchecks.go b/lib/wgchecks/wgchecks.go index 5a7ea41..4bc69a9 100644 --- a/lib/wgchecks/wgchecks.go +++ b/lib/wgchecks/wgchecks.go @@ -4,7 +4,7 @@ import ( "fmt" "net" - "git.ruekov.eu/ruakij/routingtabletowg/lib/netchecks" + "git.ruekov.eu/ruakij/routingtabletowg/lib/wgchecks/netchecks" "golang.zx2c4.com/wireguard/wgctrl/wgtypes" )