-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
293 lines (250 loc) · 8.74 KB
/
main.go
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
//go:generate go install -v github.com/kevinburke/go-bindata/v4/go-bindata
//go:generate go-bindata -prefix res/ -pkg assets -o assets/assets.go res/FirefoxESR.lnk
//go:generate go install -v github.com/josephspurrier/goversioninfo/cmd/goversioninfo
//go:generate goversioninfo -icon=res/papp.ico -manifest=res/papp.manifest
package main
import (
"fmt"
"html/template"
"os"
"path"
"strings"
"github.com/Jeffail/gabs"
"github.com/pkg/errors"
"github.com/portapps/phyrox-esr-portable/assets"
"github.com/portapps/portapps/v3"
"github.com/portapps/portapps/v3/pkg/log"
"github.com/portapps/portapps/v3/pkg/mutex"
"github.com/portapps/portapps/v3/pkg/shortcut"
"github.com/portapps/portapps/v3/pkg/utl"
"github.com/portapps/portapps/v3/pkg/win"
)
type config struct {
Profile string `yaml:"profile" mapstructure:"profile"`
MultipleInstances bool `yaml:"multiple_instances" mapstructure:"multiple_instances"`
Locale string `yaml:"locale" mapstructure:"locale"`
Cleanup bool `yaml:"cleanup" mapstructure:"cleanup"`
}
var (
app *portapps.App
cfg *config
)
const (
defaultLocale = "en-US"
)
func init() {
var err error
// Default config
cfg = &config{
Profile: "default",
MultipleInstances: false,
Locale: defaultLocale,
Cleanup: false,
}
// Init app
if app, err = portapps.NewWithCfg("phyrox-esr-portable", "Phyrox ESR", cfg); err != nil {
log.Fatal().Err(err).Msg("Cannot initialize application. See log file for more info.")
}
}
func main() {
utl.CreateFolder(app.DataPath)
profileFolder := utl.CreateFolder(app.DataPath, "profile", cfg.Profile)
app.Process = utl.PathJoin(app.AppPath, "firefox.exe")
app.Args = []string{
"--profile",
profileFolder,
}
// Set env vars
crashreporterFolder := utl.CreateFolder(app.DataPath, "crashreporter")
pluginsFolder := utl.CreateFolder(app.DataPath, "plugins")
os.Setenv("MOZ_CRASHREPORTER", "0")
os.Setenv("MOZ_CRASHREPORTER_DATA_DIRECTORY", crashreporterFolder)
os.Setenv("MOZ_CRASHREPORTER_DISABLE", "1")
os.Setenv("MOZ_CRASHREPORTER_NO_REPORT", "1")
os.Setenv("MOZ_DATA_REPORTING", "0")
os.Setenv("MOZ_MAINTENANCE_SERVICE", "0")
os.Setenv("MOZ_PLUGIN_PATH", pluginsFolder)
os.Setenv("MOZ_UPDATER", "0")
// Create and check mutex
mu, err := mutex.Create(app.ID)
defer mutex.Release(mu)
if err != nil {
if !cfg.MultipleInstances {
log.Error().Msg("You have to enable multiple instances in your configuration if you want to launch another instance")
if _, err = win.MsgBox(
fmt.Sprintf("%s portable", app.Name),
"Other instance detected. You have to enable multiple instances in your configuration if you want to launch another instance.",
win.MsgBoxBtnOk|win.MsgBoxIconError); err != nil {
log.Error().Err(err).Msg("Cannot create dialog box")
}
return
} else {
log.Warn().Msg("Another instance is already running")
}
}
// Cleanup on exit
if cfg.Cleanup {
defer func() {
utl.Cleanup([]string{
path.Join(os.Getenv("APPDATA"), "Mozilla", "Firefox"),
path.Join(os.Getenv("LOCALAPPDATA"), "Mozilla", "Firefox"),
path.Join(os.Getenv("USERPROFILE"), "AppData", "LocalLow", "Mozilla"),
})
}()
}
// Locale
locale, err := checkLocale()
if err != nil {
log.Error().Err(err).Msg("Cannot set locale")
}
// Multiple instances
if cfg.MultipleInstances {
log.Info().Msg("Multiple instances enabled")
app.Args = append(app.Args, "--no-remote")
}
// Policies
if err := createPolicies(); err != nil {
log.Fatal().Err(err).Msg("Cannot create policies")
}
// Autoconfig
prefFolder := utl.CreateFolder(app.AppPath, "defaults/pref")
autoconfig := utl.PathJoin(prefFolder, "autoconfig.js")
if err := utl.CreateFile(autoconfig, `//
pref("general.config.filename", "portapps.cfg");
pref("general.config.obscure_value", 0);`); err != nil {
log.Fatal().Err(err).Msg("Cannot write autoconfig.js")
}
// Mozilla cfg
mozillaCfgPath := utl.PathJoin(app.AppPath, "portapps.cfg")
mozillaCfgFile, err := os.Create(mozillaCfgPath)
if err != nil {
log.Fatal().Err(err).Msg("Cannot create portapps.cfg")
}
mozillaCfgData := struct {
Locale string
}{
locale,
}
mozillaCfgTpl := template.Must(template.New("mozillaCfg").Parse(`// Set locale
pref("intl.locale.requested", "{{ .Locale }}");
// Extensions scopes
lockPref("extensions.enabledScopes", 4);
lockPref("extensions.autoDisableScopes", 3);
// Don't show 'know your rights' on first run
pref("browser.rights.3.shown", true);
// Don't show WhatsNew on first run after every update
pref("browser.startup.homepage_override.mstone", "ignore");
`))
if err := mozillaCfgTpl.Execute(mozillaCfgFile, mozillaCfgData); err != nil {
log.Fatal().Err(err).Msg("Cannot write portapps.cfg")
}
// Fix extensions path
if err := updateAddonStartup(profileFolder); err != nil {
log.Error().Err(err).Msg("Cannot fix extensions path")
}
// Copy default shortcut
shortcutPath := path.Join(os.Getenv("APPDATA"), "Microsoft", "Windows", "Start Menu", "Programs", "Phyrox ESR Portable.lnk")
defaultShortcut, err := assets.Asset("FirefoxESR.lnk")
if err != nil {
log.Error().Err(err).Msg("Cannot load asset FirefoxESR.lnk")
}
err = os.WriteFile(shortcutPath, defaultShortcut, 0644)
if err != nil {
log.Error().Err(err).Msg("Cannot write default shortcut")
}
// Update default shortcut
err = shortcut.Create(shortcut.Shortcut{
ShortcutPath: shortcutPath,
TargetPath: app.Process,
Arguments: shortcut.Property{Clear: true},
Description: shortcut.Property{Value: "Phyrox ESR Portable by Portapps"},
IconLocation: shortcut.Property{Value: app.Process},
WorkingDirectory: shortcut.Property{Value: app.AppPath},
})
if err != nil {
log.Error().Err(err).Msg("Cannot create shortcut")
}
defer func() {
if err := os.Remove(shortcutPath); err != nil {
log.Error().Err(err).Msg("Cannot remove shortcut")
}
}()
defer app.Close()
app.Launch(os.Args[1:])
}
func createPolicies() error {
appFile := utl.PathJoin(utl.CreateFolder(app.AppPath, "distribution"), "policies.json")
dataFile := utl.PathJoin(app.DataPath, "policies.json")
defaultPolicies := struct {
Policies map[string]interface{} `json:"policies"`
}{
Policies: map[string]interface{}{
"DisableAppUpdate": true,
"DontCheckDefaultBrowser": true,
},
}
jsonPolicies, err := gabs.Consume(defaultPolicies)
if err != nil {
return errors.Wrap(err, "Cannot consume default policies")
}
log.Debug().Msgf("Default policies: %s", jsonPolicies.String())
if utl.Exists(dataFile) {
rawCustomPolicies, err := os.ReadFile(dataFile)
if err != nil {
return errors.Wrap(err, "Cannot read custom policies")
}
jsonPolicies, err = gabs.ParseJSON(rawCustomPolicies)
if err != nil {
return errors.Wrap(err, "Cannot consume custom policies")
}
log.Debug().Msgf("Custom policies: %s", jsonPolicies.String())
jsonPolicies.Set(true, "policies", "DisableAppUpdate")
jsonPolicies.Set(true, "policies", "DontCheckDefaultBrowser")
}
log.Debug().Msgf("Applied policies: %s", jsonPolicies.String())
err = os.WriteFile(appFile, []byte(jsonPolicies.StringIndent("", " ")), 0644)
if err != nil {
return errors.Wrap(err, "Cannot write policies")
}
return nil
}
func checkLocale() (string, error) {
extSourceFile := fmt.Sprintf("%s.xpi", cfg.Locale)
extDestFile := fmt.Sprintf("langpack-%[email protected]", cfg.Locale)
extsFolder := utl.CreateFolder(app.AppPath, "distribution", "extensions")
localeXpi := utl.PathJoin(app.AppPath, "langs", extSourceFile)
// If default locale skip (already embedded)
if cfg.Locale == defaultLocale {
return cfg.Locale, nil
}
// Check .xpi file exists
if !utl.Exists(localeXpi) {
return defaultLocale, fmt.Errorf("XPI file does not exist in %s", localeXpi)
}
// Copy .xpi
if err := utl.CopyFile(localeXpi, utl.PathJoin(extsFolder, extDestFile)); err != nil {
return defaultLocale, err
}
return cfg.Locale, nil
}
func updateAddonStartup(profileFolder string) error {
lz4File := path.Join(profileFolder, "addonStartup.json.lz4")
if !utl.Exists(lz4File) || app.Prev.RootPath == "" {
return nil
}
lz4Raw, err := mozLz4Decompress(lz4File)
if err != nil {
return err
}
prevPathLin := strings.Replace(utl.FormatUnixPath(app.Prev.RootPath), ` `, `%20`, -1)
currPathLin := strings.Replace(utl.FormatUnixPath(app.RootPath), ` `, `%20`, -1)
lz4Str := strings.Replace(string(lz4Raw), prevPathLin, currPathLin, -1)
prevPathWin := strings.Replace(strings.Replace(utl.FormatWindowsPath(app.Prev.RootPath), `\`, `\\`, -1), ` `, `%20`, -1)
currPathWin := strings.Replace(strings.Replace(utl.FormatWindowsPath(app.RootPath), `\`, `\\`, -1), ` `, `%20`, -1)
lz4Str = strings.Replace(lz4Str, prevPathWin, currPathWin, -1)
lz4Enc, err := mozLz4Compress([]byte(lz4Str))
if err != nil {
return err
}
return os.WriteFile(lz4File, lz4Enc, 0644)
}