-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathuMain.pas
More file actions
631 lines (575 loc) · 17.1 KB
/
uMain.pas
File metadata and controls
631 lines (575 loc) · 17.1 KB
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
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
unit uMain;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ComCtrls, ExtCtrls, StdCtrls, Spin, EditBtn,
uRemotes, process, LMessages, Menus, LCLType, ActnList;
const
WMU_MOUNT_NEXT = WM_USER + 1;
type
{ TfmMain }
TfmMain = class(TForm)
PageControl1: TPageControl;
tsConnection: TTabSheet;
tsStatus: TTabSheet;
ImageList1: TImageList;
Panel1: TPanel;
Button1: TButton;
Button2: TButton;
tmrStatusUpdate: TTimer;
Button3: TButton;
TrayIcon1: TTrayIcon;
Button4: TButton;
Label1: TLabel;
edActName: TEdit;
Label2: TLabel;
edActHost: TEdit;
Label3: TLabel;
Label4: TLabel;
edActUsername: TEdit;
Label5: TLabel;
Label6: TLabel;
edActAuthPassword: TEdit;
Label7: TLabel;
Label8: TLabel;
edActOptions: TEdit;
cbActAuth: TComboBox;
seActPort: TSpinEdit;
feActSecKey: TFileNameEdit;
Label9: TLabel;
cbActPath: TComboBox;
meInfoStart: TMemo;
Label10: TLabel;
lbInfoPID: TLabel;
Label11: TLabel;
cbActDrive: TComboBox;
Panel2: TPanel;
lvDefs: TListView;
Panel3: TPanel;
Button5: TButton;
Button6: TButton;
cbActAutomount: TCheckBox;
Button7: TButton;
pmTray: TPopupMenu;
miShow: TMenuItem;
miStartMount: TMenuItem;
miExit: TMenuItem;
acMain: TActionList;
acRemoteMount: TAction;
acRemoteUnmount: TAction;
acExploreTarget: TAction;
acRemoteSaveChanges: TAction;
acRemoteAdd: TAction;
acRemoteRemove: TAction;
acRemoteCopy: TAction;
acTrayShow: TAction;
acTrayBeginMount: TAction;
acExit: TAction;
pmList: TPopupMenu;
MenuItem1: TMenuItem;
MenuItem2: TMenuItem;
N1: TMenuItem;
MenuItem3: TMenuItem;
MenuItem4: TMenuItem;
MenuItem5: TMenuItem;
tsGlobalSettings: TTabSheet;
Label12: TLabel;
cbSSHFSExe: TComboBox;
lbSSHFSInfo: TLabel;
Panel4: TPanel;
btnGlobalSave: TButton;
btnGlobalUndo: TButton;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure lvDefsData(Sender: TObject; Item: TListItem);
procedure lvDefsSelectItem(Sender: TObject; Item: TListItem; Selected: Boolean);
procedure tmrStatusUpdateTimer(Sender: TObject);
procedure cbActDriveDropDown(Sender: TObject);
procedure cbActAuthSelect(Sender: TObject);
procedure FormWindowStateChange(Sender: TObject);
procedure cbSSHFSExeChange(Sender: TObject);
procedure btnGlobalSaveClick(Sender: TObject);
procedure btnGlobalUndoClick(Sender: TObject);
procedure cbSSHFSExeEnter(Sender: TObject);
procedure cbSSHFSExeDropDown(Sender: TObject);
procedure lvDefsMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
procedure acMainUpdate(AAction: TBasicAction; var Handled: Boolean);
procedure acRemoteAddExecute(Sender: TObject);
procedure acRemoteRemoveExecute(Sender: TObject);
procedure acRemoteCopyExecute(Sender: TObject);
procedure acRemoteSaveChangesExecute(Sender: TObject);
procedure acRemoteMountExecute(Sender: TObject);
procedure acRemoteUnmountExecute(Sender: TObject);
procedure acExploreTargetExecute(Sender: TObject);
procedure acExitExecute(Sender: TObject);
procedure acTrayShowExecute(Sender: TObject);
procedure acTrayBeginMountExecute(Sender: TObject);
private
fRemotes: TRemoteList;
fExe: string;
fActiveSelected: TRemote;
procedure UpdateEditSelection(NewSel: TRemote);
procedure RemoteMount(aRemote: TRemote);
procedure RemotesChanged;
procedure StartupActions;
procedure WMUMountNext(var msg: TLMessage); message WMU_MOUNT_NEXT;
public
end;
var
fmMain: TfmMain;
implementation
uses
LCLIntf, FileUtil, uConfig;
{$R *.lfm}
{ TfmMain }
procedure TfmMain.FormCreate(Sender: TObject);
var
ini: TIniFile;
begin
TrayIcon1.Icon:= Application.Icon;
fRemotes:= TRemoteList.Create;
ini:= OpenIniFile;
try
fExe:= ini.ReadString('Config', 'Exe', '');
fRemotes.LoadFromIni(ini);
finally
FreeAndNil(ini);
end;
btnGlobalUndo.Click;
fActiveSelected:= nil;
lvDefs.Items.Count:= fRemotes.Count;
lvDefs.Selected:= nil;
lvDefsSelectItem(nil, nil, False);
PageControl1.ActivePageIndex:= 0;
StartupActions;
end;
procedure TfmMain.FormDestroy(Sender: TObject);
begin
FreeAndNil(fRemotes);
end;
procedure TfmMain.StartupActions;
var
i: Integer;
begin
for i:= 1 to ParamCount do begin
case ParamStr(i) of
'/min': begin
//PostMessage(Handle, LM_SYSCOMMAND, SC_MINIMIZE, 0);
Application.ShowMainForm:= False;
end;
'/automount': PostMessage(Handle, WMU_MOUNT_NEXT, 0, 0);
end;
end;
end;
procedure TfmMain.WMUMountNext(var msg: TLMessage);
var
idx: Integer;
r: TRemote;
begin
idx:= msg.wParam;
if idx >= fRemotes.Count then
Exit;
r:= fRemotes[idx];
if (r.Status in [rsNone]) and r.AutoMount then begin
RemoteMount(r);
PostMessage(Handle, WMU_MOUNT_NEXT, idx + 1, 0);
end;
end;
procedure TfmMain.lvDefsData(Sender: TObject; Item: TListItem);
var
r: TRemote;
begin
if (Item.Index<0) or (Item.Index>=fRemotes.Count) then
exit;
r:= fRemotes[Item.Index];
Item.Caption:= r.GetDriveStr;
Item.SubItems.Add(r.GetConnectStr(True));
Item.ImageIndex:= Ord(r.Status);
end;
procedure TfmMain.lvDefsSelectItem(Sender: TObject; Item: TListItem; Selected: Boolean);
begin
if lvDefs.ItemIndex >= 0 then
UpdateEditSelection(fRemotes[lvDefs.ItemIndex])
else
UpdateEditSelection(nil);
end;
procedure TfmMain.UpdateEditSelection(NewSel: TRemote);
begin
if Assigned(NewSel) then begin
if fActiveSelected = NewSel then
Exit;
edActName.Text:= NewSel.Name;
edActHost.Text:= NewSel.Host;
seActPort.Value:= NewSel.Port;
edActUsername.Text:= NewSel.User;
cbActAuth.ItemIndex:= ord(NewSel.Auth);
cbActAuthSelect(nil);
feActSecKey.FileName:= NewSel.AuthSecKey;
edActAuthPassword.Text:= NewSel.AuthPassword;
cbActPath.Text:= NewSel.Path;
cbActDrive.Text:= NewSel.Drive;
edActOptions.Text:= NewSel.Options;
cbActAutomount.Checked:= NewSel.AutoMount;
end else begin
edActName.Text:= '';
edActHost.Text:= '';
seActPort.Value:= 22;
edActUsername.Text:= '';
cbActAuth.ItemIndex:= 0;
feActSecKey.FileName:= '';
edActAuthPassword.Text:= '';
cbActPath.Text:= '';
cbActDrive.Text:= '';
edActOptions.Text:= '';
cbActAutomount.Checked:= False;
end;
fActiveSelected:= NewSel;
end;
procedure TfmMain.RemoteMount(aRemote: TRemote);
function tocygdrive(fn: string): string;
begin
fn:= ExpandFileName(fn);
Result:= '/cygdrive/' + LowerCase(fn[1]) + fn.Substring(2).Replace(PathDelim, '/');
end;
var
proc: TProcess;
tooldir: string;
BytesRead: Integer;
OutputLength: integer;
OutputString: String;
tstart: LongWord;
sshfspid: SizeUInt;
hidden_args, i: integer;
begin
if not FileExists(fExe) then begin
MessageDlg('SSHFS binary not present, please check settings!', mtError, [mbOK], 0);
PageControl1.ActivePage:= tsStatus;
exit;
end;
tooldir:= ExtractFilePath(ParamStr(0)) + 'lib\';
proc:= TProcess.Create(nil);
try
Screen.Cursor:= crHourGlass;
proc.Options:= [poUsePipes, poStderrToOutPut, poNewProcessGroup, poNoConsole];
// Use our provided env.exe, but in the target's dir - means it loads *their* cygwin1.dll
proc.Executable:= tooldir + 'env.exe';
proc.CurrentDirectory:= ExtractFileDir(fExe);
// pass all env variables we might need
proc.Parameters.AddStrings([
'-',
'PATH='+tocygdrive(tooldir)+':'+tocygdrive(ExtractFilePath(fExe)),
'DISPLAY=1',
'SSH_ASKPASS=print_pass.exe',
'SSHFS_AUTH_PASSPHRASE=' + aRemote.AuthPassword
]);
hidden_args:= proc.Parameters.Count;
aRemote.InfoStart:= '';
// the actual sshfs process
proc.Parameters.AddStrings([
tocygdrive(fExe),
aRemote.GetConnectStr,
aRemote.Drive,
'-ovolname='+aRemote.Name,
'-ossh_command=ssh_ap.exe -v',
'-f',
// do not attempt loading user profile data
'-F/dev/null', '-oIdentitiesOnly=yes',
// make message reproducible
'-oUserKnownHostsFile=/dev/null', '-oStrictHostKeyChecking=no',
// remap for write permissions, create modes: 777/666
'-ouid=-1,gid=-1', '-oidmap=none', '-ocreate_file_umask=0111', '-ocreate_dir_umask=0000',
// shorten cache intervals
'-odcache_clean_interval=60', '-odcache_timeout=20', '-odcache_stat_timeout=5',
// some defaults that are never not useful
'-odothidden', '-odisable_hardlink'
]);
// port is not part of the connect string
if aRemote.Port<>22 then
proc.Parameters.Add('-p%d', [aRemote.Port]);
// authentication
case aRemote.Auth of
amPassword: begin
proc.Parameters.AddStrings([
'-oPreferredAuthentications=password'
]);
end;
amPubkey: begin
proc.Parameters.AddStrings([
'-oPreferredAuthentications=publickey',
'-oIdentityFile='+aRemote.AuthSecKey.Replace('\','\\')
]);
end;
end;
// user options last (allow override)
proc.Parameters.AddDelimitedText(aRemote.Options);
// store effective command line
aRemote.InfoStart:= 'Command Line: ' + proc.Parameters[hidden_args+1] + sLineBreak;
for i:= hidden_args to proc.Parameters.Count - 1 do begin
if Length(aRemote.InfoStart) - LastDelimiter(sLineBreak, aRemote.InfoStart) > 64 then
aRemote.InfoStart += sLineBreak;
aRemote.InfoStart += ' ' + proc.Parameters[i];
end;
// Execute and capture PID as soon as we can
proc.Execute;
aRemote.PID:= proc.ProcessID;
// wait and see what happens
proc.CloseInput;
OutputLength:= 0;
OutputString:= '';
BytesRead:= 0;
tstart:= GetTickCount64;
while proc.Running and (GetTickCount64 - tstart < 5000) do begin
proc.ReadInputStream(proc.Output,BytesRead,OutputLength,OutputString,1);
if Pos('has been started', OutputString)>0 then
break;
Application.ProcessMessages;
end;
proc.ReadInputStream(proc.Output,BytesRead,OutputLength,OutputString,1);
// Disconnect pipes
proc.CloseOutput;
proc.CloseStderr;
aRemote.InfoStart+= sLineBreak + sLineBreak + Copy(OutputString, 1, BytesRead);
// if everything worked up to here, retarget our tracking to the subprocess
// this also cleans up the cleartext password leak in argv of /bin/env
if FindSubprocess(aRemote.PID, ExtractFileName(fExe), sshfspid) then begin
if KillProcess(aRemote.PID) then begin
aRemote.PID:= sshfspid;
end;
end;
finally
Screen.Cursor:= crDefault;
FreeAndNil(proc);
end;
RemotesChanged;
aRemote.UpdateStatus;
end;
procedure TfmMain.RemotesChanged;
var
ini: TIniFile;
begin
ini:= OpenIniFile;
try
fRemotes.SaveToIni(ini);
finally
FreeAndNil(ini);
end;
end;
procedure TfmMain.tmrStatusUpdateTimer(Sender: TObject);
const
LPAD = ' ';
var
r: TRemote;
mounted: string;
begin
mounted:= '';
for r in fRemotes do begin
if r.Status = rsConnected then
mounted+= LPAD + r.GetDriveStr + sLineBreak;
end;
if mounted = '' then
mounted:= LPAD + 'none';
TrayIcon1.Hint:= Caption + sLineBreak + 'Mounted Drives: ' + sLineBreak + mounted;
lvDefs.Refresh;
end;
procedure TfmMain.cbActAuthSelect(Sender: TObject);
begin
feActSecKey.Enabled:= TRemoteAuthMethod(cbActAuth.ItemIndex) in [amPubkey];
edActAuthPassword.Enabled:= TRemoteAuthMethod(cbActAuth.ItemIndex) in [amPassword, amPubkey];
end;
procedure TfmMain.cbActDriveDropDown(Sender: TObject);
var
d: Char;
sel: String;
begin
sel:= cbActDrive.Text;
cbActDrive.Clear;
for d in GetFreeDriveLetters do
cbActDrive.Items.Add(d + ':');
cbActDrive.Text:= sel;
end;
procedure TfmMain.FormWindowStateChange(Sender: TObject);
begin
if WindowState = wsMinimized then begin
WindowState:= wsNormal;
Hide;
ShowInTaskBar:= stNever;
end;
end;
procedure TfmMain.btnGlobalSaveClick(Sender: TObject);
begin
fExe:= cbSSHFSExe.Text;
with OpenIniFile do try
WriteString('Config', 'Exe', fExe);
finally
Free;
end;
end;
procedure TfmMain.btnGlobalUndoClick(Sender: TObject);
begin
cbSSHFSExe.Text:= fExe;
cbSSHFSExeChange(nil);
end;
procedure TfmMain.cbSSHFSExeEnter(Sender: TObject);
begin
cbSSHFSExe.Items.Clear;
end;
function GetSSHFSVersion(candidate: string): string;
var
outp: string;
begin
Result:= '';
if FileExists(candidate) and SameFileName(ExtractFileName(candidate), 'sshfs.exe') and
RunCommandIndir(ExtractFileDir(candidate), candidate, ['-V'], outp, [poNoConsole], swoHIDE) then
Result:= outp.Trim.Replace(#13,'').Replace(#10, ' ');
end;
procedure TfmMain.cbSSHFSExeDropDown(Sender: TObject);
procedure Test(guess: string);
var
v: string;
begin
if cbSSHFSExe.Items.IndexOf(guess)>= 0 then
Exit;
v:= GetSSHFSVersion(guess);
if v > '' then
cbSSHFSExe.Items.Add(guess);
end;
begin
if cbSSHFSExe.Items.Count = 0 then begin
Test(ExtractFilePath(ParamStr(0)) + 'sshfs-win\sshfs.exe');
Test(ConcatPaths([GetEnvironmentVariable('ProgramW6432'),'SSHFS-Win\bin\sshfs.exe']));
Test(ConcatPaths([GetEnvironmentVariable('ProgramFiles'),'SSHFS-Win\bin\sshfs.exe']));
end;
end;
procedure TfmMain.lvDefsMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
var
itm: TListItem;
OriginalHint, h: String;
r: TRemote;
begin
itm:= lvDefs.GetItemAt(X, Y);
OriginalHint:= lvDefs.Hint;
if not Assigned(itm) then
lvDefs.Hint:= 'nix'
else begin
h:= itm.Caption;
r:= fRemotes[itm.Index];
h += sLineBreak + 'Status: ' + StatusNames[r.Status];
if r.PID > 0 then
h += sLineBreak + 'Process: ' + IntToStr(r.PID);
lvDefs.Hint:= h;
end;
if lvDefs.Hint <> OriginalHint then
Application.CancelHint;
end;
procedure TfmMain.cbSSHFSExeChange(Sender: TObject);
var
outp: string;
begin
lbSSHFSInfo.Caption:= '<invalid>';
outp:= GetSSHFSVersion(cbSSHFSExe.Text);
if outp>'' then
lbSSHFSInfo.Caption:= outp;
end;
procedure TfmMain.acMainUpdate(AAction: TBasicAction; var Handled: Boolean);
var
pn: String;
begin
tsConnection.Enabled:= Assigned(fActiveSelected);
acRemoteMount.Enabled:= Assigned(fActiveSelected) and (fActiveSelected.Status in [rsNone]);
acRemoteUnmount.Enabled:= Assigned(fActiveSelected) and (fActiveSelected.Status in [rsConnected]);
acExploreTarget.Enabled:= Assigned(fActiveSelected) and (fActiveSelected.Status in [rsDriveTaken, rsConnected]);
acRemoteRemove.Enabled:= Assigned(fActiveSelected) and (fActiveSelected.Status in [rsNone, rsDriveTaken]);
acRemoteCopy.Enabled:= Assigned(fActiveSelected);
if Assigned(fActiveSelected) then begin
meInfoStart.Lines.Text:= fActiveSelected.InfoStart;
lbInfoPID.Caption:= IntToStr(fActiveSelected.PID);
if GetProcessInfo(fActiveSelected.PID, pn) = STILL_ACTIVE then
lbInfoPID.Caption:= lbInfoPID.Caption + ' <...' + Copy(pn, Length(pn) - 42) + '>';
end else begin
meInfoStart.Clear;
lbInfoPID.Caption:= '';
end;
end;
procedure TfmMain.acRemoteAddExecute(Sender: TObject);
var
r: TRemote;
begin
r:= TRemote.Create;
r.Name:= Format('Connection %d', [fRemotes.Count + 1]);
fRemotes.Add(r);
RemotesChanged;
lvDefs.Items.Count:= fRemotes.Count;
lvDefs.ItemIndex:= lvDefs.Items.Count - 1;
end;
procedure TfmMain.acRemoteRemoveExecute(Sender: TObject);
var
todel: TRemote;
begin
todel:= fActiveSelected;
lvDefs.ItemIndex:= -1;
fRemotes.Remove(todel);
RemotesChanged;
lvDefs.Items.Count:= fRemotes.Count;
end;
procedure TfmMain.acRemoteCopyExecute(Sender: TObject);
var
r: TRemote;
begin
r:= TRemote.Create;
r.CopyFrom(fActiveSelected);
r.Name:= Format('Connection %d', [fRemotes.Count + 1]);
fRemotes.Add(r);
RemotesChanged;
lvDefs.Items.Count:= fRemotes.Count;
lvDefs.ItemIndex:= lvDefs.Items.Count - 1;
end;
procedure TfmMain.acRemoteSaveChangesExecute(Sender: TObject);
begin
fActiveSelected.Name:= edActName.Text;
fActiveSelected.Host:= edActHost.Text;
fActiveSelected.Port:= seActPort.Value;
fActiveSelected.User:= edActUsername.Text;
fActiveSelected.Auth:= TRemoteAuthMethod(cbActAuth.ItemIndex);
fActiveSelected.AuthSecKey:= feActSecKey.FileName;
fActiveSelected.AuthPassword:= edActAuthPassword.Text;
fActiveSelected.Path:= cbActPath.Text;
fActiveSelected.Drive:= cbActDrive.Text;
fActiveSelected.Options:= edActOptions.Text;
fActiveSelected.AutoMount:= cbActAutomount.Checked;
RemotesChanged;
lvDefs.Refresh;
end;
procedure TfmMain.acRemoteMountExecute(Sender: TObject);
begin
if fActiveSelected.Status <> rsNone then
Exit;
RemoteMount(fActiveSelected);
end;
procedure TfmMain.acRemoteUnmountExecute(Sender: TObject);
begin
if fActiveSelected.Status <> rsConnected then
Exit;
if KillProcessTree(fActiveSelected.PID, 0) then begin
fActiveSelected.PID:= 0;
end;
RemotesChanged;
fActiveSelected.UpdateStatus;
end;
procedure TfmMain.acExploreTargetExecute(Sender: TObject);
begin
OpenDocument(fActiveSelected.Drive);
end;
procedure TfmMain.acExitExecute(Sender: TObject);
begin
Close;
end;
procedure TfmMain.acTrayShowExecute(Sender: TObject);
begin
ShowInTaskBar:= stDefault;
Show;
end;
procedure TfmMain.acTrayBeginMountExecute(Sender: TObject);
begin
PostMessage(Handle, WMU_MOUNT_NEXT, 0, 0);
end;
end.