mirror of
https://github.com/gravitl/netmaker.git
synced 2025-10-16 22:01:19 +08:00
got service working
This commit is contained in:
@@ -14,7 +14,7 @@ func InstallDaemon(cfg config.ClientConfig) error {
|
|||||||
case "windows":
|
case "windows":
|
||||||
err = SetupWindowsDaemon()
|
err = SetupWindowsDaemon()
|
||||||
case "darwin":
|
case "darwin":
|
||||||
err = errors.New("need to implement macos daemon0")
|
err = SetupMacDaemon()
|
||||||
case "linux":
|
case "linux":
|
||||||
err = SetupSystemDDaemon(cfg.Network)
|
err = SetupSystemDDaemon(cfg.Network)
|
||||||
default:
|
default:
|
||||||
|
@@ -11,34 +11,44 @@ import (
|
|||||||
|
|
||||||
const MAC_SERVICE_NAME = "com.gravitl.netclient"
|
const MAC_SERVICE_NAME = "com.gravitl.netclient"
|
||||||
|
|
||||||
func CreateAndRunMacDaemon() error {
|
func SetupMacDaemon() error {
|
||||||
_, err := os.Stat("~/Library/LaunchAgents")
|
_, errN := os.Stat("~/Library/LaunchAgents")
|
||||||
if os.IsNotExist(err) {
|
if os.IsNotExist(errN) {
|
||||||
os.Mkdir("~/Library/LaunchAgents", 0744)
|
os.Mkdir("~/Library/LaunchAgents", 0755)
|
||||||
}
|
}
|
||||||
err = CreateMacService(MAC_SERVICE_NAME)
|
err := CreateMacService(MAC_SERVICE_NAME)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
_, err = ncutils.RunCmd("launchctl load ~/Library/LaunchAgents/"+MAC_SERVICE_NAME+".plist", true)
|
_, err = ncutils.RunCmd("launchctl load /Library/LaunchDaemons/"+MAC_SERVICE_NAME+".plist", true)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func CleanupMac() {
|
func CleanupMac() {
|
||||||
//StopWindowsDaemon()
|
_, err := ncutils.RunCmd("launchctl unload /Library/LaunchDaemons/"+MAC_SERVICE_NAME+".plist", true)
|
||||||
//RemoveWindowsDaemon()
|
if ncutils.FileExists("/Library/LaunchDaemons/" + MAC_SERVICE_NAME + ".plist") {
|
||||||
//os.RemoveAll(ncutils.GetNetclientPath())
|
err = os.Remove("/Library/LaunchDaemons/" + MAC_SERVICE_NAME + ".plist")
|
||||||
log.Println("TODO: Not implemented yet")
|
}
|
||||||
|
if err != nil {
|
||||||
|
ncutils.PrintLog(err.Error(), 1)
|
||||||
|
}
|
||||||
|
|
||||||
|
os.RemoveAll(ncutils.GetNetclientPath())
|
||||||
}
|
}
|
||||||
|
|
||||||
func CreateMacService(servicename string) error {
|
func CreateMacService(servicename string) error {
|
||||||
tdata := MacTemplateData{
|
tdata := MacTemplateData{
|
||||||
Label: servicename,
|
Label: servicename,
|
||||||
Program: "/etc/netclient/netclient",
|
Interval: "15",
|
||||||
KeepAlive: true,
|
|
||||||
RunAtLoad: true,
|
|
||||||
}
|
}
|
||||||
fileLoc := fmt.Sprintf("%s/Library/LaunchAgents/%s.plist", os.Getenv("HOME"), tdata.Label)
|
_, err := os.Stat("/Library/LaunchDaemons")
|
||||||
|
if os.IsNotExist(err) {
|
||||||
|
os.Mkdir("/Library/LaunchDaemons", 0755)
|
||||||
|
} else if err != nil {
|
||||||
|
log.Println("couldnt find or create /Library/LaunchDaemons")
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
fileLoc := fmt.Sprintf("/Library/LaunchDaemons/%s.plist", tdata.Label)
|
||||||
launchdFile, err := os.Open(fileLoc)
|
launchdFile, err := os.Open(fileLoc)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -49,28 +59,35 @@ func CreateMacService(servicename string) error {
|
|||||||
|
|
||||||
func MacTemplate() string {
|
func MacTemplate() string {
|
||||||
return `
|
return `
|
||||||
<?xml version='1.0' encoding='UTF-8'?>
|
<?xml version='1.0' encoding='UTF-8'?>
|
||||||
<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\" >
|
<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\" >
|
||||||
<plist version='1.0'>
|
<plist version='1.0'>
|
||||||
<dict>
|
<dict>
|
||||||
<key>Label</key><string>{{.Label}}</string>
|
<key>Label</key><string>{{.Label}}</string>
|
||||||
<key>Program</key><string>{{.Program}}</string>
|
<key>ProgramArguments</key>
|
||||||
<key>StandardOutPath</key><string>/tmp/{{.Label}}.out.log</string>
|
<array>
|
||||||
<key>StandardErrorPath</key><string>/tmp/{{.Label}}.err.log</string>
|
<string>/etc/netclient/netclient</string>
|
||||||
<key>KeepAlive</key><{{.KeepAlive}}/>
|
<string>checkin</string>
|
||||||
<key>RunAtLoad</key><{{.RunAtLoad}}/>
|
<string>-n</string>
|
||||||
<key>StartCalendarInterval</key>
|
<string>all</string>
|
||||||
|
</array>
|
||||||
|
<key>StandardOutPath</key><string>/etc/netclient/{{.Label}}.log</string>
|
||||||
|
<key>StandardErrorPath</key><string>/etc/netclient/{{.Label}}.log</string>
|
||||||
|
<key>AbandonProcessGroup</key><true/>
|
||||||
|
<key>StartInterval</key>
|
||||||
|
<integer>{{.Interval}}</integer>
|
||||||
|
<key>EnvironmentVariables</key>
|
||||||
<dict>
|
<dict>
|
||||||
<key>Minute</key>
|
<key>PATH</key>
|
||||||
<value>*/1</value>
|
<string>/usr/local/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin</string>
|
||||||
</dict>
|
</dict>
|
||||||
|
</dict>
|
||||||
|
</plist>
|
||||||
</plist>
|
</plist>
|
||||||
`
|
`
|
||||||
}
|
}
|
||||||
|
|
||||||
type MacTemplateData struct {
|
type MacTemplateData struct {
|
||||||
Label string
|
Label string
|
||||||
Program string
|
Interval string
|
||||||
KeepAlive bool
|
|
||||||
RunAtLoad bool
|
|
||||||
}
|
}
|
||||||
|
@@ -107,7 +107,7 @@ func Uninstall() error {
|
|||||||
// clean up OS specific stuff
|
// clean up OS specific stuff
|
||||||
if ncutils.IsWindows() {
|
if ncutils.IsWindows() {
|
||||||
daemon.CleanupWindows()
|
daemon.CleanupWindows()
|
||||||
} else if ncutils.IsWindows() {
|
} else if ncutils.IsMac() {
|
||||||
daemon.CleanupMac()
|
daemon.CleanupMac()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -83,24 +83,11 @@ func GetMacIface(ipstring string) (string, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if wgiface == "" {
|
if wgiface == "" {
|
||||||
err = errors.New("could not find iface for network " + ipstring)
|
err = errors.New("could not find iface for address " + ipstring)
|
||||||
}
|
}
|
||||||
return wgiface, err
|
return wgiface, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func getLineAfter(value string, a string) string {
|
|
||||||
// Get substring after a string.
|
|
||||||
pos := strings.LastIndex(value, a)
|
|
||||||
if pos == -1 {
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
adjustedPos := pos + len(a)
|
|
||||||
if adjustedPos >= len(value) {
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
return value[adjustedPos:len(value)]
|
|
||||||
}
|
|
||||||
|
|
||||||
func HasNetwork(network string) bool {
|
func HasNetwork(network string) bool {
|
||||||
|
|
||||||
if ncutils.IsWindows() {
|
if ncutils.IsWindows() {
|
||||||
|
@@ -116,7 +116,7 @@ func InitWireguard(node *models.Node, privkey string, peers []wgtypes.PeerConfig
|
|||||||
}
|
}
|
||||||
defer wgclient.Close()
|
defer wgclient.Close()
|
||||||
|
|
||||||
ifacename := node.Interface
|
var ifacename string
|
||||||
if nodecfg.Interface != "" {
|
if nodecfg.Interface != "" {
|
||||||
ifacename = nodecfg.Interface
|
ifacename = nodecfg.Interface
|
||||||
} else if node.Interface != "" {
|
} else if node.Interface != "" {
|
||||||
@@ -127,6 +127,7 @@ func InitWireguard(node *models.Node, privkey string, peers []wgtypes.PeerConfig
|
|||||||
if node.Address == "" {
|
if node.Address == "" {
|
||||||
log.Fatal("no address to configure")
|
log.Fatal("no address to configure")
|
||||||
}
|
}
|
||||||
|
|
||||||
nameserver := servercfg.CoreDNSAddr
|
nameserver := servercfg.CoreDNSAddr
|
||||||
network := node.Network
|
network := node.Network
|
||||||
if nodecfg.Network != "" {
|
if nodecfg.Network != "" {
|
||||||
@@ -139,8 +140,7 @@ func InitWireguard(node *models.Node, privkey string, peers []wgtypes.PeerConfig
|
|||||||
setKernelDevice(ifacename, node.Address)
|
setKernelDevice(ifacename, node.Address)
|
||||||
}
|
}
|
||||||
|
|
||||||
var nodeport int
|
nodeport := int(node.ListenPort)
|
||||||
nodeport = int(node.ListenPort)
|
|
||||||
conf := wgtypes.Config{}
|
conf := wgtypes.Config{}
|
||||||
if nodecfg.UDPHolePunch == "yes" &&
|
if nodecfg.UDPHolePunch == "yes" &&
|
||||||
nodecfg.IsServer == "no" &&
|
nodecfg.IsServer == "no" &&
|
||||||
@@ -166,7 +166,7 @@ func InitWireguard(node *models.Node, privkey string, peers []wgtypes.PeerConfig
|
|||||||
} else {
|
} else {
|
||||||
newConf, _ = ncutils.CreateUserSpaceConf(node.Address, key.String(), "", node.MTU, node.PersistentKeepalive, peers)
|
newConf, _ = ncutils.CreateUserSpaceConf(node.Address, key.String(), "", node.MTU, node.PersistentKeepalive, peers)
|
||||||
}
|
}
|
||||||
confPath := ncutils.GetNetclientPathSpecific() + node.Interface + ".conf"
|
confPath := ncutils.GetNetclientPathSpecific() + ifacename + ".conf"
|
||||||
ncutils.PrintLog("writing wg conf file to: "+confPath, 1)
|
ncutils.PrintLog("writing wg conf file to: "+confPath, 1)
|
||||||
err = ioutil.WriteFile(confPath, []byte(newConf), 0644)
|
err = ioutil.WriteFile(confPath, []byte(newConf), 0644)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -174,7 +174,7 @@ func InitWireguard(node *models.Node, privkey string, peers []wgtypes.PeerConfig
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
// spin up userspace / windows interface + apply the conf file
|
// spin up userspace / windows interface + apply the conf file
|
||||||
_ = RemoveConf(node.Interface, false) // remove interface first
|
_ = RemoveConf(ifacename, false) // remove interface first
|
||||||
err = ApplyConf(confPath)
|
err = ApplyConf(confPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ncutils.PrintLog("failed to create wireguard interface", 1)
|
ncutils.PrintLog("failed to create wireguard interface", 1)
|
||||||
@@ -240,6 +240,7 @@ func InitWireguard(node *models.Node, privkey string, peers []wgtypes.PeerConfig
|
|||||||
_, _ = ncutils.RunCmd(ipExec+" address add dev "+ifacename+" "+node.Address6+"/64", true)
|
_, _ = ncutils.RunCmd(ipExec+" address add dev "+ifacename+" "+node.Address6+"/64", true)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user