Skip to content

Commit a8baed3

Browse files
committed
Add stubs for grpc interface
1 parent d56046c commit a8baed3

File tree

6 files changed

+91
-22
lines changed

6 files changed

+91
-22
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/sshx-core/proto/sshx.proto

Lines changed: 48 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,57 @@ syntax = "proto3";
55
package sshx;
66

77
service SshxService {
8-
// Say hello to the user.
9-
rpc Hello(HelloRequest) returns (HelloReply);
8+
// Create a new SSH session for a given computer.
9+
rpc Open(OpenRequest) returns (OpenResponse);
10+
11+
// Stream real-time commands and terminal outputs to the session.
12+
rpc Channel(stream ClientUpdate) returns (stream ServerUpdate);
13+
14+
// Gracefully shut down an existing SSH session.
15+
rpc Close(CloseRequest) returns (CloseResponse);
16+
}
17+
18+
// Details of bytes exchanged with the terminal.
19+
message TerminalData {
20+
uint32 id = 1; // ID of the shell.
21+
string data = 2;
22+
}
23+
24+
// Request to open an sshx session.
25+
message OpenRequest {}
26+
27+
// Details of a newly-created sshx session.
28+
message OpenResponse {
29+
string name = 1; // Name of the session.
30+
string url = 2; // Public web URL to view as a client.
31+
}
32+
33+
// Bidirectional streaming update from the client.
34+
message ClientUpdate {
35+
oneof message {
36+
string hello = 1; // First stream message, setting the session name.
37+
TerminalData data = 2; // Stream data from the terminal.
38+
uint32 new_shell = 3; // Acknowledge that a new shell was created.
39+
string error = 15;
40+
}
41+
}
42+
43+
// Bidirectional streaming update from the server.
44+
message ServerUpdate {
45+
oneof message {
46+
TerminalData data = 1; // Stream commands to update the terminal.
47+
uint32 create_shell = 2; // ID of a new shell.
48+
uint32 close_shell = 3; // ID of a shell to close.
49+
string error = 15;
50+
}
1051
}
1152

12-
// A request for the Hello rpc.
13-
message HelloRequest {
14-
// Request message contains the name to be greeted.
53+
// Request to stop a sshx session gracefully.
54+
message CloseRequest {
1555
string name = 1;
1656
}
1757

18-
// A reply for the Hello rpc.
19-
message HelloReply {
20-
// Reply contains the greeting message.
21-
string message = 1;
58+
// Server response to closing a session.
59+
message CloseResponse {
60+
bool exists = 1;
2261
}

crates/sshx-core/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#![warn(missing_docs)]
55

66
/// Protocol buffer and gRPC definitions, automatically generated by Tonic.
7+
#[allow(missing_docs)]
78
pub mod proto {
89
tonic::include_proto!("sshx");
910

crates/sshx-server/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,12 @@ axum = "0.4.5"
99
hyper = { version = "0.14.17", features = ["full"] }
1010
sshx-core = { path = "../sshx-core" }
1111
tokio = { version = "1.16.1", features = ["full"] }
12+
tokio-stream = "0.1.8"
1213
tonic = "0.6.2"
1314
tonic-reflection = "0.3.0"
1415
tower = { version = "0.4.11", features = ["steer"] }
1516
tower-http = { version = "0.2.2", features = ["trace"] }
1617
tracing = "0.1.30"
1718
tracing-subscriber = "0.3.8"
19+
20+
[features]

crates/sshx-server/src/grpc.rs

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,45 @@
11
//! Defines gRPC routes and application request logic.
22
3-
use sshx_core::proto::{sshx_service_server::SshxService, HelloReply, HelloRequest};
4-
use tonic::{Request, Response, Status};
3+
use sshx_core::proto::{sshx_service_server::SshxService, *};
4+
use tokio::sync::mpsc;
5+
use tokio_stream::{wrappers::ReceiverStream, StreamExt};
6+
use tonic::{Request, Response, Status, Streaming};
57

68
/// Server that handles gRPC requests from the sshx command-line client.
79
pub struct GrpcServer;
810

911
#[tonic::async_trait]
1012
impl SshxService for GrpcServer {
11-
async fn hello(&self, request: Request<HelloRequest>) -> Result<Response<HelloReply>, Status> {
12-
let reply = HelloReply {
13-
message: format!("Hello {}!", request.get_ref().name),
14-
};
13+
type ChannelStream = ReceiverStream<Result<ServerUpdate, Status>>;
1514

16-
Ok(Response::new(reply))
15+
async fn open(&self, request: Request<OpenRequest>) -> Result<Response<OpenResponse>, Status> {
16+
let _ = request;
17+
Ok(Response::new(OpenResponse {
18+
name: "placeholder".into(),
19+
url: "https://example.com".into(),
20+
}))
21+
}
22+
23+
async fn channel(
24+
&self,
25+
request: Request<Streaming<ClientUpdate>>,
26+
) -> Result<Response<Self::ChannelStream>, Status> {
27+
let mut stream = request.into_inner();
28+
let (tx, rx) = mpsc::channel(1);
29+
30+
tokio::spawn(async move {
31+
let _ = stream.next().await;
32+
let _ = tx;
33+
});
34+
35+
Ok(Response::new(ReceiverStream::new(rx)))
36+
}
37+
38+
async fn close(
39+
&self,
40+
request: Request<CloseRequest>,
41+
) -> Result<Response<CloseResponse>, Status> {
42+
let _ = request;
43+
todo!()
1744
}
1845
}

crates/sshx-server/tests/simple.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use anyhow::Result;
2-
use sshx_core::proto::HelloRequest;
2+
use sshx_core::proto::*;
33

44
use crate::common::*;
55

@@ -10,11 +10,9 @@ async fn test_rpc() -> Result<()> {
1010
let server = TestServer::new()?;
1111
let mut client = server.grpc_client().await?;
1212

13-
let req = HelloRequest {
14-
name: "adam".into(),
15-
};
16-
let resp = client.hello(req).await?;
17-
assert_eq!(&resp.into_inner().message, "Hello adam!");
13+
let req = OpenRequest {};
14+
let resp = client.open(req).await?;
15+
assert!(!resp.into_inner().name.is_empty());
1816

1917
Ok(())
2018
}

0 commit comments

Comments
 (0)