-
-
Notifications
You must be signed in to change notification settings - Fork 81
/
find_discord_linux.go
173 lines (148 loc) · 4.31 KB
/
find_discord_linux.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
/*
* 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 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)
}
}
}
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
}