Skip to content

Commit c1b3556

Browse files
authored
Add --name option, default user@hostname (#91)
* Add --name option, default user@hostname This appears in the title of the browser tab. If not specified, it will default to detecting the user and hostname of your machine using OS APIs. This change is backwards-compatible and forwards-compatible between old clients and old server versions. Resolves #90 and #85. * Remove console.log() * Fix clippy issue
1 parent ef30dde commit c1b3556

File tree

19 files changed

+104
-29
lines changed

19 files changed

+104
-29
lines changed

Cargo.lock

Lines changed: 31 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ members = ["crates/*"]
33
resolver = "2"
44

55
[workspace.package]
6-
version = "0.2.3"
6+
version = "0.2.4"
77
authors = ["Eric Zhang <[email protected]>"]
88
license = "MIT"
99
description = "A secure web-based, collaborative terminal."
@@ -17,7 +17,7 @@ clap = { version = "4.4.2", features = ["derive", "env"] }
1717
prost = "0.12.0"
1818
rand = "0.8.5"
1919
serde = { version = "1.0.188", features = ["derive", "rc"] }
20-
sshx-core = { version = "0.2.3", path = "crates/sshx-core" }
20+
sshx-core = { version = "0.2.4", path = "crates/sshx-core" }
2121
tokio = { version = "1.32.0", features = ["full"] }
2222
tokio-stream = { version = "0.1.14", features = ["sync"] }
2323
tonic = { version = "0.10.0", features = ["tls", "tls-webpki-roots"] }

crates/sshx-core/proto/sshx.proto

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ message TerminalSize {
4040
message OpenRequest {
4141
string origin = 1; // Web origin of the server.
4242
bytes encrypted_zeros = 2; // Encrypted zero block, for client verification.
43+
string name = 3; // Name of the session (user@hostname).
4344
}
4445

4546
// Details of a newly-created sshx session.
@@ -101,6 +102,7 @@ message SerializedSession {
101102
map<uint32, SerializedShell> shells = 2;
102103
uint32 next_sid = 3;
103104
uint32 next_uid = 4;
105+
string name = 5;
104106
}
105107

106108
message SerializedShell {

crates/sshx-server/src/grpc.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ impl SshxService for GrpcServer {
5555
None => {
5656
let metadata = Metadata {
5757
encrypted_zeros: request.encrypted_zeros,
58+
name: request.name,
5859
};
5960
self.0.insert(&name, Arc::new(Session::new(metadata)));
6061
}

crates/sshx-server/src/session.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ const SHELL_STORED_BYTES: u64 = 1 << 21; // 2 MiB
3030
pub struct Metadata {
3131
/// Used to validate that clients have the correct encryption key.
3232
pub encrypted_zeros: Bytes,
33+
34+
/// Name of the session (human-readable).
35+
pub name: String,
3336
}
3437

3538
/// In-memory state for a single sshx session.

crates/sshx-server/src/session/snapshot.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ impl Session {
6161
.collect(),
6262
next_sid: ids.0 .0,
6363
next_uid: ids.1 .0,
64+
name: self.metadata().name.clone(),
6465
};
6566
let data = message.encode_to_vec();
6667
ensure!(data.len() < MAX_SNAPSHOT_SIZE, "snapshot too large");
@@ -73,6 +74,7 @@ impl Session {
7374
let message = SerializedSession::decode(&*data)?;
7475
let metadata = Metadata {
7576
encrypted_zeros: message.encrypted_zeros,
77+
name: message.name,
7678
};
7779

7880
let session = Self::new(metadata);

crates/sshx-server/src/web/protocol.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ pub struct WsUser {
4646
#[serde(rename_all = "camelCase")]
4747
pub enum WsServer {
4848
/// Initial server message, with the user's ID and session metadata.
49-
Hello(Uid),
49+
Hello(Uid, String),
5050
/// The user's authentication was invalid.
5151
InvalidAuth(),
5252
/// A snapshot of all current users in the session.

crates/sshx-server/src/web/socket.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,13 @@ async fn handle_socket(socket: &mut WebSocket, session: Arc<Session>) -> Result<
9090
})
9191
}
9292

93+
let metadata = session.metadata();
9394
let user_id = session.counter().next_uid();
9495
session.sync_now();
95-
send(socket, WsServer::Hello(user_id)).await?;
96+
send(socket, WsServer::Hello(user_id, metadata.name.clone())).await?;
9697

9798
match recv(socket).await? {
98-
Some(WsClient::Authenticate(bytes)) if bytes == session.metadata().encrypted_zeros => {}
99+
Some(WsClient::Authenticate(bytes)) if bytes == metadata.encrypted_zeros => {}
99100
_ => {
100101
send(socket, WsServer::InvalidAuth()).await?;
101102
return Ok(());

crates/sshx-server/tests/common/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ impl ClientSocket {
154154
let flush_task = async {
155155
while let Some(msg) = self.recv().await {
156156
match msg {
157-
WsServer::Hello(user_id) => self.user_id = user_id,
157+
WsServer::Hello(user_id, _) => self.user_id = user_id,
158158
WsServer::InvalidAuth() => panic!("invalid authentication"),
159159
WsServer::Users(users) => self.users = BTreeMap::from_iter(users),
160160
WsServer::UserDiff(id, maybe_user) => {

crates/sshx-server/tests/simple.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ async fn test_rpc() -> Result<()> {
1414
let req = OpenRequest {
1515
origin: "sshx.io".into(),
1616
encrypted_zeros: Encrypt::new("").zeros().into(),
17+
name: String::new(),
1718
};
1819
let resp = client.open(req).await?;
1920
assert!(!resp.into_inner().name.is_empty());

0 commit comments

Comments
 (0)