forked from irwir/eMule
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AsyncProxySocketLayer.cpp
1241 lines (1126 loc) · 39.4 KB
/
AsyncProxySocketLayer.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
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*CAsyncProxySocketLayer by Tim Kosse ([email protected])
Version 1.6 (2003-03-26)
--------------------------------------------------------
Introduction:
-------------
This class is layer class for CAsyncSocketEx. With this class you
can connect through SOCKS4/5 and HTTP 1.1 proxies. This class works
as semi-transparent layer between CAsyncSocketEx and the actual socket.
This class is used in FileZilla, a powerful open-source FTP client.
It can be found under http://sourceforge.net/projects/filezilla
For more information about SOCKS4/5 goto
http://www.socks.nec.com/socksprot.html
For more information about HTTP 1.1 goto http://www.rfc-editor.org
and search for RFC2616
How to use?
-----------
You don't have to change much in you already existing code to use
CAsyncProxySocketLayer.
To use it, create an instance of CAsyncProxySocketLayer, call SetProxy
and attach it to a CAsyncSocketEx instance.
You have to process OnLayerCallback in you CAsyncSocketEx instance as it will
receive all layer nofications.
The following notifications are sent:
//Error codes
PROXYERROR_NOERROR 0
PROXYERROR_NOCONN 1 //Can't connect to proxy server, use GetLastError for more information
PROXYERROR_REQUESTFAILED 2 //Request failed, can't send data
PROXYERROR_AUTHREQUIRED 3 //Authentication required
PROXYERROR_AUTHTYPEUNKNOWN 4 //Authtype unknown or not supported
PROXYERROR_AUTHFAILED 5 //Authentication failed
PROXYERROR_AUTHNOLOGON 6
PROXYERROR_CANTRESOLVEHOST 7
//Status messages
PROXYSTATUS_LISTENSOCKETCREATED 8 //Called when a listen socket was created successfully. Unlike the normal listen function,
//a socksified socket has to connect to the proxy to negotiate the details with the server
//on which the listen socket will be created
//The two parameters will contain the ip and port of the listen socket on the server.
If you want to use CAsyncProxySocketLayer to create a listen socket, you
have to use this overloaded function:
BOOL PrepareListen(unsigned long serverIp);
serverIP is the IP of the server you are already connected
through the SOCKS proxy. You can't use listen sockets over a
SOCKS proxy without a primary connection. Listen sockets are only
supported by SOCKS proxies, this won't work with HTTP proxies.
When the listen socket is created successfully, the PROXYSTATUS_LISTENSOCKETCREATED
notification is sent. The parameters will tell you the ip and the port of the listen socket.
After it you have to handle the OnAccept message and accept the
connection.
Be carful when calling Accept: rConnected socket will NOT be filled! Instead use the instance which created the
listen socket, it will handle the data connection.
If you want to accept more than one connection, you have to create a listing socket for each of them!
Description of important functions and their parameters:
--------------------------------------------------------
void SetProxy(int nProxyType);
void SetProxy(int nProxyType, const char * pProxyHost, int nProxyPort);
void SetProxy(int nProxyType, const char * pProxyHost, int nProxyPort, const char *pProxyUser, const char * pProxyPass);
Call one of this functions to set the proxy type.
Parametes:
- nProxyType specifies the Proxy Type.
- ProxyHost and nProxyPort specify the address of the proxy
- ProxyUser and ProxyPass are only available for SOCKS5 proxies.
supported proxy types:
PROXYTYPE_NOPROXY
PROXYTYPE_SOCKS4
PROXYTYPE_SOCKS4A
PROXYTYPE_SOCKS5
PROXYTYPE_HTTP11
There are also some other functions:
GetProxyPeerName
Like GetPeerName of CAsyncSocket, but returns the address of the
server connected through the proxy. If using proxies, GetPeerName
only returns the address of the proxy.
int GetProxyType();
Returns the used proxy
const int GetLastProxyError() const;
Returns the last proxy error
License
-------
Feel free to use this class, as long as you don't claim that you wrote it
and this copyright notice stays intact in the source files.
If you use this class in commercial applications, please send a short message
Version history
---------------
- 1.6 got rid of MFC
- 1.5 released CAsyncSocketExLayer version
- 1.4 added Unicode support
- 1.3 added basic HTTP1.1 authentication
fixed memory leak in SOCKS5 code
OnSocksOperationFailed will be called after Socket has been closed
fixed some minor bugs
- 1.2 renamed into CAsyncProxySocketLayer
added HTTP1.1 proxy support
- 1.1 fixes all known bugs, mostly with SOCKS5 authentication
- 1.0 initial release
*/
#include "stdafx.h"
#include "AsyncProxySocketLayer.h"
#include "CBase64coding.hpp"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
extern CStringA ipstrA(uint32 nIP);
//////////////////////////////////////////////////////////////////////
// Konstruktion/Destruktion
//////////////////////////////////////////////////////////////////////
CAsyncProxySocketLayer::CAsyncProxySocketLayer()
{
m_nProxyOpID = 0;
m_nProxyOpState = 0;
m_pRecvBuffer = 0;
m_nRecvBufferPos = 0;
m_nProxyPeerIP = 0;
m_nProxyPeerPort = 0;
m_pProxyPeerHost = NULL;
m_pStrBuffer = NULL;
m_iStrBuffSize = 0;
m_ProxyData.nProxyType = PROXYTYPE_NOPROXY;
}
CAsyncProxySocketLayer::~CAsyncProxySocketLayer()
{
delete[] m_pProxyPeerHost;
ClearBuffer();
}
/////////////////////////////////////////////////////////////////////////////
// Member-Funktion CAsyncProxySocketLayer
void CAsyncProxySocketLayer::SetProxy(int nProxyType)
{
//Validate the parameters
ASSERT( nProxyType == PROXYTYPE_NOPROXY );
m_ProxyData.nProxyType = nProxyType;
}
void CAsyncProxySocketLayer::SetProxy(int nProxyType, const CStringA& strProxyHost, int ProxyPort)
{
//Validate the parameters
ASSERT( nProxyType == PROXYTYPE_SOCKS4 ||
nProxyType == PROXYTYPE_SOCKS4A ||
nProxyType == PROXYTYPE_SOCKS5 ||
nProxyType == PROXYTYPE_HTTP10 ||
nProxyType == PROXYTYPE_HTTP11 );
ASSERT( !m_nProxyOpID );
ASSERT( !strProxyHost.IsEmpty() );
ASSERT( ProxyPort > 0);
ASSERT( ProxyPort <= 65535 );
m_ProxyData.nProxyType = nProxyType;
m_ProxyData.strProxyHost = strProxyHost;
m_ProxyData.nProxyPort = ProxyPort;
m_ProxyData.bUseLogon = FALSE;
}
void CAsyncProxySocketLayer::SetProxy(int nProxyType, const CStringA& strProxyHost, int ProxyPort,
const CStringA& strProxyUser, const CStringA& strProxyPass)
{
//Validate the parameters
ASSERT( nProxyType == PROXYTYPE_SOCKS5 || nProxyType == PROXYTYPE_HTTP10 || nProxyType == PROXYTYPE_HTTP11 );
ASSERT( !m_nProxyOpID );
ASSERT( !strProxyHost.IsEmpty() );
ASSERT( ProxyPort > 0 );
ASSERT( ProxyPort <= 65535 );
m_ProxyData.nProxyType = nProxyType;
m_ProxyData.strProxyHost = strProxyHost;
m_ProxyData.nProxyPort = ProxyPort;
m_ProxyData.bUseLogon = TRUE;
m_ProxyData.strProxyUser = strProxyUser;
m_ProxyData.strProxyPass = strProxyPass;
}
CStringA GetSocks4Error(UINT ver, UINT cd)
{
if (ver != 0) {
CStringA strError;
strError.Format("Unknown protocol version: %u", ver);
return strError;
}
switch (cd)
{
case 90: return "";
case 91: return "Request rejected or failed";
case 92: return "Failed to connect to 'identd'";
case 93: return "'identd' user-id error";
default:{
CStringA strError;
strError.Format("Unknown command: %u", cd);
return strError;
}
}
}
CStringA GetSocks5Error(UINT rep)
{
switch (rep)
{
case 0x00: return "";
case 0x01: return "General SOCKS server failure";
case 0x02: return "Connection not allowed by ruleset";
case 0x03: return "Network unreachable";
case 0x04: return "Host unreachable";
case 0x05: return "Connection refused";
case 0x06: return "TTL expired";
case 0x07: return "Command not supported";
case 0x08: return "Address type not supported";
default:{
CStringA strError;
strError.Format("Unknown reply: %u", rep);
return strError;
}
}
}
void CAsyncProxySocketLayer::OnClose(int nErrorCode)
{
// We must route that event with the same functionality (PostMessage) which is used by
// the 'OnReceive' and 'OnConnect' event handlers. Otherwise the socket event queue of
// the underlying 'CAsyncSocketEx' may get out of sync.
TriggerEvent(FD_CLOSE, nErrorCode, TRUE);
}
void CAsyncProxySocketLayer::OnAccept(int nErrorCode)
{
// We must route that event with the same functionality (PostMessage) which is used by
// the 'OnReceive' and 'OnConnect' event handlers. Otherwise the socket event queue of
// the underlying 'CAsyncSocketEx' may get out of sync.
TriggerEvent(FD_ACCEPT, nErrorCode, TRUE);
}
void CAsyncProxySocketLayer::OnSend(int nErrorCode)
{
// We must route that event with the same functionality (PostMessage) which is used by
// the 'OnReceive' and 'OnConnect' event handlers. Otherwise the socket event queue of
// the underlying 'CAsyncSocketEx' may get out of sync.
TriggerEvent(FD_WRITE, nErrorCode, TRUE);
}
void CAsyncProxySocketLayer::OnReceive(int nErrorCode)
{
if (m_nProxyOpID == 0) {
TriggerEvent(FD_READ, nErrorCode, TRUE);
return;
}
if (nErrorCode)
TriggerEvent(FD_READ, nErrorCode, TRUE);
if (m_nProxyOpState == 0) //We should not receive a response yet!
return;
if (m_ProxyData.nProxyType == PROXYTYPE_SOCKS4 || m_ProxyData.nProxyType == PROXYTYPE_SOCKS4A)
{
if ( m_nProxyOpState == 1 // Response to initial connect or bind request
|| m_nProxyOpState == 2)// Response (2nd) to bind request
{
if (!m_pRecvBuffer)
m_pRecvBuffer = new char[8];
int numread = ReceiveNext(m_pRecvBuffer + m_nRecvBufferPos, 8 - m_nRecvBufferPos);
if (numread == SOCKET_ERROR) {
int nErrorCode = WSAGetLastError();
if (nErrorCode != WSAEWOULDBLOCK) {
DoLayerCallback(LAYERCALLBACK_LAYERSPECIFIC, PROXYERROR_REQUESTFAILED, nErrorCode);
if (m_nProxyOpID == PROXYOP_CONNECT)
TriggerEvent(FD_CONNECT, nErrorCode, TRUE);
else
TriggerEvent(FD_ACCEPT, nErrorCode, TRUE);
Reset();
ClearBuffer();
}
return;
}
m_nRecvBufferPos += numread;
// +----+----+----+----+----+----+----+----+
// | VN | CD | DSTPORT | DSTIP |
// +----+----+----+----+----+----+----+----+
// # of bytes: 1 1 2 4
//
// VN is the version of the reply code and should be 0. CD is the result
// code with one of the following values:
//
// 90: request granted
// 91: request rejected or failed
// 92: request rejected becasue SOCKS server cannot connect to
// identd on the client
// 93: request rejected because the client program and identd
// report different user-ids
if (m_nRecvBufferPos == 8)
{
TRACE("SOCKS4 response: VN=%u CD=%u DSTPORT=%u DSTIP=%s\n", (BYTE)m_pRecvBuffer[0], (BYTE)m_pRecvBuffer[1], ntohs(*(u_short*)&m_pRecvBuffer[2]), ipstrA(*(u_long*)&m_pRecvBuffer[4]));
if (m_pRecvBuffer[0] != 0 || m_pRecvBuffer[1] != 90) {
DoLayerCallback(LAYERCALLBACK_LAYERSPECIFIC, PROXYERROR_REQUESTFAILED, 0, (LPARAM)(LPCSTR)GetSocks4Error(m_pRecvBuffer[0], m_pRecvBuffer[1]));
if (m_nProxyOpID == PROXYOP_CONNECT)
TriggerEvent(FD_CONNECT, WSAECONNABORTED, TRUE);
else
TriggerEvent(FD_ACCEPT, WSAECONNABORTED, TRUE);
Reset();
ClearBuffer();
return;
}
// Proxy SHOULD answer with DSTPORT and DSTIP. Most proxies do, but some answer with 0.0.0.0:0
//ASSERT( m_nProxyPeerPort == *(u_short*)&m_pRecvBuffer[2] );
//ASSERT( m_nProxyPeerIP == 0 || m_nProxyPeerIP == *(u_long*)&m_pRecvBuffer[4] );
if ( (m_nProxyOpID == PROXYOP_CONNECT && m_nProxyOpState == 1) ||
(m_nProxyOpID == PROXYOP_BIND && m_nProxyOpState == 2) )
{
int nOpIDEvent = m_nProxyOpID == PROXYOP_CONNECT ? FD_CONNECT : FD_ACCEPT;
ClearBuffer();
Reset();
TriggerEvent(nOpIDEvent, 0, TRUE);
TriggerEvent(FD_READ, 0, TRUE);
TriggerEvent(FD_WRITE, 0, TRUE);
return;
}
else if (m_nProxyOpID == PROXYOP_BIND && m_nProxyOpState == 1)
{
// Listen socket created
m_nProxyOpState = 2;
u_long ip = *(u_long*)&m_pRecvBuffer[4];
if (ip == 0)
{
// No IP returned, use the IP of the proxy server
SOCKADDR SockAddr = {0};
int SockAddrLen = sizeof(SockAddr);
if (GetPeerName(&SockAddr, &SockAddrLen)) {
ip = ((LPSOCKADDR_IN)&SockAddr)->sin_addr.S_un.S_addr;
}
else {
DoLayerCallback(LAYERCALLBACK_LAYERSPECIFIC, PROXYERROR_REQUESTFAILED, 0);
if (m_nProxyOpID == PROXYOP_CONNECT)
TriggerEvent(FD_CONNECT, WSAECONNABORTED, TRUE);
else
TriggerEvent(FD_ACCEPT, WSAECONNABORTED, TRUE);
Reset();
ClearBuffer();
return;
}
}
t_ListenSocketCreatedStruct data;
data.ip = ip;
data.nPort = *(u_short*)&m_pRecvBuffer[2];
DoLayerCallback(LAYERCALLBACK_LAYERSPECIFIC, PROXYSTATUS_LISTENSOCKETCREATED, 0, (LPARAM)&data);
// Wait for 2nd response to bind request
}
ClearBuffer();
}
}
}
else if (m_ProxyData.nProxyType == PROXYTYPE_SOCKS5)
{
if ( m_nProxyOpState == 1 // Response to initialization request
|| m_nProxyOpState == 2)// Response to authentication request
{
if (!m_pRecvBuffer)
m_pRecvBuffer = new char[2];
int numread = ReceiveNext(m_pRecvBuffer + m_nRecvBufferPos, 2 - m_nRecvBufferPos);
if (numread == SOCKET_ERROR) {
int nErrorCode = WSAGetLastError();
if (nErrorCode != WSAEWOULDBLOCK) {
DoLayerCallback(LAYERCALLBACK_LAYERSPECIFIC, PROXYERROR_REQUESTFAILED, nErrorCode);
if (m_nProxyOpID == PROXYOP_CONNECT)
TriggerEvent(FD_CONNECT, nErrorCode, TRUE);
else
TriggerEvent(FD_ACCEPT, nErrorCode, TRUE);
Reset();
}
return;
}
m_nRecvBufferPos += numread;
if (m_nRecvBufferPos == 2)
{
TRACE("SOCKS5 response: VER=%u METHOD=%u\n", (BYTE)m_pRecvBuffer[0], (BYTE)m_pRecvBuffer[1]);
bool bIniReqFailed = m_nProxyOpState == 1 && m_pRecvBuffer[0] != 5; // Response to initialization request
bool bAuthReqFailed = m_nProxyOpState == 2 && m_pRecvBuffer[1] != 0; // Response to authentication request
if (bIniReqFailed || bAuthReqFailed) {
DoLayerCallback(LAYERCALLBACK_LAYERSPECIFIC, bAuthReqFailed ? PROXYERROR_AUTHFAILED : PROXYERROR_REQUESTFAILED, 0);
if (m_nProxyOpID == PROXYOP_CONNECT)
TriggerEvent(FD_CONNECT, WSAECONNABORTED, TRUE);
else
TriggerEvent(FD_ACCEPT, WSAECONNABORTED, TRUE);
Reset();
ClearBuffer();
return;
}
if (m_nProxyOpState == 1 && m_pRecvBuffer[1] != 0) // Authentication needed
{
if (m_pRecvBuffer[1] != 2) { // Unknown authentication type
DoLayerCallback(LAYERCALLBACK_LAYERSPECIFIC, PROXYERROR_AUTHTYPEUNKNOWN, 0);
if (m_nProxyOpID == PROXYOP_CONNECT)
TriggerEvent(FD_CONNECT, WSAECONNABORTED, TRUE);
else
TriggerEvent(FD_ACCEPT, WSAECONNABORTED, TRUE);
Reset();
ClearBuffer();
return;
}
if (!m_ProxyData.bUseLogon) {
DoLayerCallback(LAYERCALLBACK_LAYERSPECIFIC, PROXYERROR_AUTHNOLOGON, 0);
if (m_nProxyOpID == PROXYOP_CONNECT)
TriggerEvent(FD_CONNECT, WSAECONNABORTED, TRUE);
else
TriggerEvent(FD_ACCEPT, WSAECONNABORTED, TRUE);
Reset();
ClearBuffer();
return;
}
// Send authentication
//
// RFC 1929 - Username/Password Authentication for SOCKS V5
//
// +----+------+----------+------+----------+
// |VER | ULEN | UNAME | PLEN | PASSWD |
// +----+------+----------+------+----------+
// | 1 | 1 | 1 to 255 | 1 | 1 to 255 |
// +----+------+----------+------+----------+
//
// The VER field contains the current version of the subnegotiation,
// which is X'01'. The ULEN field contains the length of the UNAME field
// that follows. The UNAME field contains the username as known to the
// source operating system. The PLEN field contains the length of the
// PASSWD field that follows. The PASSWD field contains the password
// association with the given UNAME.
char cBuff[1 + 1 + 255 + 1 + 255];
int iLen = _snprintf(cBuff, _countof(cBuff), "\x01%c%s%c%s",
m_ProxyData.strProxyUser.GetLength(), m_ProxyData.strProxyUser,
m_ProxyData.strProxyPass.GetLength(), m_ProxyData.strProxyPass);
int res = SendNext(cBuff, iLen);
if (res == SOCKET_ERROR || res < iLen)
{
int nErrorCode = WSAGetLastError();
if (nErrorCode != WSAEWOULDBLOCK || res < iLen) {
DoLayerCallback(LAYERCALLBACK_LAYERSPECIFIC, PROXYERROR_REQUESTFAILED, nErrorCode);
if (m_nProxyOpID == PROXYOP_CONNECT)
TriggerEvent(FD_CONNECT, nErrorCode, TRUE);
else
TriggerEvent(FD_ACCEPT, nErrorCode, TRUE);
Reset();
return;
}
}
ClearBuffer();
m_nProxyOpState = 2;
return;
}
// Send connection request
const char* pszAsciiProxyPeerHost;
int iLenAsciiProxyPeerHost;
if (m_nProxyPeerIP == 0) {
pszAsciiProxyPeerHost = m_pProxyPeerHost;
iLenAsciiProxyPeerHost = strlen(pszAsciiProxyPeerHost);
}
else {
pszAsciiProxyPeerHost = 0;
iLenAsciiProxyPeerHost = 0;
}
char* pcReq = (char*)_alloca(10 + 1 + iLenAsciiProxyPeerHost);
pcReq[0] = 5;
pcReq[1] = (m_nProxyOpID == PROXYOP_CONNECT) ? 1 : 2;
pcReq[2] = 0;
int iReqLen = 3;
if (m_nProxyPeerIP) {
pcReq[iReqLen++] = 1;
*(u_long*)&pcReq[iReqLen] = m_nProxyPeerIP;
iReqLen += 4;
}
else {
pcReq[iReqLen++] = 3;
pcReq[iReqLen++] = (char)iLenAsciiProxyPeerHost;
memcpy(&pcReq[iReqLen], pszAsciiProxyPeerHost, iLenAsciiProxyPeerHost);
iReqLen += iLenAsciiProxyPeerHost;
}
*(u_short*)&pcReq[iReqLen] = m_nProxyPeerPort;
iReqLen += 2;
int res = SendNext(pcReq, iReqLen);
if (res == SOCKET_ERROR || res < iReqLen)
{
int nErrorCode = WSAGetLastError();
if (nErrorCode != WSAEWOULDBLOCK || res < iReqLen) {
DoLayerCallback(LAYERCALLBACK_LAYERSPECIFIC, PROXYERROR_REQUESTFAILED, nErrorCode);
if (m_nProxyOpID == PROXYOP_CONNECT)
TriggerEvent(FD_CONNECT, nErrorCode, TRUE);
else
TriggerEvent(FD_ACCEPT, nErrorCode, TRUE);
Reset();
return;
}
}
m_nProxyOpState = 3;
ClearBuffer();
}
}
else if ( m_nProxyOpState == 3 // Response to connection or bind request
|| m_nProxyOpState == 4) // Response (2nd) to bind request
{
if (!m_pRecvBuffer)
m_pRecvBuffer = new char[10];
int numread = ReceiveNext(m_pRecvBuffer + m_nRecvBufferPos, 10 - m_nRecvBufferPos);
if (numread == SOCKET_ERROR) {
int nErrorCode = WSAGetLastError();
if (nErrorCode != WSAEWOULDBLOCK) {
DoLayerCallback(LAYERCALLBACK_LAYERSPECIFIC, PROXYERROR_REQUESTFAILED, nErrorCode);
if (m_nProxyOpID == PROXYOP_CONNECT)
TriggerEvent(FD_CONNECT, nErrorCode, TRUE);
else
TriggerEvent(FD_ACCEPT, nErrorCode, TRUE);
Reset();
}
return;
}
m_nRecvBufferPos += numread;
if (m_nRecvBufferPos == 10)
{
TRACE("SOCKS5 response: VER=%u REP=%u RSV=%u ATYP=%u BND.ADDR=%s BND.PORT=%u\n", (BYTE)m_pRecvBuffer[0], (BYTE)m_pRecvBuffer[1], (BYTE)m_pRecvBuffer[2], (BYTE)m_pRecvBuffer[3], ipstrA(*(u_long*)&m_pRecvBuffer[4]), ntohs(*(u_short*)&m_pRecvBuffer[8]));
if (m_pRecvBuffer[0] != 5 || m_pRecvBuffer[1] != 0) {
DoLayerCallback(LAYERCALLBACK_LAYERSPECIFIC, PROXYERROR_REQUESTFAILED, 0, (LPARAM)(LPCSTR)GetSocks5Error(m_pRecvBuffer[1]));
if (m_nProxyOpID == PROXYOP_CONNECT)
TriggerEvent(FD_CONNECT, WSAECONNABORTED, TRUE);
else
TriggerEvent(FD_ACCEPT, WSAECONNABORTED, TRUE);
Reset();
ClearBuffer();
return;
}
if ( (m_nProxyOpID == PROXYOP_CONNECT && m_nProxyOpState == 3) ||
(m_nProxyOpID == PROXYOP_BIND && m_nProxyOpState == 4) )
{
int nOpIDEvent = m_nProxyOpID == PROXYOP_CONNECT ? FD_CONNECT : FD_ACCEPT;
Reset();
ClearBuffer();
TriggerEvent(nOpIDEvent, 0, TRUE);
TriggerEvent(FD_READ, 0, TRUE);
TriggerEvent(FD_WRITE, 0, TRUE);
return;
}
else if (m_nProxyOpID == PROXYOP_BIND && m_nProxyOpState == 3)
{
// Listen socket created
if (m_pRecvBuffer[3] != 1) { // Check which kind of address the response contains
DoLayerCallback(LAYERCALLBACK_LAYERSPECIFIC, PROXYERROR_REQUESTFAILED, 0, (LPARAM)"Unexpected ATYP received");
if (m_nProxyOpID == PROXYOP_CONNECT)
TriggerEvent(FD_CONNECT, WSAECONNABORTED, TRUE);
else
TriggerEvent(FD_ACCEPT, WSAECONNABORTED, TRUE);
Reset();
ClearBuffer();
return;
}
m_nProxyOpState = 4;
t_ListenSocketCreatedStruct data;
data.ip = *(u_long*)&m_pRecvBuffer[4];
data.nPort = *(u_short*)&m_pRecvBuffer[8];
DoLayerCallback(LAYERCALLBACK_LAYERSPECIFIC, PROXYSTATUS_LISTENSOCKETCREATED, 0, (LPARAM)&data);
// Wait for 2nd response to bind request
}
ClearBuffer();
}
}
}
else if (m_ProxyData.nProxyType == PROXYTYPE_HTTP10 || m_ProxyData.nProxyType == PROXYTYPE_HTTP11)
{
ASSERT( m_nProxyOpID == PROXYOP_CONNECT );
// Read everything which is currently available at the socket
//
bool bFoundEOH = false;
while (!bFoundEOH)
{
char cBuff[4096];
int iRead = ReceiveNext(cBuff, sizeof cBuff);
if (iRead == SOCKET_ERROR) {
int nErrorCode = WSAGetLastError();
if (nErrorCode != WSAEWOULDBLOCK) {
DoLayerCallback(LAYERCALLBACK_LAYERSPECIFIC, PROXYERROR_REQUESTFAILED, nErrorCode);
Reset();
ClearBuffer();
TriggerEvent(FD_CONNECT, nErrorCode, TRUE);
}
return;
}
else if (iRead == 0)
return;
// Safety check: Don't allow buffer to grow too large
char *pNewStrBuffer;
if ( m_iStrBuffSize + iRead > 4096
|| (pNewStrBuffer = (char*)realloc(m_pStrBuffer, m_iStrBuffSize + iRead)) == NULL) {
DoLayerCallback(LAYERCALLBACK_LAYERSPECIFIC, PROXYERROR_REQUESTFAILED, 0, (LPARAM)"Invalid HTTP response - Header size exceeds limit");
Reset();
ClearBuffer();
TriggerEvent(FD_CONNECT, WSAECONNABORTED, TRUE);
return;
}
m_pStrBuffer = pNewStrBuffer;
// Append read chunk to buffer
memcpy(m_pStrBuffer + m_iStrBuffSize, cBuff, iRead);
m_iStrBuffSize += iRead;
// Search for EOH (CRLFCRLF)
const char* pc = m_pStrBuffer;
int iMaxOff = m_iStrBuffSize - sizeof(DWORD);
for (int i = 0; i <= iMaxOff; i++) {
if (*(DWORD*)(pc++) == 0x0A0D0A0D) { // VC-BUG?: '\r\n\r\n' results in 0x0A0D0A0D too, although it should not!
bFoundEOH = true;
break;
}
}
}
ASSERT( bFoundEOH );
// Evaluate HTTP status
//
// We already know that we have a (CR)NL-character which can be safely used
// as a NUL-character in the context of 'sscanf'.
//
UINT uHttpStatus;
if (sscanf(m_pStrBuffer, "HTTP/%*u.%*u %u", &uHttpStatus) != 1 || uHttpStatus != 200)
{
if (*(DWORD*)m_pStrBuffer == 'PTTH')
{
TRACE("%hs\n", CStringA(m_pStrBuffer, m_iStrBuffSize).TrimRight("\r\n"));
char* pcNl = (char*)memchr(m_pStrBuffer, '\n', m_iStrBuffSize);
if (pcNl) {
*pcNl = '\0';
if (pcNl[-1] == '\r')
pcNl[-1] = '\0';
}
if (uHttpStatus == 407)
DoLayerCallback(LAYERCALLBACK_LAYERSPECIFIC, PROXYERROR_AUTHREQUIRED, 0);
else
DoLayerCallback(LAYERCALLBACK_LAYERSPECIFIC, PROXYERROR_REQUESTFAILED, 0, (LPARAM)m_pStrBuffer);
}
else {
DoLayerCallback(LAYERCALLBACK_LAYERSPECIFIC, PROXYERROR_REQUESTFAILED, 0, (LPARAM)"Invalid HTTP response");
}
Reset();
ClearBuffer();
TriggerEvent(FD_CONNECT, WSAECONNABORTED, TRUE);
return;
}
TRACE("%hs\n", CStringA(m_pStrBuffer, m_iStrBuffSize).TrimRight("\r\n"));
Reset();
ClearBuffer();
TriggerEvent(FD_CONNECT, 0, TRUE);
TriggerEvent(FD_READ, 0, TRUE);
TriggerEvent(FD_WRITE, 0, TRUE);
}
}
BOOL CAsyncProxySocketLayer::Connect(LPCSTR lpszHostAddress, UINT nHostPort)
{
ASSERT( lpszHostAddress != NULL );
if (!m_ProxyData.nProxyType)
//Connect normally because there is no proxy
return ConnectNext(lpszHostAddress, nHostPort);
//Translate the host address
SOCKADDR_IN sockAddr = {0};
sockAddr.sin_addr.s_addr = inet_addr(lpszHostAddress);
if (sockAddr.sin_addr.s_addr == INADDR_NONE)
{
LPHOSTENT lphost = gethostbyname(lpszHostAddress);
if (lphost != NULL)
sockAddr.sin_addr.s_addr = ((LPIN_ADDR)lphost->h_addr)->s_addr;
else
{
// Can't resolve hostname
if (m_ProxyData.nProxyType == PROXYTYPE_SOCKS4A ||
m_ProxyData.nProxyType == PROXYTYPE_SOCKS5 ||
m_ProxyData.nProxyType == PROXYTYPE_HTTP10 ||
m_ProxyData.nProxyType == PROXYTYPE_HTTP11)
{ //Can send domain names to proxy
//Conect to proxy server
BOOL res = ConnectNext(m_ProxyData.strProxyHost, m_ProxyData.nProxyPort);
if (!res) {
int nErrorCode = WSAGetLastError();
if (nErrorCode != WSAEWOULDBLOCK) {
DoLayerCallback(LAYERCALLBACK_LAYERSPECIFIC, PROXYERROR_NOCONN, nErrorCode);
return FALSE;
}
}
m_nProxyPeerPort = htons((u_short)nHostPort);
m_nProxyPeerIP = 0;
delete[] m_pProxyPeerHost;
m_pProxyPeerHost = NULL; // 'new' may throw an exception
m_pProxyPeerHost = new CHAR[strlen(lpszHostAddress) + 1];
strcpy(m_pProxyPeerHost, lpszHostAddress);
m_nProxyOpID = PROXYOP_CONNECT;
return TRUE;
}
else {
DoLayerCallback(LAYERCALLBACK_LAYERSPECIFIC, PROXYERROR_CANTRESOLVEHOST, 0);
WSASetLastError(WSAEINVAL);
return FALSE;
}
}
}
sockAddr.sin_family = AF_INET;
sockAddr.sin_port = htons((u_short)nHostPort);
BOOL res = CAsyncProxySocketLayer::Connect((SOCKADDR*)&sockAddr, sizeof(sockAddr));
if (res || WSAGetLastError() == WSAEWOULDBLOCK)
{
delete[] m_pProxyPeerHost;
m_pProxyPeerHost = NULL; // 'new' may throw an exception
m_pProxyPeerHost = new CHAR[strlen(lpszHostAddress) + 1];
strcpy(m_pProxyPeerHost, lpszHostAddress);
}
return res;
}
BOOL CAsyncProxySocketLayer::Connect(const SOCKADDR* lpSockAddr, int nSockAddrLen)
{
if (!m_ProxyData.nProxyType)
//Connect normally because there is no proxy
return ConnectNext(lpSockAddr, nSockAddrLen);
LPSOCKADDR_IN sockAddr = (LPSOCKADDR_IN)lpSockAddr;
//Save server details
m_nProxyPeerIP = sockAddr->sin_addr.S_un.S_addr;
m_nProxyPeerPort = sockAddr->sin_port;
delete[] m_pProxyPeerHost;
m_pProxyPeerHost = NULL;
m_nProxyOpID = PROXYOP_CONNECT;
BOOL res = ConnectNext(m_ProxyData.strProxyHost, m_ProxyData.nProxyPort);
if (!res) {
int nErrorCode = WSAGetLastError();
if (nErrorCode != WSAEWOULDBLOCK) {
DoLayerCallback(LAYERCALLBACK_LAYERSPECIFIC, PROXYERROR_NOCONN, nErrorCode);
return FALSE;
}
}
return res;
}
void CAsyncProxySocketLayer::OnConnect(int nErrorCode)
{
if (m_ProxyData.nProxyType == PROXYTYPE_NOPROXY) {
TriggerEvent(FD_CONNECT, nErrorCode, TRUE);
return;
}
if (m_nProxyOpID == 0) {
ASSERT(0);
return;
}
if (nErrorCode) {
// Can't connect to proxy
DoLayerCallback(LAYERCALLBACK_LAYERSPECIFIC, PROXYERROR_NOCONN, nErrorCode);
if (m_nProxyOpID == PROXYOP_CONNECT)
TriggerEvent(FD_CONNECT, nErrorCode, TRUE);
else
TriggerEvent(FD_ACCEPT, nErrorCode, TRUE);
Reset();
ClearBuffer();
return;
}
if (m_nProxyOpID == PROXYOP_CONNECT || m_nProxyOpID == PROXYOP_BIND)
{
if (m_nProxyOpState)
return; // Somehow OnConnect has been called more than once
ASSERT( m_ProxyData.nProxyType != PROXYTYPE_NOPROXY );
ClearBuffer();
if (m_ProxyData.nProxyType == PROXYTYPE_SOCKS4 || m_ProxyData.nProxyType == PROXYTYPE_SOCKS4A)
{
const char* pszAsciiProxyPeerHost;
int iSizeAsciiProxyPeerHost;
if (m_nProxyPeerIP == 0) {
pszAsciiProxyPeerHost = m_pProxyPeerHost;
iSizeAsciiProxyPeerHost = strlen(pszAsciiProxyPeerHost) + 1;
}
else {
pszAsciiProxyPeerHost = 0;
iSizeAsciiProxyPeerHost = 0;
}
// SOCKS 4
// ---------------------------------------------------------------------------
// +----+----+----+----+----+----+----+----+----+----+....+----+
// | VN | CD | DSTPORT | DSTIP | USERID |NULL|
// +----+----+----+----+----+----+----+----+----+----+....+----+
//# of bytes: 1 1 2 4 variable 1
char* pcReq = (char*)_alloca(9 + iSizeAsciiProxyPeerHost);
pcReq[0] = 4; // VN: 4
pcReq[1] = (m_nProxyOpID == PROXYOP_CONNECT) ? 1 : 2; // CD: 1=CONNECT, 2=BIND
*(u_short*)&pcReq[2] = m_nProxyPeerPort; // DSTPORT
int iReqLen = 4 + 4 + 1;
if (m_nProxyPeerIP == 0)
{
ASSERT( m_ProxyData.nProxyType == PROXYTYPE_SOCKS4A );
ASSERT( strcmp(pszAsciiProxyPeerHost, "") != 0 );
ASSERT( iSizeAsciiProxyPeerHost > 0 );
// For version 4A, if the client cannot resolve the destination host's
// domain name to find its IP address, it should set the first three bytes
// of DSTIP to NULL and the last byte to a non-zero value. (This corresponds
// to IP address 0.0.0.x, with x nonzero.)
// DSTIP: Set the IP to 0.0.0.x (x is nonzero)
pcReq[4] = 0;
pcReq[5] = 0;
pcReq[6] = 0;
pcReq[7] = 1;
pcReq[8] = 0; // Terminating NUL-byte for USERID
// Following the NULL byte terminating USERID, the client must send the
// destination domain name and termiantes it with another NULL byte.
// Add hostname (including terminating NUL-byte)
memcpy(&pcReq[9], pszAsciiProxyPeerHost, iSizeAsciiProxyPeerHost);
iReqLen += iSizeAsciiProxyPeerHost;
}
else {
*(u_long*)&pcReq[4] = m_nProxyPeerIP; // DSTIP
pcReq[8] = 0; // Terminating NUL-byte for USERID
}
int res = SendNext(pcReq, iReqLen);
if (res == SOCKET_ERROR) {
int nErrorCode = WSAGetLastError();
DoLayerCallback(LAYERCALLBACK_LAYERSPECIFIC, PROXYERROR_REQUESTFAILED, nErrorCode);
if (m_nProxyOpID == PROXYOP_CONNECT)
TriggerEvent(FD_CONNECT, (nErrorCode == WSAEWOULDBLOCK) ? WSAECONNABORTED : nErrorCode, TRUE);
else
TriggerEvent(FD_ACCEPT, nErrorCode, TRUE);
Reset();
ClearBuffer();
return;
}
else if (res < iReqLen) {
DoLayerCallback(LAYERCALLBACK_LAYERSPECIFIC, PROXYERROR_REQUESTFAILED, 0);
if (m_nProxyOpID == PROXYOP_CONNECT)
TriggerEvent(FD_CONNECT, WSAECONNABORTED, TRUE);
else
TriggerEvent(FD_ACCEPT, WSAECONNABORTED, TRUE);
Reset();
ClearBuffer();
return;
}
}
else if (m_ProxyData.nProxyType == PROXYTYPE_SOCKS5)
{
// SOCKS 5
// -------------------------------------------------------------------------------------------
// The client connects to the server, and sends a version identifier/method selection message:
// +----+----------+----------+
// |VER | NMETHODS | METHODS |
// +----+----------+----------+
// | 1 | 1 | 1 to 255 |
// +----+----------+----------+
//
// The values currently defined for METHOD are:
//
// o X'00' NO AUTHENTICATION REQUIRED
// o X'01' GSSAPI
// o X'02' USERNAME/PASSWORD
// o X'03' to X'7F' IANA ASSIGNED
// o X'80' to X'FE' RESERVED FOR PRIVATE METHODS
// o X'FF' NO ACCEPTABLE METHODS
int iReqLen;
char acReq[4];
acReq[0] = 5; // VER: 5
if (m_ProxyData.bUseLogon) {
acReq[1] = 2; // NMETHODS: 2
acReq[2] = 2; // METHOD #1: 2 (USERNAME/PASSWORD)
acReq[3] = 0; // METHOD #2: 0 (NO AUTHENTICATION)
iReqLen = 4;
}
else {
acReq[1] = 1; // NMETHODS: 1
acReq[2] = 0; // METHOD #1: 0 (NO AUTHENTICATION)
iReqLen = 3;
}
int res = SendNext(acReq, iReqLen);
if (res == SOCKET_ERROR) {
int nErrorCode = WSAGetLastError();
DoLayerCallback(LAYERCALLBACK_LAYERSPECIFIC, PROXYERROR_REQUESTFAILED, nErrorCode);
if (m_nProxyOpID == PROXYOP_CONNECT)
TriggerEvent(FD_CONNECT, (nErrorCode == WSAEWOULDBLOCK) ? WSAECONNABORTED : nErrorCode, TRUE);
else
TriggerEvent(FD_ACCEPT, nErrorCode, TRUE);
Reset();
ClearBuffer();
return;
}
else if (res < iReqLen) {
DoLayerCallback(LAYERCALLBACK_LAYERSPECIFIC, PROXYERROR_REQUESTFAILED, 0);
if (m_nProxyOpID == PROXYOP_CONNECT)
TriggerEvent(FD_CONNECT, WSAECONNABORTED, TRUE);
else
TriggerEvent(FD_ACCEPT, WSAECONNABORTED, TRUE);
Reset();
ClearBuffer();
return;
}
}
else if (m_ProxyData.nProxyType == PROXYTYPE_HTTP10 || m_ProxyData.nProxyType == PROXYTYPE_HTTP11)
{
const char* pszHost;
if (m_pProxyPeerHost && m_pProxyPeerHost[0] != '\0')
pszHost = m_pProxyPeerHost;
else
pszHost = inet_ntoa(*(in_addr*)&m_nProxyPeerIP);
UINT nProxyPeerPort = ntohs((u_short)m_nProxyPeerPort);
char szHttpReq[4096];
int iHttpReqLen;
if (!m_ProxyData.bUseLogon)
{
if (m_ProxyData.nProxyType == PROXYTYPE_HTTP10) {
// The reason why we offer HTTP/1.0 support is just because it
// allows us to *not *send the "Host" field, thus saving overhead.
iHttpReqLen = _snprintf(szHttpReq, _countof(szHttpReq),
"CONNECT %s:%u HTTP/1.0\r\n"
"\r\n",
pszHost, nProxyPeerPort);
}
else {
// "Host" field is a MUST for HTTP/1.1 according RFC 2161
iHttpReqLen = _snprintf(szHttpReq, _countof(szHttpReq),
"CONNECT %s:%u HTTP/1.1\r\n"
"Host: %s:%u\r\n"
"\r\n",
pszHost, nProxyPeerPort, pszHost, nProxyPeerPort);
}
}
else
{
char szUserPass[512];
int iUserPassLen = _snprintf(szUserPass, _countof(szUserPass), "%s:%s", m_ProxyData.strProxyUser, m_ProxyData.strProxyPass);
char szUserPassBase64[2048];
CBase64Coding base64coding;
if (!base64coding.Encode(szUserPass, iUserPassLen, szUserPassBase64)) {
DoLayerCallback(LAYERCALLBACK_LAYERSPECIFIC, PROXYERROR_REQUESTFAILED, 0);
if (m_nProxyOpID == PROXYOP_CONNECT)
TriggerEvent(FD_CONNECT, WSAECONNABORTED, TRUE);
else
TriggerEvent(FD_ACCEPT, WSAECONNABORTED, TRUE);
Reset();
ClearBuffer();