-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
AutoConfig.au3
277 lines (226 loc) · 9.86 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
;MIT License
;
;Copyright (c) 2023 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 <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.0.0"
; IP adress of local Snap Camera server (Docker container)
$ip = "127.0.0.1"
; Original Snap Camera server
$host = "studio-app.snapchat.com"
; 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 -----------------------------
$pid = Run("docker -v", "", @SW_HIDE)
If $pid = 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")
$dockerPID = ProcessWait("Docker Desktop.exe")
EndIf
;----------------------------- Pre Check OpenSSL Installation -----------------------------
$pid = Run($openSslPath & " help", "", @SW_HIDE)
If $pid = 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
;----------------------------- 1. Create a default .env configuration file -----------------------------
FileCopy($serverInstallDir & "\example.env", $serverInstallDir & "\.env")
;----------------------------- 2. Generate self signed certificate with OpenSSL -----------------------------
GenerateSelfSignedCertificate($openSslPath, $serverInstallDir & "\ssl", "Snap Inc.", $host)
;----------------------------- 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. 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
$hFile = FileOpen($filePath, $FO_APPEND)
If Not $hFile Then
ConsoleWrite("Failed to open " & $filePath & @CRLF)
Return 0
EndIf
TrayTip("Patching Windows Hosts File", "File " & $filePath, 10, $TIP_NOSOUND)
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, $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:' & $domain & '" -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 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
_ArrayAdd($aUninstall, RegRead($sRegHive & "\" & $sRegKey, "DisplayName"))
$i += 1
WEnd
Return _ArrayUnique($aUninstall)
EndFunc