This repository has been archived by the owner on Mar 7, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
socksd.cpp
178 lines (147 loc) · 3.68 KB
/
socksd.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
#include"socksd.h"
#include<qapplication.h>
#include"bconsole.h"
#include"bsocket.h"
#include"socks.h"
#include<stdio.h>
class App::Private
{
public:
Private() {}
ByteStream *bs;
BConsole *c;
SocksServer *serv;
SocksClient *client;
QString user, pass;
};
App::App(int port, const QString &user, const QString &pass)
:QObject(0)
{
d = new Private;
d->user = user;
d->pass = pass;
d->c = new BConsole;
connect(d->c, SIGNAL(connectionClosed()), SLOT(con_connectionClosed()));
connect(d->c, SIGNAL(readyRead()), SLOT(con_readyRead()));
d->bs = 0;
d->client = 0;
d->serv = new SocksServer;
connect(d->serv, SIGNAL(incomingReady()), SLOT(ss_incomingReady()));
d->serv->listen(port);
fprintf(stderr, "socksd: listening on port %d\n", port);
}
App::~App()
{
if(d->client)
d->client->deleteLater();
delete d->serv;
delete d->c;
delete d;
}
void App::st_connectionClosed()
{
fprintf(stderr, "socksd: Connection closed by foreign host.\n");
quit();
}
void App::st_delayedCloseFinished()
{
quit();
}
void App::st_readyRead()
{
QByteArray a = d->bs->read();
d->c->write(a);
}
void App::st_error(int x)
{
fprintf(stderr, "socksd: Stream error [%d].\n", x);
quit();
}
void App::con_connectionClosed()
{
fprintf(stderr, "adconn: Closing.\n");
d->bs->close();
if(d->bs->bytesToWrite() == 0)
quit();
}
void App::con_readyRead()
{
QByteArray a = d->c->read();
d->bs->write(a);
}
void App::ss_incomingReady()
{
fprintf(stderr, "incoming connection!\n");
SocksClient *c = d->serv->takeIncoming();
if(!c)
return;
fprintf(stderr, "accepted\n");
connect(c, SIGNAL(incomingMethods(int)), SLOT(sc_incomingMethods(int)));
connect(c, SIGNAL(incomingAuth(const QString &, const QString &)), SLOT(sc_incomingAuth(const QString &, const QString &)));
connect(c, SIGNAL(incomingRequest(const QString &, int)), SLOT(sc_incomingRequest(const QString &, int)));
connect(c, SIGNAL(error(int)), SLOT(sc_error(int)));
d->client = c;
}
void App::sc_incomingMethods(int m)
{
fprintf(stderr, "m=%d\n", m);
if(d->user.isEmpty() && m & SocksClient::AuthNone)
d->client->chooseMethod(SocksClient::AuthNone);
else if(m & SocksClient::AuthUsername)
d->client->chooseMethod(SocksClient::AuthUsername);
else {
d->client->deleteLater();
d->client = 0;
fprintf(stderr, "unsupported method!\n");
}
}
void App::sc_incomingAuth(const QString &user, const QString &pass)
{
fprintf(stderr, "incoming auth: user=[%s], pass=[%s]\n", user.latin1(), pass.latin1());
if(user == d->user && pass == d->pass) {
d->client->authGrant(true);
}
else {
d->client->authGrant(false);
d->client->deleteLater();
d->client = 0;
}
}
void App::sc_incomingRequest(const QString &host, int port)
{
fprintf(stderr, "request: host=[%s], port=[%d]\n", host.latin1(), port);
disconnect(d->client, SIGNAL(error(int)), this, SLOT(sc_error(int)));
connect(d->client, SIGNAL(connectionClosed()), SLOT(st_connectionClosed()));
connect(d->client, SIGNAL(delayedCloseFinished()), SLOT(st_delayedCloseFinished()));
connect(d->client, SIGNAL(readyRead()), SLOT(st_readyRead()));
connect(d->client, SIGNAL(error(int)), SLOT(st_error(int)));
d->bs = d->client;
d->client->requestGrant(true);
fprintf(stderr, "<< Active >>\n");
}
void App::sc_error(int)
{
d->client->deleteLater();
d->client = 0;
fprintf(stderr, "error!\n");
}
int main(int argc, char **argv)
{
QApplication app(argc, argv, false);
if(argc < 2) {
printf("usage: socksd [port] [user] [pass]\n\n");
return 0;
}
QString p = argv[1];
int port = p.toInt();
QString user, pass;
if(argc >= 4) {
user = argv[2];
pass = argv[3];
}
App *a = new App(port, user, pass);
QObject::connect(a, SIGNAL(quit()), &app, SLOT(quit()));
app.exec();
delete a;
return 0;
}