-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
219 lines (188 loc) · 6.71 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
//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/FirefoxDeveloperEdition.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"
"os"
"path"
"strings"
"github.com/Jeffail/gabs"
"github.com/pkg/errors"
"github.com/portapps/phyrox-developer-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"`
Cleanup bool `yaml:"cleanup" mapstructure:"cleanup"`
}
var (
app *portapps.App
cfg *config
)
func init() {
var err error
// Default config
cfg = &config{
Profile: "default",
MultipleInstances: false,
Cleanup: false,
}
// Init app
if app, err = portapps.NewWithCfg("phyrox-developer-portable", "Phyrox Developer Edition", 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"),
})
}()
}
// 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")
}
// 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 Developer Edition Portable.lnk")
defaultShortcut, err := assets.Asset("FirefoxDeveloperEdition.lnk")
if err != nil {
log.Error().Err(err).Msg("Cannot load asset FirefoxDeveloperEdition.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 Developer Edition 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 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)
}