mirror of
https://github.com/gravitl/netmaker.git
synced 2025-10-22 08:29:28 +08:00
got service working
This commit is contained in:
@@ -14,7 +14,7 @@ func InstallDaemon(cfg config.ClientConfig) error {
|
||||
case "windows":
|
||||
err = SetupWindowsDaemon()
|
||||
case "darwin":
|
||||
err = errors.New("need to implement macos daemon0")
|
||||
err = SetupMacDaemon()
|
||||
case "linux":
|
||||
err = SetupSystemDDaemon(cfg.Network)
|
||||
default:
|
||||
|
@@ -11,34 +11,44 @@ import (
|
||||
|
||||
const MAC_SERVICE_NAME = "com.gravitl.netclient"
|
||||
|
||||
func CreateAndRunMacDaemon() error {
|
||||
_, err := os.Stat("~/Library/LaunchAgents")
|
||||
if os.IsNotExist(err) {
|
||||
os.Mkdir("~/Library/LaunchAgents", 0744)
|
||||
func SetupMacDaemon() error {
|
||||
_, errN := os.Stat("~/Library/LaunchAgents")
|
||||
if os.IsNotExist(errN) {
|
||||
os.Mkdir("~/Library/LaunchAgents", 0755)
|
||||
}
|
||||
err = CreateMacService(MAC_SERVICE_NAME)
|
||||
err := CreateMacService(MAC_SERVICE_NAME)
|
||||
if err != nil {
|
||||
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
|
||||
}
|
||||
|
||||
func CleanupMac() {
|
||||
//StopWindowsDaemon()
|
||||
//RemoveWindowsDaemon()
|
||||
//os.RemoveAll(ncutils.GetNetclientPath())
|
||||
log.Println("TODO: Not implemented yet")
|
||||
_, err := ncutils.RunCmd("launchctl unload /Library/LaunchDaemons/"+MAC_SERVICE_NAME+".plist", true)
|
||||
if ncutils.FileExists("/Library/LaunchDaemons/" + MAC_SERVICE_NAME + ".plist") {
|
||||
err = os.Remove("/Library/LaunchDaemons/" + MAC_SERVICE_NAME + ".plist")
|
||||
}
|
||||
if err != nil {
|
||||
ncutils.PrintLog(err.Error(), 1)
|
||||
}
|
||||
|
||||
os.RemoveAll(ncutils.GetNetclientPath())
|
||||
}
|
||||
|
||||
func CreateMacService(servicename string) error {
|
||||
tdata := MacTemplateData{
|
||||
Label: servicename,
|
||||
Program: "/etc/netclient/netclient",
|
||||
KeepAlive: true,
|
||||
RunAtLoad: true,
|
||||
Label: servicename,
|
||||
Interval: "15",
|
||||
}
|
||||
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)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -49,28 +59,35 @@ func CreateMacService(servicename string) error {
|
||||
|
||||
func MacTemplate() string {
|
||||
return `
|
||||
<?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\" >
|
||||
<plist version='1.0'>
|
||||
<dict>
|
||||
<key>Label</key><string>{{.Label}}</string>
|
||||
<key>Program</key><string>{{.Program}}</string>
|
||||
<key>StandardOutPath</key><string>/tmp/{{.Label}}.out.log</string>
|
||||
<key>StandardErrorPath</key><string>/tmp/{{.Label}}.err.log</string>
|
||||
<key>KeepAlive</key><{{.KeepAlive}}/>
|
||||
<key>RunAtLoad</key><{{.RunAtLoad}}/>
|
||||
<key>StartCalendarInterval</key>
|
||||
<dict>
|
||||
<key>Minute</key>
|
||||
<value>*/1</value>
|
||||
</dict>
|
||||
<?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\" >
|
||||
<plist version='1.0'>
|
||||
<dict>
|
||||
<key>Label</key><string>{{.Label}}</string>
|
||||
<key>ProgramArguments</key>
|
||||
<array>
|
||||
<string>/etc/netclient/netclient</string>
|
||||
<string>checkin</string>
|
||||
<string>-n</string>
|
||||
<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>
|
||||
<key>PATH</key>
|
||||
<string>/usr/local/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin</string>
|
||||
</dict>
|
||||
</dict>
|
||||
</plist>
|
||||
</plist>
|
||||
`
|
||||
}
|
||||
|
||||
type MacTemplateData struct {
|
||||
Label string
|
||||
Program string
|
||||
KeepAlive bool
|
||||
RunAtLoad bool
|
||||
Label string
|
||||
Interval string
|
||||
}
|
||||
|
@@ -107,7 +107,7 @@ func Uninstall() error {
|
||||
// clean up OS specific stuff
|
||||
if ncutils.IsWindows() {
|
||||
daemon.CleanupWindows()
|
||||
} else if ncutils.IsWindows() {
|
||||
} else if ncutils.IsMac() {
|
||||
daemon.CleanupMac()
|
||||
}
|
||||
|
||||
|
@@ -83,24 +83,11 @@ func GetMacIface(ipstring string) (string, error) {
|
||||
}
|
||||
}
|
||||
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
|
||||
}
|
||||
|
||||
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 {
|
||||
|
||||
if ncutils.IsWindows() {
|
||||
|
@@ -116,7 +116,7 @@ func InitWireguard(node *models.Node, privkey string, peers []wgtypes.PeerConfig
|
||||
}
|
||||
defer wgclient.Close()
|
||||
|
||||
ifacename := node.Interface
|
||||
var ifacename string
|
||||
if nodecfg.Interface != "" {
|
||||
ifacename = nodecfg.Interface
|
||||
} else if node.Interface != "" {
|
||||
@@ -127,6 +127,7 @@ func InitWireguard(node *models.Node, privkey string, peers []wgtypes.PeerConfig
|
||||
if node.Address == "" {
|
||||
log.Fatal("no address to configure")
|
||||
}
|
||||
|
||||
nameserver := servercfg.CoreDNSAddr
|
||||
network := node.Network
|
||||
if nodecfg.Network != "" {
|
||||
@@ -139,8 +140,7 @@ func InitWireguard(node *models.Node, privkey string, peers []wgtypes.PeerConfig
|
||||
setKernelDevice(ifacename, node.Address)
|
||||
}
|
||||
|
||||
var nodeport int
|
||||
nodeport = int(node.ListenPort)
|
||||
nodeport := int(node.ListenPort)
|
||||
conf := wgtypes.Config{}
|
||||
if nodecfg.UDPHolePunch == "yes" &&
|
||||
nodecfg.IsServer == "no" &&
|
||||
@@ -166,7 +166,7 @@ func InitWireguard(node *models.Node, privkey string, peers []wgtypes.PeerConfig
|
||||
} else {
|
||||
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)
|
||||
err = ioutil.WriteFile(confPath, []byte(newConf), 0644)
|
||||
if err != nil {
|
||||
@@ -174,7 +174,7 @@ func InitWireguard(node *models.Node, privkey string, peers []wgtypes.PeerConfig
|
||||
return err
|
||||
}
|
||||
// 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)
|
||||
if err != nil {
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user