Add project

This commit is contained in:
2022-11-15 18:12:16 +01:00
parent b250277bab
commit 5d40cde0c8
11 changed files with 538 additions and 10 deletions

View File

@@ -0,0 +1,27 @@
package environmentchecks
import (
"os"
"fmt"
)
func HandleDefaults(envDefaults map[string]string){
for env, defaultValue := range envDefaults{
// Check if env is set
_, isSet := os.LookupEnv(env)
if(!isSet){
os.Setenv(env, defaultValue)
}
}
}
func HandleRequired(envRequired []string) error {
for _, env := range envRequired{
// Check if env is set
_, isSet := os.LookupEnv(env)
if(!isSet){
return fmt.Errorf("env '%s' required, but not set", env)
}
}
return nil
}

View File

@@ -0,0 +1,102 @@
package iproute2mapping
import (
"os"
"bufio"
"strings"
"strconv"
"fmt"
)
// Storage for mapping
var ByName = make(map[int]map[string]int)
var ById = make(map[int]map[int]string)
// Mapping types
const (
PROTOCOL = iota
TABLE
)
// Paths
var filePaths = map[int]string{
PROTOCOL: "/etc/iproute2/rt_protos",
TABLE: "/etc/iproute2/rt_tables",
}
// Export error-check
var Errors []error;
func init() {
var err error
for mapType, filePath := range filePaths{
ByName[mapType], ById[mapType], err = readFromFile(filePath)
if(err != nil){
Errors = []error{
fmt.Errorf("failed reading iproute2 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;
}
defer file.Close()
scanner := bufio.NewScanner(file)
scanner.Split(bufio.ScanLines)
mapByName = make(map[string]int)
mapById = make(map[int]string)
// Go through file line-by-line
for scanner.Scan() {
text := scanner.Text()
if(strings.HasPrefix(text, "#") || text == ""){
continue
}
id, name, err := getMappingFromText(text)
if(err != nil){
// Only warn as we can continue processing the file
Errors = append(Errors,
fmt.Errorf("mappig-data invalid '%s': %s", text, err),
)
continue
}
mapByName[name] = id
mapById[id] = name
}
return
}
func getMappingFromText(text string) (int, string, error) {
// Split and read/convert data
data := strings.Split(text, "\t")
id, err := strconv.Atoi(data[0])
if(err != nil){
return 0, "", err
}
return id, data[1], nil
}
// Try getting an id from a name (or a string containing an id) with a specified type
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
var found bool
id, found = ByName[mappingType][name]
if(!found){
return 0, fmt.Errorf("no id found from name")
}
}
return id, nil
}

View File

@@ -0,0 +1,2 @@
package iproute2mapping

View File

@@ -0,0 +1,25 @@
package netchecks
import (
"fmt"
"net"
"reflect"
)
func IPNetIndexByIP(list []net.IPNet, ip net.IP) (int, error) {
for index, ipNetEntry := range list {
if ipNetEntry.Contains(ip) {
return index, nil
}
}
return -1, fmt.Errorf("ip not in ipNet-list")
}
func IPNetIndexByIPNet(list []net.IPNet, ipNet net.IPNet) (int, error) {
for index, ipNetEntry := range list {
if reflect.DeepEqual(ipNetEntry, ipNet) {
return index, nil
}
}
return -1, fmt.Errorf("ipNet not in ipNet-list")
}

42
lib/wgchecks/wgchecks.go Normal file
View File

@@ -0,0 +1,42 @@
package wgChecks
import (
"fmt"
"net"
"git.ruekov.eu/ruakij/routingtabletowg/lib/netchecks"
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
)
func PeerIndexByIP(peers []wgtypes.Peer, ip net.IP) (int, int, error) {
for index, peer := range peers {
if ipIndex, err := netchecks.IPNetIndexByIP(peer.AllowedIPs, ip); err == nil {
return index, ipIndex, nil
}
}
return -1, -1, fmt.Errorf("no peer by ip in list")
}
func PeerByIP(peers []wgtypes.Peer, ip net.IP) (*wgtypes.Peer, error) {
index, _, err := PeerIndexByIP(peers, ip)
if(err != nil) {
return nil, err
}
return &peers[index], nil
}
func PeerIndexByIPNet(peers []wgtypes.Peer, ipNet net.IPNet) (int, int, error) {
for index, peer := range peers {
if ipNetIndex, err := netchecks.IPNetIndexByIPNet(peer.AllowedIPs, ipNet); err == nil {
return index, ipNetIndex, nil
}
}
return -1, -1, fmt.Errorf("no peer by ipNet in list")
}
func PeerByIPNet(peers []wgtypes.Peer, ipNet net.IPNet) (*wgtypes.Peer, error) {
index, _, err := PeerIndexByIPNet(peers, ipNet)
if(err != nil) {
return nil, err
}
return &peers[index], nil
}