-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(client-dotnet): implement C# NOW-proto client (#14)
This PR includes the following: - NOW-proto C# client implementation - NowProto core protocol library bugfixes --------- Co-authored-by: Marc-André Moreau <[email protected]>
- Loading branch information
1 parent
3affff3
commit 4074684
Showing
71 changed files
with
2,030 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
using System.Threading.Channels; | ||
|
||
using Devolutions.NowClient.Worker; | ||
|
||
namespace Devolutions.NowClient | ||
{ | ||
/// <summary> | ||
/// Common remote execution parameters for all execution styles. | ||
/// </summary> | ||
public abstract class AExecParams | ||
{ | ||
/// <summary> | ||
/// Callback for stdout data collection. | ||
/// </summary> | ||
public StdoutHandler OnStdout | ||
{ | ||
set | ||
{ | ||
_stdoutHandler = value; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Callback for stderr data collection. | ||
/// </summary> | ||
public StderrHandler OnStderr | ||
{ | ||
set | ||
{ | ||
_stderrHandler = value; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Callback to be called when the execution session starts on the remote host. | ||
/// </summary> | ||
public StartedHandler OnStarted | ||
{ | ||
set | ||
{ | ||
_startedHandler = value; | ||
} | ||
} | ||
|
||
internal ExecSession ToExecSession(uint sessionId, ChannelWriter<IClientCommand> commandWriter) | ||
{ | ||
return new ExecSession( | ||
sessionId, | ||
commandWriter, | ||
_stdoutHandler, | ||
_stderrHandler, | ||
_startedHandler | ||
); | ||
} | ||
|
||
private StdoutHandler? _stdoutHandler; | ||
private StderrHandler? _stderrHandler; | ||
private StartedHandler? _startedHandler; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
<Platforms>x86;x64;ARM64</Platforms> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Devolutions.NowProto\Devolutions.NowProto.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
using Devolutions.NowProto.Messages; | ||
|
||
namespace Devolutions.NowClient | ||
{ | ||
/// <summary> | ||
/// Batch (Windows CMD) remote execution session parameters. | ||
/// </summary> | ||
/// <param name="command">Command/script to execute.</param> | ||
public class ExecBatchParams(string command) : AExecParams | ||
{ | ||
/// <summary> | ||
/// Set the working directory for the command/script. | ||
/// </summary> | ||
public ExecBatchParams Directory(string directory) | ||
{ | ||
_directory = directory; | ||
return this; | ||
} | ||
|
||
internal NowMsgExecBatch ToNowMessage(uint sessionId) | ||
{ | ||
var builder = new NowMsgExecBatch.Builder(sessionId, command); | ||
|
||
if (_directory != null) | ||
{ | ||
builder.Directory(_directory); | ||
} | ||
|
||
return builder.Build(); | ||
} | ||
|
||
private string? _directory = null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
using Devolutions.NowProto.Messages; | ||
|
||
namespace Devolutions.NowClient | ||
{ | ||
/// <summary> | ||
/// Plain process execution session parameters (e.g. CreateProcessW on Windows hosts). | ||
/// </summary> | ||
/// <param name="filename"></param> | ||
public class ExecProcessParams(string filename) : AExecParams | ||
{ | ||
/// <summary> | ||
/// Set the command line parameters for the process. | ||
/// </summary> | ||
public ExecProcessParams Parameters(string parameters) | ||
{ | ||
_parameters = parameters; | ||
return this; | ||
} | ||
|
||
/// <summary> | ||
/// Set the working directory for the process. | ||
/// </summary> | ||
public ExecProcessParams Directory(string directory) | ||
{ | ||
_directory = directory; | ||
return this; | ||
} | ||
|
||
internal NowMsgExecProcess ToNowMessage(uint sessionId) | ||
{ | ||
var builder = new NowMsgExecProcess.Builder(sessionId, filename); | ||
|
||
if (_parameters != null) | ||
{ | ||
builder.Parameters(_parameters); | ||
} | ||
|
||
if (_directory != null) | ||
{ | ||
builder.Directory(_directory); | ||
} | ||
|
||
return builder.Build(); | ||
} | ||
|
||
|
||
private string? _parameters = null; | ||
private string? _directory = null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
using Devolutions.NowProto.Messages; | ||
|
||
namespace Devolutions.NowClient | ||
{ | ||
/// <summary> | ||
/// Powershell 7 and higher (pwsh) remote execution session parameters. | ||
/// </summary> | ||
/// <param name="command">PowerShell command/script to execute.</param> | ||
public class ExecPwshParams(string command) : AExecParams | ||
{ | ||
/// <summary> | ||
/// Set the working directory for the command/script. | ||
/// </summary> | ||
public ExecPwshParams Directory(string directory) | ||
{ | ||
_directory = directory; | ||
return this; | ||
} | ||
|
||
/// <summary> | ||
/// Set the configuration name for the PowerShell session. (-ConfigurationName) | ||
/// </summary> | ||
public ExecPwshParams ConfigurationName(string configurationName) | ||
{ | ||
_configurationName = configurationName; | ||
return this; | ||
} | ||
|
||
/// <summary> | ||
/// Set the execution policy for the PowerShell session. (-ExecutionPolicy) | ||
/// </summary> | ||
public ExecPwshParams ExecutionPolicy(string executionPolicy) | ||
{ | ||
_executionPolicy = executionPolicy; | ||
return this; | ||
} | ||
|
||
/// <summary> | ||
/// Set the apartment state for the PowerShell session. (-Sta/-Mta) | ||
/// </summary> | ||
public ExecPwshParams ApartmentState(NowMsgExecPwsh.ApartmentStateKind apartmentState) | ||
{ | ||
_apartmentState = apartmentState; | ||
return this; | ||
} | ||
|
||
/// <summary> | ||
/// Disable the PowerShell logo display. (-NoLogo) | ||
/// </summary> | ||
public ExecPwshParams NoLogo() | ||
{ | ||
_noLogo = true; | ||
return this; | ||
} | ||
|
||
/// <summary> | ||
/// Do not close the PowerShell session after the command/script execution. (-NoExit) | ||
/// </summary> | ||
public ExecPwshParams NoExit() | ||
{ | ||
_noExit = true; | ||
return this; | ||
} | ||
|
||
/// <summary> | ||
/// Do not load the PowerShell profile. (-NoProfile) | ||
/// </summary> | ||
public ExecPwshParams NoProfile() | ||
{ | ||
_noProfile = true; | ||
return this; | ||
} | ||
|
||
/// <summary> | ||
/// Run the PowerShell session in non-interactive mode. (-NonInteractive) | ||
/// </summary> | ||
public ExecPwshParams NonInteractive() | ||
{ | ||
_nonInteractive = true; | ||
return this; | ||
} | ||
|
||
internal NowMsgExecPwsh ToNowMessage(uint sessionId) | ||
{ | ||
var builder = new NowMsgExecPwsh.Builder(sessionId, command); | ||
|
||
if (_directory != null) | ||
{ | ||
builder.Directory(_directory); | ||
} | ||
|
||
if (_configurationName != null) | ||
{ | ||
builder.ConfigurationName(_configurationName); | ||
} | ||
|
||
if (_executionPolicy != null) | ||
{ | ||
builder.ExecutionPolicy(_executionPolicy); | ||
} | ||
|
||
if (_apartmentState != null) | ||
{ | ||
builder.ApartmentState(_apartmentState.Value); | ||
} | ||
|
||
if (_noLogo) | ||
{ | ||
builder.SetNoLogo(); | ||
} | ||
|
||
if (_noExit) | ||
{ | ||
builder.SetNoExit(); | ||
} | ||
|
||
if (_noProfile) | ||
{ | ||
builder.SetNoProfile(); | ||
} | ||
|
||
if (_nonInteractive) | ||
{ | ||
builder.SetNonInteractive(); | ||
} | ||
|
||
return builder.Build(); | ||
} | ||
|
||
|
||
private string? _configurationName = null; | ||
private string? _executionPolicy = null; | ||
private string? _directory = null; | ||
private NowMsgExecPwsh.ApartmentStateKind? _apartmentState = null; | ||
private bool _noLogo = false; | ||
private bool _noExit = false; | ||
private bool _noProfile = false; | ||
private bool _nonInteractive = false; | ||
} | ||
} |
Oops, something went wrong.