|
1 | 1 | package main
|
2 | 2 |
|
3 | 3 | import (
|
4 |
| - "fmt" |
5 | 4 | "os"
|
| 5 | + "path/filepath" |
6 | 6 |
|
| 7 | + "golang.org/x/sys/windows" |
7 | 8 | "golang.org/x/sys/windows/registry"
|
8 | 9 | )
|
9 | 10 |
|
10 | 11 | func init() {
|
| 12 | + ex, err := os.Executable() |
| 13 | + if err != nil { |
| 14 | + panic(err) |
| 15 | + } |
| 16 | + |
| 17 | + exPath := filepath.Dir(ex) |
| 18 | + err = os.Chdir(exPath) |
| 19 | + if err != nil { |
| 20 | + panic(err) |
| 21 | + } |
| 22 | + |
| 23 | + hasAdmin, err := CheckAdmin() |
| 24 | + if err != nil { |
| 25 | + panic(err) |
| 26 | + } |
| 27 | + |
| 28 | + if hasAdmin { |
| 29 | + err = UpdateRegistry() |
| 30 | + if err != nil { |
| 31 | + panic(err) |
| 32 | + } |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +func UpdateRegistry() error { |
11 | 37 | key, _, err := registry.CreateKey(registry.CLASSES_ROOT, "twitch", registry.WRITE)
|
12 | 38 | if err != nil {
|
13 |
| - fmt.Println(err) |
| 39 | + return err |
14 | 40 | }
|
15 | 41 |
|
16 | 42 | defer key.Close()
|
17 | 43 |
|
18 | 44 | err = key.SetStringValue("", "URL: Twitch Handler")
|
19 | 45 | if err != nil {
|
20 |
| - fmt.Println(err) |
| 46 | + return err |
21 | 47 | }
|
22 | 48 |
|
23 | 49 | err = key.SetStringValue("URL Protocol", "")
|
24 | 50 | if err != nil {
|
25 |
| - fmt.Println(err) |
| 51 | + return err |
26 | 52 | }
|
27 | 53 |
|
28 | 54 | iconKey, _, err := registry.CreateKey(key, "DefaultIcon", registry.WRITE)
|
29 | 55 | if err != nil {
|
30 |
| - fmt.Println(err) |
| 56 | + return err |
31 | 57 | }
|
32 | 58 |
|
33 | 59 | defer iconKey.Close()
|
34 | 60 |
|
35 | 61 | dir, err := os.Getwd()
|
36 | 62 | if err != nil {
|
37 |
| - fmt.Println(err) |
| 63 | + return err |
38 | 64 | }
|
39 | 65 |
|
40 | 66 | err = iconKey.SetStringValue("", dir+"\\MultiMC.exe")
|
41 | 67 | if err != nil {
|
42 |
| - fmt.Println(err) |
| 68 | + return err |
43 | 69 | }
|
44 | 70 |
|
45 | 71 | shellKey, _, err := registry.CreateKey(key, "shell", registry.WRITE)
|
46 | 72 | if err != nil {
|
47 |
| - fmt.Println(err) |
| 73 | + return err |
48 | 74 | }
|
49 | 75 |
|
50 | 76 | defer shellKey.Close()
|
51 | 77 |
|
52 | 78 | openKey, _, err := registry.CreateKey(shellKey, "open", registry.WRITE)
|
53 | 79 | if err != nil {
|
54 |
| - fmt.Println(err) |
| 80 | + return err |
55 | 81 | }
|
56 | 82 |
|
57 | 83 | defer openKey.Close()
|
58 | 84 |
|
59 | 85 | commandKey, _, err := registry.CreateKey(openKey, "command", registry.WRITE)
|
60 | 86 | if err != nil {
|
61 |
| - fmt.Println(err) |
| 87 | + return err |
62 | 88 | }
|
63 | 89 |
|
64 | 90 | defer commandKey.Close()
|
65 | 91 |
|
66 | 92 | executable, err := os.Executable()
|
67 | 93 | if err != nil {
|
68 |
| - fmt.Println(err) |
| 94 | + return err |
69 | 95 | }
|
70 | 96 |
|
71 | 97 | err = commandKey.SetStringValue("", "\""+executable+"\" \"%1\"")
|
72 | 98 | if err != nil {
|
73 |
| - fmt.Println(err) |
| 99 | + return err |
| 100 | + } |
| 101 | + |
| 102 | + return nil |
| 103 | +} |
| 104 | + |
| 105 | +// CheckAdmin determines if we have administrative privileges. |
| 106 | +// Ref: https://coolaj86.com/articles/golang-and-windows-and-admins-oh-my/ |
| 107 | +func CheckAdmin() (bool, error) { |
| 108 | + var sid *windows.SID |
| 109 | + |
| 110 | + // Although this looks scary, it is directly copied from the |
| 111 | + // official windows documentation. The Go API for this is a |
| 112 | + // direct wrap around the official C++ API. |
| 113 | + // See https://docs.microsoft.com/en-us/windows/desktop/api/securitybaseapi/nf-securitybaseapi-checktokenmembership |
| 114 | + err := windows.AllocateAndInitializeSid( |
| 115 | + &windows.SECURITY_NT_AUTHORITY, |
| 116 | + 2, |
| 117 | + windows.SECURITY_BUILTIN_DOMAIN_RID, |
| 118 | + windows.DOMAIN_ALIAS_RID_ADMINS, |
| 119 | + 0, 0, 0, 0, 0, 0, |
| 120 | + &sid) |
| 121 | + if err != nil { |
| 122 | + return false, err |
74 | 123 | }
|
| 124 | + |
| 125 | + // This appears to cast a null pointer so I'm not sure why this |
| 126 | + // works, but this guy says it does and it Works for Me™: |
| 127 | + // https://github.com/golang/go/issues/28804#issuecomment-438838144 |
| 128 | + token := windows.Token(0) |
| 129 | + |
| 130 | + member, err := token.IsMember(sid) |
| 131 | + if err != nil { |
| 132 | + return false, err |
| 133 | + } |
| 134 | + |
| 135 | + return member, nil |
75 | 136 | }
|
0 commit comments