Skip to content

Commit 15f5a88

Browse files
committed
This time SOCKS5 on the correct branch
1 parent 669fa96 commit 15f5a88

File tree

6 files changed

+37
-17
lines changed

6 files changed

+37
-17
lines changed

Socks5Connection.cpp

+22-9
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ void Socks5Connection::tryUdpAssociateRequest(struct bufferevent * bev){
8181

8282
bufferevent_write(bev, static_cast<const void *>(mem), 10);
8383
this->setCurrentState(UdpRequestSent);
84+
printf("SOCKS5 UDP request sent\n");
8485
}
8586

8687
void Socks5Connection::setBindAddress(Address address){
@@ -159,7 +160,7 @@ int Socks5Connection::tryReadUdpAssociateResponse(struct bufferevent * bev){
159160
this->setBindAddress(bind_address);
160161
this->setCurrentState(UdpAssociated);
161162

162-
printf("Socks5 UDP proxy ready at %s:%d\n", bind_address.ipstr(), bind_address.port());
163+
printf("Socks5 UDP proxy ready at %s:%d\n", bind_address.ipstr().c_str(), bind_address.port());
163164
this->working_ = true;
164165

165166
// this->setUDPsocket(AF_INET, SOCK_DGRAM, 0)
@@ -247,14 +248,14 @@ int Socks5Connection::unwrapDatagram(Address & addr, struct evbuffer * evb){
247248
addr.set_ipv4(header.address.ipv4());
248249
addr.set_port(header.address.port());
249250

250-
return sizeof(UdpEncapsulationHeader);
251+
return udpHeaderRead;
251252
}
252253

253254
/**
254255
* Called by libevent when there is data to read.
255256
*/
256257
void Socks5Connection::buffered_on_read(struct bufferevent *bev, void *cbarg) {
257-
258+
printf("Got SOCKS5 data\n");
258259
Socks5Connection *s5 = static_cast<Socks5Connection *>(cbarg);
259260

260261
switch(s5->getCurrentState()){
@@ -285,7 +286,7 @@ void Socks5Connection::buffered_on_event(struct bufferevent *bev, short int what
285286
// Sock5 server closed the connection!
286287
if((what & BEV_EVENT_EOF) == BEV_EVENT_EOF){
287288
Socks5Connection * s5 = static_cast<Socks5Connection *> (cbarg);
288-
s5->setCurrentState(Closed);
289+
//s5->setCurrentState(Closed);
289290
}
290291

291292
// Sock5 server closed the connection!
@@ -311,6 +312,7 @@ Socks5Connection::Socks5Connection(): Operational(true){
311312
}
312313

313314
void Socks5Connection::open(struct event_base *evbase, Address socks5_server){
315+
printf("Opening Sock5 tunnel to %s:%d \n", socks5_server.ipstr().c_str(), socks5_server.port());
314316
working_ = false;
315317

316318
struct bufferevent *bev;
@@ -321,14 +323,25 @@ void Socks5Connection::open(struct event_base *evbase, Address socks5_server){
321323
bufferevent_setcb(bev, buffered_on_read, buffered_on_write, buffered_on_event, this);
322324
bufferevent_enable(bev, EV_READ|EV_WRITE);
323325

324-
if(bufferevent_socket_connect(bev, (struct sockaddr*)&(socks5_server.addr), addrlen))
325-
errorOut("Cannot connect!");
326-
326+
if(bufferevent_socket_connect(bev, (struct sockaddr*)&(socks5_server.addr), addrlen)){
327+
printf("Cannot connect to SOCKS5 proxy at %s\n",socks5_server.ipstr(true).c_str());
328+
bufferevent_free(bev);
327329

328-
sendHandshake(bev);
329-
this->setCurrentState(HandshakeSent);
330+
return;
331+
} else {
332+
sendHandshake(bev);
333+
this->setCurrentState(HandshakeSent);
334+
printf("SOCKS5 handshake sent");
335+
}
330336
}
331337

338+
Socks5Connection::~Socks5Connection(){
339+
if(this->isOpen()){
340+
bufferevent_free(bev);
341+
this->state = Closed;
342+
}
343+
}
344+
332345
bool Socks5Connection::isOpen(){
333346
return this->getCurrentState() == 4;
334347
}

Socks5Connection.h

+3
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ enum Socks5ConnectionState {
3030
class Socks5Connection : public Operational{
3131
public:
3232
Socks5Connection();
33+
~Socks5Connection();
34+
3335
void open(struct event_base *evbase, Address socks5_server);
3436
bool isOpen();
3537

@@ -56,6 +58,7 @@ class Socks5Connection : public Operational{
5658
Socks5ConnectionState state;
5759
void sendHandshake(struct bufferevent *bev);
5860
Address bind_address;
61+
struct bufferevent * bev;
5962
};
6063

6164
typedef Socks5Connection Socks5Connection;

channel.cpp

+9-2
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,8 @@ int Channel::SendTo (evutil_socket_t sock, const Address& addr, struct evbuffer
313313
if(Channel::socks5_connection.isOpen()){
314314
lengthLoss = Channel::socks5_connection.prependHeader(addr, evb);
315315
destination = Channel::socks5_connection.getBindAddress();
316+
} else if(Channel::socks5_connection.getBindAddress() != Address()){
317+
printf("ERROR: Sending to %s:%d WITHOUT socks5\n", destination.ipstr(false).c_str(), destination.port());
316318
}
317319

318320
int length = evbuffer_get_length(evb);
@@ -367,15 +369,20 @@ int Channel::RecvFrom (evutil_socket_t sock, Address& addr, struct evbuffer *evb
367369
global_dgrams_down++;
368370

369371
// If there is a SOCKS5 connection set we need to unwrap the packet!
370-
if(Channel::socks5_connection.isOpen() && addr == Channel::socks5_connection.getBindAddress()){
372+
if(addr == Channel::socks5_connection.getBindAddress()){
371373
int result = Channel::socks5_connection.unwrapDatagram(addr, evb);
372374

373-
printf("Got SOCKS5 packet from %s:%d via proxy at %s:%d\n", addr.ipstr(), addr.port(), Channel::socks5_connection.getBindAddress().ipstr(), Channel::socks5_connection.getBindAddress().port());
375+
// printf("Got SOCKS5 packet from %s:%d via proxy at %s:%d\n", addr.ipstr().c_str(), addr.port(), Channel::socks5_connection.getBindAddress().ipstr().c_str(), Channel::socks5_connection.getBindAddress().port());
374376

375377
if(result > -1){
376378
length -= result;
377379

380+
// printf("Got %d byte packet from %s:%d via SOCKS5 proxy at %s:%d\n", length, addr.ipv4str(), addr.port(), socks5_connection.getBindAddress().ipv4str(), socks5_connection.getBindAddress().port());
381+
} else{
382+
printf("Got corrupted SOCKS5 packet from %s:%d\n", socks5_connection.getBindAddress().ipstr(false).c_str(), socks5_connection.getBindAddress().port());
378383
}
384+
} else if(Channel::socks5_connection.getBindAddress() != Address()){
385+
printf("Got %d bytes NON SOCKS5 from %s:%d\n", length, addr.ipstr(false).c_str(), addr.port());
379386
}
380387

381388
global_raw_bytes_down+=length;

hashtree.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ struct Sha1Hash {
4646
// for values up to 8192. Above that you'll have to edit the
4747
// SWIFT_MAX_SEND_DGRAM_SIZE in swift.h
4848
//
49-
#define SWIFT_DEFAULT_CHUNK_SIZE 1024
49+
#define SWIFT_DEFAULT_CHUNK_SIZE 6144
5050

5151

5252
class Storage;

swift.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ int utf8main (int argc, char** argv)
185185
Address socks5Address;
186186

187187
int c,n;
188-
while ( -1 != (c = getopt_long (argc, argv, ":h:f:d:l:t:D:pg:s:c:o:u:y:z:wBNHmM:e:r:ji:kC:1:2:3:T:G", long_options, 0)) ) {
188+
while ( -1 != (c = getopt_long (argc, argv, ":h:f:d:l:t:S:D:pg:s:c:o:u:y:z:wBNHmM:e:r:ji:kC:1:2:3:T:G", long_options, 0)) ) {
189189
switch (c) {
190190
case 'h':
191191
if (strlen(optarg)!=40)
@@ -197,7 +197,6 @@ int utf8main (int argc, char** argv)
197197
case 'S':
198198
socks5Address = Address(optarg);
199199
Channel::socks5_connection.open(Channel::evbase, socks5Address);
200-
printf("Opening Sock5 tunnel to %s:%d \n", socks5Address.ipstr(), socks5Address.port());
201200
fflush(stdout); // For testing
202201
break;
203202
case 'f':

swift.h

+1-3
Original file line numberDiff line numberDiff line change
@@ -659,9 +659,6 @@ namespace swift {
659659
static tint epoch, start;
660660
static uint64_t global_dgrams_up, global_dgrams_down, global_raw_bytes_up, global_raw_bytes_down, global_bytes_up, global_bytes_down;
661661
static void CloseChannelByAddress(const Address &addr);
662-
663-
friend void SetSocks5Connection(const Socks5Connection& tracker);
664-
void SetSocks5Connection(const Socks5Connection& tracker);
665662

666663
// SOCKMGMT
667664
// Arno: channel is also a "singleton" class that manages all sockets
@@ -1142,6 +1139,7 @@ namespace swift {
11421139
/** Set the default tracker that is used when Open is not passed a tracker
11431140
address. */
11441141
void SetTracker( const Address& tracker);
1142+
void SetSocks5Connection(const Socks5Connection& tracker);
11451143
/** Returns size of the file in bytes, 0 if unknown. Might be rounded up
11461144
to a kilobyte before the transmission is complete. */
11471145
uint64_t Size( int td);

0 commit comments

Comments
 (0)