Skip to content

Commit

Permalink
fix: split isAdmin to platform files
Browse files Browse the repository at this point in the history
  • Loading branch information
rxri committed Feb 27, 2025
1 parent 54f4cad commit ebe60cd
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 32 deletions.
34 changes: 2 additions & 32 deletions spicetify.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
"github.com/spicetify/cli/src/cmd"
spotifystatus "github.com/spicetify/cli/src/status/spotify"
"github.com/spicetify/cli/src/utils"
"golang.org/x/sys/windows"
"github.com/spicetify/cli/src/utils/isAdmin"
)

var (
Expand All @@ -36,36 +36,6 @@ var (
bypassAdminCheck = false
)

func isAdmin(bypassAdminCheck bool) bool {
if bypassAdminCheck {
return false
}

switch runtime.GOOS {
case "windows":
var sid *windows.SID
err := windows.AllocateAndInitializeSid(
&windows.SECURITY_NT_AUTHORITY,
2,
windows.SECURITY_BUILTIN_DOMAIN_RID,
windows.DOMAIN_ALIAS_RID_ADMINS,
0, 0, 0, 0, 0, 0,
&sid)
if err != nil {
return false
}
defer windows.FreeSid(sid)

token := windows.Token(0)
member, err := token.IsMember(sid)
return err == nil && member

case "linux", "darwin":
return os.Geteuid() == 0
}
return false
}

func init() {
if runtime.GOOS != "windows" &&
runtime.GOOS != "darwin" &&
Expand Down Expand Up @@ -144,7 +114,7 @@ func init() {
os.Stdout = nil
}

if isAdmin(bypassAdminCheck) {
if isAdmin.Check(bypassAdminCheck) {
utils.PrintError("Spicetify should not be run with administrator/root privileges")
utils.PrintError("Running as admin can cause Spotify to show a black/blank window after applying spicetify")
utils.PrintError("This happens because Spotify (running as a normal user) can't access files modified with admin privileges")
Expand Down
13 changes: 13 additions & 0 deletions src/utils/isAdmin/unix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//go:build !windows
// +build !windows

package isAdmin

import "os"

func Check(bypassAdminCheck bool) bool {
if bypassAdminCheck {
return false
}
return os.Geteuid() == 0
}
31 changes: 31 additions & 0 deletions src/utils/isAdmin/windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//go:build windows
// +build windows

package isAdmin

import (
"golang.org/x/sys/windows"
)

func Check(bypassAdminCheck bool) bool {
if bypassAdminCheck {
return false
}

var sid *windows.SID
err := windows.AllocateAndInitializeSid(
&windows.SECURITY_NT_AUTHORITY,
2,
windows.SECURITY_BUILTIN_DOMAIN_RID,
windows.DOMAIN_ALIAS_RID_ADMINS,
0, 0, 0, 0, 0, 0,
&sid)
if err != nil {
return false
}
defer windows.FreeSid(sid)

token := windows.Token(0)
member, err := token.IsMember(sid)
return err == nil && member
}

0 comments on commit ebe60cd

Please sign in to comment.