Skip to content

Commit

Permalink
fix: make sure sudo mode can still open browser
Browse files Browse the repository at this point in the history
  • Loading branch information
huwf5 committed Oct 1, 2023
1 parent 379fdb5 commit bbed36a
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
4 changes: 2 additions & 2 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -462,10 +462,10 @@ func (c *Client) Close() {
// Shutdown stops the client gracefully.
func (c *Client) Shutdown() {
defer c.Logger.Close()
c.ShutdownWithClosingLogger()
c.ShutdownWithoutClosingLogger()
}

func (c *Client) ShutdownWithClosingLogger() {
func (c *Client) ShutdownWithoutClosingLogger() {
if !atomic.CompareAndSwapUint32(&c.closing, 0, 1) {
return
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func main() {
continue
}
// avoid port conflict
c.ShutdownWithClosingLogger()
c.ShutdownWithoutClosingLogger()

err = runCmd(os.Args)
if err != nil {
Expand Down
12 changes: 11 additions & 1 deletion web/server/util/browser.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"net/http"
"net/url"
"os"
"os/exec"
"runtime"
"strings"
Expand All @@ -21,7 +22,16 @@ func OpenBrowser(url string) error {
case "darwin":
cmd = exec.Command("open", webUrl)
case "linux":
cmd = exec.Command("xdg-open", webUrl)
// Check if running as root
if os.Geteuid() == 0 {
originalUser := os.Getenv("SUDO_USER")
if originalUser == "" {
originalUser = "nobody" // fallback user if SUDO_USER is not set
}
cmd = exec.Command("sudo", "-u", originalUser, "xdg-open", webUrl)
} else {
cmd = exec.Command("xdg-open", webUrl)
}
default:
return errors.New("unsupported platform to open browser")
}
Expand Down

0 comments on commit bbed36a

Please sign in to comment.