-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbrowserScan.go
More file actions
261 lines (224 loc) · 6.88 KB
/
browserScan.go
File metadata and controls
261 lines (224 loc) · 6.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
//go:build windows
// +build windows
package main
import (
"encoding/json"
"errors"
"fmt"
"log"
"os"
"path/filepath"
"strings"
"github.com/google/uuid"
walkd "github.com/lxn/walk/declarative"
"golang.org/x/sys/windows/registry"
"github.com/stuartleeks/pick-a-browser/pkg/config"
)
func HandleBrowserScan(settings *config.Settings) error {
allBrowsers, err := getAllBrowsers()
if err != nil {
return err
}
mergedBrowsers := mergeSettings(settings.Browsers, allBrowsers)
newSettings := map[string]interface{}{
"browsers": mergedBrowsers,
}
buf, err := json.MarshalIndent(newSettings, "", "\t")
if err != nil {
return err
}
settingsString := strings.ReplaceAll(string(buf), "\n", "\r\n")
window := walkd.MainWindow{
Title: "pick-a-browser...",
MinSize: walkd.Size{Width: 500, Height: 400},
Size: walkd.Size{Width: 500, Height: 400},
Layout: walkd.VBox{MarginsZero: true},
Children: []walkd.Widget{
walkd.TextEdit{
Text: settingsString,
ReadOnly: true,
AlwaysConsumeSpace: true,
VScroll: true,
},
},
}
if _, err := window.Run(); err != nil {
log.Fatal(err)
}
return nil
}
func mergeSettings(baseBrowsers []config.Browser, newBrowsers []config.Browser) []config.Browser {
result := []config.Browser{}
result = append(result, baseBrowsers...)
for _, newBrowser := range newBrowsers {
found := hasMatchOnExeAndArgs(baseBrowsers, newBrowser)
if !found {
result = append(result, newBrowser)
}
}
return result
}
func hasMatchOnExeAndArgs(browsers []config.Browser, browserToMatch config.Browser) bool {
for _, browser := range browsers {
if browser.Exe == browserToMatch.Exe &&
((browser.Args == nil && browserToMatch.Args == nil) ||
*browser.Args == *browserToMatch.Args) {
return true
}
}
return false
}
func getAllBrowsers() ([]config.Browser, error) {
hklmBrowsers, err := getBrowsersFor(registry.LOCAL_MACHINE)
if err != nil {
return []config.Browser{}, fmt.Errorf("failed to local HKLM browsers: %s", err)
}
hkcuBrowsers, err := getBrowsersFor(registry.CURRENT_USER)
if err != nil {
return []config.Browser{}, fmt.Errorf("failed to local HKCU browsers: %s", err)
}
browsersTemp := append(hkcuBrowsers, hklmBrowsers...)
browsersExpanded := []config.Browser{}
for _, browser := range browsersTemp {
switch browser.Name {
case "Microsoft Edge":
edgeProfiles, err := getEdgeProfiles()
if err != nil {
return []config.Browser{}, fmt.Errorf("failed to get Edge profiles: %s", err)
}
for _, edgeProfile := range edgeProfiles {
args := fmt.Sprintf("--profile-directory=\"%s\"", edgeProfile)
browsersExpanded = append(browsersExpanded, config.Browser{
Id: uuid.NewString(),
Name: browser.Name + " - " + edgeProfile,
Exe: browser.Exe,
Args: &args,
IconPath: browser.IconPath,
Hidden: false,
})
}
case "Google Chrome":
edgeProfiles, err := getChromeProfiles()
if err != nil {
return []config.Browser{}, fmt.Errorf("failed to get Chrome profiles: %s", err)
}
for _, edgeProfile := range edgeProfiles {
args := fmt.Sprintf("--profile-directory=\"%s\"", edgeProfile)
browsersExpanded = append(browsersExpanded, config.Browser{
Id: uuid.NewString(),
Name: browser.Name + " - " + edgeProfile,
Exe: browser.Exe,
Args: &args,
IconPath: browser.IconPath,
Hidden: false,
})
}
default:
browsersExpanded = append(browsersExpanded, browser)
}
}
return browsersExpanded, nil
}
const reg_READ = registry.ENUMERATE_SUB_KEYS | registry.QUERY_VALUE
func getBrowsersFor(rootKey registry.Key) ([]config.Browser, error) {
browsersKey, err := registry.OpenKey(rootKey, "SOFTWARE\\Clients\\StartMenuInternet", reg_READ)
if err != nil {
return []config.Browser{}, fmt.Errorf("failed to open SOFTWARE\\Clients\\StartMenuInternet")
}
browserKeyNames, err := browsersKey.ReadSubKeyNames(-1)
if err != nil {
return []config.Browser{}, fmt.Errorf("failed to get subkeys for SOFTWARE\\Clients\\StartMenuInternet")
}
browsers := []config.Browser{}
for _, browserKeyName := range browserKeyNames {
if browserKeyName == "pick-a-browser" {
continue
}
browserKey, err := registry.OpenKey(browsersKey, browserKeyName, reg_READ)
if err != nil {
return []config.Browser{}, fmt.Errorf("failed to get open browser key %q", browserKeyName)
}
browser, err := getBrowserFromRegistry(browserKey, browserKeyName)
// TODO - decide whether to skip on error or fail?
if err != nil {
return []config.Browser{}, fmt.Errorf("failed to parse browser settings (%q): %s", browserKeyName, err)
}
browsers = append(browsers, browser)
}
return browsers, nil
}
func getBrowserFromRegistry(key registry.Key, keyName string) (config.Browser, error) {
commandKey, err := registry.OpenKey(key, "shell\\open\\command", reg_READ)
if err != nil {
return config.Browser{}, fmt.Errorf("failed to open shell\\open\\command key: %s", err)
}
exe, _, err := commandKey.GetStringValue("")
if err != nil {
return config.Browser{}, fmt.Errorf("failed to get value for shell\\open\\command key: %s", err)
}
exe = strings.Trim(exe, "\"")
name, _, err := key.GetStringValue("")
if err != nil {
name = keyName
}
var iconPath *string
iconKey, err := registry.OpenKey(key, "DefaultIcon", reg_READ)
if err == nil {
iconValue, _, err := iconKey.GetStringValue("")
if err != nil {
return config.Browser{}, fmt.Errorf("failed to get DefaultIcon value: %s", err)
}
iconPath = &iconValue
} else {
if !errors.Is(err, registry.ErrNotExist) {
return config.Browser{}, fmt.Errorf("failed to open DefaultIcon key: %s", err)
}
}
return config.Browser{
Id: uuid.NewString(),
Name: name,
Exe: exe,
IconPath: iconPath,
Hidden: false,
}, nil
}
func getEdgeProfiles() ([]string, error) {
userProfile := os.Getenv("LOCALAPPDATA")
edgeUserDataPath := filepath.Join(userProfile, "Microsoft\\Edge\\User Data")
entries, err := os.ReadDir(edgeUserDataPath)
if err != nil {
return []string{}, nil
}
profiles := []string{"Default"}
for _, entry := range entries {
if entry.IsDir() && strings.HasPrefix(entry.Name(), "Profile ") {
profiles = append(profiles, entry.Name())
}
}
return profiles, nil
}
func getChromeProfiles() ([]string, error) {
userProfile := os.Getenv("LOCALAPPDATA")
chromeUserDataPath := filepath.Join(userProfile, "Google\\Chrome\\User Data")
entries, err := os.ReadDir(chromeUserDataPath)
if err != nil {
return []string{}, nil
}
profiles := []string{"Default"}
for _, entry := range entries {
if entry.IsDir() && strings.HasPrefix(entry.Name(), "Profile ") {
profiles = append(profiles, entry.Name())
}
}
return profiles, nil
}
func getRootKeyName(key registry.Key) string {
switch key {
case registry.CLASSES_ROOT:
return "HKCR"
case registry.LOCAL_MACHINE:
return "HKLM"
default:
return "unhandled root key"
}
}