fixing model

This commit is contained in:
afeiszli
2021-09-18 11:01:34 -04:00
parent 74b15a6a13
commit b828f7b6d9
8 changed files with 48 additions and 21 deletions

View File

@@ -506,7 +506,7 @@ func createEgressGateway(w http.ResponseWriter, r *http.Request) {
func CreateEgressGateway(gateway models.EgressGatewayRequest) (models.Node, error) { func CreateEgressGateway(gateway models.EgressGatewayRequest) (models.Node, error) {
node, err := functions.GetNodeByMacAddress(gateway.NetID, gateway.NodeID) node, err := functions.GetNodeByMacAddress(gateway.NetID, gateway.NodeID)
if node.OS == "windows" { // add in darwin later if node.OS == "windows" || node.OS == "macos" { // add in darwin later
return models.Node{}, errors.New(node.OS + " is unsupported for egress gateways") return models.Node{}, errors.New(node.OS + " is unsupported for egress gateways")
} }
if err != nil { if err != nil {
@@ -635,7 +635,7 @@ func createIngressGateway(w http.ResponseWriter, r *http.Request) {
func CreateIngressGateway(netid string, macaddress string) (models.Node, error) { func CreateIngressGateway(netid string, macaddress string) (models.Node, error) {
node, err := functions.GetNodeByMacAddress(netid, macaddress) node, err := functions.GetNodeByMacAddress(netid, macaddress)
if node.OS == "windows" { // add in darwin later if node.OS == "windows" || node.OS == "macos" { // add in darwin later
return models.Node{}, errors.New(node.OS + " is unsupported for ingress gateways") return models.Node{}, errors.New(node.OS + " is unsupported for ingress gateways")
} }

View File

@@ -35,7 +35,7 @@ func createRelay(w http.ResponseWriter, r *http.Request) {
func CreateRelay(relay models.RelayRequest) (models.Node, error) { func CreateRelay(relay models.RelayRequest) (models.Node, error) {
node, err := functions.GetNodeByMacAddress(relay.NetID, relay.NodeID) node, err := functions.GetNodeByMacAddress(relay.NetID, relay.NodeID)
if node.OS == "windows" { // add in darwin later if node.OS == "windows" || node.OS == "macos" { // add in darwin later
return models.Node{}, errors.New(node.OS + " is unsupported for relay") return models.Node{}, errors.New(node.OS + " is unsupported for relay")
} }
if err != nil { if err != nil {

View File

@@ -132,5 +132,5 @@ type EgressGatewayRequest struct {
type RelayRequest struct { type RelayRequest struct {
NodeID string `json:"nodeid" bson:"nodeid"` NodeID string `json:"nodeid" bson:"nodeid"`
NetID string `json:"netid" bson:"netid"` NetID string `json:"netid" bson:"netid"`
RelayAddrs []string `json:"addrs" bson:"addrs"` RelayAddrs []string `json:"relayaddrs" bson:"relayaddrs"`
} }

View File

@@ -37,7 +37,7 @@ func Join(cfg config.ClientConfig, privateKey string) error {
} }
} }
if cfg.Daemon != "off" { if cfg.Daemon != "off" {
if !netclientutils.IsWindows() { if netclientutils.IsLinux() {
err = local.RemoveSystemDServices(cfg.Network) err = local.RemoveSystemDServices(cfg.Network)
} }
if err != nil { if err != nil {
@@ -51,6 +51,8 @@ func Join(cfg config.ClientConfig, privateKey string) error {
if cfg.Daemon != "off" { if cfg.Daemon != "off" {
if netclientutils.IsWindows() { if netclientutils.IsWindows() {
err = local.CreateAndRunWindowsDaemon() err = local.CreateAndRunWindowsDaemon()
} else if netclientutils.IsMac() {
err = local.CreateAndRunMacDaemon()
} else { } else {
err = functions.InstallDaemon(cfg) err = functions.InstallDaemon(cfg)
} }

View File

@@ -166,7 +166,7 @@ func Pull(network string, manual bool) (*models.Node, error) {
return nil, err return nil, err
} }
} }
conn, err := grpc.Dial(cfg.Server.GRPCAddress, conn, err := grpc.Dial(cfg.Server.GRPCAddress,
netclientutils.GRPCRequestOpts(cfg.Server.GRPCSSL)) netclientutils.GRPCRequestOpts(cfg.Server.GRPCSSL))
if err != nil { if err != nil {
log.Println("Cant dial GRPC server:", err) log.Println("Cant dial GRPC server:", err)
@@ -230,7 +230,7 @@ func Pull(network string, manual bool) (*models.Node, error) {
} }
} }
} }
if !netclientutils.IsWindows() { if netclientutils.IsLinux() {
setDNS(&resNode, servercfg, &cfg.Node) setDNS(&resNode, servercfg, &cfg.Node)
} }
@@ -248,7 +248,7 @@ func Push(network string) error {
var header metadata.MD var header metadata.MD
var wcclient nodepb.NodeServiceClient var wcclient nodepb.NodeServiceClient
conn, err := grpc.Dial(cfg.Server.GRPCAddress, conn, err := grpc.Dial(cfg.Server.GRPCAddress,
netclientutils.GRPCRequestOpts(cfg.Server.GRPCSSL)) netclientutils.GRPCRequestOpts(cfg.Server.GRPCSSL))
if err != nil { if err != nil {
log.Println("Cant dial GRPC server:", err) log.Println("Cant dial GRPC server:", err)

View File

@@ -104,8 +104,11 @@ func Uninstall() error {
} }
// clean up OS specific stuff // clean up OS specific stuff
if netclientutils.IsWindows() { if netclientutils.IsWindows() {
local.Cleanup() local.CleanupWindows()
} else if netclientutils.IsWindows() {
local.CleanupMac()
} }
return err return err
} }
@@ -119,7 +122,7 @@ func LeaveNetwork(network string) error {
node := cfg.Node node := cfg.Node
var wcclient nodepb.NodeServiceClient var wcclient nodepb.NodeServiceClient
conn, err := grpc.Dial(cfg.Server.GRPCAddress, conn, err := grpc.Dial(cfg.Server.GRPCAddress,
netclientutils.GRPCRequestOpts(cfg.Server.GRPCSSL)) netclientutils.GRPCRequestOpts(cfg.Server.GRPCSSL))
if err != nil { if err != nil {
log.Printf("Unable to establish client connection to "+servercfg.GRPCAddress+": %v", err) log.Printf("Unable to establish client connection to "+servercfg.GRPCAddress+": %v", err)
@@ -183,7 +186,7 @@ func DeleteInterface(ifacename string, postdown string) error {
if err != nil { if err != nil {
log.Println(err) log.Println(err)
} }
_, err = local.RunCmd(ipExec + " link del " + ifacename, false) _, err = local.RunCmd(ipExec+" link del "+ifacename, false)
if postdown != "" { if postdown != "" {
runcmds := strings.Split(postdown, "; ") runcmds := strings.Split(postdown, "; ")
err = local.RunCmds(runcmds, true) err = local.RunCmds(runcmds, true)

View File

@@ -1,6 +1,7 @@
package local package local
import ( import (
"errors"
"fmt" "fmt"
"io" "io"
"io/ioutil" "io/ioutil"
@@ -21,14 +22,14 @@ func IsWindowsWGInstalled() bool {
} }
func ApplyWindowsConf(confPath string) error { func ApplyWindowsConf(confPath string) error {
if _, err := RunCmd("wireguard.exe /installtunnelservice " + confPath, true); err != nil { if _, err := RunCmd("wireguard.exe /installtunnelservice "+confPath, true); err != nil {
return err return err
} }
return nil return nil
} }
func RemoveWindowsConf(ifacename string) error { func RemoveWindowsConf(ifacename string) error {
if _, err := RunCmd("wireguard.exe /uninstalltunnelservice " + ifacename, true); err != nil { if _, err := RunCmd("wireguard.exe /uninstalltunnelservice "+ifacename, true); err != nil {
return err return err
} }
return nil return nil
@@ -58,12 +59,12 @@ func writeServiceConfig() error {
func StopWindowsDaemon() { func StopWindowsDaemon() {
netclientutils.Log("no networks detected, stopping Windows, Netclient daemon") netclientutils.Log("no networks detected, stopping Windows, Netclient daemon")
// stop daemon, will not overwrite // stop daemon, will not overwrite
RunCmd(strings.Replace(netclientutils.GetNetclientPathSpecific(), `\\`, `\`, -1) + `winsw.exe stop`, true) RunCmd(strings.Replace(netclientutils.GetNetclientPathSpecific(), `\\`, `\`, -1)+`winsw.exe stop`, true)
} }
func RemoveWindowsDaemon() { func RemoveWindowsDaemon() {
// uninstall daemon, will not restart or start another // uninstall daemon, will not restart or start another
RunCmd(strings.Replace(netclientutils.GetNetclientPathSpecific(), `\\`, `\`, -1) + `winsw.exe uninstall`, true) RunCmd(strings.Replace(netclientutils.GetNetclientPathSpecific(), `\\`, `\`, -1)+`winsw.exe uninstall`, true)
netclientutils.Log("uninstalled Windows, Netclient daemon") netclientutils.Log("uninstalled Windows, Netclient daemon")
} }
@@ -121,6 +122,11 @@ func downloadWinsw() error {
return nil return nil
} }
func CreateAndRunMacDaemon() error {
log.Println("TODO: Create Mac Daemon")
return errors.New("no mac daemon yet")
}
func CreateAndRunWindowsDaemon() error { func CreateAndRunWindowsDaemon() error {
if !FileExists(netclientutils.GetNetclientPathSpecific() + "winsw.xml") { if !FileExists(netclientutils.GetNetclientPathSpecific() + "winsw.xml") {
@@ -144,14 +150,14 @@ func CreateAndRunWindowsDaemon() error {
netclientutils.Log("finished daemon setup") netclientutils.Log("finished daemon setup")
} }
// install daemon, will not overwrite // install daemon, will not overwrite
RunCmd(strings.Replace(netclientutils.GetNetclientPathSpecific(), `\\`, `\`, -1) + `winsw.exe install`, true) RunCmd(strings.Replace(netclientutils.GetNetclientPathSpecific(), `\\`, `\`, -1)+`winsw.exe install`, true)
// start daemon, will not restart or start another // start daemon, will not restart or start another
RunCmd(strings.Replace(netclientutils.GetNetclientPathSpecific(), `\\`, `\`, -1) + `winsw.exe start`, true) RunCmd(strings.Replace(netclientutils.GetNetclientPathSpecific(), `\\`, `\`, -1)+`winsw.exe start`, true)
netclientutils.Log(strings.Replace(netclientutils.GetNetclientPathSpecific(), `\\`, `\`, -1) + `winsw.exe start`) netclientutils.Log(strings.Replace(netclientutils.GetNetclientPathSpecific(), `\\`, `\`, -1) + `winsw.exe start`)
return nil return nil
} }
func Cleanup() { func CleanupWindows() {
if !FileExists(netclientutils.GetNetclientPathSpecific() + "winsw.xml") { if !FileExists(netclientutils.GetNetclientPathSpecific() + "winsw.xml") {
writeServiceConfig() writeServiceConfig()
} }
@@ -160,3 +166,10 @@ func Cleanup() {
os.RemoveAll(netclientutils.GetNetclientPath()) os.RemoveAll(netclientutils.GetNetclientPath())
log.Println("Netclient on Windows, uninstalled") log.Println("Netclient on Windows, uninstalled")
} }
func CleanupMac() {
//StopWindowsDaemon()
//RemoveWindowsDaemon()
//os.RemoveAll(netclientutils.GetNetclientPath())
log.Println("TODO: Not implemented yet")
}

View File

@@ -1,6 +1,7 @@
package netclientutils package netclientutils
import ( import (
"crypto/tls"
"errors" "errors"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
@@ -13,11 +14,11 @@ import (
"strconv" "strconv"
"strings" "strings"
"time" "time"
"crypto/tls"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"golang.zx2c4.com/wireguard/wgctrl" "golang.zx2c4.com/wireguard/wgctrl"
"golang.zx2c4.com/wireguard/wgctrl/wgtypes" "golang.zx2c4.com/wireguard/wgctrl/wgtypes"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
) )
const NO_DB_RECORD = "no result found" const NO_DB_RECORD = "no result found"
@@ -37,6 +38,14 @@ func IsWindows() bool {
return runtime.GOOS == "windows" return runtime.GOOS == "windows"
} }
func IsMac() bool {
return runtime.GOOS == "macos"
}
func IsLinux() bool {
return runtime.GOOS == "linux"
}
// == database returned nothing error == // == database returned nothing error ==
func IsEmptyRecord(err error) bool { func IsEmptyRecord(err error) bool {
if err == nil { if err == nil {