use serde::{Deserialize, Serialize};
pub const CHANNEL_PREFIX: &str = "cmd";
pub fn is_command_channel(channel: &str) -> bool {
channel.starts_with(CHANNEL_PREFIX)
}
pub const MAX_FRAME: usize = 256 * 1024;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum ToMachine {
Hello,
Command {
sealed: Vec<u8>,
},
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum FromMachine {
Challenge {
nonce: Vec<u8>,
key: Vec<u8>,
signature: Vec<u8>,
},
Reply {
sealed: Vec<u8>,
signature: Vec<u8>,
},
Refused {
reason: String,
},
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum Command {
List,
New {
name: String,
},
Kill {
name: String,
},
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Signed {
pub command: Command,
pub signature: Vec<u8>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum Outcome {
Done,
Sessions {
sessions: Vec<SessionInfo>,
},
Failed {
reason: String,
},
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct SessionInfo {
pub name: String,
pub id: String,
pub running: bool,
#[serde(default)]
pub size: Option<(u16, u16)>,
}
pub fn challenge_bytes(machine: &str, nonce: &[u8], key: &[u8]) -> Vec<u8> {
let mut bytes = b"teter-challenge-v1 ".to_vec();
bytes.extend_from_slice(machine.as_bytes());
bytes.push(0);
bytes.extend_from_slice(nonce);
bytes.extend_from_slice(key);
bytes
}
pub fn command_bytes(machine: &str, nonce: &[u8], command: &Command) -> Vec<u8> {
let mut bytes = b"teter-command-v1 ".to_vec();
bytes.extend_from_slice(machine.as_bytes());
bytes.push(0);
bytes.extend_from_slice(nonce);
bytes.extend_from_slice(&serde_json::to_vec(command).unwrap_or_default());
bytes
}
pub fn reply_bytes(machine: &str, nonce: &[u8], sealed: &[u8]) -> Vec<u8> {
let mut bytes = b"teter-reply-v1 ".to_vec();
bytes.extend_from_slice(machine.as_bytes());
bytes.push(0);
bytes.extend_from_slice(nonce);
bytes.extend_from_slice(sealed);
bytes
}
pub fn command_info(machine: &str, nonce: &[u8]) -> Vec<u8> {
let mut info = b"command ".to_vec();
info.extend_from_slice(machine.as_bytes());
info.push(b' ');
info.extend_from_slice(crate::account::hex(nonce).as_bytes());
info
}
pub fn reply_info(machine: &str, nonce: &[u8]) -> Vec<u8> {
let mut info = b"command reply ".to_vec();
info.extend_from_slice(machine.as_bytes());
info.push(b' ');
info.extend_from_slice(crate::account::hex(nonce).as_bytes());
info
}