Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/cli/src/tunnels/protocol.rs
3314 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
use std::collections::HashMap;
6
7
use crate::{
8
constants::{PROTOCOL_VERSION, VSCODE_CLI_VERSION},
9
options::Quality,
10
update_service::Platform,
11
};
12
use serde::{Deserialize, Serialize};
13
14
#[derive(Serialize, Debug)]
15
#[serde(tag = "method", content = "params", rename_all = "camelCase")]
16
#[allow(non_camel_case_types)]
17
pub enum ClientRequestMethod<'a> {
18
servermsg(RefServerMessageParams<'a>),
19
serverclose(ServerClosedParams),
20
serverlog(ServerLog<'a>),
21
makehttpreq(HttpRequestParams<'a>),
22
version(VersionResponse),
23
}
24
25
#[derive(Deserialize, Debug)]
26
pub struct HttpBodyParams {
27
#[serde(with = "serde_bytes")]
28
pub segment: Vec<u8>,
29
pub complete: bool,
30
pub req_id: u32,
31
}
32
33
#[derive(Serialize, Debug)]
34
pub struct HttpRequestParams<'a> {
35
pub url: &'a str,
36
pub method: &'static str,
37
pub req_id: u32,
38
}
39
40
#[derive(Deserialize, Debug)]
41
pub struct HttpHeadersParams {
42
pub status_code: u16,
43
pub headers: Vec<(String, String)>,
44
pub req_id: u32,
45
}
46
47
#[derive(Deserialize, Debug)]
48
pub struct ForwardParams {
49
pub port: u16,
50
#[serde(default)]
51
pub public: bool,
52
}
53
54
#[derive(Deserialize, Debug)]
55
pub struct UnforwardParams {
56
pub port: u16,
57
}
58
59
#[derive(Serialize)]
60
pub struct ForwardResult {
61
pub uri: String,
62
}
63
64
#[derive(Deserialize, Debug)]
65
pub struct ServeParams {
66
pub socket_id: u16,
67
pub commit_id: Option<String>,
68
pub quality: Quality,
69
pub extensions: Vec<String>,
70
/// Optional preferred connection token.
71
#[serde(default)]
72
pub connection_token: Option<String>,
73
#[serde(default)]
74
pub use_local_download: bool,
75
/// If true, the client and server should gzip servermsg's sent in either direction.
76
#[serde(default)]
77
pub compress: bool,
78
}
79
80
#[derive(Deserialize, Serialize, Debug)]
81
pub struct EmptyObject {}
82
83
#[derive(Serialize, Deserialize, Debug)]
84
pub struct UpdateParams {
85
pub do_update: bool,
86
}
87
88
#[derive(Deserialize, Debug)]
89
pub struct ServerMessageParams {
90
pub i: u16,
91
#[serde(with = "serde_bytes")]
92
pub body: Vec<u8>,
93
}
94
95
#[derive(Serialize, Debug)]
96
pub struct ServerClosedParams {
97
pub i: u16,
98
}
99
100
#[derive(Serialize, Debug)]
101
pub struct RefServerMessageParams<'a> {
102
pub i: u16,
103
#[serde(with = "serde_bytes")]
104
pub body: &'a [u8],
105
}
106
107
#[derive(Serialize)]
108
pub struct UpdateResult {
109
pub up_to_date: bool,
110
pub did_update: bool,
111
}
112
113
#[derive(Serialize, Debug)]
114
pub struct ToClientRequest<'a> {
115
pub id: Option<u32>,
116
#[serde(flatten)]
117
pub params: ClientRequestMethod<'a>,
118
}
119
120
#[derive(Debug, Default, Serialize)]
121
pub struct ServerLog<'a> {
122
pub line: &'a str,
123
pub level: u8,
124
}
125
126
#[derive(Serialize)]
127
pub struct GetHostnameResponse {
128
pub value: String,
129
}
130
131
#[derive(Serialize)]
132
pub struct GetEnvResponse {
133
pub env: HashMap<String, String>,
134
pub os_platform: &'static str,
135
pub os_release: String,
136
}
137
138
/// Method: `kill`. Sends a generic, platform-specific kill command to the process.
139
#[derive(Deserialize)]
140
pub struct SysKillRequest {
141
pub pid: u32,
142
}
143
144
#[derive(Serialize)]
145
pub struct SysKillResponse {
146
pub success: bool,
147
}
148
149
/// Methods: `fs_read`/`fs_write`/`fs_rm`/`fs_mkdirp`/`fs_stat`
150
/// - fs_read: reads into a stream returned from the method,
151
/// - fs_write: writes from a stream passed to the method.
152
/// - fs_rm: recursively removes the file
153
/// - fs_mkdirp: recursively creates the directory
154
/// - fs_readdir: reads directory contents
155
/// - fs_stat: stats the given path
156
/// - fs_connect: connect to the given unix or named pipe socket, streaming
157
/// data in and out from the method's stream.
158
#[derive(Deserialize)]
159
pub struct FsSinglePathRequest {
160
pub path: String,
161
}
162
163
#[derive(Serialize)]
164
pub enum FsFileKind {
165
#[serde(rename = "dir")]
166
Directory,
167
#[serde(rename = "file")]
168
File,
169
#[serde(rename = "link")]
170
Link,
171
}
172
173
impl From<std::fs::FileType> for FsFileKind {
174
fn from(kind: std::fs::FileType) -> Self {
175
if kind.is_dir() {
176
Self::Directory
177
} else if kind.is_file() {
178
Self::File
179
} else if kind.is_symlink() {
180
Self::Link
181
} else {
182
unreachable!()
183
}
184
}
185
}
186
187
#[derive(Serialize, Default)]
188
pub struct FsStatResponse {
189
pub exists: bool,
190
pub size: Option<u64>,
191
#[serde(rename = "type")]
192
pub kind: Option<FsFileKind>,
193
}
194
195
#[derive(Serialize)]
196
pub struct FsReadDirResponse {
197
pub contents: Vec<FsReadDirEntry>,
198
}
199
200
#[derive(Serialize)]
201
pub struct FsReadDirEntry {
202
pub name: String,
203
#[serde(rename = "type")]
204
pub kind: Option<FsFileKind>,
205
}
206
207
/// Method: `fs_reaname`. Renames a file.
208
#[derive(Deserialize)]
209
pub struct FsRenameRequest {
210
pub from_path: String,
211
pub to_path: String,
212
}
213
214
/// Method: `net_connect`. Connects to a port.
215
#[derive(Deserialize)]
216
pub struct NetConnectRequest {
217
pub port: u16,
218
pub host: String,
219
}
220
221
#[derive(Deserialize, Debug)]
222
pub struct CallServerHttpParams {
223
pub path: String,
224
pub method: String,
225
pub headers: HashMap<String, String>,
226
pub body: Option<Vec<u8>>,
227
}
228
229
#[derive(Serialize)]
230
pub struct CallServerHttpResult {
231
pub status: u16,
232
#[serde(with = "serde_bytes")]
233
pub body: Vec<u8>,
234
pub headers: HashMap<String, String>,
235
}
236
237
#[derive(Serialize, Debug)]
238
pub struct VersionResponse {
239
pub version: &'static str,
240
pub protocol_version: u32,
241
}
242
243
impl Default for VersionResponse {
244
fn default() -> Self {
245
Self {
246
version: VSCODE_CLI_VERSION.unwrap_or("dev"),
247
protocol_version: PROTOCOL_VERSION,
248
}
249
}
250
}
251
252
#[derive(Deserialize)]
253
pub struct SpawnParams {
254
pub command: String,
255
pub args: Vec<String>,
256
#[serde(default)]
257
pub cwd: Option<String>,
258
#[serde(default)]
259
pub env: HashMap<String, String>,
260
}
261
262
#[derive(Deserialize)]
263
pub struct AcquireCliParams {
264
pub platform: Platform,
265
pub quality: Quality,
266
pub commit_id: Option<String>,
267
#[serde(flatten)]
268
pub spawn: SpawnParams,
269
}
270
271
#[derive(Serialize)]
272
pub struct SpawnResult {
273
pub message: String,
274
pub exit_code: i32,
275
}
276
277
pub const METHOD_CHALLENGE_ISSUE: &str = "challenge_issue";
278
pub const METHOD_CHALLENGE_VERIFY: &str = "challenge_verify";
279
280
#[derive(Serialize, Deserialize)]
281
pub struct ChallengeIssueParams {
282
pub token: Option<String>,
283
}
284
285
#[derive(Serialize, Deserialize)]
286
pub struct ChallengeIssueResponse {
287
pub challenge: String,
288
}
289
290
#[derive(Deserialize, Serialize)]
291
pub struct ChallengeVerifyParams {
292
pub response: String,
293
}
294
295
#[derive(Serialize, Deserialize, PartialEq, Eq, Copy, Clone, Debug)]
296
#[serde(rename_all = "lowercase")]
297
pub enum PortPrivacy {
298
Public,
299
Private,
300
}
301
302
#[derive(Serialize, Deserialize, PartialEq, Copy, Eq, Clone, Debug)]
303
#[serde(rename_all = "lowercase")]
304
pub enum PortProtocol {
305
Auto,
306
Http,
307
Https,
308
}
309
310
impl std::fmt::Display for PortProtocol {
311
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
312
write!(f, "{}", self.to_contract_str())
313
}
314
}
315
316
impl Default for PortProtocol {
317
fn default() -> Self {
318
Self::Auto
319
}
320
}
321
322
impl PortProtocol {
323
pub fn to_contract_str(&self) -> &'static str {
324
match *self {
325
Self::Auto => tunnels::contracts::TUNNEL_PROTOCOL_AUTO,
326
Self::Http => tunnels::contracts::TUNNEL_PROTOCOL_HTTP,
327
Self::Https => tunnels::contracts::TUNNEL_PROTOCOL_HTTPS,
328
}
329
}
330
}
331
332
pub mod forward_singleton {
333
use serde::{Deserialize, Serialize};
334
335
use super::{PortPrivacy, PortProtocol};
336
337
pub const METHOD_SET_PORTS: &str = "set_ports";
338
339
#[derive(Serialize, Deserialize, PartialEq, Eq, Clone, Debug)]
340
pub struct PortRec {
341
pub number: u16,
342
pub privacy: PortPrivacy,
343
pub protocol: PortProtocol,
344
}
345
346
pub type PortList = Vec<PortRec>;
347
348
#[derive(Serialize, Deserialize)]
349
pub struct SetPortsParams {
350
pub ports: PortList,
351
}
352
353
#[derive(Serialize, Deserialize)]
354
pub struct SetPortsResponse {
355
pub port_format: Option<String>,
356
}
357
}
358
359
pub mod singleton {
360
use crate::log;
361
use chrono::{DateTime, Utc};
362
use serde::{Deserialize, Serialize};
363
364
pub const METHOD_RESTART: &str = "restart";
365
pub const METHOD_SHUTDOWN: &str = "shutdown";
366
pub const METHOD_STATUS: &str = "status";
367
pub const METHOD_LOG: &str = "log";
368
pub const METHOD_LOG_REPLY_DONE: &str = "log_done";
369
370
#[derive(Serialize)]
371
pub struct LogMessage<'a> {
372
pub level: Option<log::Level>,
373
pub prefix: &'a str,
374
pub message: &'a str,
375
}
376
377
#[derive(Deserialize)]
378
pub struct LogMessageOwned {
379
pub level: Option<log::Level>,
380
pub prefix: String,
381
pub message: String,
382
}
383
384
#[derive(Serialize, Deserialize, Clone, Default)]
385
pub struct StatusWithTunnelName {
386
pub name: Option<String>,
387
#[serde(flatten)]
388
pub status: Status,
389
}
390
391
#[derive(Serialize, Deserialize, Clone)]
392
pub struct Status {
393
pub started_at: DateTime<Utc>,
394
pub tunnel: TunnelState,
395
pub last_connected_at: Option<DateTime<Utc>>,
396
pub last_disconnected_at: Option<DateTime<Utc>>,
397
pub last_fail_reason: Option<String>,
398
}
399
400
impl Default for Status {
401
fn default() -> Self {
402
Self {
403
started_at: Utc::now(),
404
tunnel: TunnelState::Disconnected,
405
last_connected_at: None,
406
last_disconnected_at: None,
407
last_fail_reason: None,
408
}
409
}
410
}
411
412
#[derive(Deserialize, Serialize, Debug)]
413
pub struct LogReplayFinished {}
414
415
#[derive(Deserialize, Serialize, Debug, Default, Clone)]
416
pub enum TunnelState {
417
#[default]
418
Disconnected,
419
Connected,
420
}
421
}
422
423