-
Notifications
You must be signed in to change notification settings - Fork 0
/
Threads.txt
542 lines (471 loc) · 12.3 KB
/
Threads.txt
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
unit Threads;
interface
uses
Windows,
Dialogs,
SysUtils,
Classes,
Messages,
NetLayerU,
castle_utils;
const
BASE64_TABLE: string =
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
FILL_CHAR = '=';
US_Message = $5000 + 1;
type
TSynchronize = procedure(Method: TThreadMethod) of object;
TAdvThread = class(TThread)
protected
procedure Execute; override;
procedure DoExecute; virtual;
procedure HandleException(E: Exception); virtual;
end;
TMsgThread = class(TAdvThread)
protected
procedure DoExecute; override;
procedure HandleMessage(var Msg: TMsg); virtual;
procedure CreateMessageQueue;
public
StopEvent: THandle;
constructor Create(S: Boolean);
destructor Destroy; override;
procedure PostMessage(Msg: UINT; wParam: WPARAM; lParam: LPARAM);
procedure StopThread; virtual;
procedure CheckMessages;
property Terminated;
end;
TSockMsgThread = class(TMsgThread)
protected
fWasSocketError: Boolean;
fUserAgent,
fProxyAuth,
fProxyAddr,
fProxyPort: string;
fParentHandle: THandle;
fIpHostS: string;
fIpHostD: DWORD;
procedure EncodeUnit(const AIn1, AIn2, AIn3: Byte; var VOut: Cardinal);
function ProxyConnection: string;
function ProxyCache: string;
function BasicAuthStr: string;
function HTTPBuildRequest(RequestType, URL, Cookies, Content: string): string;
function Encode64(const Src: string): string;
public
property Proxy_Auth: string read fProxyAuth write fProxyAuth;
property Proxy_Addr: string read fProxyAddr write fProxyAddr;
property Proxy_Port: string read fProxyPort write fProxyPort;
procedure DoMainLog(msg: string);
property ParentHandle: THandle read fParentHandle;
procedure OnStatus(Msg: string);
function GetProxyIP(Host: string): DWORD;
function ProxyRead(Req, StopStr: string): string;
function GetPage(RequestType, URL, Cookies, Content, StopStr: string): string;
function HEncode(const S: string): string;
constructor Create(Parent: THandle; UserAgent, ProxyAuth, ProxyAddr, ProxyPort: string);
end;
TLocker = class
private
hEvent: THandle;
public
constructor Create;
destructor Destroy; override;
procedure Lock;
procedure UnLock;
end;
procedure WaitSignaled(E: THandle);
implementation
uses castle_interface;
procedure TAdvThread.Execute;
begin
try
DoExecute;
except
on E: Exception do
HandleException(E);
end;
end;
procedure TAdvThread.DoExecute;
begin
end;
procedure TAdvThread.HandleException(E: Exception);
begin
end;
constructor TMsgThread.Create(S: Boolean);
begin
inherited Create(S);
StopEvent := CreateEvent(nil, True, False, '');
end;
destructor TMsgThread.Destroy;
begin
inherited Destroy;
CloseHandle(StopEvent);
end;
procedure TMsgThread.CheckMessages;
var
Msg: TMsg;
begin
while PeekMessage(Msg, 0, 0, 0, PM_REMOVE) do
HandleMessage(Msg);
end;
procedure TMsgThread.DoExecute;
begin
CreateMessageQueue;
while MsgWaitForMultipleObjects(1, StopEvent, False, INFINITE, QS_ALLINPUT) <> WAIT_OBJECT_0 do
CheckMessages;
end;
procedure TMsgThread.HandleMessage(var Msg: TMsg);
begin
DispatchMessage(Msg);
end;
procedure TMsgThread.PostMessage(Msg: UINT; wParam: WPARAM; lParam: LPARAM);
begin
PostThreadMessage(ThreadID, Msg, wParam, lParam);
end;
procedure TMsgThread.CreateMessageQueue;
var
Msg: TMsg;
begin
PeekMessage(Msg, 0, 0, 0, PM_NOREMOVE);
end;
procedure TMsgThread.StopThread;
begin
Terminate;
SetEvent(StopEvent);
end;
constructor TLocker.Create;
begin
inherited Create;
hEvent := CreateEvent(nil, False, True, '');
end;
destructor TLocker.Destroy;
begin
CloseHandle(hEvent);
inherited Destroy;
end;
procedure TLocker.Lock;
begin
WaitSignaled(hEvent);
end;
procedure TLocker.UnLock;
begin
SetEvent(hEvent);
end;
procedure ProcessMessage;
var
Handled: Boolean;
Msg: TMsg;
begin
if PeekMessage(Msg, 0, 0, 0, PM_REMOVE) then
begin
if Msg.Message <> WM_QUIT then
begin
Handled := False;
if not Handled then
begin
TranslateMessage(Msg);
DispatchMessage(Msg);
end;
end;
end;
end;
procedure WaitSignaled(E: THandle);
begin
if GetCurrentThreadID = MainThreadID then
begin
if WaitForSingleObject(E, 0) <> WAIT_OBJECT_0 then
repeat
ProcessMessage;
until MsgWaitForMultipleObjects(1, E, False, INFINITE, QS_ALLINPUT) = WAIT_OBJECT_0;
end
else
WaitForSingleObject(E, INFINITE);
end;
procedure TSockMsgThread.DoMainLog(msg: string);
begin
SendMessage(fParentHandle, UL_LogMessage, 0, StrToBuf(msg));
end;
procedure TSockMsgThread.OnStatus(Msg: string);
begin
DoMainLog(msg);
end;
function TSockMsgThread.GetProxyIP(Host: string): DWORD;
var
F: Integer;
PA: string;
begin
if fIpHostS <> Host then
begin
if (fProxyAddr <> '') then
begin
OnStatus('resolving IP (proxy) [' + fProxyAddr + ']');
F := Pos(':', fProxyAddr);
if (F = 0) then F := Length(fProxyAddr) + 1;
PA := System.Copy(fProxyAddr, 1, F - 1);
OnStatus('resolving IP (proxy only) [' + PA + ']');
Result := SocketResolveHost(PA);
end
else
begin
OnStatus('resolving IP (' + Host + ')');
Result := SocketResolveHost(Host);
end;
// fIpHostS := Host;
// fIpHostD := Result;
OnStatus('IP resolved: [' + IPToStr(Result) + ']');
end
else
Result := fIpHostD;
end;
function TSockMsgThread.ProxyRead(Req, StopStr: string): string;
var
SrvSock, IP: DWORD;
R, Len: Integer;
Host, S: string;
begin
fWasSocketError := True;
Result := '';
if (Req = '') and (not Terminated) then exit; // ïóñòûå çàïðîñû - íàôèã
// âûäåëÿåì ïîëå host èç çàïðîñà
R := Pos(#10'Host: ', Req);
if (R <> 0) then
begin
Host := Copy(Req, R + 7, 255);
R := Pos(#10, Host);
if (R <> 0) then Delete(Host, R, 255);
end
else
Host := 'localhost';
OnStatus('Host="' + Host + '"');
if Terminated then Exit;
// ñîçäàëè ñîêåò
SrvSock := SocketCreateWithTimeouts(60, 30 * 1000, 30 * 1000);
if (SrvSock = 0) then
begin
// èëè íå ñîçäàëè %-)
OnStatus('Error create socket');
exit;
end;
if Terminated then
begin
SocketClose(SrvSock);
Exit;
end;
// ïðèêîííåêòèëèñü
// ïî ïðàâèëàì òóò áû íàäî òî÷íåå ïðîâåðÿòü ProxyPort, íó äà íàôèã...
OnStatus('connecting ');
if Length(fProxyAddr) > 1 then
IP := GetProxyIP(fProxyAddr) else
IP := GetProxyIP(Host);
OnStatus('ProxyIP=[' + IPToStr(IP) + ']');
OnStatus('ProxyPort=[' + fProxyPort + ']');
OnStatus('connecting...');
if not SocketConnect(SrvSock, IP, StrToIntDef(fProxyPort, 80)) then
begin
// èëè íå ïðèêîííåêòèëèñü
SocketClose(SrvSock);
OnStatus('error, can''t connect to host');
exit;
end;
if Terminated then
begin
SocketClose(SrvSock);
Exit;
end;
// îòîñëàëè âåñü çàïðîñ ñêîïîì (îí îáû÷íî ìàëåíüêèé)
OnStatus('writing reqest [' + IntToStr(Length(Req)) + ']');
if (SocketSend(SrvSock, Req[1], Length(Req)) <> 0) then
begin
// èëè íå îòîñëàëè
SocketClose(SrvSock);
OnStatus('error, can''t write reqest');
exit;
end;
if Terminated then
begin
SocketClose(SrvSock);
Exit;
end;
OnStatus('reading 0');
repeat
R := SocketCanRead(SrvSock);
// âîò òóò åñëè R < 0 - òî îøèáêà
// åñëè R = 0 - íè÷åãî â ñîêåòå íå ëåæèò ïîêà
// åñëè R > 0 - ëåæàò êàêèå-òî áàéòèêè
if (R < 0) then break;
if (R = 0) then
Len := 1 // åñëè íè÷åãî íå ëåæèò - ïîïðîñèì, ÷òîáû ïîëîæèëè
else
begin
// ïðîâåðèì, ñêîëüêî ëåæèò áàéòèêîâ
Len := SocketBytesToRead(SrvSock);
if (Len < 0) then break;
if (Len = 0) then
begin
fWasSocketError := False;
break;
end;
end;
if (Len > 0) and (not Terminated) then
begin
SetLength(S, Len);
R := SocketReceive(SrvSock, S[1], Len);
if (R = -1) then break;
if (R = 0) then
begin
Result := Result + S;
OnStatus('reading ' + IntToStr(Length(Result)) + ' bytes');
if Pos(StopStr, Result) > 0 then Break; // Åñëè ìû íàøëè òî ÷òî íàì íàäî, òî çà÷åì ìó÷àòüñÿ äàëüøå?
end
else
OnStatus('waiting ' + IntToStr(Length(Result)));
// ïîëó÷èëè âêóñíîñòü, ðàññêàçàëè ñêàçêó
end;
until False or Terminated;
// âñ¸. îòìó÷èëèñü.
SocketClose(SrvSock);
OnStatus('socket closed - ok ');
SendMessage(fParentHandle, UC_ReadedBytes, Length(Result), 0);
// ïðèâåäåì âñ¸ íåïîòðåáñòâî (êîíöû ñòðîê) (åñëè åñòü) â þíèêñ-âèä
Result := StringReplace(Result, #13#10, #10, [rfReplaceAll, rfIgnoreCase])
end;
function TSockMsgThread.GetPage(RequestType, URL, Cookies, Content, StopStr: string): string;
var
ts: string;
begin
ts := HTTPBuildRequest(RequestType, URL, Cookies, Content);
Result := ProxyRead(ts, StopStr);
end;
procedure TSockMsgThread.EncodeUnit(const AIn1, AIn2, AIn3: Byte; var VOut: Cardinal);
var
LUnit: packed record
case Integer of
0: (Byte1, Byte2, Byte3, Byte4: Byte);
1: (Whole: Cardinal);
end;
begin
LUnit.Byte1 := ord(BASE64_TABLE[((AIn1 shr 2) and 63) + 1]);
LUnit.Byte2 := ord(BASE64_TABLE[(((AIn1 shl 4) or (AIn2 shr 4)) and 63) + 1]);
LUnit.Byte3 := ord(BASE64_TABLE[(((AIn2 shl 2) or (AIn3 shr 6)) and 63) + 1]);
LUnit.Byte4 := ord(BASE64_TABLE[(Ord(AIn3) and 63) + 1]);
VOut := LUnit.Whole;
end;
function TSockMsgThread.Encode64(const Src: string): string;
var
LBuffer: string;
LSize: Integer;
LLen: Integer;
LBufSize: Integer;
LPos: Integer;
LIn1, LIn2, LIn3: Byte;
LUnit: packed record
case Integer of
0: (Byte1, Byte2, Byte3, Byte4: Byte);
1: (Whole: Cardinal);
end;
begin
Result := '';
LIn3 := 0;
LBufSize := Length(Src);
if LBufSize = 0 then Exit;
SetLength(Result, ((LBufSize + 2) div 3) * 4);
LLen := 0;
LBuffer := Src;
LPos := 1;
while (LPos <= LBufSize) do
begin
LIn1 := Byte(LBuffer[LPos]);
Inc(LPos);
if (LPos <= LBufSize) then
begin
LIn2 := Byte(LBuffer[LPos]);
Inc(LPos);
if (LPos <= LBufSize) then
begin
LIn3 := Byte(LBuffer[LPos]);
Inc(LPos);
LSize := 3;
end
else
begin
LIn3 := 0;
LSize := 2;
end;
end
else
begin
LIn2 := 0;
LSize := 1;
end;
EncodeUnit(LIn1, LIn2, LIn3, LUnit.Whole);
Move(LUnit, Result[LLen + 1], 4);
Inc(LLen, 4);
if (LSize < 3) then
begin
Result[LLen] := FILL_CHAR;
if (LSize = 1) then Result[LLen - 1] := FILL_CHAR;
end;
end;
end;
function TSockMsgThread.ProxyConnection: string;
begin
if (fProxyAddr <> '') then
Result := 'Referer: http://castle.istu.edu'#10'Proxy-Connection: close'#10
else
Result := '';
end;
function TSockMsgThread.ProxyCache: string;
begin
if (fProxyAddr <> '') then
Result := 'Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0'#10
else
Result := '';
end;
function TSockMsgThread.BasicAuthStr: string;
begin
if (fProxyAuth <> '') then
Result := 'Proxy-Authorization: Basic ' + Encode64(fProxyAuth) + #10
else
Result := '';
end;
function TSockMsgThread.HTTPBuildRequest(RequestType, URL, Cookies, Content: string): string;
var
H: string;
begin
H := URL;
Delete(H, 1, Pos('://', H) + 2);
H := Copy(H, 1, Pos('/', H) - 1);
if (Cookies <> '') then Cookies := 'Cookie: ' + Cookies + #10 + 'Cookie2: $Version="1"'#10;
Result :=
RequestType + ' ' + URL + ' HTTP/1.0'#10 +
'User-Agent: ' + fUserAgent + #10 +
'Host: ' + H + #10 +
'Accept: */*'#10 +
'Content-Type: application/x-www-form-urlencoded'#10 +
BasicAuthStr +
// ProxyConnection +
ProxyCache +
Cookies +
'Content-length: ' + IntToStr(Length(Content)) + #10#10 +
Content;
// ShowMessage(Result);
// if RequestType = 'POST' then ShowMessage(Result);
end;
function TSockMsgThread.HEncode(const S: string): string;
var
F: Integer;
begin
Result := '';
for F := 1 to Length(S) do
Result := Result + '%' + IntToHex(Byte(S[F]), 2);
end;
constructor TSockMsgThread.Create(Parent: THandle; UserAgent, ProxyAuth, ProxyAddr, ProxyPort: string);
begin
fUserAgent := UserAgent;
fProxyAuth := ProxyAuth;
fProxyAddr := ProxyAddr;
fProxyPort := ProxyPort;
fParentHandle := Parent;
inherited Create(False);
end;
end.