|
| 1 | +//go:build gui || (!gui && !cli) |
| 2 | + |
| 3 | +package main |
| 4 | + |
| 5 | +import ( |
| 6 | + "fmt" |
| 7 | + "golang.org/x/sys/windows" |
| 8 | + "syscall" |
| 9 | + "unsafe" |
| 10 | +) |
| 11 | + |
| 12 | +func init() { |
| 13 | + if IsAdmin() { |
| 14 | + const ( |
| 15 | + MB_OKCANCEL = 0x1 |
| 16 | + MB_ICONEXCLAMATION = 0x30 |
| 17 | + |
| 18 | + IDCANCEL = 2 |
| 19 | + ) |
| 20 | + |
| 21 | + ret, _, _ := syscall.NewLazyDLL("user32.dll").NewProc("MessageBoxW").Call( |
| 22 | + uintptr(0), // hwnd (NULL) |
| 23 | + uintptr(unsafe.Pointer(Unwrap(syscall.UTF16PtrFromString( |
| 24 | + "Run me as a normal user, not administrator!\n"+ |
| 25 | + "If you didn't explicitly run me as Administrator, make sure you don't have UAC set to 'Never Notify'.\n\n"+ |
| 26 | + "VencordInstaller will close once you press OK.\n"+ |
| 27 | + "Alternatively, press Cancel to proceed anyway, but this may cause issues. Only choose this option as a last resort", |
| 28 | + )))), |
| 29 | + uintptr(unsafe.Pointer(Unwrap(syscall.UTF16PtrFromString("Do not run me as Administrator")))), |
| 30 | + uintptr(MB_OKCANCEL|MB_ICONEXCLAMATION), // flags |
| 31 | + ) |
| 32 | + |
| 33 | + if ret != IDCANCEL { |
| 34 | + panic("Ran as Administrator") |
| 35 | + } |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +func IsAdmin() bool { |
| 40 | + // most sane windows code, copy-pasted from https://github.com/golang/go/issues/28804#issuecomment-505326268 |
| 41 | + |
| 42 | + var sid *windows.SID |
| 43 | + |
| 44 | + // Although this looks scary, it is directly copied from the |
| 45 | + // official windows documentation. The Go API for this is a |
| 46 | + // direct wrap around the official C++ API. |
| 47 | + // See https://docs.microsoft.com/en-us/windows/desktop/api/securitybaseapi/nf-securitybaseapi-checktokenmembership |
| 48 | + err := windows.AllocateAndInitializeSid( |
| 49 | + &windows.SECURITY_NT_AUTHORITY, |
| 50 | + 2, |
| 51 | + windows.SECURITY_BUILTIN_DOMAIN_RID, |
| 52 | + windows.DOMAIN_ALIAS_RID_ADMINS, |
| 53 | + 0, 0, 0, 0, 0, 0, |
| 54 | + &sid) |
| 55 | + if err != nil { |
| 56 | + fmt.Println("SID Error: ", err) |
| 57 | + return false |
| 58 | + } |
| 59 | + defer windows.FreeSid(sid) |
| 60 | + |
| 61 | + token := windows.Token(0) |
| 62 | + |
| 63 | + isMember, err := token.IsMember(sid) |
| 64 | + if err != nil { |
| 65 | + fmt.Println("Token Membership Error: ", err) |
| 66 | + return false |
| 67 | + } |
| 68 | + |
| 69 | + return token.IsElevated() || isMember |
| 70 | +} |
0 commit comments