2024-02-05 18:47:26 +01:00
|
|
|
//go:build !windows
|
|
|
|
|
|
|
|
|
|
package config
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"os"
|
|
|
|
|
"os/exec"
|
2024-02-09 16:20:40 +01:00
|
|
|
"path/filepath"
|
2024-03-09 15:49:09 +01:00
|
|
|
"strings"
|
2024-02-05 18:47:26 +01:00
|
|
|
)
|
|
|
|
|
|
2024-03-09 15:49:09 +01:00
|
|
|
func ExecuteCmd(executablePath string, background bool, args ...string) (string, error) {
|
2024-02-09 23:36:44 +01:00
|
|
|
cwd := filepath.Dir(executablePath)
|
|
|
|
|
if appimage := os.Getenv("APPIMAGE"); appimage != "" {
|
2024-03-09 15:49:09 +01:00
|
|
|
executablePath = appimage
|
2024-02-14 14:54:45 +01:00
|
|
|
if !background {
|
2024-03-09 15:49:09 +01:00
|
|
|
return "Fail", fmt.Errorf("Appimage cannot have service")
|
2024-02-14 14:54:45 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-09 15:49:09 +01:00
|
|
|
commands := [][]string{
|
|
|
|
|
{"cocoasudo", "--prompt=Hiddify needs root for tunneling.", executablePath},
|
|
|
|
|
{"gksu", executablePath},
|
|
|
|
|
{"pkexec", executablePath},
|
|
|
|
|
{"xterm", "-e", "sudo", executablePath, strings.Join(args, " ")},
|
|
|
|
|
{"sudo", executablePath},
|
2024-02-05 18:47:26 +01:00
|
|
|
}
|
2024-02-09 23:36:44 +01:00
|
|
|
|
2024-03-09 15:49:09 +01:00
|
|
|
var err error
|
|
|
|
|
var cmd *exec.Cmd
|
|
|
|
|
for _, command := range commands {
|
|
|
|
|
cmd = exec.Command(command[0], command[1:]...)
|
|
|
|
|
cmd.Dir = cwd
|
|
|
|
|
cmd.Stdout = os.Stdout
|
|
|
|
|
cmd.Stderr = os.Stderr
|
|
|
|
|
fmt.Printf("Running command: %v\n", command)
|
|
|
|
|
if background {
|
|
|
|
|
err = cmd.Start()
|
|
|
|
|
} else {
|
|
|
|
|
err = cmd.Run()
|
2024-02-09 16:20:40 +01:00
|
|
|
}
|
2024-03-09 15:49:09 +01:00
|
|
|
if err == nil {
|
|
|
|
|
return "Ok", nil
|
2024-02-09 16:20:40 +01:00
|
|
|
}
|
2024-02-05 18:47:26 +01:00
|
|
|
}
|
2024-03-09 15:49:09 +01:00
|
|
|
|
|
|
|
|
return "", fmt.Errorf("Error executing run as root shell command")
|
2024-02-05 18:47:26 +01:00
|
|
|
}
|