-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainUnitServer.pas
331 lines (281 loc) · 9.16 KB
/
MainUnitServer.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
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
unit MainUnitServer;
{
Article:
ScreenThief - stealing screen shots over the Network
http://delphi.about.com/library/weekly/aa012004a.htm
A free network screen shot grabber application,
with source code. Learn how to send / receive raw (binary)
data (screen shot JPG images) using TCP connections.
ScreenThief is a network application designed to "steal"
screen shot images from client computers and display them
in one central location (server application).
..............................................
Zarko Gajic, BSCS
About Guide to Delphi Programming
http://delphi.about.com
how to advertise: http://delphi.about.com/library/bladvertise.htm
free newsletter: http://delphi.about.com/library/blnewsletter.htm
forum: http://forums.about.com/ab-delphi/start/
..............................................
}
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, IdTCPServer, IdSocketHandle, IdThreadMgr, IdThreadMgrDefault, IdBaseComponent,
IdComponent, Buttons, ExtCtrls, Jpeg, ImgList, ComCtrls, ToolWin,
IdAntiFreezeBase, IdAntiFreeze, IdStack, SyncObjs;
type
PClient = ^TClient;
TClient = record
PeerIP : string[15]; { Cleint IP address }
HostName : String[40]; { Hostname }
TakeShot : boolean;
Connected, { Time of connect }
LastAction : TDateTime; { Time of last transaction }
Thread : Pointer; { Pointer to thread }
end;
TMainFormServer = class(TForm)
TCPServer: TIdTCPServer;
ThreadManager: TIdThreadMgrDefault;
pnlMain: TPanel;
ImageScrollBox: TScrollBox;
Image: TImage;
InfoLabel: TStaticText;
pnlLeft: TPanel;
ClientsBox: TGroupBox;
ClientsListBox: TListBox;
DetailsBox: TGroupBox;
DetailsMemo: TMemo;
ActionPanel: TPanel;
AutoCaptureCheckBox: TCheckBox;
SecondsCombo: TComboBox;
GetImageNowButton: TBitBtn;
Protocol: TMemo;
Timer: TTimer;
IdAntiFreeze: TIdAntiFreeze;
procedure TCPServerConnect(AThread: TIdPeerThread);
procedure TCPServerExecute(AThread: TIdPeerThread);
procedure TCPServerDisconnect(AThread: TIdPeerThread);
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure ClientsListBoxClick(Sender: TObject);
procedure AutoCaptureCheckBoxClick(Sender: TObject);
procedure GetImageNowButtonClick(Sender: TObject);
procedure TimerTimer(Sender: TObject);
private
procedure RefreshListDisplay;
procedure RefreshImage(const ClientDNS, ImageName : string);
public
end;
const
// DefaultServerIP = '192.168.0.1';
DefaultServerIP = '127.0.0.1';
DefaultServerPort = 7676;
var
MainFormServer : TMainFormServer;
Clients : TThreadList; // Holds the data of all clients
implementation
{$R *.DFM}
procedure TMainFormServer.TCPServerConnect(AThread: TIdPeerThread);
var
NewClient: PClient;
begin
GetMem(NewClient, SizeOf(TClient));
NewClient.PeerIP := AThread.Connection.Socket.Binding.PeerIP;
NewClient.HostName := GStack.WSGetHostByAddr(NewClient.PeerIP);
NewClient.TakeShot := False;
NewClient.Connected := Now;
NewClient.LastAction := NewClient.Connected;
NewClient.Thread := AThread;
AThread.Data := TObject(NewClient);
try
Clients.LockList.Add(NewClient);
finally
Clients.UnlockList;
end;
Protocol.Lines.Add(TimeToStr(Time)+' Connection from "' + NewClient.HostName + '" on ' + NewClient.PeerIP);
RefreshListDisplay;
end; (* TCPServer Connect *)
procedure TMainFormServer.TCPServerExecute(AThread: TIdPeerThread);
var
Client : PClient;
Command : string;
Size : integer;
PicturePathName : string;
ftmpStream : TFileStream;
begin
if not AThread.Terminated and AThread.Connection.Connected then
begin
Client := PClient(AThread.Data);
Client.LastAction := Now;
Command := AThread.Connection.ReadLn;
if Command = 'CheckMe' then
begin
if Client.TakeShot = True then
begin
Client.TakeShot := False;
AThread.Connection.WriteLn('TakeShot');
PicturePathName := ExtractFileDir(ParamStr(0)) + '\' + Client.HostName + '_Screen.JPG';
if FileExists (PicturePathName) then DeleteFile(PicturePathName);
ftmpStream := TFileStream.Create(PicturePathName,fmCreate);
Size := AThread.Connection.ReadInteger;
AThread.Connection.ReadStream(fTmpStream,Size,False);
FreeAndNil(fTmpStream);
AThread.Connection.WriteLn('DONE');
RefreshImage(Client.HostName, PicturePathName);
ClientsListBoxClick(nil);
end
else
AThread.Connection.WriteLn('DONE');
end;
end;
end;
procedure TMainFormServer.TCPServerDisconnect(AThread: TIdPeerThread);
var
Client: PClient;
begin
Client := PClient(AThread.Data);
Protocol.Lines.Add (TimeToStr(Time)+' Disconnect from "' + Client.HostName+'"');
try
Clients.LockList.Remove(Client);
finally
Clients.UnlockList;
end;
FreeMem(Client);
AThread.Data := nil;
RefreshListDisplay;
end; (* TCPServer Disconnect *)
procedure TMainFormServer.FormCreate(Sender: TObject);
var
Bindings: TIdSocketHandles;
begin
//setup and start TCPServer
Bindings := TIdSocketHandles.Create(TCPServer);
try
with Bindings.Add do
begin
IP := DefaultServerIP;
Port := DefaultServerPort;
end;
try
TCPServer.Bindings:=Bindings;
TCPServer.Active:=True;
except on E:Exception do
ShowMessage(E.Message);
end;
finally
Bindings.Free;
end;
//setup TCPServer
//other startup settings
Clients := TThreadList.Create;
Clients.Duplicates := dupAccept;
SecondsCombo.ItemIndex := 0;
Timer.Enabled := False;
DetailsMemo.Clear;
InfoLabel.Caption := 'Waiting...';
RefreshListDisplay;
if TCPServer.Active then
begin
Protocol.Lines.Add('ScreenThief Server running on ' + TCPServer.Bindings[0].IP + ':' + IntToStr(TCPServer.Bindings[0].Port));
end;
end; (* Form Create *)
procedure TMainFormServer.FormClose(Sender: TObject; var Action: TCloseAction);
var
ClientsCount : integer;
begin
try
ClientsCount := Clients.LockList.Count;
finally
Clients.UnlockList;
end;
if (ClientsCount > 0) then //and (TCPServer.Active) then
begin
Action := caNone;
ShowMessage('Can''t close ScreenThief while there are connected clients!');
end
else
begin
TCPServer.Active := False;
Clients.Free;
end;
end; (* Form Close *)
procedure TMainFormServer.RefreshListDisplay;
var
AClient :PClient;
i:integer;
begin
GetImageNowButton.Enabled := False;
ClientsListBox.Clear;
DetailsMemo.Clear;
with Clients.LockList do
try
for i := 0 to Count-1 do
begin
AClient := Items[i];
ClientsListBox.AddItem(AClient.HostName,TObject(AClient));
end;
finally
Clients.UnlockList;
end;
GetImageNowButton.Enabled := ClientsListBox.Items.Count > 0;
AutoCaptureCheckBox.Enabled := GetImageNowButton.Enabled;
end; (* RefreshListDisplay *)
procedure TMainFormServer.ClientsListBoxClick(Sender: TObject);
var
SelClient: PClient;
begin
DetailsMemo.Clear;
if ClientsListBox.ItemIndex <> -1 then
begin
try
SelClient := PClient(Clients.LockList.Items[ClientsListBox.ItemIndex]);
with DetailsMemo do
begin
Lines.Add('IP : ' + SelClient.PeerIP);
Lines.Add('Host name : ' + SelClient.HostName);
Lines.Add('Connected : ' + DateTimeToStr(SelClient.Connected));
Lines.Add('Last shot : ' + DateTimeToStr(SelClient.LastAction));
Lines.Add('Waiting : ' + BoolToStr(SelClient.TakeShot, True));
end;
finally
Clients.UnlockList;
end;
end;
end; (* ClientsListBox Click *)
procedure TMainFormServer.AutoCaptureCheckBoxClick(Sender: TObject);
begin
GetImageNowButton.Enabled := NOT AutoCaptureCheckBox.Checked;
Timer.Interval := 1000 * StrToInt(SecondsCombo.Items[SecondsCombo.ItemIndex]);
Timer.Enabled := AutoCaptureCheckBox.Checked;
end; (* AutoCaptureCheckBoxClick *)
procedure TMainFormServer.GetImageNowButtonClick(Sender: TObject);
var
SelClient : PClient;
begin
if ClientsListBox.ItemIndex = -1 then
begin
if Sender is TBitBtn then ShowMessage('Please select a client from the list!');
Exit;
end;
try
SelClient := PClient(Clients.LockList.Items[ClientsListBox.ItemIndex]);
SelClient.TakeShot := True;
finally
Clients.UnLockList
end;
//refresh DetailsMemo
ClientsListBoxClick(Sender);
end; (* GetImageNowButton Click *)
procedure TMainFormServer.RefreshImage(const ClientDNS, ImageName : string);
begin
Image.Picture.LoadFromFile(ImageName);
InfoLabel.Caption := 'Screen shot from: ' + ClientDNS;
end; (* RefreshImage *)
procedure TMainFormServer.TimerTimer(Sender: TObject);
begin
Timer.Enabled := False;
GetImageNowButtonClick(Sender);
Timer.Enabled := True;
end; (* TimerTimer *)
end.