forked from InftyAI/SandD
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprotocol.rs
More file actions
146 lines (138 loc) · 3.39 KB
/
Copy pathprotocol.rs
File metadata and controls
146 lines (138 loc) · 3.39 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
// Re-export the protocol from server crate for consistency
// In production, you'd want a shared protocol crate
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum Message {
Register {
daemon_id: String,
metadata: DaemonMetadata,
},
RegisterAck {
success: bool,
message: String,
},
Heartbeat,
Pong,
ExecuteCommand {
request_id: String,
command: String,
#[serde(default = "default_timeout")]
timeout_secs: u64,
#[serde(default)]
env: std::collections::HashMap<String, String>,
#[serde(default)]
cwd: Option<String>,
},
CommandOutput {
request_id: String,
stdout: String,
stderr: String,
exit_code: i32,
duration_ms: u64,
},
CommandError {
request_id: String,
error: String,
},
StartShell {
request_id: String,
rows: u16,
cols: u16,
#[serde(default = "default_term")]
term: String,
},
ShellStarted {
request_id: String,
success: bool,
error: Option<String>,
},
ShellInput {
request_id: String,
#[serde(with = "base64_bytes")]
data: Vec<u8>,
},
ShellOutput {
request_id: String,
#[serde(with = "base64_bytes")]
data: Vec<u8>,
},
ShellResize {
request_id: String,
rows: u16,
cols: u16,
},
ShellExit {
request_id: String,
exit_code: i32,
},
FileUploadStart {
request_id: String,
path: String,
total_size: u64,
#[serde(default)]
mode: Option<u32>,
},
FileUploadChunk {
request_id: String,
#[serde(with = "base64_bytes")]
data: Vec<u8>,
offset: u64,
},
FileUploadComplete {
request_id: String,
success: bool,
error: Option<String>,
},
FileDownloadStart {
request_id: String,
path: String,
},
FileDownloadChunk {
request_id: String,
#[serde(with = "base64_bytes")]
data: Vec<u8>,
offset: u64,
is_last: bool,
},
FileDownloadError {
request_id: String,
error: String,
},
Error {
message: String,
#[serde(default)]
recoverable: bool,
},
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DaemonMetadata {
pub hostname: String,
pub platform: String,
pub arch: String,
#[serde(default)]
pub version: String,
#[serde(default)]
pub labels: std::collections::HashMap<String, String>,
}
fn default_timeout() -> u64 {
300
}
fn default_term() -> String {
"xterm-256color".to_string()
}
mod base64_bytes {
use serde::{Deserialize, Deserializer, Serializer};
pub fn serialize<S: Serializer>(v: &Vec<u8>, s: S) -> Result<S::Ok, S::Error> {
use base64::Engine;
let base64 = base64::engine::general_purpose::STANDARD.encode(v);
s.serialize_str(&base64)
}
pub fn deserialize<'de, D: Deserializer<'de>>(d: D) -> Result<Vec<u8>, D::Error> {
use base64::Engine;
let base64 = String::deserialize(d)?;
base64::engine::general_purpose::STANDARD
.decode(base64.as_bytes())
.map_err(serde::de::Error::custom)
}
}