Skip to content

Commit

Permalink
cmd/awl-tray: add macos specific root hacks (#107)
Browse files Browse the repository at this point in the history
* cmd/awl-tray: add macos specific root hacks
  • Loading branch information
pymq authored Aug 2, 2023
1 parent 0224c2f commit 7cfb978
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 2 deletions.
68 changes: 68 additions & 0 deletions cmd/awl-tray/os_hacks_macos.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
//go:build darwin

package main

import (
"fmt"
"os"
"os/exec"

"github.com/anywherelan/awl/config"
"github.com/skratchdot/open-golang/open"
)

func initOSSpecificHacks() {
uid := os.Geteuid()
if uid != 0 {
fmt.Printf("process is run under non-root uid: %d, ask for root permissions with osascript\n", uid)
runItselfWithRoot()
return
}

// this is required to allow listening on config.AdminHttpServerIP address
//nolint:gosec
err := exec.Command("ifconfig", "lo0", "alias", config.AdminHttpServerIP, "up").Run()
if err != nil {
fmt.Printf("error: `ifconfig lo0 alias %s up`: %v\n", config.AdminHttpServerIP, err)
}
}

func openURL(input string) error {
return open.Run(input)
}

func getRealUserID() (uint32, bool) {
return 0, false
}

func runItselfWithRoot() {
// TODO: show pop-up describing that awl needs root for vpn? show it only on first launch?

executable, err := os.Executable()
if err != nil {
fmt.Printf("error finding executable path: %v\n", err)
executable = os.Args[0]
}

osaScript := fmt.Sprintf("do shell script \"%s\" with administrator privileges", executable)

//nolint:gosec
cmd := exec.Command("osascript", "-e", osaScript)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr

err = cmd.Start()
if err != nil {
fmt.Printf("error executing osascript: %v\n", err)
os.Exit(1)
}

err = cmd.Wait()
if err != nil {
fmt.Printf("error from waiting osascript to finish: %v\n", err)
exitCode := cmd.ProcessState.ExitCode()
os.Exit(exitCode)
}

os.Exit(0)
}
4 changes: 2 additions & 2 deletions cmd/awl-tray/os_hacks_other.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//go:build !linux
// +build !linux
//go:build !linux && !darwin
// +build !linux,!darwin

package main

Expand Down
12 changes: 12 additions & 0 deletions cmd/awl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package main

import (
"context"
"fmt"
"os"
"os/exec"
"os/signal"
"syscall"
"time"
Expand All @@ -17,6 +19,16 @@ import (
func main() {
cli.New(update.AppTypeAwl).Run()

uid := os.Geteuid()
if uid == 0 {
// this is required to allow listening on config.AdminHttpServerIP address
//nolint:gosec
err := exec.Command("ifconfig", "lo0", "alias", config.AdminHttpServerIP, "up").Run()
if err != nil {
fmt.Printf("error: `ifconfig lo0 alias %s up`: %v\n", config.AdminHttpServerIP, err)
}
}

app := awl.New()
logger := app.SetupLoggerAndConfig()
ctx, ctxCancel := context.WithCancel(context.Background())
Expand Down

0 comments on commit 7cfb978

Please sign in to comment.