-
Notifications
You must be signed in to change notification settings - Fork 29.9k
/
tunnels.rs
613 lines (546 loc) · 16.3 KB
/
tunnels.rs
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
use async_trait::async_trait;
use base64::{engine::general_purpose as b64, Engine as _};
use futures::{stream::FuturesUnordered, StreamExt};
use serde::Serialize;
use sha2::{Digest, Sha256};
use std::{str::FromStr, time::Duration};
use sysinfo::Pid;
use tokio::{
io::{AsyncBufReadExt, BufReader},
sync::watch,
};
use super::{
args::{
AuthProvider, CliCore, CommandShellArgs, ExistingTunnelArgs, TunnelForwardArgs,
TunnelRenameArgs, TunnelServeArgs, TunnelServiceSubCommands, TunnelUserSubCommands,
},
CommandContext,
};
use crate::{
async_pipe::{get_socket_name, listen_socket_rw_stream, AsyncRWAccepter},
auth::Auth,
constants::{
APPLICATION_NAME, CONTROL_PORT, IS_A_TTY, TUNNEL_CLI_LOCK_NAME, TUNNEL_SERVICE_LOCK_NAME,
},
log,
state::LauncherPaths,
tunnels::{
code_server::CodeServerArgs,
create_service_manager,
dev_tunnels::{self, DevTunnels},
forwarding, legal,
paths::get_all_servers,
protocol, serve_stream,
shutdown_signal::ShutdownRequest,
singleton_client::do_single_rpc_call,
singleton_server::{
make_singleton_server, start_singleton_server, BroadcastLogSink, SingletonServerArgs,
},
AuthRequired, Next, ServeStreamParams, ServiceContainer, ServiceManager,
},
util::{
app_lock::AppMutex,
errors::{wrap, AnyError, CodeError},
prereqs::PreReqChecker,
},
};
use crate::{
singleton::{acquire_singleton, SingletonConnection},
tunnels::{
dev_tunnels::ActiveTunnel,
singleton_client::{start_singleton_client, SingletonClientArgs},
SleepInhibitor,
},
};
impl From<AuthProvider> for crate::auth::AuthProvider {
fn from(auth_provider: AuthProvider) -> Self {
match auth_provider {
AuthProvider::Github => crate::auth::AuthProvider::Github,
AuthProvider::Microsoft => crate::auth::AuthProvider::Microsoft,
}
}
}
fn fulfill_existing_tunnel_args(
d: ExistingTunnelArgs,
name_arg: &Option<String>,
) -> Option<dev_tunnels::ExistingTunnel> {
let tunnel_name = d.tunnel_name.or_else(|| name_arg.clone());
match (d.tunnel_id, d.cluster, d.host_token) {
(Some(tunnel_id), None, Some(host_token)) => {
let i = tunnel_id.find('.')?;
Some(dev_tunnels::ExistingTunnel {
tunnel_id: tunnel_id[..i].to_string(),
cluster: tunnel_id[i + 1..].to_string(),
tunnel_name,
host_token,
})
}
(Some(tunnel_id), Some(cluster), Some(host_token)) => Some(dev_tunnels::ExistingTunnel {
tunnel_id,
tunnel_name,
host_token,
cluster,
}),
_ => None,
}
}
struct TunnelServiceContainer {
args: CliCore,
}
impl TunnelServiceContainer {
fn new(args: CliCore) -> Self {
Self { args }
}
}
#[async_trait]
impl ServiceContainer for TunnelServiceContainer {
async fn run_service(
&mut self,
log: log::Logger,
launcher_paths: LauncherPaths,
) -> Result<(), AnyError> {
let csa = (&self.args).into();
serve_with_csa(
launcher_paths,
log,
TunnelServeArgs {
random_name: true, // avoid prompting
..Default::default()
},
csa,
TUNNEL_SERVICE_LOCK_NAME,
)
.await?;
Ok(())
}
}
pub async fn command_shell(ctx: CommandContext, args: CommandShellArgs) -> Result<i32, AnyError> {
let platform = PreReqChecker::new().verify().await?;
let mut params = ServeStreamParams {
log: ctx.log,
launcher_paths: ctx.paths,
platform,
requires_auth: args
.require_token
.map(AuthRequired::VSDAWithToken)
.unwrap_or(AuthRequired::VSDA),
exit_barrier: ShutdownRequest::create_rx([ShutdownRequest::CtrlC]),
code_server_args: (&ctx.args).into(),
};
let mut listener: Box<dyn AsyncRWAccepter> = match (args.on_port, args.on_socket) {
(_, true) => {
let socket = get_socket_name();
let listener = listen_socket_rw_stream(&socket)
.await
.map_err(|e| wrap(e, "error listening on socket"))?;
params
.log
.result(format!("Listening on {}", socket.display()));
Box::new(listener)
}
(true, _) => {
let listener = tokio::net::TcpListener::bind("127.0.0.1:0")
.await
.map_err(|e| wrap(e, "error listening on port"))?;
params
.log
.result(format!("Listening on {}", listener.local_addr().unwrap()));
Box::new(listener)
}
_ => {
serve_stream(tokio::io::stdin(), tokio::io::stderr(), params).await;
return Ok(0);
}
};
let mut servers = FuturesUnordered::new();
loop {
tokio::select! {
Some(_) = servers.next() => {},
socket = listener.accept_rw() => {
match socket {
Ok((read, write)) => servers.push(serve_stream(read, write, params.clone())),
Err(e) => {
error!(params.log, &format!("Error accepting connection: {}", e));
return Ok(1);
}
}
},
_ = params.exit_barrier.wait() => {
// wait for all servers to finish up:
while (servers.next().await).is_some() { }
return Ok(0);
}
}
}
}
pub async fn service(
ctx: CommandContext,
service_args: TunnelServiceSubCommands,
) -> Result<i32, AnyError> {
let manager = create_service_manager(ctx.log.clone(), &ctx.paths);
match service_args {
TunnelServiceSubCommands::Install(args) => {
let auth = Auth::new(&ctx.paths, ctx.log.clone());
if let Some(name) = &args.name {
// ensure the name matches, and tunnel exists
dev_tunnels::DevTunnels::new_remote_tunnel(&ctx.log, auth, &ctx.paths)
.rename_tunnel(name)
.await?;
} else {
// still ensure they're logged in, otherwise subsequent serving will fail
auth.get_credential().await?;
}
// likewise for license consent
legal::require_consent(&ctx.paths, args.accept_server_license_terms)?;
let current_exe =
std::env::current_exe().map_err(|e| wrap(e, "could not get current exe"))?;
manager
.register(
current_exe,
&[
"--verbose",
"--cli-data-dir",
ctx.paths.root().as_os_str().to_string_lossy().as_ref(),
"tunnel",
"service",
"internal-run",
],
)
.await?;
ctx.log.result(format!("Service successfully installed! You can use `{} tunnel service log` to monitor it, and `{} tunnel service uninstall` to remove it.", APPLICATION_NAME, APPLICATION_NAME));
}
TunnelServiceSubCommands::Uninstall => {
manager.unregister().await?;
}
TunnelServiceSubCommands::Log => {
manager.show_logs().await?;
}
TunnelServiceSubCommands::InternalRun => {
manager
.run(ctx.paths.clone(), TunnelServiceContainer::new(ctx.args))
.await?;
}
}
Ok(0)
}
pub async fn user(ctx: CommandContext, user_args: TunnelUserSubCommands) -> Result<i32, AnyError> {
let auth = Auth::new(&ctx.paths, ctx.log.clone());
match user_args {
TunnelUserSubCommands::Login(login_args) => {
auth.login(
login_args.provider.map(|p| p.into()),
login_args.access_token.to_owned(),
)
.await?;
}
TunnelUserSubCommands::Logout => {
auth.clear_credentials()?;
}
TunnelUserSubCommands::Show => {
if let Ok(Some(_)) = auth.get_current_credential() {
ctx.log.result("logged in");
} else {
ctx.log.result("not logged in");
return Ok(1);
}
}
}
Ok(0)
}
/// Remove the tunnel used by this tunnel, if any.
pub async fn rename(ctx: CommandContext, rename_args: TunnelRenameArgs) -> Result<i32, AnyError> {
let auth = Auth::new(&ctx.paths, ctx.log.clone());
let mut dt = dev_tunnels::DevTunnels::new_remote_tunnel(&ctx.log, auth, &ctx.paths);
dt.rename_tunnel(&rename_args.name).await?;
ctx.log.result(format!(
"Successfully renamed this tunnel to {}",
&rename_args.name
));
Ok(0)
}
/// Remove the tunnel used by this tunnel, if any.
pub async fn unregister(ctx: CommandContext) -> Result<i32, AnyError> {
let auth = Auth::new(&ctx.paths, ctx.log.clone());
let mut dt = dev_tunnels::DevTunnels::new_remote_tunnel(&ctx.log, auth, &ctx.paths);
dt.remove_tunnel().await?;
Ok(0)
}
pub async fn restart(ctx: CommandContext) -> Result<i32, AnyError> {
do_single_rpc_call::<_, ()>(
&ctx.paths.tunnel_lockfile(),
ctx.log,
protocol::singleton::METHOD_RESTART,
protocol::EmptyObject {},
)
.await
.map(|_| 0)
.map_err(|e| e.into())
}
pub async fn kill(ctx: CommandContext) -> Result<i32, AnyError> {
do_single_rpc_call::<_, ()>(
&ctx.paths.tunnel_lockfile(),
ctx.log,
protocol::singleton::METHOD_SHUTDOWN,
protocol::EmptyObject {},
)
.await
.map(|_| 0)
.map_err(|e| e.into())
}
#[derive(Serialize)]
pub struct StatusOutput {
pub tunnel: Option<protocol::singleton::TunnelState>,
pub service_installed: bool,
}
pub async fn status(ctx: CommandContext) -> Result<i32, AnyError> {
let tunnel_status = do_single_rpc_call::<_, protocol::singleton::Status>(
&ctx.paths.tunnel_lockfile(),
ctx.log.clone(),
protocol::singleton::METHOD_STATUS,
protocol::EmptyObject {},
)
.await;
let service_installed = create_service_manager(ctx.log.clone(), &ctx.paths)
.is_installed()
.await
.unwrap_or(false);
ctx.log.result(
serde_json::to_string(&StatusOutput {
service_installed,
tunnel: match tunnel_status {
Ok(s) => Some(s.tunnel),
Err(CodeError::NoRunningTunnel) => None,
Err(e) => return Err(e.into()),
},
})
.unwrap(),
);
Ok(0)
}
/// Removes unused servers.
pub async fn prune(ctx: CommandContext) -> Result<i32, AnyError> {
get_all_servers(&ctx.paths)
.into_iter()
.map(|s| s.server_paths(&ctx.paths))
.filter(|s| s.get_running_pid().is_none())
.try_for_each(|s| {
ctx.log
.result(format!("Deleted {}", s.server_dir.display()));
s.delete()
})
.map_err(AnyError::from)?;
ctx.log.result("Successfully removed all unused servers");
Ok(0)
}
/// Starts the gateway server.
pub async fn serve(ctx: CommandContext, gateway_args: TunnelServeArgs) -> Result<i32, AnyError> {
let CommandContext {
log, paths, args, ..
} = ctx;
let no_sleep = match gateway_args.no_sleep.then(SleepInhibitor::new) {
Some(i) => match i.await {
Ok(i) => Some(i),
Err(e) => {
warning!(log, "Could not inhibit sleep: {}", e);
None
}
},
None => None,
};
legal::require_consent(&paths, gateway_args.accept_server_license_terms)?;
let csa = (&args).into();
let result = serve_with_csa(paths, log, gateway_args, csa, TUNNEL_CLI_LOCK_NAME).await;
drop(no_sleep);
result
}
/// Internal command used by port forwarding. It reads requests for forwarded ports
/// on lines from stdin, as JSON. It uses singleton logic as well (though on
/// a different tunnel than the main one used for the control server) so that
/// all forward requests on a single machine go through a single hosted tunnel
/// process. Without singleton logic, requests could get routed to processes
/// that aren't forwarding a given port and then fail.
pub async fn forward(
ctx: CommandContext,
mut forward_args: TunnelForwardArgs,
) -> Result<i32, AnyError> {
// Spooky: check IS_A_TTY before starting the stdin reader, since IS_A_TTY will
// access stdin but a lock will later be held on stdin by the line-reader.
if *IS_A_TTY {
trace!(ctx.log, "port forwarding is an internal preview feature");
}
// #region stdin reading logic:
let (own_ports_tx, own_ports_rx) = watch::channel(vec![]);
let ports_process_log = ctx.log.clone();
tokio::spawn(async move {
let mut lines = BufReader::new(tokio::io::stdin()).lines();
while let Ok(Some(line)) = lines.next_line().await {
match serde_json::from_str(&line) {
Ok(p) => {
let _ = own_ports_tx.send(p);
}
Err(e) => warning!(ports_process_log, "error parsing ports: {}", e),
}
}
});
// #region singleton acquisition
let shutdown = ShutdownRequest::create_rx([ShutdownRequest::CtrlC]);
let server = loop {
if shutdown.is_open() {
return Ok(0);
}
match acquire_singleton(&ctx.paths.forwarding_lockfile()).await {
Ok(SingletonConnection::Client(stream)) => {
debug!(ctx.log, "starting as client to singleton");
let r = forwarding::client(forwarding::SingletonClientArgs {
log: ctx.log.clone(),
shutdown: shutdown.clone(),
stream,
port_requests: own_ports_rx.clone(),
})
.await;
if let Err(e) = r {
warning!(ctx.log, "error contacting forwarding singleton: {}", e);
}
}
Ok(SingletonConnection::Singleton(server)) => break server,
Err(e) => {
warning!(ctx.log, "error access singleton, retrying: {}", e);
tokio::time::sleep(Duration::from_secs(2)).await
}
}
};
// #region singleton handler
let auth = Auth::new(&ctx.paths, ctx.log.clone());
if let (Some(p), Some(at)) = (
forward_args.login.provider.take(),
forward_args.login.access_token.take(),
) {
auth.login(Some(p.into()), Some(at)).await?;
}
let mut tunnels = DevTunnels::new_port_forwarding(&ctx.log, auth, &ctx.paths);
let tunnel = tunnels
.start_new_launcher_tunnel(None, true, &forward_args.ports)
.await?;
forwarding::server(ctx.log, tunnel, server, own_ports_rx, shutdown).await?;
Ok(0)
}
fn get_connection_token(tunnel: &ActiveTunnel) -> String {
let mut hash = Sha256::new();
hash.update(tunnel.id.as_bytes());
let result = hash.finalize();
b64::URL_SAFE_NO_PAD.encode(result)
}
async fn serve_with_csa(
paths: LauncherPaths,
mut log: log::Logger,
gateway_args: TunnelServeArgs,
mut csa: CodeServerArgs,
app_mutex_name: Option<&'static str>,
) -> Result<i32, AnyError> {
let log_broadcast = BroadcastLogSink::new();
log = log.tee(log_broadcast.clone());
log::install_global_logger(log.clone()); // re-install so that library logs are captured
debug!(
log,
"Starting tunnel with `{} {}`",
APPLICATION_NAME,
std::env::args().collect::<Vec<_>>().join(" ")
);
// Intentionally read before starting the server. If the server updated and
// respawn is requested, the old binary will get renamed, and then
// current_exe will point to the wrong path.
let current_exe = std::env::current_exe().unwrap();
let mut vec = vec![
ShutdownRequest::CtrlC,
ShutdownRequest::ExeUninstalled(current_exe.to_owned()),
];
if let Some(p) = gateway_args
.parent_process_id
.and_then(|p| Pid::from_str(&p).ok())
{
vec.push(ShutdownRequest::ParentProcessKilled(p));
}
let shutdown = ShutdownRequest::create_rx(vec);
let server = loop {
if shutdown.is_open() {
return Ok(0);
}
match acquire_singleton(&paths.tunnel_lockfile()).await {
Ok(SingletonConnection::Client(stream)) => {
debug!(log, "starting as client to singleton");
let should_exit = start_singleton_client(SingletonClientArgs {
log: log.clone(),
shutdown: shutdown.clone(),
stream,
})
.await;
if should_exit {
return Ok(0);
}
}
Ok(SingletonConnection::Singleton(server)) => break server,
Err(e) => {
warning!(log, "error access singleton, retrying: {}", e);
tokio::time::sleep(Duration::from_secs(2)).await
}
}
};
debug!(log, "starting as new singleton");
let mut server =
make_singleton_server(log_broadcast.clone(), log.clone(), server, shutdown.clone());
let platform = spanf!(log, log.span("prereq"), PreReqChecker::new().verify())?;
let _lock = app_mutex_name.map(AppMutex::new);
let auth = Auth::new(&paths, log.clone());
let mut dt = dev_tunnels::DevTunnels::new_remote_tunnel(&log, auth, &paths);
loop {
let tunnel = if let Some(t) =
fulfill_existing_tunnel_args(gateway_args.tunnel.clone(), &gateway_args.name)
{
dt.start_existing_tunnel(t).await
} else {
dt.start_new_launcher_tunnel(
gateway_args.name.as_deref(),
gateway_args.random_name,
&[CONTROL_PORT],
)
.await
}?;
csa.connection_token = Some(get_connection_token(&tunnel));
let mut r = start_singleton_server(SingletonServerArgs {
log: log.clone(),
tunnel,
paths: &paths,
code_server_args: &csa,
platform,
log_broadcast: &log_broadcast,
shutdown: shutdown.clone(),
server: &mut server,
})
.await?;
r.tunnel.close().await.ok();
match r.next {
Next::Respawn => {
warning!(log, "respawn requested, starting new server");
// reuse current args, but specify no-forward since tunnels will
// already be running in this process, and we cannot do a login
let args = std::env::args().skip(1).collect::<Vec<String>>();
let exit = std::process::Command::new(current_exe)
.args(args)
.spawn()
.map_err(|e| wrap(e, "error respawning after update"))?
.wait()
.map_err(|e| wrap(e, "error waiting for child"))?;
return Ok(exit.code().unwrap_or(1));
}
Next::Exit => {
debug!(log, "Tunnel shut down");
return Ok(0);
}
Next::Restart => continue,
}
}
}