-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathSimpleDiscordCryptInstaller.ps1
244 lines (194 loc) · 8.92 KB
/
SimpleDiscordCryptInstaller.ps1
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
$ErrorActionPreference = 'Stop'
$startMenuPath = [Environment]::GetFolderPath('StartMenu')+'\Programs\Discord Inc\'
$desktopPath = [Environment]::GetFolderPath('Desktop')+'\'
$taskbarPath = $env:APPDATA+'\Microsoft\Internet Explorer\Quick Launch\User Pinned\TaskBar\'
$discordPath = $env:LOCALAPPDATA+'\Discord'
$discordDataPath = $env:APPDATA+'\discord'
$discordResourcesPath = $discordPath+'\app-*'
$discordIconPath = $startMenuPath+'Discord.lnk'
$discordDesktopIconPath = $desktopPath+'Discord.lnk'
$discordTaskbarIconPath = $taskbarPath+'Discord.lnk'
$discordExeName = 'Discord.exe'
$discordPtbPath = $env:LOCALAPPDATA+'\DiscordPTB'
$discordPtbDataPath = $env:APPDATA+'\discordptb'
$discordPtbResourcesPath = $discordPtbPath+'\app-*'
$discordPtbIconPath = $startMenuPath+'Discord PTB.lnk'
$discordPtbDesktopIconPath = $desktopPath+'Discord PTB.lnk'
$discordPtbTaskbarIconPath = $taskbarPath+'Discord PTB.lnk'
$discordPtbExeName = 'DiscordPTB.exe'
$discordCanaryPath = $env:LOCALAPPDATA+'\DiscordCanary'
$discordCanaryDataPath = $env:APPDATA+'\discordcanary'
$discordCanaryResourcesPath = $discordCanaryPath+'\app-*'
$discordCanaryIconPath = $startMenuPath+'Discord Canary.lnk'
$discordCanaryDesktopIconPath = $desktopPath+'Discord Canary.lnk'
$discordCanaryTaskbarIconPath = $taskbarPath+'Discord Canary.lnk'
$discordCanaryExeName = 'DiscordCanary.exe'
$iconLocation = '\app.ico,0'
$pluginPath = $env:LOCALAPPDATA+'\SimpleDiscordCrypt'
$startupRegistry = 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Run'
$shell = New-Object -ComObject WScript.Shell
function RootElectron([string]$discordIconPath, [string]$exeName, [string]$path, [string]$resourcesPath, [string]$desktopIconPath, [string]$taskbarIconPath) {
'rooting'
$shortcut = $shell.CreateShortcut($discordIconPath)
$workingDirectory = $shortcut.WorkingDirectory
if($workingDirectory -eq "") {
$shortcut.WorkingDirectory = $workingDirectory = (Resolve-Path $resourcesPath | % { $_.Path } | Measure -Maximum).Maximum
$shortcut.IconLocation = $path + $iconLocation
}
$electronLink = "$workingDirectory\electron.exe"
if(!(Test-Path $electronLink)) {
[void](New-Item -Path $electronLink -Value "$workingDirectory\$exeName" -ItemType HardLink)
}
$shortcut.TargetPath = $env:WINDIR+'\System32\cmd.exe'
$shortcut.Arguments = "/c `"set NODE_OPTIONS=-r ../../SimpleDiscordCrypt/NodeLoad.js && start ^`"^`" ^`"$path\Update.exe^`" --processStart electron.exe`""
$shortcut.WindowStyle = 7
$shortcut.Save()
if(Test-Path $desktopIconPath) {
copy $discordIconPath $desktopIconPath -Force
}
if(Test-Path $taskbarIconPath) {
copy $discordIconPath $taskbarIconPath -Force
}
}
function RemoveExtension([string]$electonDataPath) {
$extensionListPath = "$electonDataPath\DevTools Extensions"
if(Test-Path $extensionListPath) {
[string]$s = Get-Content $extensionListPath
if($s.Length -ne 0) {
$extensionList = ConvertFrom-Json $s
$newExtensionList = @($extensionList | ? { $_ -notmatch '(?:^|[\\\/])SimpleDiscordcrypt[\\\/]?$' })
if($newExtensionList.Length -ne $extensionList.Length) {
'removing old extension'
Set-Content $extensionListPath (ConvertTo-Json $newExtensionList)
}
}
}
}
function ReplaceStartup([string]$registryKey, [string]$newPath) {
if((Get-ItemProperty -Path $startupRegistry -Name $registryKey -ErrorAction SilentlyContinue).$registryKey -ne $null) {
'replacing startup'
Set-ItemProperty -Path $startupRegistry -Name $registryKey -Value $newPath
}
}
$install = $false
try {
while(Test-Path $discordPath) {
'Discord found'
if(Test-Path $discordDataPath) { 'data directory found' } else { 'data directory not found'; break }
if(Test-Path $discordResourcesPath) { 'resources directory found' } else { 'resources directory not found'; break }
RemoveExtension $discordDataPath
RootElectron $discordIconPath $discordExeName $discordPath $discordResourcesPath $discordDesktopIconPath $discordTaskbarIconPath
ReplaceStartup 'Discord' $discordIconPath
$install = $true
break
}
while(Test-Path $discordPtbPath) {
'DiscordPTB found'
if(Test-Path $discordPtbDataPath) { 'data directory found' } else { 'data directory not found'; break }
if(Test-Path $discordPtbResourcesPath) { 'resources directory found' } else { 'resources directory not found'; break }
RemoveExtension $discordPtbDataPath
RootElectron $discordPtbIconPath $discordPtbExeName $discordPtbPath $discordPtbResourcesPath $discordPtbDesktopIconPath $discordPtbTaskbarIconPath
ReplaceStartup 'DiscordPTB' $discordPtbIconPath
$install = $true
break
}
while(Test-Path $discordCanaryPath) {
'DiscordCanary found'
if(Test-Path $discordCanaryDataPath) { 'data directory found' } else { 'data directory not found'; break }
if(Test-Path $discordCanaryResourcesPath) { 'resources directory found' } else { 'resources directory not found'; break }
RemoveExtension $discordCanaryDataPath
RootElectron $discordCanaryIconPath $discordCanaryExeName $discordCanaryPath $discordCanaryResourcesPath $discordCanaryDesktopIconPath $discordCanaryTaskbarIconPath
ReplaceStartup 'DiscordCanary' $discordCanaryIconPath
$install = $true
break
}
if($install) {
'installing'
[void](New-Item "$pluginPath\NodeLoad.js" -Type File -Force -Value @'
const onHeadersReceived = (details, callback) => {
let response = { cancel: false };
let responseHeaders = details.responseHeaders;
if(responseHeaders['content-security-policy'] != null) {
responseHeaders['content-security-policy'] = [""];
response.responseHeaders = responseHeaders;
}
callback(response);
};
let originalBrowserWindow;
function browserWindowHook(options) {
if(options?.webPreferences?.preload != null && options.title?.startsWith("Discord")) {
let webPreferences = options.webPreferences;
let originalPreload = webPreferences.preload;
webPreferences.preload = `${__dirname}/SimpleDiscordCryptLoader.js`;
webPreferences.additionalArguments = [...(webPreferences.additionalArguments || []), `--sdc-preload=${originalPreload}`];
}
return new originalBrowserWindow(options);
}
browserWindowHook.ISHOOK = true;
let originalElectronBinding;
function electronBindingHook(name) {
let result = originalElectronBinding.apply(this, arguments);
if(name === 'electron_browser_window' && !result.BrowserWindow.ISHOOK) {
originalBrowserWindow = result.BrowserWindow;
Object.assign(browserWindowHook, originalBrowserWindow);
browserWindowHook.prototype = originalBrowserWindow.prototype;
result.BrowserWindow = browserWindowHook;
const electron = require('electron');
electron.app.whenReady().then(() => { electron.session.defaultSession.webRequest.onHeadersReceived(onHeadersReceived) });
}
return result;
}
electronBindingHook.ISHOOK = true;
originalElectronBinding = process._linkedBinding;
if(originalElectronBinding.ISHOOK) return;
Object.assign(electronBindingHook, originalElectronBinding);
electronBindingHook.prototype = originalElectronBinding.prototype;
process._linkedBinding = electronBindingHook;
'@)
[void](New-Item "$pluginPath\SimpleDiscordCryptLoader.js" -Type File -Force -Value @'
let requireGrab = require;
if(requireGrab != null) {
const require = requireGrab;
if(window.chrome?.storage) delete chrome.storage;
const localStorage = window.localStorage;
const CspDisarmed = true;
const electron = require('electron');
require('https').get("https://gitlab.com/An0/SimpleDiscordCrypt/-/raw/master/SimpleDiscordCrypt.user.js", (response) => {
response.setEncoding('utf8');
let code = "";
response.on('data', (chunk) => code += chunk);
response.on('end', () => {
const unsafeWindow = electron.webFrame.top.context;
eval(code);
});
});
const commandLineSwitches = process._linkedBinding('electron_common_command_line');
let originalPreloadScript = commandLineSwitches.getSwitchValue('sdc-preload');
if(originalPreloadScript != null) {
commandLineSwitches.appendSwitch('preload', originalPreloadScript);
require(originalPreloadScript);
}
}
else console.error("Uh-oh, looks like something is blocking require");
'@)
'FINISHED'
$needsWait = $false
$discordProcesses = Get-Process 'Discord' -ErrorAction SilentlyContinue
$discordProcesses | % { $needsWait = $_.CloseMainWindow() -or $needsWait }
$discordPtbProcesses = Get-Process 'DiscordPTB' -ErrorAction SilentlyContinue
$discordPtbProcesses | % { $needsWait = $_.CloseMainWindow() -or $needsWait }
$discordCanaryProcesses = Get-Process 'DiscordCanary' -ErrorAction SilentlyContinue
$discordCanaryProcesses | % { $needsWait = $_.CloseMainWindow() -or $needsWait }
if($needsWait) { sleep 1 }
$processes = ($discordProcesses + $discordPtbProcesses + $discordCanaryProcesses)
if($processes.Length -ne 0) {
$processes | Stop-Process
if($discordProcesses.Length -ne 0) { [void](start $discordIconPath) }
if($discordPtbProcesses.Length -ne 0) { [void](start $discordPtbIconPath) }
if($discordCanaryProcesses.Length -ne 0) { [void](start $discordCanaryIconPath) }
}
}
else { 'Discord not found' }
}
catch { $_ }
finally { [Console]::ReadLine() }