gofmt
This commit is contained in:
@@ -10,6 +10,7 @@ type Log struct {
|
||||
Warn log.Logger
|
||||
Error log.Logger
|
||||
}
|
||||
|
||||
var logger Log = Log{
|
||||
Info: *log.New(os.Stdout, "[INFO]\t", log.Ltime|log.Lshortfile),
|
||||
Warn: *log.New(os.Stderr, "[WARN]\t", log.Ltime|log.Lshortfile),
|
||||
|
||||
+8
-8
@@ -33,7 +33,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)
|
||||
@@ -88,7 +88,6 @@ func main() {
|
||||
logger.Error.Fatalf("Couldn't get wg-interface '%s': %s", iface, err)
|
||||
}
|
||||
|
||||
|
||||
// Subscribe to route-change events
|
||||
routeSubChan, routeSubDoneChan := make(chan netlink.RouteUpdate), make(chan struct{})
|
||||
|
||||
@@ -105,7 +104,7 @@ func main() {
|
||||
logger.Info.Printf("Initially setting all current routes")
|
||||
syncCurrentRoutesToHandler(routeSubChan, routeList)
|
||||
|
||||
if(periodicSync > 0){
|
||||
if periodicSync > 0 {
|
||||
go runPeriodicSync(periodicSync, link, routeSubChan)
|
||||
}
|
||||
|
||||
@@ -132,7 +131,7 @@ func syncCurrentRoutesToHandler(routeSubChan chan netlink.RouteUpdate, routeList
|
||||
|
||||
for _, route := range routeList {
|
||||
// Ignore routes with empty gateway
|
||||
if(route.Gw == nil){
|
||||
if route.Gw == nil {
|
||||
continue
|
||||
}
|
||||
|
||||
@@ -148,6 +147,7 @@ var routeUpdateTypeMapFromId = map[uint16]string{
|
||||
unix.RTM_NEWROUTE: "+",
|
||||
unix.RTM_DELROUTE: "-",
|
||||
}
|
||||
|
||||
// TODO: Add proxy to apply filter in channels rather than.. this mess
|
||||
func handleRouteEvents(routeSubChan <-chan netlink.RouteUpdate, filterOptions FilterOptions, iface string) {
|
||||
// Create wg-client
|
||||
@@ -162,7 +162,7 @@ func handleRouteEvents(routeSubChan <-chan netlink.RouteUpdate, filterOptions Fi
|
||||
route := routeUpdate.Route
|
||||
|
||||
// Check filter
|
||||
if(!CheckFilter(filterOptions, routeUpdate.Route)){
|
||||
if !CheckFilter(filterOptions, routeUpdate.Route) {
|
||||
continue
|
||||
}
|
||||
|
||||
@@ -208,7 +208,7 @@ func handleRouteEvents(routeSubChan <-chan netlink.RouteUpdate, filterOptions Fi
|
||||
|
||||
// Get peer containing gateway-addr
|
||||
peer, err := wgChecks.PeerByIP(&wgDevice.Peers, &route.Gw)
|
||||
if(err != nil){
|
||||
if err != nil {
|
||||
logger.Warn.Printf("No peer found containing gw-IP '%s', ignoring", route.Gw)
|
||||
continue
|
||||
}
|
||||
@@ -224,7 +224,7 @@ func handleRouteEvents(routeSubChan <-chan netlink.RouteUpdate, filterOptions Fi
|
||||
case unix.RTM_DELROUTE:
|
||||
// Get peer containing dst-NetIP
|
||||
peerIndex, ipNetIndex, err := wgChecks.PeerIndexByIPNet(&wgDevice.Peers, route.Dst)
|
||||
if(err != nil){
|
||||
if err != nil {
|
||||
logger.Warn.Printf("No peer found having dst-IPNet '%s', ignoring", route.Dst)
|
||||
continue
|
||||
}
|
||||
@@ -247,7 +247,7 @@ func handleRouteEvents(routeSubChan <-chan netlink.RouteUpdate, filterOptions Fi
|
||||
}
|
||||
|
||||
err = client.ConfigureDevice(iface, wgConfig)
|
||||
if(err != nil){
|
||||
if err != nil {
|
||||
logger.Error.Fatalf("Error configuring wg-device '%s': %s", iface, err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,15 +1,15 @@
|
||||
package environmentchecks
|
||||
|
||||
import (
|
||||
"os"
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
func HandleDefaults(envDefaults map[string]string) {
|
||||
for env, defaultValue := range envDefaults {
|
||||
// Check if env is set
|
||||
_, isSet := os.LookupEnv(env)
|
||||
if(!isSet){
|
||||
if !isSet {
|
||||
os.Setenv(env, defaultValue)
|
||||
}
|
||||
}
|
||||
@@ -19,7 +19,7 @@ func HandleRequired(envRequired []string) error {
|
||||
for _, env := range envRequired {
|
||||
// Check if env is set
|
||||
_, isSet := os.LookupEnv(env)
|
||||
if(!isSet){
|
||||
if !isSet {
|
||||
return fmt.Errorf("env '%s' required, but not set", env)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
package iproute2mapping
|
||||
|
||||
import (
|
||||
"os"
|
||||
"bufio"
|
||||
"strings"
|
||||
"strconv"
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Storage for mapping
|
||||
@@ -25,24 +25,24 @@ var filePaths = map[int]string{
|
||||
}
|
||||
|
||||
// Export error-check
|
||||
var Errors []error;
|
||||
var Errors []error
|
||||
|
||||
func init() {
|
||||
var err error
|
||||
for mapType, filePath := range filePaths {
|
||||
ByName[mapType], ById[mapType], err = readFromFile(filePath)
|
||||
if(err != nil){
|
||||
if err != nil {
|
||||
Errors = append(Errors,
|
||||
fmt.Errorf("failed reading mapping-file '%s': %s", filePath, err),
|
||||
);
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func readFromFile(filePath string) (mapByName map[string]int, mapById map[int]string, err error) {
|
||||
file, err := os.Open(filePath)
|
||||
if(err != nil){
|
||||
return nil, nil, err;
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
@@ -55,12 +55,12 @@ func readFromFile(filePath string) (mapByName map[string]int, mapById map[int]st
|
||||
// Go through file line-by-line
|
||||
for scanner.Scan() {
|
||||
text := scanner.Text()
|
||||
if(text == "" || strings.HasPrefix(text, "#")){
|
||||
if text == "" || strings.HasPrefix(text, "#") {
|
||||
continue
|
||||
}
|
||||
|
||||
id, name, err := getMappingFromText(text)
|
||||
if(err != nil){
|
||||
if err != nil {
|
||||
// Only warn as we can continue processing the file
|
||||
Errors = append(Errors,
|
||||
fmt.Errorf("mappig-data invalid '%s': %s", text, err),
|
||||
@@ -80,7 +80,7 @@ func getMappingFromText(text string) (int, string, error) {
|
||||
data := strings.Split(text, "\t")
|
||||
|
||||
id, err := strconv.Atoi(data[0])
|
||||
if(err != nil){
|
||||
if err != nil {
|
||||
return 0, "", err
|
||||
}
|
||||
|
||||
@@ -91,10 +91,10 @@ func getMappingFromText(text string) (int, string, error) {
|
||||
func TryGetId(mappingType int, name string) (int, error) {
|
||||
// Try to convert name to id
|
||||
id, err := strconv.Atoi(name)
|
||||
if(err != nil){ // name given -> Convert to id
|
||||
if err != nil { // name given -> Convert to id
|
||||
var found bool
|
||||
id, found = ByName[mappingType][name]
|
||||
if(!found){
|
||||
if !found {
|
||||
return 0, fmt.Errorf("no id found from name")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,2 +1 @@
|
||||
package iproute2mapping
|
||||
|
||||
|
||||
@@ -19,7 +19,7 @@ func PeerIndexByIP(peers *[]wgtypes.Peer, ip *net.IP) (int, int, error) {
|
||||
}
|
||||
func PeerByIP(peers *[]wgtypes.Peer, ip *net.IP) (*wgtypes.Peer, error) {
|
||||
index, _, err := PeerIndexByIP(peers, ip)
|
||||
if(err != nil) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &(*peers)[index], nil
|
||||
@@ -35,7 +35,7 @@ func PeerIndexByIPNet(peers *[]wgtypes.Peer, ipNet *net.IPNet) (int, int, error)
|
||||
}
|
||||
func PeerByIPNet(peers *[]wgtypes.Peer, ipNet *net.IPNet) (*wgtypes.Peer, error) {
|
||||
index, _, err := PeerIndexByIPNet(peers, ipNet)
|
||||
if(err != nil) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &(*peers)[index], nil
|
||||
|
||||
Reference in New Issue
Block a user