forked from Vencord/Installer
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpatcher.go
308 lines (274 loc) · 8.21 KB
/
patcher.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/*
* SPDX-License-Identifier: GPL-3.0
* Vencord Installer, a cross platform gui/cli app for installing Vencord
* Copyright (c) 2023 Vendicated and Vencord contributors
*/
package main
import (
"encoding/json"
"errors"
"fmt"
"github.com/ProtonMail/go-appdir"
"os"
"os/exec"
path "path/filepath"
"strings"
)
var BaseDir string
var FilesDir string
var FilesDirErr error
var Patcher string
var PackageJson = []byte(`{
"name": "discord",
"main": "index.js"
}
`)
func init() {
if dir := os.Getenv("VENCORD_USER_DATA_DIR"); dir != "" {
fmt.Println("Using VENCORD_USER_DATA_DIR")
BaseDir = dir
} else if dir = os.Getenv("DISCORD_USER_DATA_DIR"); dir != "" {
fmt.Println("Using DISCORD_USER_DATA_DIR/../VencordData")
BaseDir = path.Join(dir, "..", "VencordData")
} else {
fmt.Println("Using UserConfig")
BaseDir = appdir.New("Vencord").UserConfig()
}
FilesDir = path.Join(BaseDir, "dist")
if !ExistsFile(FilesDir) {
FilesDirErr = os.MkdirAll(FilesDir, 0755)
if FilesDirErr != nil {
fmt.Println("Failed to create", FilesDir, FilesDirErr)
} else {
FilesDirErr = FixOwnership(BaseDir)
}
}
Patcher = path.Join(FilesDir, "patcher.js")
}
type DiscordInstall struct {
path string // the base path
branch string // canary / stable / ...
appPath string // List of app folder to patch
isPatched bool
isFlatpak bool
isSystemElectron bool // Needs special care https://aur.archlinux.org/packages/discord_arch_electron
isOpenAsar *bool
}
// IsSafeToDelete returns nil if path is safe to delete.
// In other cases, the returned error should give more info
func IsSafeToDelete(path string) error {
files, err := os.ReadDir(path)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
err = nil
}
return err
}
for _, file := range files {
name := file.Name()
if name != "package.json" && name != "index.js" {
return errors.New("Found file '" + name + "' which doesn't belong to Vencord.")
}
}
return nil
}
func writeFiles(dir string) error {
if err := os.RemoveAll(dir); err != nil {
return err
}
if err := os.Mkdir(dir, 0755); err != nil {
return err
}
if err := os.WriteFile(path.Join(dir, "package.json"), PackageJson, 0644); err != nil {
return err
}
patcherPath, _ := json.Marshal(Patcher)
return os.WriteFile(path.Join(dir, "index.js"), []byte("require("+string(patcherPath)+")"), 0644)
}
func patchRenames(dir string, isSystemElectron bool) (err error) {
appAsar := path.Join(dir, "app.asar")
_appAsar := path.Join(dir, "_app.asar")
var renamesDone [][]string
defer func() {
if err != nil && len(renamesDone) > 0 {
fmt.Println("Failed to patch. Undoing partial patch")
for _, rename := range renamesDone {
if innerErr := os.Rename(rename[1], rename[0]); innerErr != nil {
fmt.Println("Failed to undo partial patch. This install is probably bricked.", innerErr)
} else {
fmt.Println("Successfully undid all changes")
}
}
}
}()
fmt.Println("Renaming", appAsar, "to", _appAsar)
if err := os.Rename(appAsar, _appAsar); err != nil {
err = CheckIfErrIsCauseItsBusyRn(err)
fmt.Println(err)
return err
}
renamesDone = append(renamesDone, []string{appAsar, _appAsar})
if isSystemElectron {
from, to := appAsar+".unpacked", _appAsar+".unpacked"
fmt.Println("Renaming", from, "to", to)
err := os.Rename(from, to)
if err != nil {
return err
}
renamesDone = append(renamesDone, []string{from, to})
}
fmt.Println("Writing files to", appAsar)
if err := writeFiles(appAsar); err != nil {
return err
}
return nil
}
func (di *DiscordInstall) patch() error {
fmt.Println("Patching " + di.path + "...")
if LatestHash != InstalledHash {
if err := InstallLatestBuilds(); err != nil {
return nil // already shown dialog so don't return same error again
}
}
PreparePatch(di)
if di.isPatched {
fmt.Println(di.path, "is already patched. Unpatching first...")
if err := di.unpatch(); err != nil {
if errors.Is(err, os.ErrPermission) {
return err
}
return errors.New("patch: Failed to unpatch already patched install '" + di.path + "':\n" + err.Error())
}
}
if di.isSystemElectron {
if err := patchRenames(di.path, true); err != nil {
return err
}
} else {
if err := patchRenames(path.Join(di.appPath, ".."), false); err != nil {
return err
}
}
fmt.Println("Successfully patched", di.path)
di.isPatched = true
if di.isFlatpak {
pathElements := strings.Split(di.path, "/")
var name string
for _, e := range pathElements {
if strings.HasPrefix(e, "com.discordapp") {
name = e
break
}
}
fmt.Println("This is a flatpak. Trying to grant the Flatpak access to", FilesDir+"...")
isSystemFlatpak := strings.HasPrefix(di.path, "/var")
var args []string
if !isSystemFlatpak {
args = append(args, "--user")
}
args = append(args, "override", name, "--filesystem="+FilesDir)
fullCmd := "flatpak " + strings.Join(args, " ")
fmt.Println("Running", fullCmd)
var err error
if !isSystemFlatpak && os.Getuid() == 0 {
// We are operating on a user flatpak but are root
actualUser := os.Getenv("SUDO_USER")
fmt.Println("This is a user install but we are root. Using su to run as", actualUser)
cmd := exec.Command("su", "-", actualUser, "-c", "sh", "-c", fullCmd)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Run()
} else {
cmd := exec.Command("flatpak", args...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Run()
}
if err != nil {
return errors.New("Failed to grant Discord Flatpak access to " + FilesDir + ": " + err.Error())
}
}
return nil
}
func unpatchRenames(dir string, isSystemElectron bool) (errOut error) {
appAsar := path.Join(dir, "app.asar")
appAsarTmp := path.Join(dir, "app.asar.tmp")
_appAsar := path.Join(dir, "_app.asar")
var renamesDone [][]string
defer func() {
if errOut != nil && len(renamesDone) > 0 {
fmt.Println("Failed to unpatch. Undoing partial unpatch")
for _, rename := range renamesDone {
if innerErr := os.Rename(rename[1], rename[0]); innerErr != nil {
fmt.Println("Failed to undo partial unpatch. This install is probably bricked.", innerErr)
} else {
fmt.Println("Successfully undid all changes")
}
}
} else if errOut == nil {
if innerErr := os.RemoveAll(appAsarTmp); innerErr != nil {
fmt.Println("Failed to delete temporary app.asar (patch folder) backup. This is whatever but you might want to delete it manually.", innerErr)
}
}
}()
fmt.Println("Deleting", appAsar)
if err := os.Rename(appAsar, appAsarTmp); err != nil {
err = CheckIfErrIsCauseItsBusyRn(err)
fmt.Println(err)
errOut = err
} else {
renamesDone = append(renamesDone, []string{appAsar, appAsarTmp})
}
fmt.Println("Renaming", _appAsar, "to", appAsar)
if err := os.Rename(_appAsar, appAsar); err != nil {
err = CheckIfErrIsCauseItsBusyRn(err)
fmt.Println(err)
errOut = err
} else {
renamesDone = append(renamesDone, []string{_appAsar, appAsar})
}
if isSystemElectron {
fmt.Println("Renaming", _appAsar+".unpacked", "to", appAsar+".unpacked")
if err := os.Rename(_appAsar+".unpacked", appAsar+".unpacked"); err != nil {
fmt.Println(err)
errOut = err
}
}
return
}
func (di *DiscordInstall) unpatch() error {
fmt.Println("Unpatching " + di.path + "...")
PreparePatch(di)
if di.isSystemElectron {
fmt.Println("Detected as System Electron Install")
// See comment in Patch
if err := unpatchRenames(di.path, true); err != nil {
return err
}
} else {
isCanaryHack := IsDirectory(path.Join(di.appPath, "..", "app.asar"))
if isCanaryHack {
if err := unpatchRenames(path.Join(di.appPath, ".."), false); err != nil {
return err
}
} else {
err := IsSafeToDelete(di.appPath)
if errors.Is(err, os.ErrPermission) {
fmt.Println("Permission to read", di.appPath, "denied")
return err
}
fmt.Println("Checking if", di.appPath, "is safe to delete:", Ternary(err == nil, "Yes", "No"))
if err != nil {
return errors.New("Deleting patch folder '" + di.appPath + "' is possibly unsafe. Please do it manually: " + err.Error())
}
fmt.Println("Deleting", di.appPath)
err = os.RemoveAll(di.appPath)
if err != nil {
return err
}
}
}
fmt.Println("Successfully unpatched", di.path)
di.isPatched = false
return nil
}