Repository navigation
Expand file tree
/
Copy pathfind_discord_linux.go
More file actions
238 lines (206 loc) · 5.92 KB
/
Copy pathfind_discord_linux.go
File metadata and controls
238 lines (206 loc) · 5.92 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
/*
* 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 (
"errors"
"io/fs"
"os"
"os/user"
path "path/filepath"
"strconv"
"strings"
)
var (
Home string
DiscordDirs []string
)
func init() {
// If ran as root, the HOME environment variable will be that of root.
// SUDO_USER and DOAS_USER tell us the actual user
var sudoUser = os.Getenv("SUDO_USER")
if sudoUser == "" {
sudoUser = os.Getenv("DOAS_USER")
if sudoUser != "" {
_ = os.Setenv("SUDO_USER", sudoUser)
}
}
if sudoUser != "" {
if sudoUser == "root" {
panic("VencordInstaller must not be run as the root user. Please rerun as normal user. Use sudo or doas to run as root.")
}
Log.Debug("VencordInstaller was run with root privileges, actual user is", sudoUser)
Log.Debug("Looking up HOME of", sudoUser)
u, err := user.Lookup(sudoUser)
if err != nil {
Log.Warn("Failed to lookup HOME", err)
} else {
Log.Debug("Actual HOME is", u.HomeDir)
_ = os.Setenv("HOME", u.HomeDir)
}
} else if os.Getuid() == 0 {
panic("VencordInstaller was run as root but neither SUDO_USER nor DOAS_USER are set. Please rerun me as a normal user, with sudo/doas, or manually set SUDO_USER to your username")
}
Home = os.Getenv("HOME")
DiscordDirs = []string{
"/usr/share",
"/usr/lib64",
"/opt",
path.Join(Home, ".local/share"),
path.Join(Home, ".dvm"),
"/var/lib/flatpak/app",
path.Join(Home, "/.local/share/flatpak/app"),
}
}
func ParseDiscordNew(p, branch string, isFlatpak bool) *DiscordInstall {
entries, err := os.ReadDir(p)
if err != nil {
if !errors.Is(err, os.ErrNotExist) {
Log.Warn("Error during readdir "+p+":", err)
}
return nil
}
isPatched := false
appPath := ""
for _, dir := range entries {
if dir.IsDir() && strings.HasPrefix(dir.Name(), "app-") {
resources := path.Join(p, dir.Name(), "resources")
if !ExistsFile(resources) {
continue
}
app := path.Join(resources, "app")
if app > appPath {
appPath = app
isPatched = ExistsFile(path.Join(resources, "_app.asar"))
}
}
}
if appPath == "" {
return nil
}
if branch == "" {
branch = GetBranch(p)
}
return &DiscordInstall{
path: p,
branch: branch,
appPath: appPath,
isPatched: isPatched,
isFlatpak: isFlatpak,
isSystemElectron: false,
}
}
func ParseDiscord(p, _ string) *DiscordInstall {
name := path.Base(p)
needsFlatpakResolve := strings.Contains(p, "/flatpak/") && !strings.Contains(p, "/current/active/files/")
if needsFlatpakResolve {
discordName := strings.ToLower(name[len("com.discordapp."):])
if discordName != "discord" { //
// DiscordCanary -> discord-canary
discordName = discordName[:7] + "-" + discordName[7:]
}
p = path.Join(p, "current/active/files", discordName)
}
resources := path.Join(p, "resources")
app := path.Join(resources, "app")
isPatched, isSystemElectron := false, false
if ExistsFile(resources) { // normal install
isPatched = ExistsFile(path.Join(resources, "_app.asar"))
} else if ExistsFile(path.Join(p, "app.asar")) { // System electron doesn't have resources folder
isSystemElectron = true
isPatched = ExistsFile(path.Join(p, "_app.asar.unpacked"))
} else {
// Log.Warn("Tried to parse invalid Location:", p)
return nil
}
return &DiscordInstall{
path: p,
branch: GetBranch(name),
appPath: app,
isPatched: isPatched,
isFlatpak: needsFlatpakResolve,
isSystemElectron: isSystemElectron,
}
}
func FindDiscords() []any {
var discords []any
for _, dir := range DiscordDirs {
children, err := os.ReadDir(dir)
if err != nil {
if !errors.Is(err, os.ErrNotExist) {
Log.Warn("Error during readdir "+dir+":", err)
}
continue
}
for _, child := range children {
name := child.Name()
if !child.IsDir() || !SliceContains(LinuxDiscordNames, name) {
continue
}
discordDir := path.Join(dir, name)
if discord := ParseDiscord(discordDir, ""); discord != nil {
Log.Debug("Found Discord install at ", discordDir)
discords = append(discords, discord)
}
}
}
for _, name := range []string{"discord", "discordcanary", "discordptb"} {
discordDir := path.Join(Home, ".config", name)
if !ExistsFile(discordDir) {
continue
}
if discord := ParseDiscordNew(discordDir, GetBranch(name), false); discord != nil {
Log.Debug("Found Discord install at ", discordDir)
discords = append(discords, discord)
}
}
for _, name := range []string{"Discord", "DiscordCanary", "DiscordPTB"} {
discordDir := path.Join(Home, ".var/app", "com.discordapp."+name, "config/discord")
if !ExistsFile(discordDir) {
continue
}
if discord := ParseDiscordNew(discordDir, GetBranch(name), true); discord != nil {
Log.Debug("Found Discord install at ", discordDir)
discords = append(discords, discord)
}
}
return discords
}
func PreparePatch(di *DiscordInstall) {}
// FixOwnership fixes file ownership on Linux
func FixOwnership(p string) error {
if os.Geteuid() != 0 {
return nil
}
Log.Debug("Fixing Ownership of", p)
sudoUser := os.Getenv("SUDO_USER")
if sudoUser == "" {
panic("SUDO_USER was empty. This point should never be reached")
}
Log.Debug("Looking up User", sudoUser)
u, err := user.Lookup(sudoUser)
if err != nil {
Log.Error("Lookup failed:", err)
return err
}
Log.Debug("Lookup successful, Uid", u.Uid, "Gid", u.Gid)
// This conversion is safe because of the GOOS guard above
uid, _ := strconv.Atoi(u.Uid)
gid, _ := strconv.Atoi(u.Gid)
err = path.WalkDir(p, func(path string, d fs.DirEntry, err error) error {
if err == nil {
err = os.Chown(path, uid, gid)
Log.Debug("chown", u.Uid+":"+u.Gid, path+":", Ternary(err == nil, "Success!", "Failed"))
}
return err
})
if err != nil {
Log.Error("Failed to fix ownership:", err)
}
return err
}
func CheckScuffedInstall() bool {
return false
}