Skip to content

Commit f159021

Browse files
committed
alpha 0.81 - enforcing SSL
1 parent 2dc83bc commit f159021

11 files changed

+129
-17
lines changed

WPDaemon.pro

+4-2
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ SOURCES += main.cpp \
3737
src_base/submodulecommand.cpp \
3838
src_base/commandparameter.cpp \
3939
src_base/website.cpp \
40-
src_interfaces/websitecontroller.cpp
40+
src_interfaces/websitecontroller.cpp \
41+
src_interfaces/configcontroller.cpp
4142

4243
HEADERS += \
4344
src_interfaces/clicontroller.h \
@@ -57,7 +58,8 @@ HEADERS += \
5758
src_base/submodulecommand.h \
5859
src_base/commandparameter.h \
5960
src_base/website.h \
60-
src_interfaces/websitecontroller.h
61+
src_interfaces/websitecontroller.h \
62+
src_interfaces/configcontroller.h
6163

6264
RESOURCES += \
6365
resources.qrc \

res_html/index.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,8 @@ <h4><a class="w3-button" href="javascript: display_all_submodules()" ><span id="
160160

161161
<div class="w3-card-2 w3-animate-left" id="parameter-container">
162162
<h5><a class="w3-button" href="javascript: display_all_commands()" ><span id="command_name">Feeds</span></a></h5>
163-
<table class="w3-table w3-striped w3-white w3-animate-left" id="parameter_selector" >
164-
<tr w3-repeat="parameters" class="w3-animate-left"><td><a href="javascript: set_command('{{name}}');" class="w3-label w3-padding"><i class="fa fa-angle-right"></i>&nbsp; {{name}}</a></td><td>{{html}}</tr>
163+
<table class="w3-table w3-white w3-animate-left" id="parameter_selector" >
164+
<tr w3-repeat="parameters" class="w3-animate-left"><td><i class="fa fa-angle-right"></i>&nbsp; {{name}}</td><td>{{html}}</tr>
165165
</table>
166166
</div>
167167
</div>

res_html/sockets.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
'use strict'
2-
var wsUri = "ws://localhost:65300"
2+
var wsUri = "wss://localhost:65300"
33
window.loggedin = false
44

55
window.onload = function () {

res_python/simple_https_server.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#!/usr/bin/python
2+
13
#taken from http://www.piware.de/2011/01/creating-an-https-server-in-python/
24
# generate certificates with the following command:
35
@@ -17,5 +19,5 @@
1719

1820
os.chdir('../public')
1921
httpd = BaseHTTPServer.HTTPServer(('127.0.0.1', 65531), SimpleHTTPServer.SimpleHTTPRequestHandler)
20-
#httpd.socket = ssl.wrap_socket(httpd.socket, certfile='../fullchain.cert', keyfile='../fullchain.key', server_side=True, ssl_version=ssl.PROTOCOL_TLSv1_2)
22+
httpd.socket = ssl.wrap_socket(httpd.socket, certfile='../cert.pem', keyfile='../key.pem', server_side=True, ssl_version=ssl.PROTOCOL_TLSv1_2)
2123
httpd.serve_forever()

src_base/commandparameter.cpp

+34
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,27 @@ QString CommandParameter::html()
3535
if (isOptional) {
3636
rv = QString("<input type='text' name='%1' class='w3-input w3-rest w3-border-blue'>").arg(parameterIndex);
3737
}
38+
if (isSwitch) {
39+
rv = QString("<input type='checkbox' name='%1' class='w3-checkbox w3-input w3-rest w3-large'>").arg(parameterIndex);
40+
}
41+
if (isLong) {
42+
flagValue.remove("&");
43+
flagValue.remove(">");
44+
flagValue.remove("<");
45+
flagValue.remove("--");
46+
flagValue.remove("\"");
47+
flagValue.remove("[");
48+
flagValue.remove("]");
49+
flagValue.remove("}");
50+
flagValue.remove("{");
51+
flagValue.remove("\\n");
52+
flagValue.remove(" ");
53+
flagValue.remove(QChar(QChar::LineFeed));
54+
flagValue.remove(QChar(QChar::CarriageReturn));
55+
flagValue.remove(QChar(QChar::LineSeparator));
56+
57+
rv = QString("<input type='text' name='%1' class=' w3-input w3-rest w3-large' placeholder='%2'>").arg(parameterIndex).arg(flagValue);
58+
}
3859

3960
return rv;
4061

@@ -53,21 +74,34 @@ void CommandParameter::parse()
5374
optional << this->find_captures("(\\[\\[^\\>\\<]+\\]\\B)");
5475
if (optional.count() == 0) {
5576
// qDebug() << "Switch" << m_string;
77+
5678
isSwitch = true;
79+
flagProperty = m_string;
80+
if (flagProperty.length() > 2) {
81+
flagProperty.remove(0, 3);
82+
}
83+
5784

5885
} else {
5986
// qDebug() << "optional" << optional.first();
6087
isOptional = true;
6188
m_string = optional.first();
89+
flagProperty = "";
6290
}
6391
} else {
6492
// qDebug() << "requiured" << required.first();
6593
isRequired = true;
6694
m_string = required.first();
95+
flagProperty = "";
6796
}
6897
} else {
6998
// qDebug() << "optional flag" << optionalFlags.first();
7099
m_string = optionalFlags.first();
100+
flagProperty = m_string.split("=").first();
101+
if (flagProperty.length() > 3) {
102+
flagProperty.remove(0, 3);
103+
}
104+
flagValue = m_string.split("=").last();
71105
isLong = true;
72106
}
73107

