This repository has been archived by the owner on May 30, 2023. It is now read-only.
/
phantom.cpp
441 lines (370 loc) · 12.3 KB
/
phantom.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
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
/*
This file is part of the PhantomJS project from Ofi Labs.
Copyright (C) 2011 Ariya Hidayat <ariya.hidayat@gmail.com>
Copyright (C) 2011 Ivan De Marino <ivan.de.marino@gmail.com>
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the <organization> nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "phantom.h"
#include <QApplication>
#include <QDir>
#include <QFileInfo>
#include <QFile>
#include <QWebPage>
#include "consts.h"
#include "terminal.h"
#include "utils.h"
#include "webpage.h"
#include "webserver.h"
#include "repl.h"
#include "system.h"
#include "callback.h"
#include "cookiejar.h"
static Phantom *phantomInstance = NULL;
// private:
Phantom::Phantom(QObject *parent)
: REPLCompletable(parent)
, m_terminated(false)
, m_returnValue(0)
, m_filesystem(0)
, m_system(0)
{
QStringList args = QApplication::arguments();
// Prepare the configuration object based on the command line arguments.
// Because this object will be used by other classes, it needs to be ready ASAP.
m_config.init(&args);
// Apply debug configuration as early as possible
Utils::printDebugMessages = m_config.printDebugMessages();
}
void Phantom::init()
{
if (m_config.helpFlag()) {
Terminal::instance()->cout(QString("%1").arg(m_config.helpText()));
Terminal::instance()->cout("Without any argument, PhantomJS will launch in interactive mode (REPL).");
Terminal::instance()->cout("");
Terminal::instance()->cout("Documentation can be found at the web site, http://phantomjs.org.");
Terminal::instance()->cout("");
m_terminated = true;
return;
}
if (m_config.versionFlag()) {
m_terminated = true;
Terminal::instance()->cout(QString("%1").arg(PHANTOMJS_VERSION_STRING));
return;
}
if (!m_config.unknownOption().isEmpty()) {
Terminal::instance()->cerr(m_config.unknownOption());
m_terminated = true;
return;
}
// Initialize the CookieJar
CookieJar::instance(m_config.cookiesFile());
m_page = new WebPage(this, QUrl::fromLocalFile(m_config.scriptFile()));
m_pages.append(m_page);
QString proxyType = m_config.proxyType();
if (proxyType != "none") {
if (m_config.proxyHost().isEmpty()) {
QNetworkProxyFactory::setUseSystemConfiguration(true);
} else {
QNetworkProxy::ProxyType networkProxyType = QNetworkProxy::HttpProxy;
if (proxyType == "socks5") {
networkProxyType = QNetworkProxy::Socks5Proxy;
}
if(!m_config.proxyAuthUser().isEmpty() && !m_config.proxyAuthPass().isEmpty()) {
QNetworkProxy proxy(networkProxyType, m_config.proxyHost(), m_config.proxyPort(), m_config.proxyAuthUser(), m_config.proxyAuthPass());
QNetworkProxy::setApplicationProxy(proxy);
} else {
QNetworkProxy proxy(networkProxyType, m_config.proxyHost(), m_config.proxyPort());
QNetworkProxy::setApplicationProxy(proxy);
}
}
}
// Set output encoding
Terminal::instance()->setEncoding(m_config.outputEncoding());
// Set script file encoding
m_scriptFileEnc.setEncoding(m_config.scriptEncoding());
connect(m_page, SIGNAL(javaScriptConsoleMessageSent(QString)),
SLOT(printConsoleMessage(QString)));
connect(m_page, SIGNAL(initialized()),
SLOT(onInitialized()));
m_defaultPageSettings[PAGE_SETTINGS_LOAD_IMAGES] = QVariant::fromValue(m_config.autoLoadImages());
m_defaultPageSettings[PAGE_SETTINGS_JS_ENABLED] = QVariant::fromValue(true);
m_defaultPageSettings[PAGE_SETTINGS_XSS_AUDITING] = QVariant::fromValue(false);
m_defaultPageSettings[PAGE_SETTINGS_USER_AGENT] = QVariant::fromValue(m_page->userAgent());
m_defaultPageSettings[PAGE_SETTINGS_LOCAL_ACCESS_REMOTE] = QVariant::fromValue(m_config.localToRemoteUrlAccessEnabled());
m_defaultPageSettings[PAGE_SETTINGS_WEB_SECURITY_ENABLED] = QVariant::fromValue(m_config.webSecurityEnabled());
m_defaultPageSettings[PAGE_SETTINGS_JS_CAN_OPEN_WINDOWS] = QVariant::fromValue(m_config.javascriptCanOpenWindows());
m_defaultPageSettings[PAGE_SETTINGS_JS_CAN_CLOSE_WINDOWS] = QVariant::fromValue(m_config.javascriptCanCloseWindows());
m_page->applySettings(m_defaultPageSettings);
setLibraryPath(QFileInfo(m_config.scriptFile()).dir().absolutePath());
}
// public:
Phantom *Phantom::instance() {
if (NULL == phantomInstance) {
phantomInstance = new Phantom();
phantomInstance->init();
}
return phantomInstance;
}
Phantom::~Phantom()
{
// Nothing to do: cleanup is handled by QObject relationships
}
QStringList Phantom::args() const
{
return m_config.scriptArgs();
}
QVariantMap Phantom::defaultPageSettings() const
{
return m_defaultPageSettings;
}
QString Phantom::outputEncoding() const
{
return Terminal::instance()->getEncoding();
}
void Phantom::setOutputEncoding(const QString &encoding)
{
Terminal::instance()->setEncoding(encoding);
}
bool Phantom::execute()
{
if (m_terminated)
return false;
if (m_config.scriptFile().isEmpty()) {
// REPL mode requested
// Create the REPL: it will launch itself, no need to store this variable.
REPL::getInstance(m_page->mainFrame(), this);
} else {
// Load the User Script
if (m_config.debug()) {
// Debug enabled
if (!Utils::loadJSForDebug(m_config.scriptFile(), m_scriptFileEnc, QDir::currentPath(), m_page->mainFrame(), m_config.remoteDebugAutorun())) {
m_returnValue = -1;
return false;
}
m_page->showInspector(m_config.remoteDebugPort());
} else {
if (!Utils::injectJsInFrame(m_config.scriptFile(), m_scriptFileEnc, QDir::currentPath(), m_page->mainFrame(), true)) {
m_returnValue = -1;
return false;
}
}
}
return !m_terminated;
}
int Phantom::returnValue() const
{
return m_returnValue;
}
QString Phantom::libraryPath() const
{
return m_page->libraryPath();
}
void Phantom::setLibraryPath(const QString &libraryPath)
{
m_page->setLibraryPath(libraryPath);
}
QString Phantom::scriptName() const
{
return QFileInfo(m_config.scriptFile()).fileName();
}
QVariantMap Phantom::version() const
{
QVariantMap result;
result["major"] = PHANTOMJS_VERSION_MAJOR;
result["minor"] = PHANTOMJS_VERSION_MINOR;
result["patch"] = PHANTOMJS_VERSION_PATCH;
return result;
}
QObject *Phantom::page() const
{
return m_page;
}
Config *Phantom::config()
{
return &m_config;
}
bool Phantom::printDebugMessages() const
{
return m_config.printDebugMessages();
}
bool Phantom::areCookiesEnabled() const
{
return CookieJar::instance()->isEnabled();
}
void Phantom::setCookiesEnabled(const bool value)
{
if (value) {
CookieJar::instance()->enable();
} else {
CookieJar::instance()->disable();
}
}
// public slots:
QObject *Phantom::createWebPage()
{
WebPage *page = new WebPage(this);
// Store pointer to the page for later cleanup
m_pages.append(page);
// Apply default settings to the page
page->applySettings(m_defaultPageSettings);
// Show web-inspector if in debug mode
if (m_config.debug()) {
page->showInspector(m_config.remoteDebugPort());
}
return page;
}
QObject* Phantom::createWebServer()
{
WebServer *server = new WebServer(this);
m_servers.append(server);
return server;
}
QObject *Phantom::createFilesystem()
{
if (!m_filesystem)
m_filesystem = new FileSystem(this);
return m_filesystem;
}
QObject *Phantom::createSystem()
{
if (!m_system) {
m_system = new System(this);
QStringList systemArgs;
systemArgs += m_config.scriptFile();
systemArgs += m_config.scriptArgs();
m_system->setArgs(systemArgs);
}
return m_system;
}
QObject* Phantom::createCallback()
{
return new Callback(this);
}
void Phantom::loadModule(const QString &moduleSource, const QString &filename)
{
if (m_terminated)
return;
QString scriptSource =
"(function(require, exports, module) {" +
moduleSource +
"}.call({}," +
"require.cache['" + filename + "']._getRequire()," +
"require.cache['" + filename + "'].exports," +
"require.cache['" + filename + "']" +
"));";
m_page->mainFrame()->evaluateJavaScript(scriptSource, filename);
}
bool Phantom::injectJs(const QString &jsFilePath)
{
if (m_terminated)
return false;
return Utils::injectJsInFrame(jsFilePath, libraryPath(), m_page->mainFrame());
}
void Phantom::exit(int code)
{
if (m_config.debug()) {
Terminal::instance()->cout("Phantom::exit() called but not quitting in debug mode.");
} else {
doExit(code);
}
}
void Phantom::debugExit(int code)
{
doExit(code);
}
// private slots:
void Phantom::printConsoleMessage(const QString &message)
{
Terminal::instance()->cout(message);
}
void Phantom::onInitialized()
{
// Add 'phantom' object to the global scope
m_page->mainFrame()->addToJavaScriptWindowObject("phantom", this);
// Bootstrap the PhantomJS scope
m_page->mainFrame()->evaluateJavaScript(
Utils::readResourceFileUtf8(":/bootstrap.js"),
QString("phantomjs://bootstrap.js")
);
}
bool Phantom::setCookies(const QVariantList &cookies)
{
// Delete all the cookies from the CookieJar
CookieJar::instance()->clearCookies();
// Add a new set of cookies
return CookieJar::instance()->addCookiesFromMap(cookies);
}
QVariantList Phantom::cookies() const
{
// Return all the Cookies in the CookieJar, as a list of Maps (aka JSON in JS space)
return CookieJar::instance()->cookiesToMap();
}
bool Phantom::addCookie(const QVariantMap &cookie)
{
return CookieJar::instance()->addCookieFromMap(cookie);
}
bool Phantom::deleteCookie(const QString &cookieName)
{
if (!cookieName.isEmpty()) {
return CookieJar::instance()->deleteCookie(cookieName);
}
return false;
}
void Phantom::clearCookies()
{
CookieJar::instance()->clearCookies();
}
// private:
void Phantom::doExit(int code)
{
if (m_config.debug())
{
Utils::cleanupFromDebug();
}
emit aboutToExit(code);
m_terminated = true;
m_returnValue = code;
qDeleteAll(m_pages);
m_pages.clear();
m_page = 0;
QApplication::instance()->exit(code);
}
void Phantom::initCompletions()
{
// Add completion for the Dynamic Properties of the 'phantom' object
// properties
addCompletion("args");
addCompletion("defaultPageSettings");
addCompletion("libraryPath");
addCompletion("outputEncoding");
addCompletion("scriptName");
addCompletion("version");
addCompletion("cookiesEnabled");
addCompletion("cookies");
// functions
addCompletion("exit");
addCompletion("debugExit");
addCompletion("injectJs");
addCompletion("addCookie");
addCompletion("deleteCookie");
addCompletion("clearCookies");
}