-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconnection.cpp
More file actions
202 lines (159 loc) · 5.86 KB
/
Copy pathconnection.cpp
File metadata and controls
202 lines (159 loc) · 5.86 KB
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
#include <QSslSocket>
#include <QSslKey>
#include <QSslCertificate>
#include "bitslinger.h"
#include "journal.h"
#include "utils/certificategenerator.h"
#include "utils/httpproxyrequesthandler.h"
#include "utils/sslconfigurationwidget.h"
#include "connection.h"
const qint64 MAX_CHUNK_SIZE = 32*1024; // 32K
Connection::Connection(QSslSocket *sock, QObject *parent)
: QObject(parent),
m_slinger(0),
m_server(0),
m_client(sock),
m_proxyType(Listener::TcpProxy),
m_sslMode(Listener::SslAutoMode),
m_connectionId(-1),
m_journal(0)
{
}
void Connection::startHttpProxy()
{
m_httpProxyHandler = new HttpProxyRequestHandler(this);
m_httpProxyHandler->setSocket(m_client);
connect(m_httpProxyHandler, SIGNAL(requestReady()), this, SLOT(proxyRequestReady()));
}
void Connection::proxyRequestReady()
{
connectToHost(m_httpProxyHandler->host(), m_httpProxyHandler->port());
}
void Connection::connectToHost(const QString &hostname, int port)
{
qDebug() << Q_FUNC_INFO;
m_journal->recordEvent(this, Journal::ClientConnectionEvent, QByteArray());
m_server = new QSslSocket(this);
qDebug() << "Connecting via" << m_slinger->upstreamProxy();
m_server->setProxy(m_slinger->upstreamProxy());
if (m_sslMode == Listener::SslStripClientMode)
m_server->connectToHostEncrypted(hostname, port);
else
m_server->connectToHost(hostname, port);
connect(m_client, SIGNAL(disconnected()), this, SLOT(clientDisconnected()));
connect(m_server, SIGNAL(connected()), this, SLOT(connected()));
connect(m_server, SIGNAL(disconnected()), this, SLOT(disconnected()));
connect(m_server, SIGNAL(encrypted()), this, SLOT(serverEncrypted()));
// TODO: we should log these event though we're going to ignore them
connect(m_server, SIGNAL(sslErrors(QList<QSslError>)), m_server, SLOT(ignoreSslErrors()));
}
void Connection::connected()
{
qDebug() << "Connected to server";
m_journal->recordEvent(this, Journal::ServerConnectionEvent, QByteArray());
if (m_proxyType == Listener::HttpProxy) {
m_httpProxyHandler->connectionSucceeded();
m_httpProxyHandler->deleteLater();
m_httpProxyHandler = 0;
}
connect(m_client, SIGNAL(readyRead()), this, SLOT(clientData()));
connect(m_server, SIGNAL(readyRead()), this, SLOT(serverData()));
// The client may have already sent some data at this point
if (m_client->bytesAvailable())
clientData();
}
void Connection::disconnected()
{
qDebug() << "Disconnected from server";
m_journal->recordEvent(this, Journal::ServerDisconnectionEvent, QByteArray());
m_client->close();
}
void Connection::clientDisconnected()
{
qDebug() << "Client has disconnected";
m_journal->recordEvent(this, Journal::ClientDisconnectionEvent, QByteArray());
m_server->close();
}
int Connection::findSslClientHello(const QByteArray &data)
{
// Search the message for a client hello, this might false negative if we have a very
// fragemented write
int start = data.indexOf("\x16\x03");
if (start < 0) {
return -1;
}
if (data[start+5] == 0x01 && data[start+9] == 0x03) {
qDebug() << QString("Client hello, record version 3.%1, handshake version 3.%2")
.arg(int(data[start+2])).arg(int(start+data[10]));
return start;
}
return -1;
}
void Connection::clientData()
{
while (m_client->bytesAvailable()) {
// Detect SSL client hello
QByteArray peeked = m_client->peek(qMin(m_client->bytesAvailable(), MAX_CHUNK_SIZE));
int index = findSslClientHello(peeked);
// If we found one
if (index > 0) {
transferClientData(index);
}
if (index >= 0) {
qDebug() << "SSL client hello detected";
m_journal->recordEvent(this, Journal::ClientSwitchedToSslEvent, QByteArray());
if ((m_sslMode != Listener::DumbMode) && (m_sslMode != Listener::SslStripServerMode)) {
if (!m_server->isEncrypted()) {
m_server->flush();
m_server->startClientEncryption();
qDebug() << "Swtiching to ssl for server connection";
return; // Force exit without processing the data
}
else {
encryptClientConnection();
}
}
}
transferClientData(qMin(m_client->bytesAvailable(), MAX_CHUNK_SIZE));
}
}
void Connection::transferClientData(int bytes)
{
QByteArray data = m_client->read(bytes);
qDebug() << ">>>" << bytes << "bytes";
m_journal->recordEvent(this, Journal::ClientDataEvent, data);
m_server->write(data);
}
void Connection::serverData()
{
while (m_server->bytesAvailable()) {
QByteArray data = m_server->read(qMin(m_server->bytesAvailable(), MAX_CHUNK_SIZE));
qDebug() << "<<<" << data.size() << "bytes";
m_journal->recordEvent(this, Journal::ServerDataEvent, data);
m_client->write(data);
}
}
void Connection::serverEncrypted()
{
m_journal->recordEvent(this, Journal::ServerSwitchedToSslEvent, QByteArray());
qDebug() << "Server connection is now encrypted, responding to client";
#if 0
SslConfigurationWidget *w = new SslConfigurationWidget();
w->setConfiguration(m_server->sslConfiguration());
w->show();
SslConfigurationWidget *w2 = new SslConfigurationWidget();
w2->setReadOnly(true);
w2->setConfiguration(m_server->sslConfiguration());
w2->show();
#endif
encryptClientConnection();
}
void Connection::encryptClientConnection()
{
CertificateGenerator *gen = m_slinger->certificateGenerator();
QSslCertificate leaf = gen->createClone(m_server->peerCertificate());
qDebug() << leaf.toText();
m_client->setPrivateKey(gen->leafKey());
m_client->setLocalCertificate(leaf);
m_client->startServerEncryption();
}