mirror of
https://github.com/gravitl/netmaker.git
synced 2025-02-27 09:36:08 +08:00
adding freebsd logic and using wgctrl.Device
This commit is contained in:
parent
9a66674a5c
commit
93a25bb9a6
2 changed files with 60 additions and 8 deletions
|
@ -1,30 +1,32 @@
|
||||||
|
//go:build !freebsd
|
||||||
|
// +build !freebsd
|
||||||
|
|
||||||
package functions
|
package functions
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/gravitl/netmaker/logger"
|
"github.com/gravitl/netmaker/logger"
|
||||||
"github.com/gravitl/netmaker/netclient/config"
|
"github.com/gravitl/netmaker/netclient/config"
|
||||||
"github.com/gravitl/netmaker/netclient/local"
|
"github.com/gravitl/netmaker/netclient/local"
|
||||||
"github.com/gravitl/netmaker/netclient/ncutils"
|
"github.com/gravitl/netmaker/netclient/ncutils"
|
||||||
|
"golang.zx2c4.com/wireguard/wgctrl"
|
||||||
)
|
)
|
||||||
|
|
||||||
// GetLocalListenPort - Gets the port running on the local interface
|
// GetLocalListenPort - Gets the port running on the local interface
|
||||||
func GetLocalListenPort(ifacename string) (int32, error) {
|
func GetLocalListenPort(ifacename string) (int32, error) {
|
||||||
portstring, err := ncutils.RunCmd("wg show "+ifacename+" listen-port", false)
|
client, err := wgctrl.New()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
logger.Log(0, "failed to start wgctrl")
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
portstring = strings.TrimSuffix(portstring, "\n")
|
defer client.Close()
|
||||||
i, err := strconv.ParseInt(portstring, 10, 32)
|
device, err := client.Device(ifacename)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
logger.Log(0, "failed to parse interface")
|
||||||
return 0, err
|
return 0, err
|
||||||
} else if i == 0 {
|
|
||||||
return 0, errors.New("parsed port is unset or invalid")
|
|
||||||
}
|
}
|
||||||
return int32(i), nil
|
return int32(device.ListenPort), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// UpdateLocalListenPort - check local port, if different, mod config and publish
|
// UpdateLocalListenPort - check local port, if different, mod config and publish
|
||||||
|
|
50
netclient/functions/localport_freebsd.go
Normal file
50
netclient/functions/localport_freebsd.go
Normal file
|
@ -0,0 +1,50 @@
|
||||||
|
//go:build freebsd
|
||||||
|
// +build freebsd
|
||||||
|
|
||||||
|
package functions
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/gravitl/netmaker/logger"
|
||||||
|
"github.com/gravitl/netmaker/netclient/config"
|
||||||
|
"github.com/gravitl/netmaker/netclient/ncutils"
|
||||||
|
)
|
||||||
|
|
||||||
|
// GetLocalListenPort - Gets the port running on the local interface
|
||||||
|
func GetLocalListenPort(ifacename string) (int32, error) {
|
||||||
|
portstring, err := ncutils.RunCmd("wg show "+ifacename+" listen-port", false)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
portstring = strings.TrimSuffix(portstring, "\n")
|
||||||
|
i, err := strconv.ParseInt(portstring, 10, 32)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
} else if i == 0 {
|
||||||
|
return 0, errors.New("parsed port is unset or invalid")
|
||||||
|
}
|
||||||
|
return int32(i), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// UpdateLocalListenPort - check local port, if different, mod config and publish
|
||||||
|
func UpdateLocalListenPort(nodeCfg *config.ClientConfig) error {
|
||||||
|
var err error
|
||||||
|
localPort, err := GetLocalListenPort(nodeCfg.Node.Interface)
|
||||||
|
if err != nil {
|
||||||
|
logger.Log(1, "error encountered checking local listen port: ", err.Error())
|
||||||
|
} else if nodeCfg.Node.LocalListenPort != localPort && localPort != 0 {
|
||||||
|
logger.Log(1, "local port has changed from ", strconv.Itoa(int(nodeCfg.Node.LocalListenPort)), " to ", strconv.Itoa(int(localPort)))
|
||||||
|
nodeCfg.Node.LocalListenPort = localPort
|
||||||
|
err = config.ModConfig(&nodeCfg.Node)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := PublishNodeUpdate(nodeCfg); err != nil {
|
||||||
|
logger.Log(0, "could not publish local port change")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
Loading…
Reference in a new issue