-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkabotconsole.dpr
412 lines (379 loc) · 9.12 KB
/
kabotconsole.dpr
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
program kabotconsole;
{$APPTYPE CONSOLE}
uses
Sockets,
SysUtils,
Classes;
type
TEvent = class
private
FClient: TTCPClient;
FDisconnectOnError: boolean;
protected
procedure ClientConnect(Sender: TObject);
procedure ClientCreateHandle(Sender: TObject);
procedure ClientDestroyHandle(Sender: TObject);
procedure ClientDisconnect(Sender: TObject);
procedure ClientError(Sender: TObject; SocketError: Integer);
procedure ClientReceive(Sender: TObject; Buf: PAnsiChar;
var DataLen: Integer);
procedure ClientSend(Sender: TObject; Buf: PAnsiChar; var DataLen: Integer);
procedure SetDisconnectOnError(Value: boolean);
public
constructor Create;
destructor Destroy; override;
procedure Connect;
procedure Disconnect;
procedure Send(Msg: string; const Prefix: string = #13#10);
procedure UnwireMethods;
procedure WireMethods;
property Client: TTCPClient read FClient;
property DisconnectOnError: boolean read FDisconnectOnError
write SetDisconnectOnError;
end;
TReadLnThread = class(TThread)
public
procedure Execute; override;
end;
var
gEvent: TEvent;
gCommand: string;
gInput: string;
gParameter: array of string;
gThread: TReadLnThread;
// targetlist: TStringlist;
// runvar: Integer;
// errorcodetostring
//
function ErrorCodeToString(ErrorCode: Integer): string;
begin
case ErrorCode of
10004:
Result := 'interrupted function call';
10013:
Result := 'permission denied';
10014:
Result := 'bad address';
10022:
Result := 'invalid argument';
10024:
Result := 'too many open files';
10035:
Result := 'resource temporarily unavailable';
10036:
Result := 'operation now in progress';
10037:
Result := 'operation already in progress';
10038:
Result := 'socket operation on non-socket';
10039:
Result := 'destination address required';
10040:
Result := 'message too long';
10041:
Result := 'protocol wrong type for socket';
10042:
Result := 'bad protocol option';
10043:
Result := 'protocol not supported';
10044:
Result := 'socket type not supported';
10045:
Result := 'operation not supported';
10046:
Result := 'protocol family not supported';
10047:
Result := 'address family not supported by protocol family';
10048:
Result := 'address already in use';
10049:
Result := 'cannot assign requested address';
10050:
Result := 'network is down';
10051:
Result := 'network is unreachable';
10052:
Result := 'network dropped connection on reset';
10053:
Result := 'software caused connection abort';
10054:
Result := 'connection reset by peer';
10055:
Result := 'no buffer space available';
10056:
Result := 'socket is already connected';
10057:
Result := 'socket is not connected';
10058:
Result := 'cannot send after socket shutdown';
10060:
Result := 'connection timed out';
10061:
Result := 'connection refused';
10064:
Result := 'host is down';
10065:
Result := 'no route to host';
10067:
Result := 'too many processes';
10091:
Result := 'network subsystem is unavailable';
10092:
Result := 'winsock.dll version out of range';
10093:
Result := 'successful wsastartup not yet performed';
10094:
Result := 'graceful shutdown in progress';
11001:
Result := 'host not found';
11002:
Result := 'non-authoritative host not found';
11003:
Result := 'this is a non-recoverable error';
11004:
Result := 'valid name, no data record of requested type';
end;
end;
// gettoken
//
function GetToken(Src: string; Index: Integer; Delimiter: char): string;
var
I: Integer;
J: Integer;
Count: Integer;
S: string;
begin
Result := '';
if Index = 0 then
begin
Result := Src;
Exit;
end
else if Index < 0 then
begin
Index := -Index;
J := 1;
for I := 1 to Length(Src) do
begin
if Src[I] = Delimiter then
Inc(J);
if J >= Index then
Break;
end;
if J = 1 then
begin
Result := Src;
Exit;
end;
Result := Copy(Src, I + 1, Length(Src)); // MaxInt
Exit;
end;
S := Src;
I := 0;
Count := 1;
while (I <= (Index - 2)) do
begin
J := Pos(Delimiter, S);
if J = 0 then
Break;
Delete(S, 1, J);
Inc(I);
end;
for I := 1 to Length(Src) do
if Src[I] = Delimiter then
Inc(Count);
if Index > Count then
Exit;
J := Pos(Delimiter, S);
if J = 0 then
begin
J := Length(S);
Result := Copy(S, 1, J);
end
else
Result := Copy(S, 1, J - 1);
end;
{ TEvent }
// clientconnect
//
procedure TEvent.ClientConnect(Sender: TObject);
begin
WriteLn('client connected');
end;
// clientcreatehandle
//
procedure TEvent.ClientCreateHandle(Sender: TObject);
begin
WriteLn('client handle created');
end;
// clientdestroyhandle
//
procedure TEvent.ClientDestroyHandle(Sender: TObject);
begin
WriteLn('client handle destroyed');
end;
// clientdisconnect
//
procedure TEvent.ClientDisconnect(Sender: TObject);
begin
WriteLn('client disconnected');
end;
// clienterror
//
procedure TEvent.ClientError(Sender: TObject; SocketError: Integer);
begin
WriteLn('client error: ' + ErrorCodeToString(SocketError));
if FDisconnectOnError then
Disconnect;
end;
// clientreceive
//
procedure TEvent.ClientReceive(Sender: TObject; Buf: PAnsiChar;
var DataLen: Integer);
var
lBuffer: string;
begin
SetLength(lBuffer, DataLen);
lBuffer := StrPas(Buf);
WriteLn('> ' + lBuffer);
end;
// clientsend
//
procedure TEvent.ClientSend(Sender: TObject; Buf: PAnsiChar;
var DataLen: Integer);
var
lBuffer: string;
begin
SetLength(lBuffer, DataLen);
lBuffer := StrPas(Buf);
// WriteLn('< ' + lBuffer);
end;
// connect
//
procedure TEvent.Connect;
begin
WriteLn('client connecting...');
if FClient.RemoteHost = '' then
FClient.RemoteHost := '127.0.0.1';
if FClient.RemotePort = '' then
FClient.RemotePort := '6667';
FClient.Connect;
end;
// create
//
constructor TEvent.Create;
begin
inherited Create;
WriteLn('creating client...');
FClient := TTCPClient.Create(nil);
FClient.BlockMode := bmBlocking;
FDisconnectOnError := True;
WireMethods;
WriteLn('client created');
end;
// disconnect
//
procedure TEvent.Disconnect;
begin
WriteLn('client disconnecting...');
FClient.Disconnect;
end;
// destroy
//
destructor TEvent.Destroy;
begin
WriteLn('destroying client...');
if FClient.Connected then
FClient.Disconnect;
UnwireMethods;
FClient.Free;
FClient := nil;
WriteLn('client destroyed');
inherited Destroy;
end;
// send
//
procedure TEvent.Send(Msg: string; const Prefix: string = #13#10);
begin
if FClient.Connected then
FClient.Sendln(Msg, Prefix);
end;
// setdisconnectonerror
//
procedure TEvent.SetDisconnectOnError(Value: boolean);
begin
if FDisconnectOnError <> Value then
FDisconnectOnError := Value;
end;
// unwiremethods
//
procedure TEvent.UnwireMethods;
begin
WriteLn('unwiring client methods...');
FClient.OnCreateHandle := nil;
FClient.OnDestroyHandle := nil;
FClient.OnConnect := nil;
FClient.OnDisconnect := nil;
FClient.OnReceive := nil;
FClient.OnSend := nil;
FClient.OnError := nil;
WriteLn('unwiring done');
end;
// wiremethods
//
procedure TEvent.WireMethods;
begin
WriteLn('wiring client methods...');
FClient.OnCreateHandle := ClientCreateHandle;
FClient.OnDestroyHandle := ClientDestroyHandle;
FClient.OnConnect := ClientConnect;
FClient.OnDisconnect := ClientDisconnect;
FClient.OnReceive := ClientReceive;
FClient.OnSend := ClientSend;
FClient.OnError := ClientError;
WriteLn('wiring done');
end;
{ TReadLnThread }
// execute
//
procedure TReadLnThread.Execute;
begin
if not Assigned(gEvent) then
begin
Terminate;
Exit;
end;
while not Terminated do
begin
if gEvent.Client.Connected then
WriteLn(gEvent.Client.ReceiveLn);
end;
end;
// var
// cputmp: TStringlist;
// i: Integer;
begin
gEvent := TEvent.Create;
gThread := TReadLnThread.Create(False);
repeat
ReadLn(gInput);
gCommand := GetToken(gInput, 1, ' ');
if gCommand = 'connect' then
begin
SetLength(gParameter, 2);
gParameter[0] := GetToken(gInput, 2, ' ');
gParameter[1] := GetToken(gInput, 3, ' ');
if gParameter[0] <> '' then
gEvent.Client.RemoteHost := gParameter[0];
if gParameter[1] <> '' then
gEvent.Client.RemotePort := gParameter[1];
gEvent.Connect;
end
else if gCommand = 'disconnect' then
gEvent.Disconnect
else
gEvent.Send(gInput);
until gCommand = 'quit';
FreeAndNil(gThread);
FreeAndNil(gEvent);
end.