src_base/commandparameter.h

+2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ class CommandParameter : public QObject
1919
QStringList find_captures(QString regexString);
2020

2121
QString html();
22+
QString flagProperty;
23+
QString flagValue;
2224
signals:
2325

2426
public slots:

src_interfaces/clientinteraction.cpp

+29-4
Original file line numberDiff line numberDiff line change
@@ -153,16 +153,41 @@ QVariant ClientInteraction::set_current_command(QString i_command)
153153
param->m_string.remove("&");
154154
param->m_string.remove(">");
155155
param->m_string.remove("<");
156+
param->m_string.remove("--");
156157
param->m_string.remove("\"");
157158
param->m_string.remove("[");
158159
param->m_string.remove("]");
160+
param->m_string.remove("}");
161+
param->m_string.remove("{");
159162
param->m_string.remove("\\n");
160163
param->m_string.remove(QChar(QChar::LineFeed));
161164
param->m_string.remove(QChar(QChar::CarriageReturn));
162-
163-
QString newStr = QString("{\"name\":\"%1\",\"type\":\"%2\",\"html\":\"%3\"}").arg(param->m_string).arg(paramType).arg(param->html());
164-
rv.append(newStr);
165-
165+
param->m_string.remove(QChar(QChar::LineSeparator));
166+
if ((param->isRequired) || (param->isOptional)) {
167+
QString newStr = QString("{\"name\":\"%1\",\"type\":\"%2\",\"html\":\"%3\"}").arg(param->m_string).arg(paramType).arg(param->html());
168+
rv.append(newStr);
169+
}
170+
if ((param->isSwitch) || (param->isLong)) {
171+
172+
param->flagProperty.remove("&");
173+
param->flagProperty.remove(">");
174+
param->flagProperty.remove("<");
175+
param->flagProperty.remove("--");
176+
param->flagProperty.remove("\"");
177+
param->flagProperty.remove("[");
178+
param->flagProperty.remove("]");
179+
param->flagProperty.remove("}");
180+
param->flagProperty.remove("{");
181+
param->flagProperty.remove("\\n");
182+
param->flagProperty.remove(" ");
183+
param->flagProperty.remove(QChar(QChar::LineFeed));
184+
param->flagProperty.remove(QChar(QChar::CarriageReturn));
185+
param->flagProperty.remove(QChar(QChar::LineSeparator));
186+
187+
188+
QString newStr = QString("{\"name\":\"%1\",\"type\":\"%2\",\"html\":\"%3\"}").arg(param->flagProperty).arg(paramType).arg(param->html());
189+
rv.append(newStr);
190+
}
166191
if (i < (this->currentSubmodule->currentCommand->parameters.values().count() - 1)) {
167192
rv.append(",");
168193
}

src_interfaces/configcontroller.cpp

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include "configcontroller.h"
2+
#include <QCoreApplication>
3+
#include <QSettings>
4+
#include <QDir>
5+
6+
ConfigController::ConfigController(QObject *parent) : QObject(parent)
7+
{
8+
9+
}
10+
11+
QSettings* ConfigController::settings()
12+
{
13+
QSettings* _settings = new QSettings(qApp->applicationDirPath().append(QDir::separator()).append("config").append(QDir::separator()).append("wpdaemon.conf"), QSettings::IniFormat);
14+
// _settings->setValue("enable_ssl", "true");
15+
// _settings->setValue("ssl_certificate_file", qApp->applicationDirPath().append(QDir::separator()).append("cert.pem"));
16+
// _settings->setValue("ssl_key_file", qApp->applicationDirPath().append(QDir::separator()).append("key.pem"));
17+
return _settings;
18+
}

