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
|
/*
* Copyright (C) 2010 Bolloré telecom
*
* Author:
* Jeremy Lainé
*
* Source:
* http://code.google.com/p/qxmpp
*
* This file is a part of QXmpp library.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
*/
#include <QEventLoop>
#include <QTcpServer>
#include <QTcpSocket>
#include <QTimer>
#ifdef Q_OS_WIN
#include <winsock2.h>
#else
#include <arpa/inet.h>
#endif
#include "QXmppSocks.h"
const static char SocksVersion = 5;
enum AuthenticationMethod {
NoAuthentication = 0,
GSSAPI = 1,
UsernamePassword = 2,
};
enum Command {
ConnectCommand = 1,
BindCommand = 2,
AssociateCommand = 3,
};
enum AddressType {
IPv4Address = 1,
DomainName = 3,
IPv6Address = 4,
};
enum ReplyType {
Succeeded = 0,
SocksFailure = 1,
ConnectionNotAllowed = 2,
NetworkUnreachable = 3,
HostUnreachable = 4,
ConnectionRefused = 5,
TtlExpired = 6,
CommandNotSupported = 7,
AddressTypeNotSupported = 8,
};
enum State {
ConnectState = 0,
CommandState = 1,
ReadyState = 2,
};
QByteArray encodeHostAndPort(quint8 type, const QByteArray &host, quint16 port)
{
QByteArray buffer;
buffer.resize(2);
// set host name
buffer[0] = type;
buffer[1] = host.size();
buffer.append(host);
// set port
int pos = buffer.size();
buffer.resize(pos + 2);
quint16 p = htons(port);
memcpy(buffer.data() + pos, &p, 2);
return buffer;
}
bool parseHostAndPort(const QByteArray buffer, quint8 &type, QByteArray &host, quint16 &port)
{
if (buffer.size() < 4)
return false;
// parse host type
int pos = 0;
type = buffer.at(pos);
pos++;
// parse host name
quint8 hostLength = buffer.at(pos);
pos++;
if (buffer.size() < hostLength + 4)
{
qWarning("Invalid host length");
return false;
}
host = buffer.mid(pos, hostLength);
pos += hostLength;
// parse host port
quint16 p;
memcpy(&p, buffer.data() + pos, 2);
port = ntohs(p);
return true;
}
QXmppSocksClient::QXmppSocksClient(const QHostAddress &proxyAddress, quint16 proxyPort, QObject *parent)
: QObject(parent),
m_proxyAddress(proxyAddress),
m_proxyPort(proxyPort),
m_step(ConnectState)
{
m_socket = new QTcpSocket(this);
connect(m_socket, SIGNAL(disconnected()), this, SIGNAL(disconnected()));
connect(m_socket, SIGNAL(connected()), this, SLOT(slotConnected()));
connect(m_socket, SIGNAL(readyRead()), this, SLOT(slotReadyRead()));
}
void QXmppSocksClient::close()
{
m_socket->close();
}
void QXmppSocksClient::connectToHost(const QString &hostName, quint16 hostPort)
{
m_hostName = hostName;
m_hostPort = hostPort;
m_socket->connectToHost(m_proxyAddress, m_proxyPort);
}
QString QXmppSocksClient::errorString() const
{
return m_socket->errorString();
}
QByteArray QXmppSocksClient::readAll()
{
return m_socket->readAll();
}
void QXmppSocksClient::slotConnected()
{
// send connect to server
QByteArray buffer;
buffer.resize(3);
buffer[0] = SocksVersion;
buffer[1] = 0x01; // number of methods
buffer[2] = NoAuthentication;
m_socket->write(buffer);
}
void QXmppSocksClient::slotReadyRead()
{
if (m_step == ConnectState)
{
m_step++;
// receive connect to server response
QByteArray buffer = m_socket->readAll();
if (buffer.size() != 2 || buffer.at(0) != SocksVersion || buffer.at(1) != NoAuthentication)
{
qWarning("QXmppSocksClient received an invalid response during handshake");
m_socket->close();
return;
}
// send CONNECT command
buffer.resize(3);
buffer[0] = SocksVersion;
buffer[1] = ConnectCommand;
buffer[2] = 0x00; // reserved
buffer.append(encodeHostAndPort(
DomainName,
m_hostName.toAscii(),
m_hostPort));
m_socket->write(buffer);
} else if (m_step == CommandState) {
m_step++;
// receive CONNECT response
QByteArray buffer = m_socket->readAll();
if (buffer.size() < 6 ||
buffer.at(0) != SocksVersion ||
buffer.at(1) != Succeeded ||
buffer.at(2) != 0)
{
qWarning("QXmppSocksClient received an invalid response to CONNECT command");
m_socket->close();
return;
}
// parse host
quint8 hostType;
QByteArray hostName;
quint16 hostPort;
if (!parseHostAndPort(buffer.mid(3), hostType, hostName, hostPort))
{
qWarning("QXmppSocksClient could not parse type/host/port");
m_socket->close();
return;
}
// FIXME : what do we do with the resulting name / port?
// from now on, forward signals
disconnect(m_socket, SIGNAL(readyRead()), this, SLOT(slotReadyRead()));
connect(m_socket, SIGNAL(readyRead()), this, SIGNAL(readyRead()));
// notify of connection
emit connected();
}
}
bool QXmppSocksClient::waitForConnected(int msecs)
{
QEventLoop loop;
connect(this, SIGNAL(connected()), &loop, SLOT(quit()));
connect(this, SIGNAL(disconnected()), &loop, SLOT(quit()));
QTimer::singleShot(msecs, &loop, SLOT(quit()));
loop.exec();
if (m_step == ReadyState && m_socket->isValid())
return true;
else
return false;
}
qint64 QXmppSocksClient::write(const QByteArray &data)
{
return m_socket->write(data);
}
QTcpSocket *QXmppSocksClient::socket()
{
return m_socket;
}
QXmppSocksServer::QXmppSocksServer(QObject *parent)
: QObject(parent)
{
m_server = new QTcpServer(this);
connect(m_server, SIGNAL(newConnection()), this, SLOT(slotNewConnection()));
}
void QXmppSocksServer::close()
{
m_server->close();
}
bool QXmppSocksServer::listen(const QHostAddress &address, quint16 port)
{
return m_server->listen(address, port);
}
QHostAddress QXmppSocksServer::serverAddress() const
{
return m_server->serverAddress();
}
quint16 QXmppSocksServer::serverPort() const
{
return m_server->serverPort();
}
void QXmppSocksServer::slotNewConnection()
{
QTcpSocket *socket = m_server->nextPendingConnection();
if (!socket)
return;
// register socket
m_states.insert(socket, ConnectState);
connect(socket, SIGNAL(readyRead()), this, SLOT(slotReadyRead()));
}
void QXmppSocksServer::slotReadyRead()
{
QTcpSocket *socket = qobject_cast<QTcpSocket*>(sender());
if (!socket || !m_states.contains(socket))
return;
if (m_states.value(socket) == ConnectState)
{
m_states.insert(socket, CommandState);
// receive connect to server request
QByteArray buffer = socket->readAll();
if (buffer.size() < 3 ||
buffer.at(0) != SocksVersion ||
buffer.at(1) + 2 != buffer.size())
{
qWarning("QXmppSocksServer received invalid handshake");
socket->close();
return;
}
// check authentication method
bool foundMethod = false;
for (int i = 2; i < buffer.size(); i++)
{
if (buffer.at(i) == NoAuthentication)
{
foundMethod = true;
break;
}
}
if (!foundMethod)
{
qWarning("QXmppSocksServer received bad authentication method");
socket->close();
return;
}
// send connect to server response
buffer.resize(2);
buffer[0] = SocksVersion;
buffer[1] = NoAuthentication;
socket->write(buffer);
} else if (m_states.value(socket) == CommandState) {
m_states.insert(socket, ReadyState);
// disconnect from signals
disconnect(socket, SIGNAL(readyRead()), this, SLOT(slotReadyRead()));
// receive command
QByteArray buffer = socket->readAll();
if (buffer.size() < 4 ||
buffer.at(0) != SocksVersion ||
buffer.at(1) != ConnectCommand ||
buffer.at(2) != 0x00)
{
qWarning("QXmppSocksServer received an invalid command");
socket->close();
return;
}
// parse host
quint8 hostType;
QByteArray hostName;
quint16 hostPort;
if (!parseHostAndPort(buffer.mid(3), hostType, hostName, hostPort))
{
qWarning("QXmppSocksServer could not parse type/host/port");
socket->close();
return;
}
// notify of connection
emit newConnection(socket, hostName, hostPort);
// send response
buffer.resize(3);
buffer[0] = SocksVersion;
buffer[1] = Succeeded;
buffer[2] = 0x00;
buffer.append(encodeHostAndPort(
DomainName,
hostName,
hostPort));
socket->write(buffer);
}
}
|