-
Notifications
You must be signed in to change notification settings - Fork 11
/
mainwindow.cpp
265 lines (237 loc) · 8.7 KB
/
mainwindow.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
#include "mainwindow.h"
#include <QApplication>
#include <QDir>
#include <QFile>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QNetworkProxy>
#include <QTimer>
#include <QWebSocket>
#include <QWebSocketServer>
#include <QProcess>
void MainWindow::configureWebView() {
// Disable anti-aliasing for better performance
webView->setRenderHint(QPainter::Antialiasing, false);
webView->setRenderHint(QPainter::TextAntialiasing, false);
webView->setRenderHint(QPainter::SmoothPixmapTransform, false);
// Enable basic JavaScript support
webView->settings()->setAttribute(QWebSettings::JavascriptEnabled, true);
webView->settings()->setAttribute(QWebSettings::PluginsEnabled, true);
webView->settings()->setAttribute(QWebSettings::LocalStorageEnabled, true);
webView->settings()->setAttribute(QWebSettings::OfflineStorageDatabaseEnabled,
true);
webView->settings()->setAttribute(
QWebSettings::OfflineWebApplicationCacheEnabled, true);
// Enable WebAudio
webView->settings()->setAttribute(QWebSettings::WebAudioEnabled, true);
// Don't allow JavaScript to open/close windows
webView->settings()->setAttribute(QWebSettings::JavascriptCanOpenWindows,
false);
webView->settings()->setAttribute(QWebSettings::JavascriptCanCloseWindows,
false);
// Allow JavaScript to access clipboard
webView->settings()->setAttribute(QWebSettings::JavascriptCanAccessClipboard,
true);
// Allow universal access from file URLs
webView->settings()->setAttribute(QWebSettings::LocalContentCanAccessFileUrls,
true);
webView->settings()->setAttribute(
QWebSettings::LocalContentCanAccessRemoteUrls, true);
// Show Web Inspector
webView->settings()->setAttribute(QWebSettings::DeveloperExtrasEnabled, true);
// Disallow cache
}
MainWindow::MainWindow()
: QMainWindow(),
webSocketServer("", QWebSocketServer::SslMode::NonSecureMode, this),
jsBridge(this) ,
osBridge(this) {
loadConfig();
if (optWebSocketServerPort > 0) {
QHostAddress address;
if (optWebSocketServerKey == "") {
qDebug() << "[ws] WebSocketServer listening at 127.0.0.1 because no key was "
"provided.";
address = QHostAddress::LocalHost;
} else {
qDebug() << "[ws] WebSocketServer listening at 0.0.0.0 (AnyIPv4) because a key "
"was provided.";
address = QHostAddress::AnyIPv4;
}
// TODO: Wait and retry if port is busy
if (webSocketServer.listen(address, optWebSocketServerPort)) {
qDebug() << "WebSocketServer listening on port" << optWebSocketServerPort;
} else {
qDebug() << "WebSocketServer failed to listen on port"
<< optWebSocketServerPort;
}
connect(&webSocketServer, SIGNAL(newConnection()), this,
SLOT(onNewWebSocketConnection()));
}
// Load proxy settings
if (optProxyHost != "") {
qDebug() << "Setting proxy to" << optProxyHost << ":" << optProxyPort;
QNetworkProxy proxy;
proxy.setType(QNetworkProxy::HttpProxy);
proxy.setHostName(optProxyHost);
proxy.setPort(optProxyPort);
QNetworkProxy::setApplicationProxy(proxy);
}
// Set window size
if (optWidth) {
resize(optWidth, optHeight);
}
// Make our window full-screen
// setWindowState(Qt::WindowFullScreen);
// Create the web view
webView = new QWebView(this);
configureWebView();
// Add the web view to our window
setCentralWidget(webView);
// Handle page load events
connect(webView, SIGNAL(loadStarted()), this, SLOT(onLoadStarted()));
// After 1000ms, load the URL
QTimer::singleShot(1000, this, SLOT(loadStartupUrl()));
}
MainWindow::~MainWindow() {}
void MainWindow::onLoadStarted() {
qDebug() << "onLoadStarted";
bool enableJSBridge = false;
bool enableOSBridge = false;
enableJSBridge = optEnableJSBridge;
enableOSBridge = optEnableOSBridge;
if (enableJSBridge) {
webView->page()->mainFrame()->addToJavaScriptWindowObject("FB_JSBridge", &jsBridge);
}
if (enableOSBridge) {
webView->page()->mainFrame()->addToJavaScriptWindowObject("FB_OSBridge", &osBridge);
}
}
void MainWindow::loadUrl(QString url) {
if (url.startsWith("./")) {
url = "file:///" + QDir::currentPath() + QDir::separator() + url.mid(2);
}
qDebug() << "Loading URL:" << url;
webView->load(QUrl(url));
}
void MainWindow::loadStartupUrl() { loadUrl(optStartupUrl); }
void MainWindow::loadConfig() {
bool haveUrlInCmdLine = false;
// Check for url in argv
if (qApp->arguments().size() > 1) {
optStartupUrl = qApp->arguments()[1];
haveUrlInCmdLine = true;
}
// Load config.json
QFile configFile("config.json");
if (!configFile.open(QIODevice::ReadOnly)) {
qDebug() << "Could not open config.json";
return;
}
QByteArray configData = configFile.readAll();
configFile.close();
QJsonDocument configJson = QJsonDocument::fromJson(configData);
QJsonObject configObject = configJson.object();
// Load url
if (configObject.contains("url") && (!haveUrlInCmdLine)) {
optStartupUrl = configObject["url"].toString();
}
// Load websocket port
if (configObject.contains("wsServerPort")) {
optWebSocketServerPort = configObject["wsServerPort"].toInt();
}
// Load websocket key
if (configObject.contains("wsServerKey")) {
optWebSocketServerKey = configObject["wsServerKey"].toString();
}
// Load window size
if (configObject.contains("width")) {
optWidth = configObject["width"].toInt();
}
if (configObject.contains("height")) {
optHeight = configObject["height"].toInt();
}
// Load proxy settings
if (configObject.contains("proxyHost")) {
optProxyHost = configObject["proxyHost"].toString();
}
if (configObject.contains("proxyPort")) {
optProxyPort = configObject["proxyPort"].toInt();
}
if (configObject.contains("enableJSBridge")) {
optEnableJSBridge = configObject["enableJSBridge"].toBool();
}
if (configObject.contains("enableOSBridge")) {
optEnableOSBridge = configObject["enableOSBridge"].toBool();
}
}
void MainWindow::onNewWebSocketConnection() {
bool isAuthenticated = false;
qDebug() << "[ws] New WebSocket connection";
QWebSocket *webSocket = webSocketServer.nextPendingConnection();
QString host = webSocket->peerAddress().toString();
qDebug() << "[ws] Connection from " << host;
QString clientSentKey = webSocket->request().rawHeader("X-FB-Key");
if (optWebSocketServerKey != "") {
if (clientSentKey == optWebSocketServerKey) {
qDebug() << "[ws] Accepted connection from " << host << " by key.";
isAuthenticated = true;
}
} else {
// Only allow 127.0.0.1 if no key is set
if (host == "127.0.0.1") {
qDebug() << "[ws] Accepted connection from " << host << " by default.";
isAuthenticated = true;
} else {
qDebug() << "[ws] Rejected connection from " << host << " by default.";
}
}
if (!isAuthenticated) {
webSocket->close();
return;
}
connect(webSocket, SIGNAL(textMessageReceived(QString)), this,
SLOT(onWebSocketTextMessageReceived(QString)));
connect(webSocket, SIGNAL(disconnected()), webSocket, SLOT(deleteLater()));
}
void MainWindow::onWebSocketTextMessageReceived(QString message) {
qDebug() << "[ws] Received message: " << message;
QWebSocket *webSocket = qobject_cast<QWebSocket *>(sender());
// Parse message as JSON Array
QJsonDocument jsonDoc = QJsonDocument::fromJson(message.toUtf8());
QJsonArray jsonArray = jsonDoc.array();
if (jsonArray.size() == 0) {
return;
}
QString cmd = jsonArray[0].toString();
QString arg1 = jsonArray[1].toString();
QJsonArray resp = QJsonArray();
resp.append("ok");
if (cmd == "loadUrl") {
qDebug() << "[ws] Loading URL: " << arg1;
webView->load(QUrl(arg1));
} else if (cmd == "clearCache") {
qDebug() << "[ws] Clearing cache";
webView->page()->networkAccessManager()->clearAccessCache();
webView->page()->networkAccessManager()->clearConnectionCache();
} else if (cmd == "exit") {
qDebug() << "[ws] Exiting";
QApplication::quit();
} else if (cmd == "eval") {
// Eval JavaScript in current page
qDebug() << "[ws] Evaluating JavaScript: " << arg1;
resp.append(
webView->page()->mainFrame()->evaluateJavaScript(arg1).toString());
}
webSocket->sendTextMessage(QJsonDocument(resp).toJson());
}
QString OSBridge::runCmd(QStringList args) {
QProcess proc;
QString program = args[0];
QStringList arguments = args.mid(1);
proc.start(program, arguments);
proc.waitForFinished();
QString output = proc.readAllStandardOutput();
return output;
}