fix: appimage admin service bug

This commit is contained in:
Hiddify-com
2024-02-09 16:20:40 +01:00
parent 737aa6ab83
commit 66e8beac53
3 changed files with 35 additions and 14 deletions

View File

@@ -24,8 +24,6 @@ func main() {
args = append(args, "")
}
os.Chdir(os.Args[0])
arg := C.CString(args[1])
defer C.free(unsafe.Pointer(arg))

View File

@@ -6,30 +6,52 @@ import (
"fmt"
"os"
"os/exec"
"path/filepath"
)
func ExecuteCmd(executablePath, args string) (string, error) {
if err := execCmdImp([]string{"gksu", executablePath, args}); err == nil {
cwd:=filepath.Dir(executablePath)
background=false
if appimage:=os.Getenv("APPIMAGE"); appimage!=""{
executablePath= appimage+" HiddifyService"
args=""
background=true
}
if err := execCmdImp([]string{"gksu", executablePath, args}, cwd, background); err == nil {
return "Ok", nil
}
if err := execCmdImp([]string{"pkexec", executablePath, args}); err == nil {
if err := execCmdImp([]string{"pkexec", executablePath, args}, cwd, background); err == nil {
return "Ok", nil
}
if err := execCmdImp([]string{"/bin/sh", "-c", "sudo " + executablePath + " " + args}); err == nil {
if err := execCmdImp([]string{"xterm", "-e", "sudo " + executablePath + " " + args }, cwd, background); err == nil {
return "Ok", nil
}
if err := execCmdImp([]string{"sudo", executablePath, args }, cwd, background); err == nil {
return "Ok", nil
}
return "", fmt.Errorf("Error executing run as root shell command")
}
func execCmdImp(commands []string) error {
func execCmdImp(commands []string,cwd string, background bool) error {
cmd := exec.Command(commands[0], commands[1:]...)
cmd.Dir = cwd
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
fmt.Printf("Running command: %v", commands)
if err := cmd.Run(); err != nil {
fmt.Printf("Error: %v\n", err)
return err
if background{
if err := cmd.Start(); err != nil {
fmt.Printf("Error: %v\n", err)
return err
}
}else{
if err := cmd.Run(); err != nil {
fmt.Printf("Error: %v\n", err)
return err
}
}
return nil
}

View File

@@ -28,7 +28,7 @@ func ActivateTunnelService(opt ConfigOptions) (bool, error) {
if !isSupportedOS() {
return false, E.New("Unsupported OS: " + runtime.GOOS)
}
go startTunnelRequest(opt, true)
return true, nil
}
@@ -93,7 +93,7 @@ func stopTunnelRequest() (bool, error) {
func runTunnelService(opt ConfigOptions) (bool, error) {
executablePath := getTunnelServicePath()
fmt.Printf("Executable path is %s",executablePath)
out, err := ExecuteCmd(executablePath, "install")
fmt.Println("Shell command executed:", out, err)
return startTunnelRequest(opt, false)
@@ -101,8 +101,8 @@ func runTunnelService(opt ConfigOptions) (bool, error) {
func getTunnelServicePath() string {
var fullPath string
binFolder := filepath.Dir(os.Args[0])
exePath, _ := os.Executable()
binFolder := filepath.Dir(exePath)
switch runtime.GOOS {
case "windows":
fullPath = "HiddifyService.exe"
@@ -112,5 +112,6 @@ func getTunnelServicePath() string {
fullPath = "HiddifyService"
}
return filepath.Join(binFolder, fullPath)
abspath,_:=filepath.Abs(filepath.Join(binFolder, fullPath))
return abspath
}