Compare commits
8 Commits
f136bcb207
...
36e8aa9b87
Author | SHA1 | Date | |
---|---|---|---|
36e8aa9b87 | |||
9a700a117c | |||
add4e8dbcf | |||
74bc32fc2c | |||
56b47e8fb6 | |||
d5886fcb4a | |||
180f11cb86 | |||
d0e6fa65d2 |
@ -2,6 +2,9 @@
|
|||||||
FROM alpine:3 AS base
|
FROM alpine:3 AS base
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
|
|
||||||
|
# Install necessary packages
|
||||||
|
RUN apk add --no-cache iproute2
|
||||||
|
|
||||||
|
|
||||||
# ---- Build ----
|
# ---- Build ----
|
||||||
FROM golang:1.19-alpine AS build
|
FROM golang:1.19-alpine AS build
|
||||||
|
11
README.md
11
README.md
@ -38,11 +38,12 @@ In case routes clash or cant be added to Wireguard, Warnings will be logged.
|
|||||||
|
|
||||||
### 1.2.1. Environment
|
### 1.2.1. Environment
|
||||||
|
|
||||||
Variable|Description|Default
|
Variable|Description|Type|Default
|
||||||
-|-|-
|
-|-|-|-
|
||||||
`INTERFACE`* | Wireguard-Interface Name |
|
`INTERFACE`* | Wireguard-Interface Name | String |
|
||||||
`FILTER_PROTOCOL` | Protocol to react on | All
|
`FILTER_PROTOCOL` | Protocol to react on | Number / iproute2-name | All
|
||||||
`FILTER_TABLE` | Table to react on | All
|
`FILTER_TABLE` | Table to react on | Number / iproute2-name | All
|
||||||
|
`PERIODIC_SYNC` | Reguarly sync the routing-table <br> Useful when the wg-interface is changed/updated without us knowing | Seconds | -1
|
||||||
|
|
||||||
*\* Required*
|
*\* Required*
|
||||||
|
|
||||||
|
@ -3,6 +3,8 @@ package main
|
|||||||
import (
|
import (
|
||||||
"net"
|
"net"
|
||||||
"os"
|
"os"
|
||||||
|
"strconv"
|
||||||
|
"time"
|
||||||
|
|
||||||
envChecks "git.ruekov.eu/ruakij/routingtabletowg/lib/environmentchecks"
|
envChecks "git.ruekov.eu/ruakij/routingtabletowg/lib/environmentchecks"
|
||||||
ip2Map "git.ruekov.eu/ruakij/routingtabletowg/lib/iproute2mapping"
|
ip2Map "git.ruekov.eu/ruakij/routingtabletowg/lib/iproute2mapping"
|
||||||
@ -24,6 +26,8 @@ var envDefaults = map[string]string{
|
|||||||
|
|
||||||
"FILTER_PROTOCOL": "-1",
|
"FILTER_PROTOCOL": "-1",
|
||||||
"FILTER_TABLE": "-1",
|
"FILTER_TABLE": "-1",
|
||||||
|
|
||||||
|
"PERIODIC_SYNC": "-1",
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@ -37,6 +41,11 @@ func main() {
|
|||||||
iface := os.Getenv("INTERFACE")
|
iface := os.Getenv("INTERFACE")
|
||||||
//MANAGE_ALL = os.Getenv("MANAGE_ALL")
|
//MANAGE_ALL = os.Getenv("MANAGE_ALL")
|
||||||
|
|
||||||
|
// Check if ip2Map has init-errors
|
||||||
|
for _, err := range ip2Map.Errors {
|
||||||
|
logger.Warn.Printf("iproute2mapping: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
// Parse filter-env-vars
|
// Parse filter-env-vars
|
||||||
filterProtocolStr := os.Getenv("FILTER_PROTOCOL")
|
filterProtocolStr := os.Getenv("FILTER_PROTOCOL")
|
||||||
filterProtocol, err := ip2Map.TryGetId(ip2Map.PROTOCOL, filterProtocolStr)
|
filterProtocol, err := ip2Map.TryGetId(ip2Map.PROTOCOL, filterProtocolStr)
|
||||||
@ -50,6 +59,12 @@ func main() {
|
|||||||
logger.Error.Fatalf("Couldn't read FILTER_TABLE '%s': %s", filterTableStr, err)
|
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
|
// Create filter
|
||||||
filterOptions := FilterOptions{
|
filterOptions := FilterOptions{
|
||||||
Table: filterTable,
|
Table: filterTable,
|
||||||
@ -86,22 +101,47 @@ func main() {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Error.Fatalf("Couldn't get route-entries: %s", err)
|
logger.Error.Fatalf("Couldn't get route-entries: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.Info.Printf("Initially setting all current routes")
|
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 {
|
for _, route := range routeList {
|
||||||
// Ignore routes with empty gateway
|
// Ignore routes with empty gateway
|
||||||
if(route.Gw == nil){
|
if(route.Gw == nil){
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
// Send current routes to handler
|
// Send current routes to handler
|
||||||
routeSubChan <- netlink.RouteUpdate{
|
routeSubChan <- netlink.RouteUpdate{
|
||||||
Type: unix.RTM_NEWROUTE,
|
Type: unix.RTM_NEWROUTE,
|
||||||
Route: route,
|
Route: route,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
select {}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var routeUpdateTypeMapFromId = map[uint16]string{
|
var routeUpdateTypeMapFromId = map[uint16]string{
|
||||||
|
@ -32,9 +32,9 @@ func init() {
|
|||||||
for mapType, filePath := range filePaths{
|
for mapType, filePath := range filePaths{
|
||||||
ByName[mapType], ById[mapType], err = readFromFile(filePath)
|
ByName[mapType], ById[mapType], err = readFromFile(filePath)
|
||||||
if(err != nil){
|
if(err != nil){
|
||||||
Errors = []error{
|
Errors = append(Errors,
|
||||||
fmt.Errorf("failed reading iproute2 mapping-file '%s': %s", filePath, err),
|
fmt.Errorf("failed reading mapping-file '%s': %s", filePath, err),
|
||||||
};
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user