|
| 1 | +use core::time::Duration; |
| 2 | + |
| 3 | +use now_proto_pdu::{NowChannelCapsetMsg, NowExecCapsetFlags, NowProtoVersion}; |
| 4 | + |
| 5 | +use crate::NowClientError; |
| 6 | + |
| 7 | +/// Capabilities mutually supported by the client and its connected peer. |
| 8 | +/// |
| 9 | +/// The value is always calculated as an intersection with the local advertised capset, |
| 10 | +/// even if the peer echoes unsupported flags. |
| 11 | +#[derive(Clone, Debug, PartialEq, Eq)] |
| 12 | +pub struct NowCapabilities { |
| 13 | + capset: NowChannelCapsetMsg, |
| 14 | +} |
| 15 | + |
| 16 | +impl NowCapabilities { |
| 17 | + pub(crate) fn negotiate( |
| 18 | + requested: &NowChannelCapsetMsg, |
| 19 | + peer: &NowChannelCapsetMsg, |
| 20 | + ) -> Result<Self, NowClientError> { |
| 21 | + if requested.version().major != peer.version().major { |
| 22 | + return Err(NowClientError::IncompatibleVersion { |
| 23 | + client: requested.version(), |
| 24 | + peer: peer.version(), |
| 25 | + }); |
| 26 | + } |
| 27 | + |
| 28 | + Ok(Self { |
| 29 | + capset: requested.downgrade(peer), |
| 30 | + }) |
| 31 | + } |
| 32 | + |
| 33 | + /// Returns the negotiated capability-set PDU. |
| 34 | + pub fn capset(&self) -> &NowChannelCapsetMsg { |
| 35 | + &self.capset |
| 36 | + } |
| 37 | + |
| 38 | + /// Returns the negotiated NOW protocol version. |
| 39 | + pub fn version(&self) -> NowProtoVersion { |
| 40 | + self.capset.version() |
| 41 | + } |
| 42 | + |
| 43 | + /// Returns the negotiated heartbeat interval, if either peer requested one. |
| 44 | + pub fn heartbeat_interval(&self) -> Option<Duration> { |
| 45 | + self.capset.heartbeat_interval() |
| 46 | + } |
| 47 | + |
| 48 | + /// Returns whether the generic Run style is available. |
| 49 | + pub fn supports_run(&self) -> bool { |
| 50 | + self.has(NowExecCapsetFlags::STYLE_RUN) |
| 51 | + } |
| 52 | + |
| 53 | + /// Returns whether CreateProcess execution is available. |
| 54 | + pub fn supports_process(&self) -> bool { |
| 55 | + self.has(NowExecCapsetFlags::STYLE_PROCESS) |
| 56 | + } |
| 57 | + |
| 58 | + /// Returns whether Batch execution is available. |
| 59 | + pub fn supports_batch(&self) -> bool { |
| 60 | + self.has(NowExecCapsetFlags::STYLE_BATCH) |
| 61 | + } |
| 62 | + |
| 63 | + /// Returns whether Windows PowerShell execution is available. |
| 64 | + pub fn supports_win_ps(&self) -> bool { |
| 65 | + self.has(NowExecCapsetFlags::STYLE_WINPS) |
| 66 | + } |
| 67 | + |
| 68 | + /// Returns whether PowerShell 7 execution is available. |
| 69 | + pub fn supports_pwsh(&self) -> bool { |
| 70 | + self.has(NowExecCapsetFlags::STYLE_PWSH) |
| 71 | + } |
| 72 | + |
| 73 | + /// Returns whether tracked I/O redirection is available. |
| 74 | + pub fn supports_io_redirection(&self) -> bool { |
| 75 | + self.has(NowExecCapsetFlags::IO_REDIRECTION) && self.at_least(1, 3) |
| 76 | + } |
| 77 | + |
| 78 | + /// Returns whether UTF-8 and Unicode-console encoding controls are available. |
| 79 | + pub fn supports_unicode_console(&self) -> bool { |
| 80 | + self.has(NowExecCapsetFlags::UNICODE_CONSOLE) && self.version().supports_exec_unicode_console() |
| 81 | + } |
| 82 | + |
| 83 | + pub(crate) fn supports_detached(&self) -> bool { |
| 84 | + self.at_least(1, 4) |
| 85 | + } |
| 86 | + |
| 87 | + fn has(&self, capability: NowExecCapsetFlags) -> bool { |
| 88 | + self.capset.exec_capset().contains(capability) |
| 89 | + } |
| 90 | + |
| 91 | + fn at_least(&self, major: u16, minor: u16) -> bool { |
| 92 | + self.version() >= NowProtoVersion { major, minor } |
| 93 | + } |
| 94 | +} |
0 commit comments