From 3fe7cfd090d3680214c0ef601963b46455a8e40b Mon Sep 17 00:00:00 2001 From: aomizu Date: Thu, 29 May 2025 18:18:25 +0900 Subject: [PATCH] correct authentication --- FyneApp.toml | 2 +- pkg/service/service.go | 38 +++++++++++++++++++++++++++++++++++++- 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/FyneApp.toml b/FyneApp.toml index 1185d77..1733906 100644 --- a/FyneApp.toml +++ b/FyneApp.toml @@ -3,4 +3,4 @@ Name = "TurtleSilicon" ID = "com.tairasu.turtlesilicon" Version = "1.1.0" - Build = 13 + Build = 17 diff --git a/pkg/service/service.go b/pkg/service/service.go index 1a6b4e9..e7a285e 100644 --- a/pkg/service/service.go +++ b/pkg/service/service.go @@ -1,10 +1,12 @@ package service import ( + "bytes" "fmt" "log" "os/exec" "path/filepath" + "strings" "syscall" "time" @@ -121,7 +123,41 @@ func StartRosettaX87Service(myWindow fyne.Window, updateAllStatuses func()) { // startServiceWithPassword starts the service using sudo with the provided password func startServiceWithPassword(workingDir, executable, password string) error { - // Use sudo with the password + // First test if the password is correct with a simple sudo command + testCmd := exec.Command("sudo", "-S", "-v") + testStdin, err := testCmd.StdinPipe() + if err != nil { + return fmt.Errorf("failed to create test stdin pipe: %v", err) + } + + // Capture stderr to check for authentication errors + var stderr bytes.Buffer + testCmd.Stderr = &stderr + + err = testCmd.Start() + if err != nil { + return fmt.Errorf("failed to start test command: %v", err) + } + + // Send the password for testing + _, err = testStdin.Write([]byte(password + "\n")) + if err != nil { + testCmd.Process.Kill() + return fmt.Errorf("failed to send test password: %v", err) + } + testStdin.Close() + + // Wait for the test command to complete + err = testCmd.Wait() + if err != nil { + stderrOutput := stderr.String() + if strings.Contains(stderrOutput, "Sorry, try again") || strings.Contains(stderrOutput, "incorrect password") { + return fmt.Errorf("incorrect password") + } + return fmt.Errorf("sudo authentication failed: %v", err) + } + + // If we get here, the password is correct, now start the actual service cmd := exec.Command("sudo", "-S", executable) cmd.Dir = workingDir