forked from superflexible/TGPuttyLib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tgputtysftpclient.pas
405 lines (339 loc) · 13 KB
/
tgputtysftpclient.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
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
unit tgputtysftpclient;
interface
{$POINTERMATH ON}
uses
System.SysUtils, System.Classes,
tgputtylib,tgputtysftp;
type
TSFTPItem=record
filename,longname:UnicodeString;
attrs:fxp_attrs;
end;
TSFTPItems=array of TSFTPItem;
TOnSFTPMessage=procedure(Sender: TObject;const Msg:UnicodeString;const isstderr:Boolean) of object;
TOnSFTPProgress=function(Sender: TObject;const bytescopied:Int64;const isupload:Boolean):Boolean of object;
TOnSFTPListing=function(Sender: TObject;const Items:TSFTPItems):Boolean of object;
TOnSFTPGetInput=function(Sender: TObject;var cancel:Boolean):UnicodeString of object;
TOnSFTPVerifyHostKey=function(Sender: TObject;
const host:UnicodeString;const port:Integer;
const fingerprint:UnicodeString;
const verificationstatus:Integer;
var storehostkey:Boolean):Boolean of object;
TTGPuttySFTPClient = class(TComponent)
private
{ Private declarations }
FTGPuttySFTP:TTGPuttySFTP;
FOnSFTPMessage: TOnSFTPMessage;
FOnSFTPListing: TOnSFTPListing;
FOnSFTPGetInput: TOnSFTPGetInput;
FOnSFTPProgress: TOnSFTPProgress;
FOnSFTPVerifyHostKey: TOnSFTPVerifyHostKey;
function GetConnected: Boolean;
function GetErrorCode: Integer;
function GetErrorMessage: UnicodeString;
function GetHomeDir: UnicodeString;
function GetHostName: UnicodeString;
function GetKeyPassword: UnicodeString;
function GetLastMessages: UnicodeString;
function GetLibVersion: UnicodeString;
function GetPassword: UnicodeString;
function GetPort: Integer;
function GetUserName: UnicodeString;
function GetVerbose: Boolean;
function GetWorkDir: UnicodeString;
procedure SetHostName(const Value: UnicodeString);
procedure SetKeyfile(const Value: UnicodeString);
procedure SetKeyPassword(const Value: UnicodeString);
procedure SetLastMessages(const Value: UnicodeString);
procedure SetPassword(const Value: UnicodeString);
procedure SetPort(const Value: Integer);
procedure SetUserName(const Value: UnicodeString);
procedure SetVerbose(const Value: Boolean);
function ListingCallback(const names:Pfxp_names):Boolean;
procedure MessageCallback(const Msg:AnsiString;const isstderr:Boolean);
function ProgressCallback(const bytescopied:Int64;const isupload:Boolean):Boolean;
function GetInputCallback(var cancel:Boolean):AnsiString;
function VerifyHostKeyCallback(const host:PAnsiChar;const port:Integer;const fingerprint:PAnsiChar;const verificationstatus:Integer;var storehostkey:Boolean):Boolean;
protected
{ Protected declarations }
public
{ Public declarations }
constructor Create(AOwner:TComponent); override;
destructor Destroy; override;
procedure Connect;
procedure Disconnect;
procedure ChangeDir(const ADirectory:UnicodeString);
procedure MakeDir(const ADirectory:UnicodeString);
procedure RemoveDir(const ADirectory:UnicodeString);
procedure ListDir(const ADirectory:UnicodeString);
procedure GetStat(const AFileName:UnicodeString;var Attrs:fxp_attrs);
procedure SetStat(const AFileName:UnicodeString;const Attrs:fxp_attrs);
procedure SetModifiedDate(const AFileName:UnicodeString;const ATimestamp:TDateTime; const isUTC:Boolean);
procedure SetFileSize(const AFileName:UnicodeString;const ASize:Int64);
procedure Move(const AFromName,AToName:UnicodeString);
procedure DeleteFile(const AName:UnicodeString);
procedure UploadFile(const ALocalFilename,ARemoteFilename:UnicodeString;const anAppend:Boolean);
procedure DownloadFile(const ARemoteFilename,ALocalFilename:UnicodeString;const anAppend:Boolean);
procedure UploadStream(const ARemoteFilename:UnicodeString;const AStream:TStream; const anAppend:Boolean);
procedure DownloadStream(const ARemoteFilename:UnicodeString;const AStream:TStream; const anAppend:Boolean);
property HomeDir:UnicodeString read GetHomeDir;
property WorkDir:UnicodeString read GetWorkDir;
property LibVersion:UnicodeString read GetLibVersion;
property Connected:Boolean read GetConnected;
property Keyfile:UnicodeString write SetKeyfile;
property LastMessages:UnicodeString read GetLastMessages write SetLastMessages;
property ErrorCode:Integer read GetErrorCode;
property ErrorMessage:UnicodeString read GetErrorMessage;
published
{ Published declarations }
property HostName:UnicodeString read GetHostName write SetHostName;
property UserName:UnicodeString read GetUserName write SetUserName;
property Port:Integer read GetPort write SetPort;
property Password:UnicodeString read GetPassword write SetPassword;
property KeyPassword:UnicodeString read GetKeyPassword write SetKeyPassword;
property Verbose:Boolean read GetVerbose write SetVerbose;
property OnSFTPMessage:TOnSFTPMessage read FOnSFTPMessage write FOnSFTPMessage;
property OnSFTPProgress:TOnSFTPProgress read FOnSFTPProgress write FOnSFTPProgress;
property OnSFTPListing:TOnSFTPListing read FOnSFTPListing write FOnSFTPListing;
property OnSFTPGetInput:TOnSFTPGetInput read FOnSFTPGetInput write FOnSFTPGetInput;
property OnSFTPVerifyHostKey:TOnSFTPVerifyHostKey read FOnSFTPVerifyHostKey write FOnSFTPVerifyHostKey;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('TGPuttyLib', [TTGPuttySFTPClient]);
end;
{ TTGPuttySFTPClient }
procedure TTGPuttySFTPClient.ChangeDir(const ADirectory: UnicodeString);
begin
FTGPuttySFTP.ChangeDir(Utf8Encode(ADirectory));
end;
procedure TTGPuttySFTPClient.Connect;
begin
FTGPuttySFTP.Connect;
end;
constructor TTGPuttySFTPClient.Create(AOwner:TComponent);
begin
inherited Create(AOwner);
FTGPuttySFTP:=TTGPuttySFTP.Create(false);
FTGPuttySFTP.OnListing:=ListingCallback;
FTGPuttySFTP.OnMessage:=MessageCallback;
FTGPuttySFTP.OnProgress:=ProgressCallback;
FTGPuttySFTP.OnGetInput:=GetInputCallback;
FTGPuttySFTP.OnVerifyHostKey:=VerifyHostKeyCallback;
end;
procedure TTGPuttySFTPClient.DeleteFile(const AName: UnicodeString);
begin
FTGPuttySFTP.DeleteFile(Utf8Encode(AName));
end;
destructor TTGPuttySFTPClient.Destroy;
begin
FreeAndNil(FTGPuttySFTP);
inherited;
end;
procedure TTGPuttySFTPClient.Disconnect;
begin
FTGPuttySFTP.Disconnect;
end;
procedure TTGPuttySFTPClient.DownloadFile(const ARemoteFilename, ALocalFilename: UnicodeString; const anAppend: Boolean);
var LStream:TFileStream;
begin
if anAppend then begin
LStream:=TFileStream.Create(ALocalFilename,fmOpenReadWrite);
LStream.Seek(0,soEnd);
end
else
LStream:=TFileStream.Create(ALocalFilename,fmCreate);
try
FTGPuttySFTP.DownloadStream(Utf8Encode(ARemoteFilename),LStream,anAppend);
finally
FreeAndNil(LStream);
end;
end;
procedure TTGPuttySFTPClient.DownloadStream(const ARemoteFilename: UnicodeString; const AStream: TStream;
const anAppend: Boolean);
begin
FTGPuttySFTP.DownloadStream(Utf8Encode(ARemoteFilename),AStream,anAppend);
end;
function TTGPuttySFTPClient.GetConnected: Boolean;
begin
Result:=FTGPuttySFTP.Connected;
end;
function TTGPuttySFTPClient.GetErrorCode: Integer;
begin
Result:=FTGPuttySFTP.ErrorCode;
end;
function TTGPuttySFTPClient.GetErrorMessage: UnicodeString;
begin
Result:=Utf8ToString(FTGPuttySFTP.ErrorMessage);
end;
function TTGPuttySFTPClient.GetHomeDir: UnicodeString;
begin
Result:=Utf8ToString(FTGPuttySFTP.HomeDir);
end;
function TTGPuttySFTPClient.GetHostName: UnicodeString;
begin
Result:=Utf8ToString(FTGPuttySFTP.HostName);
end;
function TTGPuttySFTPClient.GetInputCallback(var cancel: Boolean): AnsiString;
begin
if Assigned(FOnSFTPGetInput) then
Result:=Utf8Encode(FOnSFTPGetInput(self,cancel))
else begin
Result:='';
cancel:=true;
end;
end;
function TTGPuttySFTPClient.GetKeyPassword: UnicodeString;
begin
Result:=Utf8ToString(FTGPuttySFTP.KeyPassword);
end;
function TTGPuttySFTPClient.GetLastMessages: UnicodeString;
begin
Result:=Utf8ToString(FTGPuttySFTP.LastMessages);
end;
function TTGPuttySFTPClient.GetLibVersion: UnicodeString;
begin
Result:=Utf8ToString(FTGPuttySFTP.LibVersion);
end;
function TTGPuttySFTPClient.GetPassword: UnicodeString;
begin
Result:=Utf8ToString(FTGPuttySFTP.Password);
end;
function TTGPuttySFTPClient.GetPort: Integer;
begin
Result:=FTGPuttySFTP.Port;
end;
procedure TTGPuttySFTPClient.GetStat(const AFileName: UnicodeString; var Attrs: fxp_attrs);
begin
FTGPuttySFTP.GetStat(Utf8Encode(AFileName),Attrs);
end;
function TTGPuttySFTPClient.GetUserName: UnicodeString;
begin
Result:=Utf8ToString(FTGPuttySFTP.UserName);
end;
function TTGPuttySFTPClient.GetVerbose: Boolean;
begin
Result:=FTGPuttySFTP.Verbose;
end;
function TTGPuttySFTPClient.GetWorkDir: UnicodeString;
begin
Result:=Utf8ToString(FTGPuttySFTP.WorkDir);
end;
procedure TTGPuttySFTPClient.ListDir(const ADirectory: UnicodeString);
begin
FTGPuttySFTP.ListDir(Utf8Encode(ADirectory));
end;
function TTGPuttySFTPClient.ListingCallback(const names: Pfxp_names): Boolean;
var Unames:TSFTPItems;
i:Integer;
begin
if Assigned(FOnSFTPListing) then begin
SetLength(Unames,names.nnames);
for i:=0 to names.nnames-1 do begin
Unames[i].filename:=Utf8ToString(names.names[i].filename);
Unames[i].longname:=Utf8ToString(names.names[i].longname);
Unames[i].attrs:=names.names[i].attrs;
end;
Result:=FOnSFTPListing(self,Unames);
end
else
Result:=true;
end;
procedure TTGPuttySFTPClient.MakeDir(const ADirectory: UnicodeString);
begin
FTGPuttySFTP.MakeDir(Utf8Encode(ADirectory));
end;
procedure TTGPuttySFTPClient.MessageCallback(const Msg: AnsiString; const isstderr: Boolean);
begin
if Assigned(FOnSFTPMessage) then
FOnSFTPMessage(self,Utf8ToString(Msg),isstderr);
end;
procedure TTGPuttySFTPClient.Move(const AFromName, AToName: UnicodeString);
begin
FTGPuttySFTP.Move(Utf8Encode(AFromName),Utf8Encode(AToName));
end;
function TTGPuttySFTPClient.ProgressCallback(const bytescopied: Int64; const isupload: Boolean): Boolean;
begin
if Assigned(FOnSFTPProgress) then
Result:=FOnSFTPProgress(self,bytescopied,isupload)
else
Result:=true;
end;
procedure TTGPuttySFTPClient.RemoveDir(const ADirectory: UnicodeString);
begin
FTGPuttySFTP.RemoveDir(Utf8Encode(ADirectory));
end;
procedure TTGPuttySFTPClient.SetFileSize(const AFileName: UnicodeString; const ASize: Int64);
begin
FTGPuttySFTP.SetFileSize(Utf8Encode(AFileName),ASize);
end;
procedure TTGPuttySFTPClient.SetHostName(const Value: UnicodeString);
begin
FTGPuttySFTP.HostName:=Utf8Encode(Value);
end;
procedure TTGPuttySFTPClient.SetKeyfile(const Value: UnicodeString);
begin
FTGPuttySFTP.Keyfile:=Utf8Encode(Value);
end;
procedure TTGPuttySFTPClient.SetKeyPassword(const Value: UnicodeString);
begin
FTGPuttySFTP.KeyPassword:=Utf8Encode(Value);
end;
procedure TTGPuttySFTPClient.SetLastMessages(const Value: UnicodeString);
begin
FTGPuttySFTP.LastMessages:=Utf8Encode(Value);
end;
procedure TTGPuttySFTPClient.SetModifiedDate(const AFileName: UnicodeString; const ATimestamp: TDateTime; const isUTC: Boolean);
begin
FTGPuttySFTP.SetModifiedDate(Utf8Encode(AFileName),ATimestamp,isUTC);
end;
procedure TTGPuttySFTPClient.SetPassword(const Value: UnicodeString);
begin
FTGPuttySFTP.Password:=Utf8Encode(Value);
end;
procedure TTGPuttySFTPClient.SetPort(const Value: Integer);
begin
FTGPuttySFTP.Port:=Value;
end;
procedure TTGPuttySFTPClient.SetStat(const AFileName: UnicodeString;const Attrs: fxp_attrs);
begin
FTGPuttySFTP.SetStat(Utf8Encode(AFileName),Attrs);
end;
procedure TTGPuttySFTPClient.SetUserName(const Value: UnicodeString);
begin
FTGPuttySFTP.UserName:=Utf8Encode(Value);
end;
procedure TTGPuttySFTPClient.SetVerbose(const Value: Boolean);
begin
FTGPuttySFTP.Verbose:=Value;
end;
procedure TTGPuttySFTPClient.UploadFile(const ALocalFilename, ARemoteFilename: UnicodeString; const anAppend: Boolean);
var LStream:TFileStream;
begin
LStream:=TFileStream.Create(ALocalFilename,fmOpenRead);
try
FTGPuttySFTP.UploadStream(Utf8Encode(ARemoteFilename),LStream,anAppend);
finally
FreeAndNil(LStream);
end;
end;
procedure TTGPuttySFTPClient.UploadStream(const ARemoteFilename: UnicodeString; const AStream: TStream; const anAppend: Boolean);
begin
FTGPuttySFTP.UploadStream(Utf8Encode(ARemoteFilename),AStream,anAppend);
end;
function TTGPuttySFTPClient.VerifyHostKeyCallback(const host: PAnsiChar;
const port: Integer;
const fingerprint: PAnsiChar;
const verificationstatus: Integer;
var storehostkey: Boolean): Boolean;
begin
if Assigned(FOnSFTPVerifyHostKey) then
Result:=FOnSFTPVerifyHostKey(self,Utf8ToString(host),port,Utf8ToString(fingerprint),verificationstatus,storehostkey)
else begin
Result:=true;
storehostkey:=false;
end;
end;
end.