src_interfaces/configcontroller.h

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#ifndef CONFIGCONTROLLER_H
2+
#define CONFIGCONTROLLER_H
3+
4+
#include <QObject>
5+
#include <QSettings>
6+
class ConfigController : public QObject
7+
{
8+
Q_OBJECT
9+
public:
10+
explicit ConfigController(QObject *parent = nullptr);
11+
QSettings* settings();
12+
signals:
13+
14+
public slots:
15+
};
16+
17+
#endif // CONFIGCONTROLLER_H

src_interfaces/websocketcontroller.cpp

+17-7
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,33 @@
88
#include "../src_websocket/websocketclient.h"
99
#include "../src_interfaces/databasecontroller.h"
1010
#include "../src_interfaces/firewallcontroller.h"
11+
#include "../src_interfaces/configcontroller.h"
1112
#include <QtCore/QFile>
1213
#include <QtNetwork/QSslCertificate>
1314
#include <QtNetwork/QSslKey>
1415
#include <QCoreApplication>
1516
#include <QDir>
17+
#include <QSettings>
1618
WebSocketController::WebSocketController(QObject *parent, DatabaseController* i_db) : QObject(parent), m_db(i_db)
1719
{
1820

19-
server = new QWebSocketServer(QStringLiteral("WP Daemon Websocket Listener"), QWebSocketServer::NonSecureMode, this);
21+
m_settings = new ConfigController(this);
22+
bool enable_ssl = m_settings->settings()->value("enable_ssl", "false").toBool();
23+
24+
if (!enable_ssl) {
25+
server = new QWebSocketServer(QStringLiteral("WP Daemon Websocket Listener"), QWebSocketServer::NonSecureMode, this);
26+
} else {
27+
qDebug() << "Running in Secure Mode";
28+
server = new QWebSocketServer(QStringLiteral("WP Daemon Websocket Listener"), QWebSocketServer::SecureMode, this);
2029

2130

2231
QSslConfiguration sslConfiguration;
23-
/* QFile certFile(QStringLiteral(":/localhost.cert"));
24-
QFile keyFile(QStringLiteral(":/localhost.key"));
32+
/* QFile certFile(QStringLiteral(":/localhost.cert"));
33+
QFile keyFile(QStringLiteral(":/localhost.key")); */
34+
2535

26-
*/
27-
/* QFile certFile(qApp->applicationDirPath().append(QDir::separator()).append(QStringLiteral("fullchain.cert")));
28-
QFile keyFile(qApp->applicationDirPath().append(QDir::separator()).append(QStringLiteral("fullchain.key")));
36+
QFile certFile(m_settings->settings()->value("ssl_certificate_file", qApp->applicationDirPath().append(QDir::separator()).append(QStringLiteral("fullchain.cert"))).toString());
37+
QFile keyFile(m_settings->settings()->value("ssl_key_file", qApp->applicationDirPath().append(QDir::separator()).append(QStringLiteral("fullchain.key"))).toString());
2938
certFile.open(QIODevice::ReadOnly);
3039
keyFile.open(QIODevice::ReadOnly);
3140
QSslCertificate certificate(&certFile, QSsl::Pem);
@@ -36,8 +45,9 @@ QFile keyFile(QStringLiteral(":/localhost.key"));
3645
sslConfiguration.setLocalCertificate(certificate);
3746
sslConfiguration.setPrivateKey(sslKey);
3847
sslConfiguration.setProtocol(QSsl::TlsV1SslV3);
48+
server->setMaxPendingConnections(3);
3949
server->setSslConfiguration(sslConfiguration);
40-
*/
50+
}
4151
if (!server->listen(QHostAddress::LocalHost, 65300)) {
4252
qFatal("Failed to open web socket server.");
4353
//return 1;

src_interfaces/websocketcontroller.h

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class LoginController;
1414
class WebSocketClient;
1515
class DatabaseController;
1616
class FirewallController;
17+
class ConfigController;
1718
class WebSocketController : public QObject
1819
{
1920
Q_OBJECT
@@ -26,6 +27,7 @@ class WebSocketController : public QObject
2627
WebSocketClient* new_client;
2728
DatabaseController* m_db;
2829
FirewallController* m_firewall;
30+
ConfigController* m_settings;
2931

3032

3133
signals:

0 commit comments

Comments
 (0)