This repository has been archived by the owner on Nov 27, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
blocksock.pas
181 lines (147 loc) · 3.87 KB
/
blocksock.pas
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
unit blocksock;
interface
uses
windows, winsock;
const
WINSOCK_VERSION = $0202;
WINSOCK2_DLL = 'ws2_32.dll';
type
PADDRINFOA = ^ADDRINFOA;
PPADDRINFOA = ^PADDRINFOA;
ADDRINFOA = record
ai_flags: integer;
ai_family: integer;
ai_socktype: integer;
ai_protocol: integer;
ai_addrlen: cardinal; // size_t
AI_CANONNAME: PAnsiChar;
ai_addr: PSockAddr;
ai_next: PADDRINFOA;
end;
TAddrInfoA = ADDRINFOA;
PADDRINFOW = ^ADDRINFOW;
PPADDRINFOW = ^PADDRINFOW;
ADDRINFOW = record
ai_flags: integer;
ai_family: integer;
ai_socktype: integer;
ai_protocol: integer;
ai_addrlen: cardinal; // size_t
AI_CANONNAME: PWideChar;
ai_addr: PSockAddr;
ai_next: PADDRINFOW;
end;
TAddrInfoW = ADDRINFOW;
{$IFDEF UNICODE}
addrinfo = ADDRINFOW;
TAddrInfo = TAddrInfoW;
PAddrInfo = PADDRINFOW;
PPAddrInfo = PPADDRINFOW;
{$ELSE}
addrinfo = ADDRINFOA;
TAddrInfo = TAddrInfoA;
PAddrInfo = PADDRINFOA;
PPAddrInfo = PPADDRINFOA;
{$ENDIF}
function GetAddrInfo(const nodename, servname: PChar; const hints: PAddrInfo; res: PPAddrInfo): integer;
stdcall; external WINSOCK2_DLL name 'GetAddrInfoW';
procedure FreeAddrInfo(ai: PAddrInfo); stdcall; external WINSOCK2_DLL name 'FreeAddrInfoW';
function GetAddrPool(
const aHost, aPort: string;
var aAddrInfo: PAddrInfo;
const aAf: integer = AF_INET;
const aSockType: integer = SOCK_STREAM;
const aProtocol: integer = IPPROTO_TCP;
const aFlags: integer = 0
): integer;
procedure FreeAddrPool(var aAddrInfo: PAddrInfo);
implementation
function GetSockError(const aError: integer = SOCKET_ERROR): string;
var
buffer: array [0 .. 255] of Char;
flags : DWORD;
begin
FillChar(buffer, 256, #0);
flags := FORMAT_MESSAGE_FROM_SYSTEM or FORMAT_MESSAGE_IGNORE_INSERTS or FORMAT_MESSAGE_ARGUMENT_ARRAY;
FormatMessage(flags, nil, aError, 0, buffer, SizeOf(buffer), nil);
Result := buffer;
while (Result[Length(Result)] in [#13, #10]) do
Delete(Result, Length(Result), 1);
end;
function GetAddrPool(
const aHost, aPort: string; var aAddrInfo: PAddrInfo; const aAf: integer;
const aSockType: integer; const aProtocol: integer; const aFlags: integer
): integer;
var
hints: TAddrInfo;
begin
ZeroMemory(@hints, SizeOf(hints));
hints.ai_family := aAf;
hints.ai_socktype := aSockType;
hints.ai_protocol := aProtocol;
hints.ai_flags := aFlags;
Result := GetAddrInfo(PChar(aHost), PChar(aPort), @hints, @aAddrInfo);
end;
{ Frees PAddrInfo. }
procedure FreeAddrPool(var aAddrInfo: PAddrInfo);
begin
if (aAddrInfo = nil) then
Exit;
FreeAddrInfo(aAddrInfo);
aAddrInfo := nil;
end;
function CreateTcpSock(const aAf: integer): TSocket;
begin
Result := socket(aAf, SOCK_STREAM, IPPROTO_TCP);
end;
function ConnectTCPSock(const aSock: TSocket; const aAddrPool: PAddrInfo): Boolean;
var
curr: PAddrInfo;
begin
Result := True;
curr := aAddrPool;
while (curr <> nil) do
begin
if (connect(aSock, curr^.ai_addr^, curr^.ai_addrlen) <> 0) then
curr := curr^.ai_next
else
Break;
if (curr = nil) then
Exit(False);
end;
end;
function MakeTcpConnection(const aHost, aPort: string; const aAf: integer): TSocket;
label Error;
var
addr: PAddrInfo;
begin
Result := CreateTcpSock(aAf);
if (Result = INVALID_SOCKET) then
Exit;
if (GetAddrPool(aHost, aPort, addr, aAf) <> 0) then
goto Error;
if (ConnectTCPSock(Result, addr) = False) then
goto Error;
Exit;
Error:
FreeAddrPool(addr);
closesocket(Result);
end;
function MakeTcpConnectionSocks5(const aHost, aPort, aProxyHost, aProxyPort, aProxyUser, aProxyPass: string; const aAf: TAddressFamily): TSocket;
begin
Result := MakeTcpConnection(aProxyHost, aProxyPort, aAf);
if (Result = INVALID_SOCKET) then
Exit;
if (NegotiateSocks5(Result, aHost, aPort, aProxyUser, aProxyPass) = SOCKET_ERROR) then
begin
DeleteSock(Result);
Result := INVALID_SOCKET;
end;
end;
var
wsadata: TWSAData;
initialization
WSAStartup($0202, wsadata);
finalization
WSACleanup;
end.