-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathSimpleDiscordCryptInstaller.ps1
338 lines (280 loc) · 12.8 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
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
$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) => { // SDCEx Hook headers to disable CSP blocking. This might be a security issue, because you can then send requests to everywhere you want?
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.ipcMain.on("SDCExMessageDialog", (event, arg) => { // SDCEx - Receive message dialog that informs about updates
event.returnValue = electron.dialog.showMessageBoxSync(null, arg);
});
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;
// SDCEx Manual Updates. Why should you trust a service not to steal your keys?
var tempDlHelper = window.tempDlHelper = {
updateInfoName: "SimpleDiscordCryptExUpdateInfo",
https: require("https"),
electronObj: require("electron"),
latestVersion: 0,
cachedObject: null,
finalEval: function(jsCode) {
const unsafeWindow = tempDlHelper.electronObj.webFrame.top.context; // Expose an unsafeWindow, so that SDC can work with it
eval(jsCode);
},
downloadAndEval: function () {
tempDlHelper.https.get(`https://raw.githubusercontent.com/Ceiridge/SimpleDiscordCrypt-Extended/${encodeURIComponent(tempDlHelper.latestVersion)}/SimpleDiscordCrypt.user.js`, {
headers: {
"User-Agent": navigator.userAgent // A User-Agent is recommended
}
}, (response) => {
response.setEncoding('utf8');
let data = "";
response.on('data', (chunk) => data += chunk);
response.on('end', async () => {
tempDlHelper.updateUpdateObject("savedScript", data); // Save current version of the script
tempDlHelper.updateUpdateObject("version", tempDlHelper.latestVersion);
tempDlHelper.finish();
tempDlHelper.finalEval(data);
});
});
},
updateUpdateObject: function (key, value) {
let dbObj = JSON.parse(localStorage.getItem(tempDlHelper.updateInfoName)); // Localstorage can only store strings
dbObj[key] = value;
localStorage.setItem(tempDlHelper.updateInfoName, JSON.stringify(dbObj));
},
getLatestVersion: function () {
return new Promise(resolve => {
tempDlHelper.https.get("https://api.github.com/repos/Ceiridge/SimpleDiscordCrypt-Extended/git/refs/heads/master", {
headers: {
"User-Agent": navigator.userAgent // Needs a User-Agent
}
}, response => {
response.setEncoding("utf8");
let data = "";
response.on('data', (chunk) => data += chunk);
response.on('end', () => {
let responseJson = JSON.parse(data);
resolve(responseJson["object"]["sha"]);
});
}); // Get latest commit sha
});
},
finish: function () {
delete tempDlHelper;
delete window.tempDlHelper;
},
userInteract: function (apply) {
if (apply) {
tempDlHelper.downloadAndEval(); // Finishes for me
} else {
tempDlHelper.finalEval(tempDlHelper.cachedObject["savedScript"]);
tempDlHelper.finish();
}
}
}
async function tmpAsyncFnc() {
tempDlHelper.latestVersion = await tempDlHelper.getLatestVersion();
if (localStorage.getItem(tempDlHelper.updateInfoName) === null) { // If no version exists, download, execute and set
localStorage.setItem(tempDlHelper.updateInfoName, "{}"); // Empty json object
tempDlHelper.downloadAndEval();
} else {
tempDlHelper.cachedObject = JSON.parse(localStorage.getItem(tempDlHelper.updateInfoName));
let currentVersion = tempDlHelper.cachedObject["version"];
if (currentVersion != tempDlHelper.latestVersion) {
let dialogAnswer = 0;
let shellObj = tempDlHelper.electronObj.shell;
while (dialogAnswer === 0) { // Open the blocking dialog again if the first button was clicked
dialogAnswer = tempDlHelper.electronObj.ipcRenderer.sendSync("SDCExMessageDialog", {
type: "question",
buttons: ["View changes", "Apply latest update", "Execute saved version"],
defaultId: 0,
title: "New SimpleDiscordCrypt Extended Update",
message: "A new SimpleDiscordCrypt Extended version has been found."
});
if (dialogAnswer === 0) {
shellObj.openExternal(`https://github.com/Ceiridge/SimpleDiscordCrypt-Extended/compare/${encodeURIComponent(currentVersion)}..master`);
}
}
tempDlHelper.userInteract(dialogAnswer === 1); // Apply if second button was clicked
} else {
tempDlHelper.userInteract(false); // Just eval the saved script and finish
}
}
}
tmpAsyncFnc();
delete tmpAsyncFnc;
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.log("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() }