-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathAutoConfig.au3
365 lines (298 loc) · 13.6 KB
/
AutoConfig.au3
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
;MIT License
;
;Copyright (c) 2023-2025 Patrick Trumpis
;
;Permission is hereby granted, free of charge, to any person obtaining a copy
;of this software and associated documentation files (the "Software"), to deal
;in the Software without restriction, including without limitation the rights
;to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
;copies of the Software, and to permit persons to whom the Software is
;furnished to do so, subject to the following conditions:
;
;The above copyright notice and this permission notice shall be included in all
;copies or substantial portions of the Software.
;
;THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
;IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
;FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
;AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
;LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
;OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
;SOFTWARE.
#include <Array.au3>
#include <File.au3>
#include <FileConstants.au3>
#include <InetConstants.au3>
#include <MsgBoxConstants.au3>
#include <TrayConstants.au3>
#include <WinAPIFiles.au3>
#RequireAdmin
;===============================================================
; Install Config - You may change these values
;===============================================================
; Installer meta
$appName = "Snap Camera Server Auto Config"
$version = "1.2.0"
; IP adress of local Snap Camera server (Docker container)
$ip = "127.0.0.1"
; Original Snap Camera server
$host = "studio-app.snapchat.com"
$subjectAltName = "*.snapchat.com"
; Application
$processName = "Snap Camera.exe"
$appPath = "C:\Program Files\Snap Inc\Snap Camera\Snap Camera.exe"
; OpenSSL Path/CMD
$openSslPath = "openssl"
$openSsl64Path = @HomeDrive & "\Program Files\OpenSSL-Win64\bin\openssl.exe"
$openSsl32Path = @HomeDrive & "\Program Files (x86)\OpenSSL-Win32\bin\openssl.exe"
; OpenSSL output files
$sslCrtFile = "studio-app.snapchat.com.crt"
$sslKeyFile = "studio-app.snapchat.com.key"
; URL's for missing software
$serverRepoUrl = "https://github.com/ptrumpis/snap-camera-server"
$latestSourcerUrl = "https://github.com/ptrumpis/snap-camera-server/releases/latest"
$dockerUrl = "https://docker.com"
$openSslUrl = "https://slproweb.com/products/Win32OpenSSL.html"
;===============================================================
; DO NOT EDIT BELOW THIS LINE UNLESS YOU KNOW WHAT YOU ARE DOING
;===============================================================
$mbTitle = $appName & " v" & $version
;----------------------------- Admin rights required for /etc/hosts changes -----------------------------
If Not IsAdmin() Then
MsgBox($MB_SYSTEMMODAL, $mbTitle, "Please run this file as Administrator")
Exit
EndIf
;----------------------------- Pre Check Docker Installation -----------------------------
$infoPID = Run("docker -v", "", @SW_HIDE)
If $infoPID = 0 Then
If IsApplicationInstalled("Docker Desktop", -1) = 0 Then
MsgBox($MB_SYSTEMMODAL, $mbTitle, "Please download Docker from:" & @CRLF & $dockerUrl)
Else
MsgBox($MB_SYSTEMMODAL, $mbTitle, "Docker is installed but not inside your PATH")
EndIf
Exit
EndIf
;----------------------------- Pre Check Docker Running -----------------------------
$dockerPID = ProcessExists("Docker Desktop.exe")
If $dockerPID = 0 Then
MsgBox($MB_SYSTEMMODAL, $mbTitle, "Please start Docker Desktop and try again.")
Exit
EndIf
;----------------------------- Pre Check OpenSSL Installation -----------------------------
$sslPID = Run($openSslPath & " help", "", @SW_HIDE)
If $sslPID = 0 Then
If IsApplicationInstalled("OpenSSL", -1) = 0 Then
MsgBox($MB_SYSTEMMODAL, $mbTitle, "Please download OpenSSL from:" & @CRLF & $openSslUrl)
Exit
EndIf
; OpenSSL is present but not included in PATH (expected default behaviour)
If FileExists($openSsl64Path) = 1 Then
$openSslPath = $openSsl64Path
ElseIf FileExists($openSsl32Path) = 1 Then
$openSslPath = $openSsl32Path
Else
$openSslPath = ""
EndIf
EndIf
;----------------------------- Ask user for permission before we start -----------------------------
$result = MsgBox($MB_OKCANCEL + $MB_ICONINFORMATION, $mbTitle, $mbTitle & @CRLF & _
"by github.com/ptrumpis" & @CRLF & @CRLF & _
"This tool will automatically handle the configuration steps of:" & @CRLF & _
$serverRepoUrl & @CRLF & @CRLF & _
"Do you want to continue?")
If $result = $IDCANCEL Then Exit
;----------------------------- Check Server installation location -----------------------------
$serverInstallDir = @WorkingDir
If isServerDir($serverInstallDir) = 0 Then
$serverInstallDir = ""
EndIf
;----------------------------- Resolve unknown Server installation location -----------------------------
If $serverInstallDir = "" Then
$result = MsgBox($MB_YESNO + $MB_ICONQUESTION, $mbTitle, "Did you already download 'Snap Camera Server' from:" & @CRLF & @CRLF & $serverRepoUrl)
If $result = $IDNO Then
MsgBox($MB_SYSTEMMODAL, $mbTitle, "Please download the latest source files from:" & @CRLF & @CRLF & $latestSourcerUrl)
Exit
EndIf
MsgBox($MB_SYSTEMMODAL + $MB_ICONINFORMATION, $mbTitle, "Ok great!" & @CRLF & _
"Please select the location of your 'Snap Camera Server' directory in the next step.")
$serverInstallDir = FileSelectFolder("Snap Camera Server' directory", "", @WorkingDir)
If $serverInstallDir = "" Then
MsgBox($MB_SYSTEMMODAL, $mbTitle, "Auto configuration canceled")
Exit
EndIf
EndIf
;----------------------------- Resolve unknown OpenSSL installation location -----------------------------
If $openSslPath = "" Then
MsgBox($MB_SYSTEMMODAL + $MB_ICONINFORMATION, $mbTitle, _
"OpenSSL has been found on your Computer but the exact installation path could not be determined." & @CRLF & @CRLF & _
"Please select the location of your 'openssl.exe' inside your OpenSSL installation directory in the next step.")
$openSslPath = FileOpenDialog("Please select the path to your openssl.exe file", @WorkingDir, "OpenSSL (*.exe)", $FD_FILEMUSTEXIST + $FD_PATHMUSTEXIST, "openssl.exe")
If $openSslPath = "" Then
MsgBox($MB_SYSTEMMODAL, $mbTitle, "Auto configuration canceled")
Exit
EndIf
EndIf
;----------------------------- Resolve Docker Windows container mode -----------------------------
If GetDockerOSType() = "windows" Then
Local $answer = MsgBox($MB_YESNO + $MB_ICONQUESTION, "Docker Container Mode", "Docker runs in Windows container mode. For the server to work properly, Docker needs to switch to Linux containers Want to switch to Linux containers?")
If $answer = $IDYES Then
SwitchToLinuxContainers()
MsgBox($MB_OK, "Successful", "Docker has been switched to Linux containers.")
EndIf
EndIf
;----------------------------- 1. Create a default .env configuration file -----------------------------
FileCopy($serverInstallDir & "\example.env", $serverInstallDir & "\.env", $FC_NOOVERWRITE)
;----------------------------- 2. Generate self signed certificate with OpenSSL -----------------------------
$certFilePath = $serverInstallDir & "\ssl\" & $sslCrtFile
If FileExists($certFilePath) = 0 Then
GenerateSelfSignedCertificate($openSslPath, $serverInstallDir & "\ssl", "Snap Inc.", $host, $subjectAltName)
EndIf
;----------------------------- 3. Install Snap Camera Server Root certificate -----------------------------
InstallRootCertificate($sslCrtFile, $serverInstallDir & "\ssl")
;----------------------------- 4. Patch Windows hosts file with local Docker IP -----------------------------
If CheckHostsFile($ip, $host) = 0 Then
If PatchHostsFile($ip, $host) = 0 Then
MsgBox($MB_SYSTEMMODAL, "Error", "Hosts file could not be accessed")
Exit
EndIf
EndIf
;----------------------------- 5. Close running Snap Camera application -----------------------------
KillProcess($processName)
;----------------------------- 6. Check the Windows Firewall -----------------------------
If IsAppBlocked($appPath) Then
UnblockApp($appPath)
EndIf
;----------------------------- 7. Initialize Docker container for the first time -----------------------------
RunDocker($serverInstallDir)
;----------------------------- Custom helper functions - magic happens here -----------------------------
Func isServerDir($dir)
Local $serverFiles[6] = ["server.js", "package.json", "docker-compose.yml", "Dockerfile", "ssl", "src"]
For $i = 0 To 5
$checkFile = $dir & "\" & $serverFiles[$i]
If FileExists($checkFile) = 0 Then
Return 0
ExitLoop
EndIf
Next
Return 1
EndFunc
Func CheckHostsFile($ip, $host)
$filePath = @WindowsDir & "\System32\drivers\etc\hosts"
$arrLines = FileReadToArray($filePath)
$nLines = Ubound($arrLines)
For $i = 0 To ($nLines-1)
If StringRegExp($arrLines[$i], "^" & $ip & "\s*" & $host) Then
Return 1
EndIf
Next
Return 0
EndFunc
Func PatchHostsFile($ip, $host)
$filePath = @WindowsDir & "\System32\drivers\etc\hosts"
$newLine = $ip & " " & $host & @CRLF
FileCopy($filePath, $filePath & ".bak", $FC_OVERWRITE)
$hFile = FileOpen($filePath, $FO_READ + $FO_UTF8_NOBOM)
If $hFile = -1 Then
ConsoleWrite("Failed to open " & $filePath & @CRLF)
Return 0
EndIf
$fileContent = FileRead($hFile)
If @error Then
FileClose($hFile)
Return 0
EndIf
FileClose($hFile)
$hFile = FileOpen($filePath, $FO_READ + $FO_OVERWRITE + $FO_UTF8_NOBOM)
If $hFile = -1 Then
ConsoleWrite("Failed to open " & $filePath & @CRLF)
Return 0
EndIf
TrayTip("Patching Windows Hosts File", "File " & $filePath, 10, $TIP_NOSOUND)
FileWrite($hFile, $fileContent & @CRLF)
FileWriteLine($hFile, "");
FileWriteLine($hFile, "# Added by " & $appName)
FileWriteLine($hFile, $newLine)
FileWriteLine($hFile, "# End of section")
FileClose($hFile)
Return 1
EndFunc
Func GenerateSelfSignedCertificate($openSslPath, $outputDir, $owner, $domain, $altName, $daysExpire = 3650)
TrayTip("Generating SSL Certificate", "Domain " & $domain, 10, $TIP_NOSOUND)
ShellExecuteWait($openSslPath, 'req -x509 -nodes -days ' & $daysExpire & ' -subj "/C=CA/ST=QC/O=' & $owner & '/CN=' & $domain & '" -addext "subjectAltName=DNS:' & $altName & '" -newkey rsa:2048 -keyout ' & $domain & '.key -out ' & $domain & '.crt', $outputDir)
EndFunc
Func InstallRootCertificate($certFile, $workingDir)
TrayTip("Installing Root Certificate", "Certificate " & $certFile, 10, $TIP_NOSOUND)
ShellExecuteWait("certutil", "-addstore -enterprise Root " & $certFile, $workingDir)
EndFunc
Func RunDocker($workingDir)
TrayTip("Running Docker", "docker compose up", 10, $TIP_NOSOUND)
ShellExecuteWait("docker", "compose up", $workingDir)
EndFunc
Func GetDockerOSType()
Local $output = Run(@ComSpec & " /c docker info | findstr /C:""OSType""", "", @SW_HIDE, $STDOUT_CHILD)
Local $result = StdoutRead($output)
If StringInStr($result, "windows") Then
Return "windows"
Else
Return "linux"
EndIf
EndFunc
Func SwitchToLinuxContainers()
RunWait('C:\Program Files\Docker\Docker\DockerCli.exe -SwitchLinuxEngine', "", @SW_HIDE)
EndFunc
Func UnblockApp($app)
TrayTip("Unblocking Snap Camera", "Allowing Snap Camera in Windows Firewall.", 10, $TIP_NOSOUND)
RunWait('powershell -Command "Remove-NetFirewallRule -Program ''' & $app & ''' -Direction Inbound"', "", @SW_HIDE)
RunWait('powershell -Command "Remove-NetFirewallRule -Program ''' & $app & ''' -Direction Outbound"', "", @SW_HIDE)
RunWait('powershell -Command "New-NetFirewallRule -DisplayName ''Snap Camera Inbound'' -Direction Inbound -Action Allow -Program ''' & $app & ''' -Enabled True"', "", @SW_HIDE)
RunWait('powershell -Command "New-NetFirewallRule -DisplayName ''Snap Camera Outbound'' -Direction Outbound -Action Allow -Program ''' & $app & ''' -Enabled True"', "", @SW_HIDE)
EndFunc
Func IsAppBlocked($app)
Local $psCommand = 'Get-NetFirewallApplicationFilter | Where-Object { $_.Program -eq "' & $app & '" } | Select-Object -ExpandProperty Name'
Local $cmd = 'powershell -Command "' & $psCommand & '"'
Local $result = RunWait(@ComSpec & " /C " & $cmd, "", @SW_HIDE)
If StringLen($result) > 0 Then
Return True
EndIf
Return False
EndFunc
Func IsApplicationInstalled($appName, $is64bit = -1)
If $is64bit = -1 Then
If IsApplicationInstalled($appName, 0) = 1 Then Return 1
$is64bit = 1
EndIf
$regList = GetRegUninstallList($is64bit)
If $regList = -1 Then Return -1
For $key in $regList
If StringLen($key) > StringLen($appName) Then
If StringInStr($key, $appName) <> 0 Then Return 1
Else
If StringCompare($key, $appName, 0) = 0 Then Return 1
EndIf
Next
Return 0
EndFunc
Func GetRegUninstallList($use64bit = 0)
$sRegHive = "HKLM\Software\Microsoft\Windows\CurrentVersion\Uninstall"
If $use64bit = 1 Then
$sRegHive = "HKLM64\Software\Microsoft\Windows\CurrentVersion\Uninstall"
EndIf
Local $aUninstall[0]
$i = 1
While 1
$sRegKey = RegEnumKey($sRegHive, $i)
If @error Then ExitLoop
$sDisplayName = RegRead($sRegHive & "\" & $sRegKey, "DisplayName")
_ArrayAdd($aUninstall, $sDisplayName)
$i += 1
WEnd
Return _ArrayUnique($aUninstall)
EndFunc
Func KillProcess($processName)
$pid = ProcessExists($processName)
If $pid Then
Return ProcessClose($pid)
Else
Return 0
EndIf
EndFunc