-
Notifications
You must be signed in to change notification settings - Fork 38
/
main.cpp
327 lines (271 loc) · 8.74 KB
/
main.cpp
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
//
// Copyright (C) Microsoft. All rights reserved.
//
#include "stdafx.h"
#include "EdgeDiagnosticsAdapter.h"
#include "Helpers.h"
#include <iostream>
#include <Shellapi.h>
#include <Shlobj.h>
#include "Aclapi.h"
#include "boost/program_options.hpp"
CHandle hChromeProcess;
namespace po = boost::program_options;
CString getPathToCurrentExeContainer()
{
// Get the current path that we are running from
CString fullPath;
DWORD count = ::GetModuleFileName(nullptr, fullPath.GetBuffer(MAX_PATH), MAX_PATH);
fullPath.ReleaseBufferSetLength(count);
LPWSTR buffer = fullPath.GetBuffer();
LPWSTR newPath = ::PathFindFileName(buffer);
if (newPath && newPath != buffer)
{
fullPath.ReleaseBufferSetLength((newPath - buffer));
}
else
{
fullPath.ReleaseBuffer();
}
return fullPath;
}
void setSecurityACLs()
{
CString fullPath = getPathToCurrentExeContainer();
// Check to make sure that the dll has the ACLs to load in an appcontainer
// We're doing this here as the adapter has no setup script and should be xcopy deployable/removeable
PACL pOldDACL = NULL, pNewDACL = NULL;
PSECURITY_DESCRIPTOR pSD = NULL;
EXPLICIT_ACCESS ea;
SECURITY_INFORMATION si = DACL_SECURITY_INFORMATION;
// The check is done on the folder and should be inherited to all objects
DWORD dwRes = GetNamedSecurityInfo(fullPath, SE_FILE_OBJECT, DACL_SECURITY_INFORMATION, NULL, NULL, &pOldDACL, NULL, &pSD);
// Initialize an EXPLICIT_ACCESS structure for the new ACE.
ZeroMemory(&ea, sizeof(EXPLICIT_ACCESS));
ea.grfAccessPermissions = GENERIC_READ | GENERIC_EXECUTE;
ea.grfAccessMode = SET_ACCESS;
ea.grfInheritance = SUB_CONTAINERS_AND_OBJECTS_INHERIT;
ea.Trustee.TrusteeForm = TRUSTEE_IS_NAME;
ea.Trustee.TrusteeType = TRUSTEE_IS_WELL_KNOWN_GROUP;
ea.Trustee.ptstrName = L"ALL APPLICATION PACKAGES";
// Create a new ACL that merges the new ACE into the existing DACL.
dwRes = SetEntriesInAcl(1, &ea, pOldDACL, &pNewDACL);
if (dwRes == ERROR_SUCCESS)
{
dwRes = SetNamedSecurityInfo(fullPath.GetBuffer(), SE_FILE_OBJECT, si, NULL, NULL, pNewDACL, NULL);
if (dwRes == ERROR_SUCCESS)
{
}
}
if (dwRes != ERROR_SUCCESS)
{
// The ACL was not set, this isn't fatal as it only impacts IE in EPM and Edge and the user can set it manually
wcout << L"Could not set ACL to allow access to IE EPM or Edge.";
wcout << L"\n";
wcout << Helpers::GetLastErrorMessage().GetBuffer();
wcout << L"\n";
wcout << L"You can set the ACL manually by adding Read & Execute permissions for 'All APPLICATION PACAKGES' to each dll.";
wcout << L"\n";
}
if (pSD != NULL)
{
LocalFree((HLOCAL)pSD);
}
if (pNewDACL != NULL)
{
LocalFree((HLOCAL)pNewDACL);
}
}
HRESULT setEdgeForDebugging(bool enable)
{
HRESULT hrResult = E_FAIL;
CString edgePackageFamilyName;
hrResult = Helpers::GetEdgePackageFamilyName(edgePackageFamilyName);
if (!SUCCEEDED(hrResult))
{
std::cerr << "Failed to get the full package name of Edge." << std::endl;
std::cerr << "HR Code: " << hrResult << std::endl;
return hrResult;
}
CComPtr<IPackageDebugSettings> spPackageDebugSettings;
hrResult = CoCreateInstance(CLSID_PackageDebugSettings, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&spPackageDebugSettings));
if (!SUCCEEDED(hrResult))
{
std::cerr << "Failed to CoCreateInstance of CLSID_PackageDebugSettings." << std::endl;
std::cerr << "HR Code: " << hrResult << std::endl;
return hrResult;
}
if (enable)
{
hrResult = spPackageDebugSettings->EnableDebugging(edgePackageFamilyName, NULL, NULL);
}
else
{
hrResult = spPackageDebugSettings->DisableDebugging(edgePackageFamilyName);
}
return hrResult;
}
BOOL WINAPI OnClose(DWORD reason)
{
if (hChromeProcess.m_h)
{
::TerminateProcess(hChromeProcess.m_h, 0);
}
setEdgeForDebugging(false);
return TRUE;
}
int wmain(int argc, wchar_t* argv[])
{
//::MessageBox(NULL, L"Stop here", L"STOP!", MB_ICONWARNING | MB_CANCELTRYCONTINUE | MB_DEFBUTTON3);
std:cout << "Edge Diagnostics Adapter" << std::endl;
po::options_description desc("Allowed options");
desc.add_options()
("help,h", "Prints this help text.")
("launch,l", po::value<string>(), "Launches Edge. Optionally at the URL specified in the value")
("killall,k", "Kills all running Edge processes.")
("chrome,c", "Launches Crhome in the background to serve the Chrome Developer Tools frontend.")
("port,p", po::value<string>(), "The port number to listen on. Default is 9222.");
po::variables_map vm;
try
{
po::store(po::parse_command_line(argc, argv, desc), vm);
}
catch (po::error& e)
{
std::cerr << "ERROR: " << e.what() << std::endl << std::endl;
std::cerr << desc << std::endl;
return E_FAIL;
}
if (vm.count("help"))
{
std::cout << desc << std::endl;
return S_OK;
}
// Initialize COM and deinitialize when we go out of scope
HRESULT hrCoInit = ::CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);
shared_ptr<HRESULT> spCoInit(&hrCoInit, [](const HRESULT* hrCom) -> void { if (SUCCEEDED(*hrCom)) { ::CoUninitialize(); } });
{
// Set a close handler to shutdown the chrome instance we launch
::SetConsoleCtrlHandler(OnClose, TRUE);
// Launch chrome
if (vm.count("chrome"))
{
CString chromePath;
// Find the chrome install location via the registry
CString keyPath = L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\chrome.exe";
CRegKey regKey;
// First see if we can find where Chrome is installed from the registry. This will only succeed if Chrome is installed for all users
if (regKey.Open(HKEY_LOCAL_MACHINE, keyPath, KEY_READ) == ERROR_SUCCESS)
{
ULONG bufferSize = MAX_PATH;
CString path;
LRESULT result = regKey.QueryStringValue(nullptr, path.GetBufferSetLength(bufferSize), &bufferSize);
path.ReleaseBufferSetLength(bufferSize);
if (result == ERROR_SUCCESS)
{
chromePath = path;
}
}
if (chromePath.GetLength() == 0)
{
// If Chrome is only installed for the current user, look in \AppData\Local\Google\Chrome\Application\ for Chrome.exe
CString appPath;
::SHGetFolderPath(NULL, CSIDL_LOCAL_APPDATA, NULL, SHGFP_TYPE_CURRENT, appPath.GetBuffer(MAX_PATH + 1));
appPath.ReleaseBuffer();
chromePath = appPath + L"\\Google\\Chrome\\Application\\chrome.exe";
}
// Get a temp location
CString temp;
Helpers::ExpandEnvironmentString(L"%Temp%", temp);
// Set arguments for the chrome that we launch
CString arguments;
arguments.Format(L"about:blank --remote-debugging-port=9223 --window-size=0,0 --silent-launch --no-first-run --no-default-browser-check --user-data-dir=\"%s\"", temp);
// Launch the process
STARTUPINFO si = { 0 };
PROCESS_INFORMATION pi = { 0 };
si.cb = sizeof(si);
si.wShowWindow = SW_MINIMIZE;
BOOL result = ::CreateProcess(
chromePath,
arguments.GetBuffer(),
nullptr,
nullptr,
FALSE,
0,
nullptr,
temp,
&si,
&pi);
arguments.ReleaseBuffer();
if (result)
{
// Store the handles
CHandle hThread(pi.hThread);
hChromeProcess.Attach(pi.hProcess);
DWORD waitResult = ::WaitForInputIdle(hChromeProcess, 30000);
}
else
{
std::cerr << "Could not open Chrome. Please ensure that Chrome is installed." << std::endl;
system("pause");
return -1;
}
}
// Kill all Edge instances if their is an aegument /killall
if (vm.count("killall"))
{
//killAllProcessByExe(L"MicrosoftEdgeCP.exe");
Helpers::KillAllProcessByExe(L"MicrosoftEdge.exe");
}
// Launch Edge if their is an argument set /launch:<url>
if (vm.count("launch"))
{
CString url(vm["launch"].as<string>().c_str());
if (url.GetLength() == 0)
{
url = L"https://www.bing.com";
}
HRESULT hr = Helpers::OpenUrlInMicrosoftEdge(url);
if (FAILED(hr))
{
std::cout << L"Failed to launch Microsoft Edge";
}
}
string port = EdgeDiagnosticsAdapter::s_Default_Port;
if (vm.count("port"))
{
port = vm["port"].as<string>();
}
// We don't care if this fails as the developer can set it manually.
setSecurityACLs();
// We don't care if this fails or not as maybe the developer wants to do something that won't hit the PLM. In case errors went to the console.
setEdgeForDebugging(true);
// Load the proxy server
EdgeDiagnosticsAdapter proxy(getPathToCurrentExeContainer(), port);
if (proxy.IsServerRunning)
{
// Create a message pump
MSG msg;
::PeekMessage(&msg, NULL, WM_USER, WM_USER, PM_NOREMOVE);
// Thread message loop
BOOL getMessageRet;
while ((getMessageRet = ::GetMessage(&msg, NULL, 0, 0)) != 0)
{
if (getMessageRet != -1)
{
::TranslateMessage(&msg);
::DispatchMessage(&msg);
}
}
// Leave the window open so we can read the log file
wcout << endl << L"Press [ENTER] to exit." << endl;
cin.ignore();
}
else
{
wcout << L"Error starting. Quiting.";
return E_FAIL;
}
}
return S_OK;
}