feature_v0.5_netclient

This commit is contained in:
afeiszli
2021-05-29 13:22:18 -04:00
parent a3c5602b4b
commit 6c13f06001
16 changed files with 232 additions and 41 deletions

View File

@@ -2,6 +2,7 @@ package functions
import (
"fmt"
"encoding/json"
"errors"
"context"
"net/http"
@@ -258,7 +259,21 @@ func GetNode(network string) nodepb.Node {
return node
}
func Uninstall() error {
networks, err := GetNetworks()
if err != nil {
log.Println("unable to retrieve networks: ", err)
log.Println("continuing uninstall without leaving networks")
} else {
for _, network := range networks {
err = LeaveNetwork(network)
if err != nil {
log.Println("Encounter issue leaving network " + network + ": ", err)
}
}
}
return err
}
func LeaveNetwork(network string) error {
//need to implement checkin on server side
@@ -330,3 +345,59 @@ func DeleteInterface(ifacename string, postdown string) error{
}
return err
}
func List() error{
networks, err := GetNetworks()
if err != nil {
return err
}
for _, network := range networks {
cfg, err := config.ReadConfig(network)
if err == nil {
//cfg2 := *cfg
listconfig := &config.ListConfig{
Name: cfg.Node.Name,
Interface: cfg.Node.Interface,
PrivateIPv4: cfg.Node.WGAddress,
PrivateIPv6: cfg.Node.WGAddress6,
PublicEndpoint: cfg.Node.Endpoint,
}
jsoncfg, _ := json.Marshal(listconfig)
fmt.Println(network + ": " + string(jsoncfg))
} else {
fmt.Println(network + ": Could not retrieve network configuration.")
}
}
return nil
}
func GetNetworks() ([]string, error) {
var networks []string
files, err := ioutil.ReadDir("/etc/netclient")
if err != nil {
return networks, err
}
for _, f := range files {
if strings.Contains(f.Name(), "netconfig-") && !strings.Contains(f.Name(), "global-001"){
networkname := stringAfter(f.Name(), "netconfig-")
networks = append(networks, networkname)
}
}
return networks, err
}
func stringAfter(original string, substring string) string {
position := strings.LastIndex(original, substring)
if position == -1 {
return ""
}
adjustedPosition := position + len(substring)
if adjustedPosition >= len(original) {
return ""
}
return original[adjustedPosition:len(original)]
}