Skip to content

Commit

Permalink
Disable Wow64 redirection when calling ssh and scp from PyScripter 32…
Browse files Browse the repository at this point in the history
…bits in 64bits Windows.
  • Loading branch information
pyscripter committed Oct 17, 2018
1 parent 8fcf351 commit 6dfbded
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 6 deletions.
67 changes: 62 additions & 5 deletions Source/cPySSHDebugger.pas
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ implementation
cPyScripterSettings,
System.StrUtils,
uCommonFunctions,
cPyControl;
cPyControl,
MPCommonUtilities;

{ TPySSHInterpreter }

Expand All @@ -100,12 +101,25 @@ constructor TPySSHInterpreter.Create(SSHServer : TSSHServer);
PythonCommand := SSHServer.PythonCommand;
// Test SSH connection and get information about the server
Task := TTask.Create(procedure
{$IFDEF CPUX86}
Var
IsWow64: LongBool;
{$ENDIF CPUX86}
begin
{$IFDEF CPUX86}
IsWow64 := IsWow64Process(GetCurrentProcess, IsWow64) and IsWow64;
if IsWow64 then Wow64EnableWow64FsRedirection_MP(False);
try
{$ENDIF CPUX86}
fServerIsAvailable := Execute(Format('ssh %s %s@%s %s -c ' +
'''"import sys,os,tempfile;print(sys.version[0]);print(os.sep);print(tempfile.gettempdir())"''',
[SshCommandOptions, UserName, HostName, PythonCommand]), CommandOutput) = 0
end);
Task.Start;
{$IFDEF CPUX86}
finally
if IsWow64 then Wow64EnableWow64FsRedirection_MP(True);
end;
{$ENDIF CPUX86}
end).Start;

if Task.Wait(3000) and fServerIsAvailable then
fServerIsAvailable := ProcessPlatformInfo(CommandOutput, fIs3K, PathSeparator, TempDir);
Expand Down Expand Up @@ -141,6 +155,9 @@ constructor TPySSHInterpreter.Create(SSHServer : TSSHServer);
procedure TPySSHInterpreter.CreateAndRunServerProcess;
Var
ErrorMsg : string;
{$IFDEF CPUX86}
IsWow64: LongBool;
{$ENDIF CPUX86}
begin
fShuttingDown := False;
fServerIsAvailable := False;
Expand Down Expand Up @@ -169,20 +186,47 @@ procedure TPySSHInterpreter.CreateAndRunServerProcess;
[SshCommandOptions, UserName, HostName, PythonCommand]) +
Format('"%s" %d "%s"', [fRemServerFile, fSocketPort, fRemRpycFile]);
ServerTask := TTask.Create(procedure
{$IFDEF CPUX86}
Var
IsWow64: LongBool;
{$ENDIF CPUX86}
begin
{$IFDEF CPUX86}
IsWow64 := IsWow64Process(GetCurrentProcess, IsWow64) and IsWow64;
if IsWow64 then Wow64EnableWow64FsRedirection_MP(False);
try
{$ENDIF CPUX86}
ExecuteCmdProcess(ServerProcessOptions);
{$IFDEF CPUX86}
finally
if IsWow64 then Wow64EnableWow64FsRedirection_MP(True);
end;
{$ENDIF CPUX86}
end).Start;
Sleep(100);
fServerIsAvailable := ServerTask.Status = TTaskStatus.Running;

Application.ProcessMessages;
if not fServerIsAvailable then Exit;

TunnelProcessOptions.CommandLine := Format('ssh -n %s %s@%s -L 127.0.0.1:%d:127.0.0.1:%3:d -N',
[SshCommandOptions, UserName, HostName, fSocketPort]);
TunnelTask := TTask.Create(procedure
{$IFDEF CPUX86}
Var
IsWow64: LongBool;
{$ENDIF CPUX86}
begin
{$IFDEF CPUX86}
IsWow64 := IsWow64Process(GetCurrentProcess, IsWow64) and IsWow64;
if IsWow64 then Wow64EnableWow64FsRedirection_MP(False);
try
{$ENDIF CPUX86}
ExecuteCmdProcess(TunnelProcessOptions);
{$IFDEF CPUX86}
finally
if IsWow64 then Wow64EnableWow64FsRedirection_MP(True);
end;
{$ENDIF CPUX86}
end).Start;
Sleep(100);
fServerIsAvailable := TunnelTask.Status = TTaskStatus.Running;
Expand Down Expand Up @@ -233,18 +277,31 @@ function TPySSHInterpreter.ProcessPlatformInfo(Info: string; out Is3k: Boolean;
procedure TPySSHInterpreter.ShutDownServer;
Var
CommandOutput : string;
{$IFDEF CPUX86}
IsWow64: LongBool;
{$ENDIF CPUX86}
begin
fShuttingDown := True;
inherited;
// Delete temp files
{$IFDEF CPUX86}
IsWow64 := IsWow64Process(GetCurrentProcess, IsWow64) and IsWow64;
if IsWow64 then Wow64EnableWow64FsRedirection_MP(False);
try
{$ENDIF CPUX86}
if (fRemServerFile <> '') and (Execute(Format('ssh %s %s@%s rm %s',
[SshCommandOptions, UserName, HostName, fRemServerFile]), CommandOutput) = 0)
[SshCommandOptions, UserName, HostName, fRemServerFile]), CommandOutput) = 0)
then
fRemServerFile := '';
if (fRemRpycFile <> '') and (Execute(Format('ssh %s %s@%s rm %s',
[SshCommandOptions, UserName, HostName, fRemRpycFile]), CommandOutput) = 0)
then
fRemRpycFile := '';
{$IFDEF CPUX86}
finally
if IsWow64 then Wow64EnableWow64FsRedirection_MP(True);
end;
{$ENDIF CPUX86}
// shut down tunnel
if Assigned(TunnelTask) and (TunnelTask.Status = TTaskStatus.Running) then begin
RaiseKeyboardInterrupt(TunnelProcessInfo.dwProcessId);
Expand Down
18 changes: 17 additions & 1 deletion Source/cSSHSupport.pas
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,16 @@ TUnc = class
implementation

uses
WinApi.Windows,
System.Threading,
Vcl.Forms,
jclSysUtils,
JvGnugettext,
dlgCollectionEditor,
dlgOptionsEditor,
StringResources,
frmCommandOutput;
frmCommandOutput,
MPCommonUtilities;

{ TSSHConfig }

Expand Down Expand Up @@ -210,8 +212,22 @@ function Scp(const FromFile, ToFile: string; out ErrorMsg: string): Boolean;
[SshCommandOptions, FromFile, ToFile]);

Task := TTask.Create(procedure
{$IFDEF CPUX86}
Var
IsWow64: LongBool;
{$ENDIF CPUX86}
begin
{$IFDEF CPUX86}
IsWow64 := IsWow64Process(GetCurrentProcess, IsWow64) and IsWow64;
if IsWow64 then Wow64EnableWow64FsRedirection_MP(False);
try
{$ENDIF CPUX86}
ExitCode := JclSysUtils.Execute(Command, Output);
{$IFDEF CPUX86}
finally
if IsWow64 then Wow64EnableWow64FsRedirection_MP(True);
end;
{$ENDIF CPUX86}
end);
Task.Start;
if not Task.Wait(ScpTimeout) then
Expand Down

0 comments on commit 6dfbded

Please sign in to comment.