Compare commits
132 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| be98fb8d91 | |||
| 0125966d2a | |||
| 8b33a5d20b | |||
| bbf45db0f6 | |||
| d8cc462077 | |||
| 4954059c6e | |||
| b1cc6bf8ae | |||
| bf50b38ce2 | |||
| dccd648ac9 | |||
| 3ab314698b | |||
| c4301f192c | |||
| 7ebc6533a7 | |||
| e67e06ebb5 | |||
| fbedd54eaa | |||
| 247a1669ce | |||
| de179dd3e9 | |||
| 7184b42b7a | |||
| 17cc1f6131 | |||
| 4b82334e02 | |||
| b9d8b32ff1 | |||
| 52a319fe9d | |||
| 6bca98d1ce | |||
| ab256285d1 | |||
| b076a84dc7 | |||
| c0b9e17214 | |||
| 0bc2a05586 | |||
| c29c43d4db | |||
| 6f055e9fc1 | |||
| 306a636e32 | |||
| a529061e58 | |||
| 5a85e6508d | |||
| efddd1544e | |||
| 0a05c68e94 | |||
| 695f356411 | |||
| fca1f9a08e | |||
| 7f48af45cd | |||
| ae619c6a78 | |||
| bea4674700 | |||
| c9332f707d | |||
| d883e1cdb2 | |||
| e061b11974 | |||
| 0f2cb82be6 | |||
| f74495765f | |||
| b2c407ab6e | |||
| 4b9d05a185 | |||
| 3d5bb126c5 | |||
| 37632a96b7 | |||
| e69be4fdf0 | |||
| fdbd58b628 | |||
| c65aba9d7a | |||
| 818b481154 | |||
| 70650e221b | |||
| 01e8870578 | |||
| 99d54899bd | |||
| 80e922957e | |||
| 1dcb33550e | |||
| 2c5b9ab30d | |||
| bfe75dfbd2 | |||
| 4ea05fc14f | |||
| bae0d0ed56 | |||
| 99e318ca6b | |||
| b2be4c2cb7 | |||
| dbf96b68d3 | |||
| d74457d198 | |||
| 03f5b88889 | |||
| 5a29925422 | |||
| 630b8dab30 | |||
| b87f277e2d | |||
| 732f53f2d0 | |||
| 6aa03cca63 | |||
| 58d8e34828 | |||
| 47e446b433 | |||
| e11ba1a652 | |||
| 07c4b321d1 | |||
| 673b8ae16e | |||
| 262dfaca04 | |||
| 5ca3e98a5c | |||
| 40810e4ba2 | |||
| 1f01338eb8 | |||
| 5c8bd68aa6 | |||
| 68cf411143 | |||
| b84f57ee9d | |||
| 1601d474e7 | |||
| 1466516053 | |||
| bab3f777dc | |||
| 039dc641b3 | |||
| 4c9e31fd16 | |||
| c1c672b188 | |||
| a23f610f20 | |||
| ff2b7fff2b | |||
| f4879090f6 | |||
| 37ec9fc520 | |||
| f3c7ec225d | |||
| d3c40a06ac | |||
| 6b12b3dfe0 | |||
| b90c34dc17 | |||
| f205e0c128 | |||
| 92c94176bd | |||
| 4b2e9a62d5 | |||
| 689d20f7e7 | |||
| 252f081a61 | |||
| b1e8326b0a | |||
| 3b4931aa8f | |||
| 5c54601cdf | |||
| c085137250 | |||
| 237ad52bef | |||
| a8ba852f16 | |||
| f1a3de7730 | |||
| 80699dcf9d | |||
| 274c0f505e | |||
| b393c0e5d5 | |||
| d5bc8e3c64 | |||
| c40f5459ba | |||
| 601510687e | |||
| 60387e9222 | |||
| a48aa15308 | |||
| 691b58e531 | |||
| 431dcc3997 | |||
| 3224a9c699 | |||
| 00f5b2e001 | |||
| 03cb5c0f75 | |||
| f8056b03b6 | |||
| 42fbf86ca9 | |||
| 13f8cb07c5 | |||
| bbf6ffd725 | |||
| 35d9c9c6de | |||
| 51247576f3 | |||
| 255f584545 | |||
| d947ccd934 | |||
| cf27ba7ddf | |||
| 02607b49b1 | |||
| 48e3de2922 |
@@ -15,8 +15,14 @@ jobs:
|
|||||||
- uses: dtolnay/rust-toolchain@stable
|
- uses: dtolnay/rust-toolchain@stable
|
||||||
- name: Format check
|
- name: Format check
|
||||||
run: cargo fmt -- --check
|
run: cargo fmt -- --check
|
||||||
|
- name: Shell syntax check
|
||||||
|
run: sh -n install.sh scripts/*.sh
|
||||||
|
- name: Clippy
|
||||||
|
run: cargo clippy --all-targets -- -D warnings
|
||||||
- name: Test
|
- name: Test
|
||||||
run: cargo test
|
run: cargo test --all-targets
|
||||||
|
- name: TUI harness
|
||||||
|
run: sh scripts/tui-harness.sh
|
||||||
- name: Build release
|
- name: Build release
|
||||||
run: cargo build --release
|
run: cargo build --release
|
||||||
- name: Docker SSH benchmark gate
|
- name: Docker SSH benchmark gate
|
||||||
@@ -86,6 +92,39 @@ jobs:
|
|||||||
target/dosh-release/dosh-*
|
target/dosh-release/dosh-*
|
||||||
!target/dosh-release/stage/**
|
!target/dosh-release/stage/**
|
||||||
|
|
||||||
|
publish-gitea-release:
|
||||||
|
if: github.event_name == 'workflow_dispatch' || startsWith(github.ref, 'refs/tags/')
|
||||||
|
needs: package-release
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
env:
|
||||||
|
GITEA_TOKEN: ${{ secrets.GITEA_TOKEN }}
|
||||||
|
GITEA_URL: ${{ secrets.GITEA_URL || 'https://git.palav.dev' }}
|
||||||
|
GITEA_REPO: ${{ secrets.GITEA_REPO || 'Palav/dosh' }}
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v4
|
||||||
|
- uses: actions/download-artifact@v4
|
||||||
|
with:
|
||||||
|
pattern: dosh-*
|
||||||
|
path: target/dosh-release
|
||||||
|
merge-multiple: true
|
||||||
|
- name: Verify release artifacts
|
||||||
|
run: sh scripts/verify-release-artifacts.sh
|
||||||
|
- name: Skip Gitea upload without token
|
||||||
|
if: env.GITEA_TOKEN == ''
|
||||||
|
run: echo "GITEA_TOKEN secret is not configured; release artifacts were verified but not uploaded."
|
||||||
|
- name: Upload Gitea release assets
|
||||||
|
if: env.GITEA_TOKEN != ''
|
||||||
|
run: |
|
||||||
|
set -eu
|
||||||
|
case "$GITHUB_REF" in
|
||||||
|
refs/tags/*) tag="${GITHUB_REF_NAME}" ;;
|
||||||
|
*)
|
||||||
|
version="$(sed -n 's/^version = "\(.*\)"/\1/p' Cargo.toml | sed -n '1p')"
|
||||||
|
tag="v$version"
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
scripts/upload-gitea-release.sh "$tag"
|
||||||
|
|
||||||
remote-bench:
|
remote-bench:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
env:
|
env:
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
/target/
|
/target/
|
||||||
**/*.rs.bk
|
**/*.rs.bk
|
||||||
.DS_Store
|
.DS_Store
|
||||||
|
vscode-extension/*.vsix
|
||||||
|
|||||||
Generated
+12
-1
@@ -436,7 +436,7 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "dosh"
|
name = "dosh"
|
||||||
version = "0.1.2"
|
version = "1.0.0-rc42"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"anyhow",
|
"anyhow",
|
||||||
"base64",
|
"base64",
|
||||||
@@ -447,6 +447,7 @@ dependencies = [
|
|||||||
"crossterm",
|
"crossterm",
|
||||||
"dirs",
|
"dirs",
|
||||||
"ed25519-dalek",
|
"ed25519-dalek",
|
||||||
|
"filetime",
|
||||||
"hkdf",
|
"hkdf",
|
||||||
"hmac",
|
"hmac",
|
||||||
"libc",
|
"libc",
|
||||||
@@ -577,6 +578,16 @@ dependencies = [
|
|||||||
"winapi",
|
"winapi",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "filetime"
|
||||||
|
version = "0.2.29"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "5c287a33c7f0a620c38e641e7f60827713987b3c0f26e8ddc9462cc69cf75759"
|
||||||
|
dependencies = [
|
||||||
|
"cfg-if",
|
||||||
|
"libc",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "foldhash"
|
name = "foldhash"
|
||||||
version = "0.1.5"
|
version = "0.1.5"
|
||||||
|
|||||||
+2
-1
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "dosh"
|
name = "dosh"
|
||||||
version = "0.1.2"
|
version = "1.0.0-rc42"
|
||||||
edition = "2024"
|
edition = "2024"
|
||||||
license = "MIT"
|
license = "MIT"
|
||||||
|
|
||||||
@@ -14,6 +14,7 @@ clap = { version = "4.5", features = ["derive"] }
|
|||||||
crossterm = "0.28"
|
crossterm = "0.28"
|
||||||
dirs = "5.0"
|
dirs = "5.0"
|
||||||
ed25519-dalek = "2.1"
|
ed25519-dalek = "2.1"
|
||||||
|
filetime = "0.2"
|
||||||
hkdf = "0.12"
|
hkdf = "0.12"
|
||||||
hmac = "0.12"
|
hmac = "0.12"
|
||||||
libc = "0.2"
|
libc = "0.2"
|
||||||
|
|||||||
@@ -1,14 +1,45 @@
|
|||||||
.PHONY: build test fmt install package-release package-release-linux package-release-windows publish-release bench-report bench-local bench-local-json bench-docker-ssh bench-docker-mosh fuzz-smoke fuzz-deep soak-local tui-harness
|
.PHONY: build test fmt clippy release-check 1.0-check reconnect-check hostile-network-check persistence-check package-check install package-release package-release-linux package-release-windows publish-release bench-report bench-local bench-local-json bench-docker-ssh bench-docker-mosh fuzz-smoke fuzz-deep soak-local tui-harness
|
||||||
|
|
||||||
build:
|
build:
|
||||||
cargo build --release
|
cargo build --release
|
||||||
|
|
||||||
test:
|
test:
|
||||||
cargo test
|
cargo test --all-targets
|
||||||
|
|
||||||
fmt:
|
fmt:
|
||||||
cargo fmt
|
cargo fmt
|
||||||
|
|
||||||
|
clippy:
|
||||||
|
cargo clippy --all-targets -- -D warnings
|
||||||
|
|
||||||
|
release-check:
|
||||||
|
cargo fmt --check
|
||||||
|
sh -n install.sh scripts/*.sh
|
||||||
|
cargo clippy --all-targets -- -D warnings
|
||||||
|
cargo test --all-targets
|
||||||
|
$(MAKE) tui-harness
|
||||||
|
|
||||||
|
1.0-check: release-check reconnect-check hostile-network-check persistence-check package-check
|
||||||
|
|
||||||
|
reconnect-check:
|
||||||
|
cargo test --test integration_smoke resume_updates_udp_endpoint_for_roaming -- --nocapture
|
||||||
|
DOSH_SOAK_SECONDS=$${DOSH_SOAK_SECONDS:-1800} cargo test --test integration_smoke sleep_roaming_soak_30m -- --ignored --nocapture
|
||||||
|
|
||||||
|
hostile-network-check:
|
||||||
|
cargo test --test hostile_network -- --nocapture
|
||||||
|
DOSH_BADNET_SOAK_SECONDS=$${DOSH_BADNET_SOAK_SECONDS:-1800} cargo test --test hostile_network bad_network_tui_work_soak_30m -- --ignored --nocapture
|
||||||
|
|
||||||
|
persistence-check:
|
||||||
|
cargo test --test integration_smoke session_survives_server_restart_same_shell_and_screen -- --nocapture
|
||||||
|
cargo test --test integration_smoke multiple_persistent_named_sessions_survive_restart_independently -- --nocapture
|
||||||
|
|
||||||
|
package-check:
|
||||||
|
$(MAKE) package-release-linux
|
||||||
|
$(MAKE) package-release-windows
|
||||||
|
sh scripts/verify-release-artifacts.sh \
|
||||||
|
target/dosh-release/dosh-linux-x86_64.tar.gz \
|
||||||
|
target/dosh-release/dosh-windows-x86_64.zip
|
||||||
|
|
||||||
install:
|
install:
|
||||||
sh install.sh --from-current
|
sh install.sh --from-current
|
||||||
|
|
||||||
|
|||||||
@@ -1,32 +1,31 @@
|
|||||||
# Dosh
|
# Dosh
|
||||||
|
|
||||||
Dosh is an encrypted remote terminal for fast reconnecting shells. It is meant
|
Dosh is an encrypted remote terminal for fast reconnecting shells.
|
||||||
to replace Mosh and day-to-day interactive SSH sessions.
|
|
||||||
|
|
||||||
It runs a `dosh-server` on the remote machine and a `dosh` client locally. The
|
It runs a `dosh-server` on a Unix-like host and a `dosh` client on macOS,
|
||||||
first setup can use SSH. After that, Dosh can attach over encrypted UDP with
|
Linux, or Windows. Setup can use SSH, then Dosh connects over encrypted UDP,
|
||||||
cached credentials, keep terminal sessions alive, reconnect after network
|
keeps sessions alive across disconnects, supports terminal apps, and can carry
|
||||||
changes, and forward TCP ports.
|
TCP forwarding, file copy, and VS Code Remote-SSH streams.
|
||||||
|
|
||||||
## Support
|
## Support
|
||||||
|
|
||||||
- Client: macOS, Linux, Windows
|
- Client: macOS, Linux, Windows
|
||||||
- Server: Unix-like systems with PTYs
|
- Server: Linux and other Unix-like systems with PTYs
|
||||||
|
- Default UDP port: `50000`
|
||||||
- Windows: client only
|
- Windows: client only
|
||||||
- UDP port: one configured server port, default `50000`
|
|
||||||
|
|
||||||
Dosh is not an SCP/SFTP client and does not implement X11 forwarding. Windows
|
Dosh is meant to replace Mosh and everyday interactive SSH sessions. It does
|
||||||
is client-only.
|
not currently include SFTP, X11 forwarding, or a Windows server.
|
||||||
|
|
||||||
## Install
|
## Install
|
||||||
|
|
||||||
Server and client on Unix/macOS:
|
Unix/macOS server and client:
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
curl -fsSL https://git.palav.dev/Palav/dosh/raw/branch/main/install.sh | sh -s -- both --repo https://git.palav.dev/Palav/dosh.git --port 50000
|
curl -fsSL https://git.palav.dev/Palav/dosh/raw/branch/main/install.sh | sh -s -- both --repo https://git.palav.dev/Palav/dosh.git --port 50000
|
||||||
```
|
```
|
||||||
|
|
||||||
Client only on Unix/macOS:
|
Unix/macOS client only:
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
curl -fsSL https://git.palav.dev/Palav/dosh/raw/branch/main/install.sh | sh -s -- client --repo https://git.palav.dev/Palav/dosh.git
|
curl -fsSL https://git.palav.dev/Palav/dosh/raw/branch/main/install.sh | sh -s -- client --repo https://git.palav.dev/Palav/dosh.git
|
||||||
@@ -38,30 +37,75 @@ Windows client:
|
|||||||
irm https://git.palav.dev/Palav/dosh/raw/branch/main/install.ps1 | iex
|
irm https://git.palav.dev/Palav/dosh/raw/branch/main/install.ps1 | iex
|
||||||
```
|
```
|
||||||
|
|
||||||
## Commands
|
## Use
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
dosh setup HOST # import SSH config and trust the Dosh host key
|
dosh setup HOST
|
||||||
dosh HOST # connect
|
dosh HOST
|
||||||
dosh HOST COMMAND # connect and run a command
|
dosh HOST COMMAND
|
||||||
dosh update # update Dosh
|
dosh update
|
||||||
dosh status HOST # show remote tmux sessions and Dosh service status
|
dosh update --client|--server|--both
|
||||||
dosh restart HOST # restart dosh-server and show service status
|
|
||||||
dosh doctor HOST # check config and connectivity
|
|
||||||
dosh recover HOST # clear cached attach state and re-check
|
|
||||||
```
|
```
|
||||||
|
|
||||||
Examples:
|
Useful commands:
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
dosh palav
|
dosh exec HOST COMMAND
|
||||||
dosh palav tm
|
dosh cp SRC DST
|
||||||
dosh forward palav -L 8080:127.0.0.1:80
|
dosh ls host:path
|
||||||
dosh forward palav -D 1080
|
dosh cat host:path
|
||||||
dosh forward palav -R 2222:127.0.0.1:22
|
dosh mkdir host:path
|
||||||
|
dosh rm [-r] host:path
|
||||||
|
dosh forward HOST -L 8080:127.0.0.1:80
|
||||||
|
dosh forward HOST -D 1080
|
||||||
|
dosh forward HOST -R 2222:127.0.0.1:22
|
||||||
|
dosh status HOST
|
||||||
|
dosh doctor HOST
|
||||||
|
dosh recover HOST
|
||||||
|
dosh restart HOST
|
||||||
```
|
```
|
||||||
|
|
||||||
Agent forwarding is opt-in with `-A` and must be enabled on the server.
|
Agent forwarding is opt-in with `-A` and must also be enabled on the server.
|
||||||
|
File copy must be enabled by the server config.
|
||||||
|
|
||||||
|
## Tracing
|
||||||
|
|
||||||
|
For terminal/reconnect bugs, run a client with:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
dosh trace HOST
|
||||||
|
```
|
||||||
|
|
||||||
|
The client log path is printed before the session starts. To choose it:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
dosh trace --client-log /tmp/dosh-client.log HOST
|
||||||
|
```
|
||||||
|
|
||||||
|
Summarize collected traces with:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
dosh trace report HOST --client-log /tmp/dosh-client.log
|
||||||
|
```
|
||||||
|
|
||||||
|
When `HOST` is given, Dosh fetches `/tmp/dosh-server.log` over SSH before
|
||||||
|
building the report. Set `DOSH_TRACE=/tmp/dosh-server.log` on `dosh-server` for
|
||||||
|
matching server events. Reports default to the latest traced client/server
|
||||||
|
process run; add `--all-runs` to include older appended log entries. Trace byte
|
||||||
|
prefixes are enabled for `dosh trace`, so use it only for short reproductions.
|
||||||
|
|
||||||
|
## VS Code
|
||||||
|
|
||||||
|
Dosh can carry VS Code Remote-SSH through its transport:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
dosh vscode setup HOST
|
||||||
|
dosh vscode HOST /remote/path
|
||||||
|
```
|
||||||
|
|
||||||
|
This creates a managed SSH config entry using `ProxyCommand dosh proxy-stdio`.
|
||||||
|
VS Code still uses Remote-SSH and its normal remote server; Dosh carries the
|
||||||
|
SSH byte stream.
|
||||||
|
|
||||||
## Config
|
## Config
|
||||||
|
|
||||||
@@ -75,8 +119,35 @@ Common client settings:
|
|||||||
|
|
||||||
```toml
|
```toml
|
||||||
default_session = "new"
|
default_session = "new"
|
||||||
auth_preference = "native,ssh"
|
|
||||||
predict = true
|
predict = true
|
||||||
cache_attach_tickets = true
|
cache_attach_tickets = true
|
||||||
disconnect_status = true
|
disconnect_status = true
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Example host entry:
|
||||||
|
|
||||||
|
```toml
|
||||||
|
[prod]
|
||||||
|
ssh = "prod"
|
||||||
|
ssh_config = "prod" # OpenSSH alias for IdentityFile, ProxyJump, SendEnv, etc.
|
||||||
|
dosh_host = "prod.example.com"
|
||||||
|
port = 50000
|
||||||
|
```
|
||||||
|
|
||||||
|
`ProxyJump` and `ProxyCommand` can be used for SSH setup, but the Dosh UDP
|
||||||
|
endpoint still has to be reachable directly from the client. Set `dosh_host` to
|
||||||
|
the reachable UDP hostname or IP when it differs from the SSH alias target.
|
||||||
|
|
||||||
|
## Rust Library
|
||||||
|
|
||||||
|
Dosh exposes a Rust transport for encrypted, reconnecting application streams.
|
||||||
|
Use `dosh::client::DoshClient` and `dosh::server::DoshServer` for the normal
|
||||||
|
native-auth path. Use `dosh::transport::DoshTransport` only when you already
|
||||||
|
own session setup and authentication.
|
||||||
|
|
||||||
|
Runnable examples:
|
||||||
|
|
||||||
|
```text
|
||||||
|
examples/sdk_echo_client.rs
|
||||||
|
examples/sdk_echo_server.rs
|
||||||
|
```
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
use std::env;
|
||||||
use std::process::Command;
|
use std::process::Command;
|
||||||
|
|
||||||
fn git(args: &[&str]) -> Option<String> {
|
fn git(args: &[&str]) -> Option<String> {
|
||||||
@@ -12,14 +13,29 @@ fn git(args: &[&str]) -> Option<String> {
|
|||||||
fn main() {
|
fn main() {
|
||||||
println!("cargo:rerun-if-changed=.git/HEAD");
|
println!("cargo:rerun-if-changed=.git/HEAD");
|
||||||
println!("cargo:rerun-if-changed=.git/index");
|
println!("cargo:rerun-if-changed=.git/index");
|
||||||
|
println!("cargo:rerun-if-env-changed=DOSH_BUILD_GIT_HASH");
|
||||||
|
println!("cargo:rerun-if-env-changed=DOSH_BUILD_COMMIT_DATE");
|
||||||
|
println!("cargo:rerun-if-env-changed=DOSH_BUILD_GIT_DIRTY");
|
||||||
|
|
||||||
let hash = git(&["rev-parse", "--short=12", "HEAD"]).unwrap_or_else(|| "unknown".into());
|
let hash = env::var("DOSH_BUILD_GIT_HASH")
|
||||||
let date = git(&["show", "-s", "--format=%cs", "HEAD"]).unwrap_or_else(|| "unknown".into());
|
.ok()
|
||||||
let dirty = Command::new("git")
|
.filter(|value| !value.is_empty())
|
||||||
|
.or_else(|| git(&["rev-parse", "--short=12", "HEAD"]))
|
||||||
|
.unwrap_or_else(|| "unknown".into());
|
||||||
|
let date = env::var("DOSH_BUILD_COMMIT_DATE")
|
||||||
|
.ok()
|
||||||
|
.filter(|value| !value.is_empty())
|
||||||
|
.or_else(|| git(&["show", "-s", "--format=%cs", "HEAD"]))
|
||||||
|
.unwrap_or_else(|| "unknown".into());
|
||||||
|
let dirty = match env::var("DOSH_BUILD_GIT_DIRTY").ok().as_deref() {
|
||||||
|
Some("1" | "true" | "yes" | "+dirty") => "+dirty",
|
||||||
|
Some(_) => "",
|
||||||
|
None => Command::new("git")
|
||||||
.args(["diff", "--quiet", "--ignore-submodules", "--"])
|
.args(["diff", "--quiet", "--ignore-submodules", "--"])
|
||||||
.status()
|
.status()
|
||||||
.map(|status| if status.success() { "" } else { "+dirty" })
|
.map(|status| if status.success() { "" } else { "+dirty" })
|
||||||
.unwrap_or("");
|
.unwrap_or(""),
|
||||||
|
};
|
||||||
|
|
||||||
println!("cargo:rustc-env=DOSH_GIT_HASH={hash}{dirty}");
|
println!("cargo:rustc-env=DOSH_GIT_HASH={hash}{dirty}");
|
||||||
println!("cargo:rustc-env=DOSH_COMMIT_DATE={date}");
|
println!("cargo:rustc-env=DOSH_COMMIT_DATE={date}");
|
||||||
|
|||||||
@@ -0,0 +1,50 @@
|
|||||||
|
use anyhow::{Result, bail};
|
||||||
|
use dosh::client::DoshClient;
|
||||||
|
use dosh::transport::{SessionEvent, TransportEvent};
|
||||||
|
|
||||||
|
#[tokio::main]
|
||||||
|
async fn main() -> Result<()> {
|
||||||
|
let mut args = std::env::args().skip(1);
|
||||||
|
let host = args.next().unwrap_or_else(|| "server".to_string());
|
||||||
|
let message = args.collect::<Vec<_>>().join(" ");
|
||||||
|
let message = if message.is_empty() {
|
||||||
|
"hello from dosh".to_string()
|
||||||
|
} else {
|
||||||
|
message
|
||||||
|
};
|
||||||
|
|
||||||
|
let client = DoshClient::load()?;
|
||||||
|
let mut transport = client
|
||||||
|
.connect(host)
|
||||||
|
.service("echo")
|
||||||
|
.connect()
|
||||||
|
.await?
|
||||||
|
.into_transport();
|
||||||
|
let stream = transport.open_service("echo").await?;
|
||||||
|
|
||||||
|
loop {
|
||||||
|
match transport.recv().await? {
|
||||||
|
SessionEvent::Stream(TransportEvent::OpenOk { stream_id, .. })
|
||||||
|
if stream_id == stream =>
|
||||||
|
{
|
||||||
|
transport.send(stream, message.as_bytes().to_vec()).await?;
|
||||||
|
}
|
||||||
|
SessionEvent::Stream(TransportEvent::Data(data)) if data.stream_id == stream => {
|
||||||
|
for chunk in data.chunks {
|
||||||
|
print!("{}", String::from_utf8_lossy(&chunk));
|
||||||
|
}
|
||||||
|
println!();
|
||||||
|
transport.close(stream).await?;
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
SessionEvent::Stream(TransportEvent::OpenReject { reason, .. }) => {
|
||||||
|
bail!("server rejected echo stream: {reason}");
|
||||||
|
}
|
||||||
|
SessionEvent::Stream(_)
|
||||||
|
| SessionEvent::Ping
|
||||||
|
| SessionEvent::Pong
|
||||||
|
| SessionEvent::Ignored => {}
|
||||||
|
}
|
||||||
|
transport.maintenance().await?;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,37 @@
|
|||||||
|
use anyhow::Result;
|
||||||
|
use dosh::server::{DoshServer, DoshServerConfig, DoshServerEvent};
|
||||||
|
use dosh::transport::{SessionEvent, TransportEvent};
|
||||||
|
|
||||||
|
#[tokio::main]
|
||||||
|
async fn main() -> Result<()> {
|
||||||
|
let config = DoshServerConfig::default().service("echo")?;
|
||||||
|
let mut server = DoshServer::bind(config).await?;
|
||||||
|
eprintln!("listening on {}", server.local_addr()?);
|
||||||
|
|
||||||
|
loop {
|
||||||
|
match server.recv().await? {
|
||||||
|
DoshServerEvent::Accepted(client) => {
|
||||||
|
eprintln!(
|
||||||
|
"accepted user={} session={} conn={:?}",
|
||||||
|
client.user, client.session, client.conn_id
|
||||||
|
);
|
||||||
|
}
|
||||||
|
DoshServerEvent::Session {
|
||||||
|
conn_id,
|
||||||
|
event: SessionEvent::Stream(TransportEvent::Open(open)),
|
||||||
|
} => {
|
||||||
|
server.accept_stream(conn_id, open.stream_id).await?;
|
||||||
|
}
|
||||||
|
DoshServerEvent::Session {
|
||||||
|
conn_id,
|
||||||
|
event: SessionEvent::Stream(TransportEvent::Data(data)),
|
||||||
|
} => {
|
||||||
|
for chunk in data.chunks {
|
||||||
|
server.send(conn_id, data.stream_id, chunk).await?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
DoshServerEvent::Session { .. } | DoshServerEvent::Ignored => {}
|
||||||
|
}
|
||||||
|
server.maintenance().await?;
|
||||||
|
}
|
||||||
|
}
|
||||||
+238
-15
@@ -1,7 +1,7 @@
|
|||||||
param(
|
param(
|
||||||
[ValidateSet("client")]
|
[ValidateSet("client")]
|
||||||
[string]$Role = $(if ($env:DOSH_ROLE) { $env:DOSH_ROLE } else { "client" }),
|
[string]$Role = $(if ($env:DOSH_ROLE) { $env:DOSH_ROLE } else { "client" }),
|
||||||
[string]$Repo = $env:DOSH_REPO,
|
[string]$Repo = $(if ($env:DOSH_REPO) { $env:DOSH_REPO } else { "https://git.palav.dev/Palav/dosh.git" }),
|
||||||
[string]$Server = $env:DOSH_SERVER,
|
[string]$Server = $env:DOSH_SERVER,
|
||||||
[string]$DoshHost = $(if ($env:DOSH_HOST) { $env:DOSH_HOST } else { $env:DOSH_DOSH_HOST }),
|
[string]$DoshHost = $(if ($env:DOSH_HOST) { $env:DOSH_HOST } else { $env:DOSH_DOSH_HOST }),
|
||||||
[int]$Port = $(if ($env:DOSH_PORT) { [int]$env:DOSH_PORT } else { 50000 }),
|
[int]$Port = $(if ($env:DOSH_PORT) { [int]$env:DOSH_PORT } else { 50000 }),
|
||||||
@@ -11,6 +11,7 @@ param(
|
|||||||
[string]$BinaryBase = $env:DOSH_BINARY_BASE,
|
[string]$BinaryBase = $env:DOSH_BINARY_BASE,
|
||||||
[string]$BinaryName = $env:DOSH_BINARY_NAME,
|
[string]$BinaryName = $env:DOSH_BINARY_NAME,
|
||||||
[string]$BinaryVersion = $(if ($env:DOSH_BINARY_VERSION) { $env:DOSH_BINARY_VERSION } else { "latest" }),
|
[string]$BinaryVersion = $(if ($env:DOSH_BINARY_VERSION) { $env:DOSH_BINARY_VERSION } else { "latest" }),
|
||||||
|
[string]$UpdateCache = $(if ($env:DOSH_UPDATE_CACHE) { $env:DOSH_UPDATE_CACHE } else { Join-Path $HOME ".cache\dosh\source" }),
|
||||||
[switch]$BinaryRequired = $($env:DOSH_BINARY_REQUIRED -and $env:DOSH_BINARY_REQUIRED -ne "0"),
|
[switch]$BinaryRequired = $($env:DOSH_BINARY_REQUIRED -and $env:DOSH_BINARY_REQUIRED -ne "0"),
|
||||||
[switch]$ForceConfig
|
[switch]$ForceConfig
|
||||||
)
|
)
|
||||||
@@ -100,14 +101,127 @@ function Release-LatestTagDownloadUrl {
|
|||||||
return $null
|
return $null
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function Version-Core($Value) {
|
||||||
|
$base = $Value.TrimStart("v").Split("+")[0]
|
||||||
|
$base.Split("-")[0]
|
||||||
|
}
|
||||||
|
|
||||||
|
function Version-Prerelease($Value) {
|
||||||
|
$base = $Value.TrimStart("v").Split("+")[0]
|
||||||
|
$dash = $base.IndexOf("-")
|
||||||
|
if ($dash -lt 0) {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
$base.Substring($dash + 1).ToLowerInvariant()
|
||||||
|
}
|
||||||
|
|
||||||
|
function Version-Parts($Value) {
|
||||||
|
[regex]::Matches((Version-Core $Value), "\d+") | ForEach-Object { [int64]$_.Value }
|
||||||
|
}
|
||||||
|
|
||||||
|
function Compare-Prerelease($Left, $Right) {
|
||||||
|
if (-not $Left -and -not $Right) {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
if (-not $Left) {
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
if (-not $Right) {
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
if ($Left -eq $Right) {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
if ($Left -match "(?i)^rc(\d+)$" -and $Right -match "(?i)^rc(\d+)$") {
|
||||||
|
$leftRc = [int64]([regex]::Match($Left, "(?i)^rc(\d+)$").Groups[1].Value)
|
||||||
|
$rightRc = [int64]([regex]::Match($Right, "(?i)^rc(\d+)$").Groups[1].Value)
|
||||||
|
return $leftRc.CompareTo($rightRc)
|
||||||
|
}
|
||||||
|
return [string]::CompareOrdinal($Left, $Right)
|
||||||
|
}
|
||||||
|
|
||||||
|
function Compare-DoshVersion($Left, $Right) {
|
||||||
|
$leftParts = @(Version-Parts $Left)
|
||||||
|
$rightParts = @(Version-Parts $Right)
|
||||||
|
$width = [Math]::Max($leftParts.Count, $rightParts.Count)
|
||||||
|
for ($i = 0; $i -lt $width; $i++) {
|
||||||
|
$l = if ($i -lt $leftParts.Count) { $leftParts[$i] } else { 0 }
|
||||||
|
$r = if ($i -lt $rightParts.Count) { $rightParts[$i] } else { 0 }
|
||||||
|
if ($l -lt $r) { return -1 }
|
||||||
|
if ($l -gt $r) { return 1 }
|
||||||
|
}
|
||||||
|
return Compare-Prerelease (Version-Prerelease $Left) (Version-Prerelease $Right)
|
||||||
|
}
|
||||||
|
|
||||||
|
function Current-SourceVersion {
|
||||||
|
if (Test-Path "Cargo.toml") {
|
||||||
|
$raw = Get-Content "Cargo.toml" -Raw
|
||||||
|
} else {
|
||||||
|
$web = Repo-WebBase $Repo
|
||||||
|
if (-not $web) {
|
||||||
|
return $null
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
$raw = (Invoke-WebRequest -UseBasicParsing -Uri "$web/raw/branch/main/Cargo.toml").Content
|
||||||
|
}
|
||||||
|
catch {
|
||||||
|
return $null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if ($raw -match '(?m)^version = "([^"]+)"') {
|
||||||
|
return $Matches[1]
|
||||||
|
}
|
||||||
|
return $null
|
||||||
|
}
|
||||||
|
|
||||||
|
function Latest-ReleaseTag {
|
||||||
|
$web = Repo-WebBase $Repo
|
||||||
|
if (-not $web) {
|
||||||
|
return $null
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
$response = Invoke-WebRequest -UseBasicParsing -Uri "$web/releases/latest" -MaximumRedirection 5
|
||||||
|
$effective = $null
|
||||||
|
if ($response.BaseResponse.ResponseUri) {
|
||||||
|
$effective = $response.BaseResponse.ResponseUri.AbsoluteUri
|
||||||
|
} elseif ($response.BaseResponse.RequestMessage -and $response.BaseResponse.RequestMessage.RequestUri) {
|
||||||
|
$effective = $response.BaseResponse.RequestMessage.RequestUri.AbsoluteUri
|
||||||
|
}
|
||||||
|
$prefix = "$web/releases/tag/"
|
||||||
|
if ($effective -and $effective.StartsWith($prefix)) {
|
||||||
|
return $effective.Substring($prefix.Length)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch {
|
||||||
|
return $null
|
||||||
|
}
|
||||||
|
return $null
|
||||||
|
}
|
||||||
|
|
||||||
|
function Latest-ReleaseIsStale {
|
||||||
|
if ($BinaryUrl -or $BinaryBase -or $BinaryVersion -ne "latest") {
|
||||||
|
return $false
|
||||||
|
}
|
||||||
|
$latest = Latest-ReleaseTag
|
||||||
|
$current = Current-SourceVersion
|
||||||
|
if (-not $latest -or -not $current) {
|
||||||
|
return $false
|
||||||
|
}
|
||||||
|
$latestVersion = $latest.TrimStart("v")
|
||||||
|
if ((Compare-DoshVersion $latestVersion $current) -lt 0) {
|
||||||
|
Write-Warning "latest release $latestVersion is older than source $current; skipping stale prebuilt"
|
||||||
|
return $true
|
||||||
|
}
|
||||||
|
return $false
|
||||||
|
}
|
||||||
|
|
||||||
function Verify-ArchiveChecksum($Url, $Archive) {
|
function Verify-ArchiveChecksum($Url, $Archive) {
|
||||||
$checksumPath = "$Archive.sha256"
|
$checksumPath = "$Archive.sha256"
|
||||||
try {
|
try {
|
||||||
Invoke-WebRequest -UseBasicParsing -Uri "$Url.sha256" -OutFile $checksumPath
|
Invoke-WebRequest -UseBasicParsing -Uri "$Url.sha256" -OutFile $checksumPath
|
||||||
}
|
}
|
||||||
catch {
|
catch {
|
||||||
Write-Warning "prebuilt checksum unavailable; continuing without sidecar verification"
|
throw "prebuilt checksum unavailable; refusing unverified binary for $Url"
|
||||||
return
|
|
||||||
}
|
}
|
||||||
$expected = ((Get-Content $checksumPath -Raw).Trim() -split "\s+")[0].ToLowerInvariant()
|
$expected = ((Get-Content $checksumPath -Raw).Trim() -split "\s+")[0].ToLowerInvariant()
|
||||||
$actual = (Get-FileHash -Algorithm SHA256 $Archive).Hash.ToLowerInvariant()
|
$actual = (Get-FileHash -Algorithm SHA256 $Archive).Hash.ToLowerInvariant()
|
||||||
@@ -116,11 +230,83 @@ function Verify-ArchiveChecksum($Url, $Archive) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function Expected-ArchiveVersion($Url) {
|
||||||
|
if ($Url -match "/releases/download/v([^/]+)/") {
|
||||||
|
return $Matches[1]
|
||||||
|
}
|
||||||
|
if (-not $BinaryUrl -and -not $BinaryBase -and $BinaryVersion -ne "latest") {
|
||||||
|
return $BinaryVersion.TrimStart("v")
|
||||||
|
}
|
||||||
|
return $null
|
||||||
|
}
|
||||||
|
|
||||||
|
function Verify-ArchiveVersion($ExtractDir, $Url) {
|
||||||
|
$expected = Expected-ArchiveVersion $Url
|
||||||
|
if (-not $expected) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
$versionFile = Get-ChildItem -Path $ExtractDir -Recurse -File -Filter VERSION | Select-Object -First 1
|
||||||
|
if (-not $versionFile) {
|
||||||
|
throw "prebuilt archive missing VERSION for $Url"
|
||||||
|
}
|
||||||
|
$actual = (Get-Content $versionFile.FullName -Raw).Trim()
|
||||||
|
if ($actual -ne $expected) {
|
||||||
|
throw "prebuilt archive version mismatch for ${Url}: expected $expected, got $actual"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function Install-Binary($Source, $Destination) {
|
||||||
|
$dir = Split-Path -Parent $Destination
|
||||||
|
$name = Split-Path -Leaf $Destination
|
||||||
|
$tmp = Join-Path $dir ".$name.tmp.$PID"
|
||||||
|
Copy-Item $Source $tmp -Force
|
||||||
|
try {
|
||||||
|
Move-Item $tmp $Destination -Force
|
||||||
|
}
|
||||||
|
catch {
|
||||||
|
Remove-Item $tmp -Force -ErrorAction SilentlyContinue
|
||||||
|
throw
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function Normalize-PathForCompare($Path) {
|
||||||
|
[System.IO.Path]::GetFullPath($Path).TrimEnd(
|
||||||
|
[System.IO.Path]::DirectorySeparatorChar,
|
||||||
|
[System.IO.Path]::AltDirectorySeparatorChar
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
function Assert-NoRelativePathSegments($Path) {
|
||||||
|
foreach ($segment in ($Path -split '[\\/]')) {
|
||||||
|
if ($segment -eq "." -or $segment -eq "..") {
|
||||||
|
throw "refusing unsafe update cache path: $Path"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function Assert-SafeUpdateCache($Path) {
|
||||||
|
if (-not $Path) {
|
||||||
|
throw "refusing unsafe update cache path: $Path"
|
||||||
|
}
|
||||||
|
Assert-NoRelativePathSegments $Path
|
||||||
|
$full = Normalize-PathForCompare $Path
|
||||||
|
$root = Normalize-PathForCompare ([System.IO.Path]::GetPathRoot($full))
|
||||||
|
$homePath = Normalize-PathForCompare $HOME
|
||||||
|
$homeCache = Normalize-PathForCompare (Join-Path $HOME ".cache")
|
||||||
|
if ($full -eq $root -or $full -eq $homePath -or $full -eq $homeCache) {
|
||||||
|
throw "refusing unsafe update cache path: $Path"
|
||||||
|
}
|
||||||
|
$full
|
||||||
|
}
|
||||||
|
|
||||||
$bindir = Join-Path $Prefix "bin"
|
$bindir = Join-Path $Prefix "bin"
|
||||||
$configDir = Join-Path $HOME ".config\dosh"
|
$configDir = Join-Path $HOME ".config\dosh"
|
||||||
New-Item -ItemType Directory -Force -Path $bindir, $configDir | Out-Null
|
New-Item -ItemType Directory -Force -Path $bindir, $configDir | Out-Null
|
||||||
|
|
||||||
function Install-Prebuilt {
|
function Install-Prebuilt {
|
||||||
|
if (Latest-ReleaseIsStale) {
|
||||||
|
return $false
|
||||||
|
}
|
||||||
$url = Release-LatestTagDownloadUrl
|
$url = Release-LatestTagDownloadUrl
|
||||||
if (-not $url) {
|
if (-not $url) {
|
||||||
$url = Release-DownloadUrl
|
$url = Release-DownloadUrl
|
||||||
@@ -137,14 +323,15 @@ function Install-Prebuilt {
|
|||||||
Invoke-WebRequest -UseBasicParsing -Uri $url -OutFile $zip
|
Invoke-WebRequest -UseBasicParsing -Uri $url -OutFile $zip
|
||||||
Verify-ArchiveChecksum $url $zip
|
Verify-ArchiveChecksum $url $zip
|
||||||
Expand-Archive -Force -Path $zip -DestinationPath $extract
|
Expand-Archive -Force -Path $zip -DestinationPath $extract
|
||||||
|
Verify-ArchiveVersion $extract $url
|
||||||
foreach ($bin in @("dosh-client.exe", "dosh-bench.exe")) {
|
foreach ($bin in @("dosh-client.exe", "dosh-bench.exe")) {
|
||||||
$found = Get-ChildItem -Path $extract -Recurse -File -Filter $bin | Select-Object -First 1
|
$found = Get-ChildItem -Path $extract -Recurse -File -Filter $bin | Select-Object -First 1
|
||||||
if (-not $found) {
|
if (-not $found) {
|
||||||
throw "prebuilt archive missing $bin"
|
throw "prebuilt archive missing $bin"
|
||||||
}
|
}
|
||||||
Copy-Item $found.FullName (Join-Path $bindir $bin) -Force
|
Install-Binary $found.FullName (Join-Path $bindir $bin)
|
||||||
}
|
}
|
||||||
Copy-Item (Join-Path $bindir "dosh-client.exe") (Join-Path $bindir "dosh.exe") -Force
|
Install-Binary (Join-Path $bindir "dosh-client.exe") (Join-Path $bindir "dosh.exe")
|
||||||
return $true
|
return $true
|
||||||
}
|
}
|
||||||
catch {
|
catch {
|
||||||
@@ -160,7 +347,6 @@ function Install-Prebuilt {
|
|||||||
|
|
||||||
function Install-FromSource {
|
function Install-FromSource {
|
||||||
Require-Command cargo
|
Require-Command cargo
|
||||||
$tmp = $null
|
|
||||||
if (Test-Path "Cargo.toml") {
|
if (Test-Path "Cargo.toml") {
|
||||||
$src = (Get-Location).Path
|
$src = (Get-Location).Path
|
||||||
} else {
|
} else {
|
||||||
@@ -168,23 +354,31 @@ function Install-FromSource {
|
|||||||
throw "DOSH_REPO is required when running the installer from irm/iex"
|
throw "DOSH_REPO is required when running the installer from irm/iex"
|
||||||
}
|
}
|
||||||
Require-Command git
|
Require-Command git
|
||||||
$tmp = Join-Path ([System.IO.Path]::GetTempPath()) ("dosh-" + [guid]::NewGuid())
|
$sourceCache = Assert-SafeUpdateCache $UpdateCache
|
||||||
git clone --depth 1 $Repo $tmp | Out-Null
|
$parent = Split-Path -Parent $sourceCache
|
||||||
$src = $tmp
|
New-Item -ItemType Directory -Force -Path $parent | Out-Null
|
||||||
|
if (Test-Path (Join-Path $sourceCache ".git")) {
|
||||||
|
git -C $sourceCache remote set-url origin $Repo
|
||||||
|
git -C $sourceCache fetch --depth 1 origin main
|
||||||
|
git -C $sourceCache checkout -q -B main FETCH_HEAD
|
||||||
|
} else {
|
||||||
|
if (Test-Path $sourceCache) {
|
||||||
|
Remove-Item -Recurse -Force $sourceCache
|
||||||
|
}
|
||||||
|
git clone --depth 1 --branch main $Repo $sourceCache | Out-Null
|
||||||
|
}
|
||||||
|
$src = $sourceCache
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
Push-Location $src
|
Push-Location $src
|
||||||
cargo build --release --bin dosh-client --bin dosh-bench
|
cargo build --release --bin dosh-client --bin dosh-bench
|
||||||
Copy-Item "target\release\dosh-client.exe" (Join-Path $bindir "dosh-client.exe") -Force
|
Install-Binary "target\release\dosh-client.exe" (Join-Path $bindir "dosh-client.exe")
|
||||||
Copy-Item "target\release\dosh-client.exe" (Join-Path $bindir "dosh.exe") -Force
|
Install-Binary "target\release\dosh-client.exe" (Join-Path $bindir "dosh.exe")
|
||||||
Copy-Item "target\release\dosh-bench.exe" (Join-Path $bindir "dosh-bench.exe") -Force
|
Install-Binary "target\release\dosh-bench.exe" (Join-Path $bindir "dosh-bench.exe")
|
||||||
}
|
}
|
||||||
finally {
|
finally {
|
||||||
Pop-Location
|
Pop-Location
|
||||||
if ($tmp) {
|
|
||||||
Remove-Item -Recurse -Force $tmp
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -221,10 +415,38 @@ predict = true
|
|||||||
predict_mode = "experimental"
|
predict_mode = "experimental"
|
||||||
cache_attach_tickets = true
|
cache_attach_tickets = true
|
||||||
credential_cache = "~/.local/share/dosh/credentials"
|
credential_cache = "~/.local/share/dosh/credentials"
|
||||||
|
auth_preference = "native,ssh"
|
||||||
|
trust_on_first_use = false
|
||||||
|
native_auth_timeout_ms = 700
|
||||||
|
known_hosts = "~/.config/dosh/known_hosts"
|
||||||
|
identity_files = ["~/.ssh/id_ed25519"]
|
||||||
|
use_ssh_agent = true
|
||||||
|
forward_agent = false
|
||||||
escape_key = "^]"
|
escape_key = "^]"
|
||||||
"@ | Set-Content -NoNewline -Encoding utf8 $clientConfig
|
"@ | Set-Content -NoNewline -Encoding utf8 $clientConfig
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$hostsConfig = Join-Path $configDir "hosts.toml"
|
||||||
|
if ($ForceConfig -or -not (Test-Path $hostsConfig)) {
|
||||||
|
$defaultServer = if ($Server) { $Server } else { "user@example.com" }
|
||||||
|
$hostUdp = if ($DoshHost) { $DoshHost } else { $defaultServer }
|
||||||
|
@"
|
||||||
|
# Example:
|
||||||
|
# [server]
|
||||||
|
# ssh = "server"
|
||||||
|
# dosh_host = "server.example.com"
|
||||||
|
# port = 50000
|
||||||
|
# default_command = "tm"
|
||||||
|
# predict = true
|
||||||
|
|
||||||
|
[default]
|
||||||
|
ssh = "$defaultServer"
|
||||||
|
dosh_host = "$hostUdp"
|
||||||
|
port = $Port
|
||||||
|
predict = true
|
||||||
|
"@ | Set-Content -NoNewline -Encoding utf8 $hostsConfig
|
||||||
|
}
|
||||||
|
|
||||||
$userPath = [Environment]::GetEnvironmentVariable("Path", "User")
|
$userPath = [Environment]::GetEnvironmentVariable("Path", "User")
|
||||||
if (-not (($userPath -split ';') -contains $bindir)) {
|
if (-not (($userPath -split ';') -contains $bindir)) {
|
||||||
[Environment]::SetEnvironmentVariable("Path", "$userPath;$bindir", "User")
|
[Environment]::SetEnvironmentVariable("Path", "$userPath;$bindir", "User")
|
||||||
@@ -241,5 +463,6 @@ Write-Host " $bindir\dosh.exe update --check"
|
|||||||
Write-Host ""
|
Write-Host ""
|
||||||
Write-Host "Client config:"
|
Write-Host "Client config:"
|
||||||
Write-Host " $configDir\client.toml"
|
Write-Host " $configDir\client.toml"
|
||||||
|
Write-Host " $configDir\hosts.toml"
|
||||||
Write-Host ""
|
Write-Host ""
|
||||||
Write-Host "Open a new terminal for PATH changes to apply."
|
Write-Host "Open a new terminal for PATH changes to apply."
|
||||||
|
|||||||
+204
-23
@@ -12,11 +12,7 @@ start_server=1
|
|||||||
force_config=0
|
force_config=0
|
||||||
update_cache="${DOSH_UPDATE_CACHE:-$HOME/.cache/dosh/source}"
|
update_cache="${DOSH_UPDATE_CACHE:-$HOME/.cache/dosh/source}"
|
||||||
quiet="${DOSH_UPDATE_QUIET:-0}"
|
quiet="${DOSH_UPDATE_QUIET:-0}"
|
||||||
if [ "${DOSH_UPDATE_QUIET:-0}" = "1" ] && [ "${DOSH_BINARY_REQUIRED:-0}" != "1" ]; then
|
|
||||||
use_prebuilt=0
|
|
||||||
else
|
|
||||||
use_prebuilt="${DOSH_USE_PREBUILT:-1}"
|
use_prebuilt="${DOSH_USE_PREBUILT:-1}"
|
||||||
fi
|
|
||||||
binary_url="${DOSH_BINARY_URL:-}"
|
binary_url="${DOSH_BINARY_URL:-}"
|
||||||
binary_base="${DOSH_BINARY_BASE:-}"
|
binary_base="${DOSH_BINARY_BASE:-}"
|
||||||
binary_name="${DOSH_BINARY_NAME:-}"
|
binary_name="${DOSH_BINARY_NAME:-}"
|
||||||
@@ -123,6 +119,37 @@ need() {
|
|||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
|
normalize_update_cache_path() {
|
||||||
|
case "$1" in
|
||||||
|
/*) printf '%s\n' "$1" ;;
|
||||||
|
*) printf '%s\n' "$(pwd)/$1" ;;
|
||||||
|
esac | sed 's#//*#/#g; s#/$##'
|
||||||
|
}
|
||||||
|
|
||||||
|
safe_update_cache_path() {
|
||||||
|
raw="$1"
|
||||||
|
if [ -z "$raw" ]; then
|
||||||
|
echo "refusing unsafe update cache path: $raw" >&2
|
||||||
|
exit 2
|
||||||
|
fi
|
||||||
|
case "$raw" in
|
||||||
|
"."|".."|./*|../*|*/.|*/..|*/./*|*/../*)
|
||||||
|
echo "refusing unsafe update cache path: $raw" >&2
|
||||||
|
exit 2
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
normalized="$(normalize_update_cache_path "$raw")"
|
||||||
|
home_norm="$(normalize_update_cache_path "$HOME")"
|
||||||
|
home_cache_norm="$(normalize_update_cache_path "$HOME/.cache")"
|
||||||
|
case "$normalized" in
|
||||||
|
""|"/"|"$home_norm"|"$home_cache_norm")
|
||||||
|
echo "refusing unsafe update cache path: $raw" >&2
|
||||||
|
exit 2
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
printf '%s\n' "$normalized"
|
||||||
|
}
|
||||||
|
|
||||||
ensure_cargo() {
|
ensure_cargo() {
|
||||||
if command -v cargo >/dev/null 2>&1; then
|
if command -v cargo >/dev/null 2>&1; then
|
||||||
return
|
return
|
||||||
@@ -226,17 +253,137 @@ release_latest_tag_download_url() {
|
|||||||
esac
|
esac
|
||||||
}
|
}
|
||||||
|
|
||||||
|
version_core() {
|
||||||
|
value="${1#v}"
|
||||||
|
value="${value%%+*}"
|
||||||
|
printf '%s\n' "${value%%-*}"
|
||||||
|
}
|
||||||
|
|
||||||
|
version_prerelease() {
|
||||||
|
value="${1#v}"
|
||||||
|
value="${value%%+*}"
|
||||||
|
case "$value" in
|
||||||
|
*-*) printf '%s\n' "${value#*-}" ;;
|
||||||
|
*) printf '\n' ;;
|
||||||
|
esac
|
||||||
|
}
|
||||||
|
|
||||||
|
version_core_part() {
|
||||||
|
version="$1"
|
||||||
|
index="$2"
|
||||||
|
version_core "$version" \
|
||||||
|
| sed 's/[^0-9][^0-9]*/ /g' \
|
||||||
|
| awk -v index="$index" '{ value=$index; if (value == "") value=0; print value }'
|
||||||
|
}
|
||||||
|
|
||||||
|
prerelease_less_than() {
|
||||||
|
left="$1"
|
||||||
|
right="$2"
|
||||||
|
if [ -z "$left" ] && [ -z "$right" ]; then
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
if [ -z "$left" ]; then
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
if [ -z "$right" ]; then
|
||||||
|
return 0
|
||||||
|
fi
|
||||||
|
if [ "$left" = "$right" ]; then
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
left_rc="$(printf '%s\n' "$left" | sed -n 's/^[Rr][Cc]\([0-9][0-9]*\)$/\1/p')"
|
||||||
|
right_rc="$(printf '%s\n' "$right" | sed -n 's/^[Rr][Cc]\([0-9][0-9]*\)$/\1/p')"
|
||||||
|
if [ -n "$left_rc" ] && [ -n "$right_rc" ]; then
|
||||||
|
[ "$left_rc" -lt "$right_rc" ]
|
||||||
|
return
|
||||||
|
fi
|
||||||
|
first="$(printf '%s\n%s\n' "$left" "$right" | LC_ALL=C sort | sed -n '1p')"
|
||||||
|
[ "$first" = "$left" ]
|
||||||
|
}
|
||||||
|
|
||||||
|
version_less_than() {
|
||||||
|
left="$1"
|
||||||
|
right="$2"
|
||||||
|
for index in 1 2 3 4 5 6 7 8; do
|
||||||
|
lpart="$(version_core_part "$left" "$index")"
|
||||||
|
rpart="$(version_core_part "$right" "$index")"
|
||||||
|
if [ "$lpart" -lt "$rpart" ]; then
|
||||||
|
return 0
|
||||||
|
fi
|
||||||
|
if [ "$lpart" -gt "$rpart" ]; then
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
prerelease_less_than "$(version_prerelease "$left")" "$(version_prerelease "$right")"
|
||||||
|
}
|
||||||
|
|
||||||
|
current_source_version() {
|
||||||
|
if [ -f Cargo.toml ]; then
|
||||||
|
sed -n 's/^version = "\(.*\)"/\1/p' Cargo.toml | sed -n '1p'
|
||||||
|
return 0
|
||||||
|
fi
|
||||||
|
[ -n "$repo" ] || return 1
|
||||||
|
web_base="$(repo_web_base "$repo")" || return 1
|
||||||
|
curl -fsSL "$web_base/raw/branch/main/Cargo.toml" 2>/dev/null \
|
||||||
|
| sed -n 's/^version = "\(.*\)"/\1/p' \
|
||||||
|
| sed -n '1p'
|
||||||
|
}
|
||||||
|
|
||||||
|
latest_release_tag() {
|
||||||
|
[ -n "$repo" ] || return 1
|
||||||
|
web_base="$(repo_web_base "$repo")" || return 1
|
||||||
|
latest_url="$(curl -fsSL -o /dev/null -w '%{url_effective}' "$web_base/releases/latest" 2>/dev/null || true)"
|
||||||
|
case "$latest_url" in
|
||||||
|
"$web_base"/releases/tag/*)
|
||||||
|
tag="${latest_url##"$web_base"/releases/tag/}"
|
||||||
|
[ -n "$tag" ] || return 1
|
||||||
|
printf '%s\n' "$tag"
|
||||||
|
;;
|
||||||
|
*)
|
||||||
|
return 1
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
}
|
||||||
|
|
||||||
|
latest_release_is_stale() {
|
||||||
|
if [ -n "$binary_url" ] || [ -n "$binary_base" ] || [ "$binary_version" != "latest" ]; then
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
latest="$(latest_release_tag || true)"
|
||||||
|
current="$(current_source_version || true)"
|
||||||
|
[ -n "$latest" ] && [ -n "$current" ] || return 1
|
||||||
|
latest="${latest#v}"
|
||||||
|
if version_less_than "$latest" "$current"; then
|
||||||
|
echo "latest release $latest is older than source $current; skipping stale prebuilt" >&2
|
||||||
|
return 0
|
||||||
|
fi
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
|
||||||
find_extracted_binary() {
|
find_extracted_binary() {
|
||||||
find "$1" -type f -name "$2" 2>/dev/null | sed -n '1p'
|
find "$1" -type f -name "$2" 2>/dev/null | sed -n '1p'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
install_binary() {
|
||||||
|
src="$1"
|
||||||
|
dst="$2"
|
||||||
|
dst_dir="$(dirname "$dst")"
|
||||||
|
dst_base="$(basename "$dst")"
|
||||||
|
tmp="$dst_dir/.$dst_base.tmp.$$"
|
||||||
|
install -m 0755 "$src" "$tmp"
|
||||||
|
if ! mv -f "$tmp" "$dst"; then
|
||||||
|
rm -f "$tmp"
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
install_extracted_binary() {
|
install_extracted_binary() {
|
||||||
found="$(find_extracted_binary "$1" "$2")"
|
found="$(find_extracted_binary "$1" "$2")"
|
||||||
if [ -z "$found" ]; then
|
if [ -z "$found" ]; then
|
||||||
echo "prebuilt archive missing $2" >&2
|
echo "prebuilt archive missing $2" >&2
|
||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
install -m 0755 "$found" "$3"
|
install_binary "$found" "$3"
|
||||||
}
|
}
|
||||||
|
|
||||||
sha256_file() {
|
sha256_file() {
|
||||||
@@ -254,8 +401,8 @@ verify_archive_checksum() {
|
|||||||
archive="$2"
|
archive="$2"
|
||||||
checksum_file="$3"
|
checksum_file="$3"
|
||||||
if ! curl -fsL "$url.sha256" -o "$checksum_file"; then
|
if ! curl -fsL "$url.sha256" -o "$checksum_file"; then
|
||||||
echo "prebuilt checksum unavailable; continuing without sidecar verification" >&2
|
echo "prebuilt checksum unavailable; refusing unverified binary for $url" >&2
|
||||||
return 0
|
return 1
|
||||||
fi
|
fi
|
||||||
expected="$(awk '{print $1}' "$checksum_file" | sed -n '1p')"
|
expected="$(awk '{print $1}' "$checksum_file" | sed -n '1p')"
|
||||||
actual="$(sha256_file "$archive")" || {
|
actual="$(sha256_file "$archive")" || {
|
||||||
@@ -268,7 +415,44 @@ verify_archive_checksum() {
|
|||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
|
expected_archive_version() {
|
||||||
|
url="$1"
|
||||||
|
case "$url" in
|
||||||
|
*/releases/download/v*/*)
|
||||||
|
tag="${url#*/releases/download/v}"
|
||||||
|
tag="${tag%%/*}"
|
||||||
|
printf '%s\n' "$tag"
|
||||||
|
return 0
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
if [ -z "$binary_url" ] && [ -z "$binary_base" ] && [ "$binary_version" != "latest" ]; then
|
||||||
|
printf '%s\n' "${binary_version#v}"
|
||||||
|
return 0
|
||||||
|
fi
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
|
||||||
|
verify_archive_version() {
|
||||||
|
extract_dir="$1"
|
||||||
|
url="$2"
|
||||||
|
expected="$(expected_archive_version "$url" || true)"
|
||||||
|
[ -n "$expected" ] || return 0
|
||||||
|
version_file="$(find "$extract_dir" -type f -name VERSION 2>/dev/null | sed -n '1p')"
|
||||||
|
if [ -z "$version_file" ]; then
|
||||||
|
echo "prebuilt archive missing VERSION for $url" >&2
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
actual="$(tr -d '\r\n' <"$version_file")"
|
||||||
|
if [ "$actual" != "$expected" ]; then
|
||||||
|
echo "prebuilt archive version mismatch for $url: expected $expected, got $actual" >&2
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
try_install_prebuilt() {
|
try_install_prebuilt() {
|
||||||
|
if latest_release_is_stale; then
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
download_url="$(release_latest_tag_download_url || release_download_url)" || return 1
|
download_url="$(release_latest_tag_download_url || release_download_url)" || return 1
|
||||||
tmpdir="$(mktemp -d)"
|
tmpdir="$(mktemp -d)"
|
||||||
archive="$tmpdir/$(release_artifact_name)"
|
archive="$tmpdir/$(release_artifact_name)"
|
||||||
@@ -291,6 +475,7 @@ try_install_prebuilt() {
|
|||||||
echo "prebuilt archive could not be extracted: $download_url" >&2
|
echo "prebuilt archive could not be extracted: $download_url" >&2
|
||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
|
verify_archive_version "$tmpdir/extract" "$download_url" || return 1
|
||||||
install_extracted_binary "$tmpdir/extract" dosh-client "$bindir/dosh-client" || return 1
|
install_extracted_binary "$tmpdir/extract" dosh-client "$bindir/dosh-client" || return 1
|
||||||
ln -sf dosh-client "$bindir/dosh"
|
ln -sf dosh-client "$bindir/dosh"
|
||||||
if [ "$role" = "server" ] || [ "$role" = "both" ]; then
|
if [ "$role" = "server" ] || [ "$role" = "both" ]; then
|
||||||
@@ -298,7 +483,7 @@ try_install_prebuilt() {
|
|||||||
install_extracted_binary "$tmpdir/extract" dosh-auth "$bindir/dosh-auth" || return 1
|
install_extracted_binary "$tmpdir/extract" dosh-auth "$bindir/dosh-auth" || return 1
|
||||||
fi
|
fi
|
||||||
if found_bench="$(find_extracted_binary "$tmpdir/extract" dosh-bench)" && [ -n "$found_bench" ]; then
|
if found_bench="$(find_extracted_binary "$tmpdir/extract" dosh-bench)" && [ -n "$found_bench" ]; then
|
||||||
install -m 0755 "$found_bench" "$bindir/dosh-bench"
|
install_binary "$found_bench" "$bindir/dosh-bench"
|
||||||
fi
|
fi
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
@@ -312,12 +497,7 @@ install_from_source() {
|
|||||||
echo "DOSH_REPO or --repo is required when running the installer from curl" >&2
|
echo "DOSH_REPO or --repo is required when running the installer from curl" >&2
|
||||||
exit 2
|
exit 2
|
||||||
fi
|
fi
|
||||||
case "$update_cache" in
|
update_cache="$(safe_update_cache_path "$update_cache")"
|
||||||
""|"/"|"$HOME"|"$HOME/"|"$HOME/.cache")
|
|
||||||
echo "refusing unsafe update cache path: $update_cache" >&2
|
|
||||||
exit 2
|
|
||||||
;;
|
|
||||||
esac
|
|
||||||
need git
|
need git
|
||||||
mkdir -p "$(dirname "$update_cache")"
|
mkdir -p "$(dirname "$update_cache")"
|
||||||
if [ -d "$update_cache/.git" ]; then
|
if [ -d "$update_cache/.git" ]; then
|
||||||
@@ -357,14 +537,14 @@ install_from_source() {
|
|||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
install -m 0755 target/release/dosh-client "$bindir/dosh-client"
|
install_binary target/release/dosh-client "$bindir/dosh-client"
|
||||||
ln -sf dosh-client "$bindir/dosh"
|
ln -sf dosh-client "$bindir/dosh"
|
||||||
if [ "$role" = "server" ] || [ "$role" = "both" ]; then
|
if [ "$role" = "server" ] || [ "$role" = "both" ]; then
|
||||||
install -m 0755 target/release/dosh-server "$bindir/dosh-server"
|
install_binary target/release/dosh-server "$bindir/dosh-server"
|
||||||
install -m 0755 target/release/dosh-auth "$bindir/dosh-auth"
|
install_binary target/release/dosh-auth "$bindir/dosh-auth"
|
||||||
fi
|
fi
|
||||||
if [ -f target/release/dosh-bench ]; then
|
if [ -f target/release/dosh-bench ]; then
|
||||||
install -m 0755 target/release/dosh-bench "$bindir/dosh-bench"
|
install_binary target/release/dosh-bench "$bindir/dosh-bench"
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -375,14 +555,14 @@ write_systemd_service() {
|
|||||||
else
|
else
|
||||||
cat >"$systemd_user_dir/dosh-server.service" <<EOF
|
cat >"$systemd_user_dir/dosh-server.service" <<EOF
|
||||||
[Unit]
|
[Unit]
|
||||||
Description=Dosh server
|
Description=dosh dormant shell server
|
||||||
After=network-online.target
|
After=network-online.target
|
||||||
Wants=network-online.target
|
Wants=network-online.target
|
||||||
|
|
||||||
[Service]
|
[Service]
|
||||||
Type=simple
|
Type=simple
|
||||||
ExecStart=$bindir/dosh-server serve
|
ExecStart=$bindir/dosh-server serve
|
||||||
Restart=on-failure
|
Restart=always
|
||||||
RestartSec=1
|
RestartSec=1
|
||||||
KillMode=process
|
KillMode=process
|
||||||
|
|
||||||
@@ -444,7 +624,8 @@ EOF
|
|||||||
mkdir -p "$systemd_user_dir"
|
mkdir -p "$systemd_user_dir"
|
||||||
write_systemd_service
|
write_systemd_service
|
||||||
if [ "$start_server" -eq 1 ] && systemctl --user daemon-reload >/dev/null 2>&1; then
|
if [ "$start_server" -eq 1 ] && systemctl --user daemon-reload >/dev/null 2>&1; then
|
||||||
systemctl --user enable --now dosh-server.service
|
systemctl --user enable dosh-server.service
|
||||||
|
systemctl --user restart dosh-server.service
|
||||||
fi
|
fi
|
||||||
elif [ "$start_server" -eq 1 ]; then
|
elif [ "$start_server" -eq 1 ]; then
|
||||||
nohup "$bindir/dosh-server" serve >"$data_dir/dosh-server.log" 2>&1 &
|
nohup "$bindir/dosh-server" serve >"$data_dir/dosh-server.log" 2>&1 &
|
||||||
@@ -512,8 +693,8 @@ EOF
|
|||||||
fi
|
fi
|
||||||
cat >"$hosts_config" <<EOF
|
cat >"$hosts_config" <<EOF
|
||||||
# Example:
|
# Example:
|
||||||
# [homelab]
|
# [server]
|
||||||
# ssh = "homelab"
|
# ssh = "server"
|
||||||
# dosh_host = "server.example.com"
|
# dosh_host = "server.example.com"
|
||||||
# port = 50000
|
# port = 50000
|
||||||
# default_command = "tm"
|
# default_command = "tm"
|
||||||
|
|||||||
@@ -6,18 +6,9 @@ Wants=network-online.target
|
|||||||
[Service]
|
[Service]
|
||||||
Type=simple
|
Type=simple
|
||||||
ExecStart=%h/.local/bin/dosh-server serve
|
ExecStart=%h/.local/bin/dosh-server serve
|
||||||
Restart=on-failure
|
Restart=always
|
||||||
RestartSec=1
|
RestartSec=1
|
||||||
KillMode=process
|
KillMode=process
|
||||||
NoNewPrivileges=true
|
|
||||||
PrivateTmp=true
|
|
||||||
ProtectSystem=strict
|
|
||||||
ProtectHome=read-only
|
|
||||||
ReadWritePaths=%h/.config/dosh %h/.local/share/dosh
|
|
||||||
RestrictAddressFamilies=AF_INET AF_INET6 AF_UNIX
|
|
||||||
RestrictRealtime=true
|
|
||||||
LockPersonality=true
|
|
||||||
MemoryDenyWriteExecute=true
|
|
||||||
|
|
||||||
[Install]
|
[Install]
|
||||||
WantedBy=default.target
|
WantedBy=default.target
|
||||||
|
|||||||
@@ -67,6 +67,7 @@ cleanup() {
|
|||||||
kill "$server_pid" 2>/dev/null || true
|
kill "$server_pid" 2>/dev/null || true
|
||||||
wait "$server_pid" 2>/dev/null || true
|
wait "$server_pid" 2>/dev/null || true
|
||||||
fi
|
fi
|
||||||
|
pkill -f "$server_bin hold --runtime-dir $home/sessions/run" 2>/dev/null || true
|
||||||
rm -rf "$home"
|
rm -rf "$home"
|
||||||
}
|
}
|
||||||
trap cleanup EXIT INT TERM
|
trap cleanup EXIT INT TERM
|
||||||
@@ -86,6 +87,7 @@ scrollback = 5000
|
|||||||
auth_ttl_secs = 30
|
auth_ttl_secs = 30
|
||||||
attach_ticket_ttl_secs = 3600
|
attach_ticket_ttl_secs = 3600
|
||||||
allow_attach_tickets = true
|
allow_attach_tickets = true
|
||||||
|
persist_sessions = false
|
||||||
client_timeout_secs = 60
|
client_timeout_secs = 60
|
||||||
retransmit_window = 256
|
retransmit_window = 256
|
||||||
default_input_mode = "read-write"
|
default_input_mode = "read-write"
|
||||||
|
|||||||
@@ -52,6 +52,22 @@ else
|
|||||||
release_dir="target/release"
|
release_dir="target/release"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
if command -v git >/dev/null 2>&1 && git rev-parse --is-inside-work-tree >/dev/null 2>&1; then
|
||||||
|
if [ -z "${DOSH_BUILD_GIT_HASH:-}" ]; then
|
||||||
|
export DOSH_BUILD_GIT_HASH="$(git rev-parse --short=12 HEAD)"
|
||||||
|
fi
|
||||||
|
if [ -z "${DOSH_BUILD_COMMIT_DATE:-}" ]; then
|
||||||
|
export DOSH_BUILD_COMMIT_DATE="$(git show -s --format=%cs HEAD)"
|
||||||
|
fi
|
||||||
|
if [ -z "${DOSH_BUILD_GIT_DIRTY+x}" ]; then
|
||||||
|
if git diff --quiet --ignore-submodules --; then
|
||||||
|
export DOSH_BUILD_GIT_DIRTY=0
|
||||||
|
else
|
||||||
|
export DOSH_BUILD_GIT_DIRTY=1
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
if [ "$os" = "windows" ]; then
|
if [ "$os" = "windows" ]; then
|
||||||
if [ -n "$target" ]; then
|
if [ -n "$target" ]; then
|
||||||
cargo build --release --target "$target" --bin dosh-client --bin dosh-bench
|
cargo build --release --target "$target" --bin dosh-client --bin dosh-bench
|
||||||
|
|||||||
@@ -99,6 +99,21 @@ fi
|
|||||||
|
|
||||||
web_repo="${base%/}/${repo}"
|
web_repo="${base%/}/${repo}"
|
||||||
failed=0
|
failed=0
|
||||||
|
artifact_version() {
|
||||||
|
artifact="$1"
|
||||||
|
case "$artifact" in
|
||||||
|
*.tar.gz)
|
||||||
|
tar -xOf "$artifact" dosh/VERSION 2>/dev/null | tr -d '\r\n'
|
||||||
|
;;
|
||||||
|
*.zip)
|
||||||
|
unzip -p "$artifact" dosh/VERSION 2>/dev/null | tr -d '\r\n'
|
||||||
|
;;
|
||||||
|
*)
|
||||||
|
return 1
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
}
|
||||||
|
|
||||||
for artifact in target/dosh-release/dosh-linux-*.tar.gz target/dosh-release/dosh-macos-*.tar.gz target/dosh-release/dosh-windows-*.zip; do
|
for artifact in target/dosh-release/dosh-linux-*.tar.gz target/dosh-release/dosh-macos-*.tar.gz target/dosh-release/dosh-windows-*.zip; do
|
||||||
[ -f "$artifact" ] || continue
|
[ -f "$artifact" ] || continue
|
||||||
name="$(basename "$artifact")"
|
name="$(basename "$artifact")"
|
||||||
@@ -107,6 +122,11 @@ for artifact in target/dosh-release/dosh-linux-*.tar.gz target/dosh-release/dosh
|
|||||||
continue
|
continue
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
|
actual="$(artifact_version "$artifact" || true)"
|
||||||
|
if [ "$actual" != "$version" ]; then
|
||||||
|
echo "skipping stale artifact $name: version ${actual:-unknown}, expected $version" >&2
|
||||||
|
continue
|
||||||
|
fi
|
||||||
url="$web_repo/releases/download/$tag/$name"
|
url="$web_repo/releases/download/$tag/$name"
|
||||||
if curl -fsSI "$url" >/dev/null; then
|
if curl -fsSI "$url" >/dev/null; then
|
||||||
echo "verified $url"
|
echo "verified $url"
|
||||||
|
|||||||
@@ -8,6 +8,8 @@ for test_name in \
|
|||||||
live_output_forwards_terminal_control_sequences \
|
live_output_forwards_terminal_control_sequences \
|
||||||
tui_control_sequences_survive_transport_verbatim \
|
tui_control_sequences_survive_transport_verbatim \
|
||||||
unicode_output_survives_transport \
|
unicode_output_survives_transport \
|
||||||
|
tui_graph_glyphs_survive_fast_repaints_without_snapshot_substitution \
|
||||||
|
btop_style_graph_output_stays_raw_when_retransmit_history_overflows \
|
||||||
large_tui_paint_is_delivered_in_mtu_safe_frames \
|
large_tui_paint_is_delivered_in_mtu_safe_frames \
|
||||||
resume_snapshot_preserves_alternate_screen_mode
|
resume_snapshot_preserves_alternate_screen_mode
|
||||||
do
|
do
|
||||||
|
|||||||
@@ -11,6 +11,8 @@ Environment:
|
|||||||
GITEA_REPO owner/repo, default Palav/dosh
|
GITEA_REPO owner/repo, default Palav/dosh
|
||||||
GITEA_TOKEN Token with release write permission
|
GITEA_TOKEN Token with release write permission
|
||||||
GITEA_TITLE Release title, default TAG
|
GITEA_TITLE Release title, default TAG
|
||||||
|
DOSH_RELEASE_ALLOW_PARTIAL=1
|
||||||
|
Allow uploading an incomplete platform set. Default: refuse.
|
||||||
EOF
|
EOF
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -30,6 +32,27 @@ base="${GITEA_URL:-https://git.palav.dev}"
|
|||||||
repo="${GITEA_REPO:-Palav/dosh}"
|
repo="${GITEA_REPO:-Palav/dosh}"
|
||||||
token="${GITEA_TOKEN:-}"
|
token="${GITEA_TOKEN:-}"
|
||||||
title="${GITEA_TITLE:-$tag}"
|
title="${GITEA_TITLE:-$tag}"
|
||||||
|
expected_version="${tag#v}"
|
||||||
|
allow_partial="${DOSH_RELEASE_ALLOW_PARTIAL:-0}"
|
||||||
|
release_prerelease=false
|
||||||
|
case "$expected_version" in
|
||||||
|
*-*) release_prerelease=true ;;
|
||||||
|
esac
|
||||||
|
|
||||||
|
artifact_version() {
|
||||||
|
artifact="$1"
|
||||||
|
case "$artifact" in
|
||||||
|
*.tar.gz)
|
||||||
|
tar -xOf "$artifact" dosh/VERSION 2>/dev/null | tr -d '\r\n'
|
||||||
|
;;
|
||||||
|
*.zip)
|
||||||
|
unzip -p "$artifact" dosh/VERSION 2>/dev/null | tr -d '\r\n'
|
||||||
|
;;
|
||||||
|
*)
|
||||||
|
return 1
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
}
|
||||||
|
|
||||||
if [ -z "$token" ] && [ -f "$HOME/.config/dosh/gitea-token" ]; then
|
if [ -z "$token" ] && [ -f "$HOME/.config/dosh/gitea-token" ]; then
|
||||||
token="$(tr -d '\r\n' <"$HOME/.config/dosh/gitea-token")"
|
token="$(tr -d '\r\n' <"$HOME/.config/dosh/gitea-token")"
|
||||||
@@ -51,6 +74,11 @@ if [ "$#" -eq 0 ]; then
|
|||||||
continue
|
continue
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
|
actual="$(artifact_version "$artifact" || true)"
|
||||||
|
if [ "$actual" != "$expected_version" ]; then
|
||||||
|
echo "skipping $artifact: version ${actual:-unknown}, expected $expected_version" >&2
|
||||||
|
continue
|
||||||
|
fi
|
||||||
if [ -f "$artifact.sha256" ]; then
|
if [ -f "$artifact.sha256" ]; then
|
||||||
set -- "$@" "$artifact" "$artifact.sha256"
|
set -- "$@" "$artifact" "$artifact.sha256"
|
||||||
else
|
else
|
||||||
@@ -59,6 +87,42 @@ if [ "$#" -eq 0 ]; then
|
|||||||
done
|
done
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
require_complete_release_artifacts() {
|
||||||
|
[ "$allow_partial" = "1" ] && return 0
|
||||||
|
missing=0
|
||||||
|
for required in \
|
||||||
|
dosh-linux-x86_64.tar.gz \
|
||||||
|
dosh-macos-aarch64.tar.gz \
|
||||||
|
dosh-macos-x86_64.tar.gz \
|
||||||
|
dosh-windows-x86_64.zip; do
|
||||||
|
found=0
|
||||||
|
checksum_found=0
|
||||||
|
for artifact in "$@"; do
|
||||||
|
name="$(basename "$artifact")"
|
||||||
|
if [ "$name" = "$required" ]; then
|
||||||
|
found=1
|
||||||
|
fi
|
||||||
|
if [ "$name" = "$required.sha256" ]; then
|
||||||
|
checksum_found=1
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
if [ "$found" -ne 1 ]; then
|
||||||
|
echo "missing required release artifact: $required" >&2
|
||||||
|
missing=1
|
||||||
|
fi
|
||||||
|
if [ "$checksum_found" -ne 1 ]; then
|
||||||
|
echo "missing required release checksum: $required.sha256" >&2
|
||||||
|
missing=1
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
if [ "$missing" -ne 0 ]; then
|
||||||
|
echo "refusing partial release upload; set DOSH_RELEASE_ALLOW_PARTIAL=1 to override" >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
require_complete_release_artifacts "$@"
|
||||||
|
|
||||||
api="$base/api/v1/repos/$repo"
|
api="$base/api/v1/repos/$repo"
|
||||||
auth_header="Authorization: token $token"
|
auth_header="Authorization: token $token"
|
||||||
|
|
||||||
@@ -71,7 +135,7 @@ elif [ -n "$release_json" ]; then
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
if [ -z "$release_id" ]; then
|
if [ -z "$release_id" ]; then
|
||||||
payload="$(printf '{"tag_name":"%s","target_commitish":"main","name":"%s","body":"Dosh release %s","draft":false,"prerelease":false}\n' "$tag" "$title" "$tag")"
|
payload="$(printf '{"tag_name":"%s","target_commitish":"main","name":"%s","body":"Dosh release %s","draft":false,"prerelease":%s}\n' "$tag" "$title" "$tag" "$release_prerelease")"
|
||||||
release_json="$(curl -fsS -H "$auth_header" -H 'Content-Type: application/json' -X POST "$api/releases" -d "$payload")"
|
release_json="$(curl -fsS -H "$auth_header" -H 'Content-Type: application/json' -X POST "$api/releases" -d "$payload")"
|
||||||
if command -v jq >/dev/null 2>&1; then
|
if command -v jq >/dev/null 2>&1; then
|
||||||
release_id="$(printf '%s\n' "$release_json" | jq -r '.id // empty')"
|
release_id="$(printf '%s\n' "$release_json" | jq -r '.id // empty')"
|
||||||
@@ -85,6 +149,14 @@ if [ -z "$release_id" ]; then
|
|||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
metadata_payload="$(printf '{"name":"%s","body":"Dosh release %s","draft":false,"prerelease":%s}\n' "$title" "$tag" "$release_prerelease")"
|
||||||
|
curl -fsS \
|
||||||
|
-H "$auth_header" \
|
||||||
|
-H 'Content-Type: application/json' \
|
||||||
|
-X PATCH \
|
||||||
|
"$api/releases/$release_id" \
|
||||||
|
-d "$metadata_payload" >/dev/null
|
||||||
|
|
||||||
delete_existing_asset() {
|
delete_existing_asset() {
|
||||||
name="$1"
|
name="$1"
|
||||||
if ! command -v jq >/dev/null 2>&1; then
|
if ! command -v jq >/dev/null 2>&1; then
|
||||||
@@ -102,12 +174,34 @@ delete_existing_asset() {
|
|||||||
done
|
done
|
||||||
}
|
}
|
||||||
|
|
||||||
|
verify_release_artifact_version() {
|
||||||
|
artifact="$1"
|
||||||
|
name="$(basename "$artifact")"
|
||||||
|
case "$name" in
|
||||||
|
*.sha256)
|
||||||
|
return 0
|
||||||
|
;;
|
||||||
|
dosh-*.tar.gz|dosh-*.zip)
|
||||||
|
actual="$(artifact_version "$artifact" || true)"
|
||||||
|
if [ -z "$actual" ]; then
|
||||||
|
echo "release artifact missing dosh/VERSION: $artifact" >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
if [ "$actual" != "$expected_version" ]; then
|
||||||
|
echo "release artifact version mismatch: $artifact has $actual, expected $expected_version" >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
}
|
||||||
|
|
||||||
uploaded=0
|
uploaded=0
|
||||||
for artifact in "$@"; do
|
for artifact in "$@"; do
|
||||||
if [ ! -f "$artifact" ]; then
|
if [ ! -f "$artifact" ]; then
|
||||||
continue
|
continue
|
||||||
fi
|
fi
|
||||||
name="$(basename "$artifact")"
|
name="$(basename "$artifact")"
|
||||||
|
verify_release_artifact_version "$artifact"
|
||||||
delete_existing_asset "$name"
|
delete_existing_asset "$name"
|
||||||
echo "uploading $name"
|
echo "uploading $name"
|
||||||
curl -fsS \
|
curl -fsS \
|
||||||
|
|||||||
@@ -0,0 +1,122 @@
|
|||||||
|
#!/usr/bin/env sh
|
||||||
|
set -eu
|
||||||
|
|
||||||
|
repo_root="$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd)"
|
||||||
|
cd "$repo_root"
|
||||||
|
|
||||||
|
version="$(sed -n 's/^version = "\(.*\)"/\1/p' Cargo.toml | sed -n '1p')"
|
||||||
|
out_dir="${DOSH_PACKAGE_DIR:-target/dosh-release}"
|
||||||
|
|
||||||
|
artifact_version() {
|
||||||
|
artifact="$1"
|
||||||
|
case "$artifact" in
|
||||||
|
*.tar.gz)
|
||||||
|
tar -xOf "$artifact" dosh/VERSION 2>/dev/null | tr -d '\r\n'
|
||||||
|
;;
|
||||||
|
*.zip)
|
||||||
|
unzip -p "$artifact" dosh/VERSION 2>/dev/null | tr -d '\r\n'
|
||||||
|
;;
|
||||||
|
*)
|
||||||
|
return 1
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
}
|
||||||
|
|
||||||
|
archive_contains() {
|
||||||
|
artifact="$1"
|
||||||
|
member="$2"
|
||||||
|
case "$artifact" in
|
||||||
|
*.tar.gz)
|
||||||
|
tar -tzf "$artifact" "$member" >/dev/null 2>&1
|
||||||
|
;;
|
||||||
|
*.zip)
|
||||||
|
unzip -Z1 "$artifact" 2>/dev/null | grep -Fx "$member" >/dev/null 2>&1
|
||||||
|
;;
|
||||||
|
*)
|
||||||
|
return 1
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
}
|
||||||
|
|
||||||
|
expected_members() {
|
||||||
|
artifact_name="$(basename -- "$1")"
|
||||||
|
case "$artifact_name" in
|
||||||
|
dosh-windows-*.zip)
|
||||||
|
cat <<EOF
|
||||||
|
dosh/VERSION
|
||||||
|
dosh/bin/dosh.exe
|
||||||
|
dosh/bin/dosh-client.exe
|
||||||
|
dosh/bin/dosh-bench.exe
|
||||||
|
EOF
|
||||||
|
;;
|
||||||
|
dosh-*.tar.gz)
|
||||||
|
cat <<EOF
|
||||||
|
dosh/VERSION
|
||||||
|
dosh/bin/dosh
|
||||||
|
dosh/bin/dosh-client
|
||||||
|
dosh/bin/dosh-server
|
||||||
|
dosh/bin/dosh-auth
|
||||||
|
dosh/bin/dosh-bench
|
||||||
|
EOF
|
||||||
|
;;
|
||||||
|
*)
|
||||||
|
cat <<EOF
|
||||||
|
dosh/VERSION
|
||||||
|
EOF
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
}
|
||||||
|
|
||||||
|
checksum_value() {
|
||||||
|
artifact="$1"
|
||||||
|
if command -v sha256sum >/dev/null 2>&1; then
|
||||||
|
sha256sum "$artifact" | awk '{print $1}'
|
||||||
|
elif command -v shasum >/dev/null 2>&1; then
|
||||||
|
shasum -a 256 "$artifact" | awk '{print $1}'
|
||||||
|
else
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
verify_checksum() {
|
||||||
|
artifact="$1"
|
||||||
|
checksum="$artifact.sha256"
|
||||||
|
expected="$(awk '{print $1; exit}' "$checksum")"
|
||||||
|
actual="$(checksum_value "$artifact" || true)"
|
||||||
|
[ -n "$actual" ] && [ "$actual" = "$expected" ]
|
||||||
|
}
|
||||||
|
|
||||||
|
if [ "$#" -gt 0 ]; then
|
||||||
|
artifacts="$*"
|
||||||
|
else
|
||||||
|
artifacts="$out_dir/dosh-linux-x86_64.tar.gz $out_dir/dosh-macos-aarch64.tar.gz $out_dir/dosh-macos-x86_64.tar.gz $out_dir/dosh-windows-x86_64.zip"
|
||||||
|
fi
|
||||||
|
|
||||||
|
failed=0
|
||||||
|
for artifact in $artifacts; do
|
||||||
|
if [ ! -f "$artifact" ]; then
|
||||||
|
echo "missing release artifact: $artifact" >&2
|
||||||
|
failed=1
|
||||||
|
continue
|
||||||
|
fi
|
||||||
|
if [ ! -f "$artifact.sha256" ]; then
|
||||||
|
echo "missing release checksum: $artifact.sha256" >&2
|
||||||
|
failed=1
|
||||||
|
elif ! verify_checksum "$artifact"; then
|
||||||
|
echo "release checksum mismatch: $artifact" >&2
|
||||||
|
failed=1
|
||||||
|
fi
|
||||||
|
actual="$(artifact_version "$artifact" || true)"
|
||||||
|
if [ "$actual" != "$version" ]; then
|
||||||
|
echo "artifact version mismatch: $artifact has ${actual:-unknown}, expected $version" >&2
|
||||||
|
failed=1
|
||||||
|
fi
|
||||||
|
for member in $(expected_members "$artifact"); do
|
||||||
|
if ! archive_contains "$artifact" "$member"; then
|
||||||
|
echo "artifact missing expected member: $artifact:$member" >&2
|
||||||
|
failed=1
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
done
|
||||||
|
|
||||||
|
exit "$failed"
|
||||||
+6841
-252
File diff suppressed because it is too large
Load Diff
+2777
-319
File diff suppressed because it is too large
Load Diff
+443
@@ -0,0 +1,443 @@
|
|||||||
|
use crate::config::{
|
||||||
|
ClientConfig, HostConfig, expand_tilde, load_client_config, load_hosts_config,
|
||||||
|
};
|
||||||
|
use crate::crypto;
|
||||||
|
use crate::native::{
|
||||||
|
self, EnvVar, ForwardingKind, ForwardingRequest, KnownHostStatus, NativeClientHello,
|
||||||
|
derive_native_session_key, generate_native_ephemeral, sign_user_auth_with_private_key,
|
||||||
|
supported_user_key_algorithms, trust_host, verify_known_host, verify_server_hello,
|
||||||
|
};
|
||||||
|
use crate::protocol::{
|
||||||
|
self, AttachReject, CLIENT_TO_SERVER, NativeAuthOkBody, NativeClientHelloBody,
|
||||||
|
NativeServerHelloBody, NativeUserAuthBody, PacketKind, SERVER_TO_CLIENT,
|
||||||
|
};
|
||||||
|
use crate::ssh_agent;
|
||||||
|
use crate::transport::{DoshTransport, SessionRole, SessionTransportConfig, TransportConfig};
|
||||||
|
use crate::udp::{recv_udp_retrying_transient, send_udp_retrying_transient};
|
||||||
|
use anyhow::{Context, Result, anyhow, bail};
|
||||||
|
use std::net::{SocketAddr, ToSocketAddrs};
|
||||||
|
use std::path::PathBuf;
|
||||||
|
use std::time::{Duration, SystemTime, UNIX_EPOCH};
|
||||||
|
use tokio::net::UdpSocket;
|
||||||
|
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub struct DoshClient {
|
||||||
|
config: ClientConfig,
|
||||||
|
hosts: crate::config::HostsConfig,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl DoshClient {
|
||||||
|
pub fn load() -> Result<Self> {
|
||||||
|
Ok(Self {
|
||||||
|
config: load_client_config(None)?,
|
||||||
|
hosts: load_hosts_config(None)?,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn load_from_paths(
|
||||||
|
client_config: Option<PathBuf>,
|
||||||
|
hosts_config: Option<PathBuf>,
|
||||||
|
) -> Result<Self> {
|
||||||
|
Ok(Self {
|
||||||
|
config: load_client_config(client_config)?,
|
||||||
|
hosts: load_hosts_config(hosts_config)?,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn with_config(config: ClientConfig, hosts: crate::config::HostsConfig) -> Self {
|
||||||
|
Self { config, hosts }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn connect(&self, host: impl Into<String>) -> DoshClientBuilder {
|
||||||
|
DoshClientBuilder::new(self.clone(), host.into())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub struct DoshClientBuilder {
|
||||||
|
client: DoshClient,
|
||||||
|
host: String,
|
||||||
|
services: Vec<String>,
|
||||||
|
identity_files: Vec<PathBuf>,
|
||||||
|
session: Option<String>,
|
||||||
|
user: Option<String>,
|
||||||
|
udp_host: Option<String>,
|
||||||
|
udp_port: Option<u16>,
|
||||||
|
trust_on_first_use: Option<bool>,
|
||||||
|
use_ssh_agent: Option<bool>,
|
||||||
|
timeout: Option<Duration>,
|
||||||
|
env: Vec<EnvVar>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl DoshClientBuilder {
|
||||||
|
pub fn new(client: DoshClient, host: String) -> Self {
|
||||||
|
Self {
|
||||||
|
client,
|
||||||
|
host,
|
||||||
|
services: Vec::new(),
|
||||||
|
identity_files: Vec::new(),
|
||||||
|
session: None,
|
||||||
|
user: None,
|
||||||
|
udp_host: None,
|
||||||
|
udp_port: None,
|
||||||
|
trust_on_first_use: None,
|
||||||
|
use_ssh_agent: None,
|
||||||
|
timeout: None,
|
||||||
|
env: Vec::new(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn service(mut self, name: impl Into<String>) -> Self {
|
||||||
|
self.services.push(name.into());
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn services(mut self, names: impl IntoIterator<Item = impl Into<String>>) -> Self {
|
||||||
|
self.services.extend(names.into_iter().map(Into::into));
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn identity_file(mut self, path: impl Into<PathBuf>) -> Self {
|
||||||
|
self.identity_files.push(path.into());
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn session(mut self, session: impl Into<String>) -> Self {
|
||||||
|
self.session = Some(session.into());
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn user(mut self, user: impl Into<String>) -> Self {
|
||||||
|
self.user = Some(user.into());
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn udp_host(mut self, host: impl Into<String>) -> Self {
|
||||||
|
self.udp_host = Some(host.into());
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn udp_port(mut self, port: u16) -> Self {
|
||||||
|
self.udp_port = Some(port);
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn trust_on_first_use(mut self, trust: bool) -> Self {
|
||||||
|
self.trust_on_first_use = Some(trust);
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn use_ssh_agent(mut self, value: bool) -> Self {
|
||||||
|
self.use_ssh_agent = Some(value);
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn timeout(mut self, timeout: Duration) -> Self {
|
||||||
|
self.timeout = Some(timeout);
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn env(mut self, name: impl Into<String>, value: impl Into<String>) -> Self {
|
||||||
|
self.env.push(EnvVar {
|
||||||
|
name: name.into(),
|
||||||
|
value: value.into(),
|
||||||
|
});
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn connect(self) -> Result<ConnectedDoshClient> {
|
||||||
|
let host_config = self
|
||||||
|
.client
|
||||||
|
.hosts
|
||||||
|
.hosts
|
||||||
|
.get(&self.host)
|
||||||
|
.cloned()
|
||||||
|
.unwrap_or_default();
|
||||||
|
let raw_server = host_config.ssh.clone().unwrap_or_else(|| self.host.clone());
|
||||||
|
let requested_user = self
|
||||||
|
.user
|
||||||
|
.clone()
|
||||||
|
.or_else(|| host_config.user.clone())
|
||||||
|
.or_else(|| user_from_destination(&raw_server))
|
||||||
|
.or_else(|| std::env::var("USER").ok())
|
||||||
|
.unwrap_or_else(|| "unknown".to_string());
|
||||||
|
let udp_host = self
|
||||||
|
.udp_host
|
||||||
|
.clone()
|
||||||
|
.or_else(|| host_config.dosh_host.clone())
|
||||||
|
.or_else(|| self.client.config.dosh_host.clone())
|
||||||
|
.unwrap_or_else(|| destination_host(&raw_server));
|
||||||
|
let udp_port = self
|
||||||
|
.udp_port
|
||||||
|
.or(host_config.port)
|
||||||
|
.unwrap_or(self.client.config.dosh_port);
|
||||||
|
let peer_addr = resolve_addr(&udp_host, udp_port)?;
|
||||||
|
let timeout = self.timeout.unwrap_or_else(|| {
|
||||||
|
Duration::from_millis(self.client.config.native_auth_timeout_ms.max(1))
|
||||||
|
});
|
||||||
|
let session = self.session.unwrap_or_else(default_sdk_session);
|
||||||
|
let requested_forwardings = self
|
||||||
|
.services
|
||||||
|
.iter()
|
||||||
|
.map(|service| {
|
||||||
|
Ok(ForwardingRequest {
|
||||||
|
kind: ForwardingKind::Local,
|
||||||
|
bind_host: None,
|
||||||
|
listen_port: 0,
|
||||||
|
target_host: Some(crate::transport::service_target(service)?),
|
||||||
|
target_port: Some(0),
|
||||||
|
})
|
||||||
|
})
|
||||||
|
.collect::<Result<Vec<_>>>()?;
|
||||||
|
let socket = UdpSocket::bind("0.0.0.0:0").await?;
|
||||||
|
let (client_secret, client_public) = generate_native_ephemeral();
|
||||||
|
let hello = NativeClientHello {
|
||||||
|
protocol_version: native::NATIVE_PROTOCOL_VERSION,
|
||||||
|
client_random: crypto::random_32(),
|
||||||
|
client_ephemeral_public: client_public,
|
||||||
|
requested_host: self.host.clone(),
|
||||||
|
requested_user,
|
||||||
|
requested_session: session.clone(),
|
||||||
|
requested_mode: "forward-only".to_string(),
|
||||||
|
terminal_size: (80, 24),
|
||||||
|
supported_aead: vec!["chacha20poly1305".to_string()],
|
||||||
|
supported_user_key_algorithms: supported_user_key_algorithms(),
|
||||||
|
cached_host_key_fingerprint: None,
|
||||||
|
attach_ticket_envelope: None,
|
||||||
|
requested_env: self.env,
|
||||||
|
};
|
||||||
|
let packet = protocol::encode_plain(
|
||||||
|
PacketKind::NativeClientHello,
|
||||||
|
[0u8; 16],
|
||||||
|
1,
|
||||||
|
0,
|
||||||
|
&protocol::to_body(&NativeClientHelloBody {
|
||||||
|
hello: hello.clone(),
|
||||||
|
})?,
|
||||||
|
)?;
|
||||||
|
send_udp_retrying_transient(&socket, &packet, peer_addr, timeout).await?;
|
||||||
|
|
||||||
|
let mut buf = vec![0u8; 65535];
|
||||||
|
let (n, _) = recv_udp_retrying_transient(&socket, &mut buf, timeout).await?;
|
||||||
|
let packet = protocol::decode(&buf[..n])?;
|
||||||
|
if packet.header.kind != PacketKind::NativeServerHello {
|
||||||
|
if packet.header.kind == PacketKind::AttachReject {
|
||||||
|
let reject: AttachReject = protocol::from_body(&packet.body)?;
|
||||||
|
bail!("native auth rejected: {}", reject.reason);
|
||||||
|
}
|
||||||
|
bail!("native auth received unexpected server response");
|
||||||
|
}
|
||||||
|
let server_hello: NativeServerHelloBody = protocol::from_body(&packet.body)?;
|
||||||
|
verify_server_hello(&hello, &server_hello.hello)?;
|
||||||
|
verify_or_trust_host(
|
||||||
|
&self.client.config,
|
||||||
|
&self.host,
|
||||||
|
&server_hello.hello.host_key,
|
||||||
|
self.trust_on_first_use,
|
||||||
|
)?;
|
||||||
|
|
||||||
|
let session_key = derive_native_session_key(
|
||||||
|
&client_secret,
|
||||||
|
server_hello.hello.server_ephemeral_public,
|
||||||
|
&hello,
|
||||||
|
&server_hello.hello,
|
||||||
|
)?;
|
||||||
|
let auth = sign_auth(
|
||||||
|
&self.client.config,
|
||||||
|
&host_config,
|
||||||
|
&hello,
|
||||||
|
&server_hello.hello,
|
||||||
|
requested_forwardings,
|
||||||
|
self.identity_files,
|
||||||
|
self.use_ssh_agent,
|
||||||
|
)?;
|
||||||
|
let mut pending_id = [0u8; 16];
|
||||||
|
pending_id.copy_from_slice(&server_hello.hello.auth_challenge[..16]);
|
||||||
|
let auth_packet = protocol::encode_encrypted(
|
||||||
|
PacketKind::NativeUserAuth,
|
||||||
|
pending_id,
|
||||||
|
2,
|
||||||
|
1,
|
||||||
|
&session_key,
|
||||||
|
CLIENT_TO_SERVER,
|
||||||
|
&protocol::to_body(&NativeUserAuthBody { auth })?,
|
||||||
|
)?;
|
||||||
|
send_udp_retrying_transient(&socket, &auth_packet, peer_addr, timeout).await?;
|
||||||
|
let (n, _) = recv_udp_retrying_transient(&socket, &mut buf, timeout).await?;
|
||||||
|
let packet = protocol::decode(&buf[..n])?;
|
||||||
|
if packet.header.kind != PacketKind::NativeAuthOk {
|
||||||
|
if packet.header.kind == PacketKind::AttachReject {
|
||||||
|
let reject: AttachReject = protocol::from_body(&packet.body)?;
|
||||||
|
bail!("native auth rejected: {}", reject.reason);
|
||||||
|
}
|
||||||
|
bail!("native auth received unexpected auth response");
|
||||||
|
}
|
||||||
|
let plain = protocol::decrypt_body(&packet, &session_key, SERVER_TO_CLIENT)?;
|
||||||
|
let ok: NativeAuthOkBody = protocol::from_body(&plain)?;
|
||||||
|
let transport = DoshTransport::new_owned(
|
||||||
|
socket,
|
||||||
|
SessionTransportConfig {
|
||||||
|
role: SessionRole::Client,
|
||||||
|
conn_id: ok.ok.client_id,
|
||||||
|
session_key: ok.ok.session_key,
|
||||||
|
peer_addr,
|
||||||
|
initial_send_seq: 2,
|
||||||
|
initial_ack: ok.ok.initial_seq,
|
||||||
|
stream: TransportConfig::default(),
|
||||||
|
},
|
||||||
|
);
|
||||||
|
Ok(ConnectedDoshClient {
|
||||||
|
host: self.host,
|
||||||
|
session: ok.ok.session,
|
||||||
|
transport,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct ConnectedDoshClient {
|
||||||
|
pub host: String,
|
||||||
|
pub session: String,
|
||||||
|
pub transport: DoshTransport,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ConnectedDoshClient {
|
||||||
|
pub fn into_transport(self) -> DoshTransport {
|
||||||
|
self.transport
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn verify_or_trust_host(
|
||||||
|
config: &ClientConfig,
|
||||||
|
host: &str,
|
||||||
|
host_key: &native::HostPublicKey,
|
||||||
|
trust_override: Option<bool>,
|
||||||
|
) -> Result<()> {
|
||||||
|
let known_hosts = expand_tilde(&config.known_hosts);
|
||||||
|
match verify_known_host(&known_hosts, host, host_key)? {
|
||||||
|
KnownHostStatus::Trusted => Ok(()),
|
||||||
|
KnownHostStatus::Unknown if trust_override.unwrap_or(config.trust_on_first_use) => {
|
||||||
|
trust_host(&known_hosts, host, host_key, "sdk-tofu", false)?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
KnownHostStatus::Unknown => Err(anyhow!(
|
||||||
|
"Dosh host key for {host} is not trusted; run `dosh trust {host}` first or enable trust_on_first_use"
|
||||||
|
)),
|
||||||
|
KnownHostStatus::Mismatch { expected, actual } => Err(anyhow!(
|
||||||
|
"Dosh host key mismatch for {host}: expected {expected}, got {actual}"
|
||||||
|
)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn sign_auth(
|
||||||
|
config: &ClientConfig,
|
||||||
|
host_config: &HostConfig,
|
||||||
|
hello: &NativeClientHello,
|
||||||
|
server_hello: &native::NativeServerHello,
|
||||||
|
requested_forwardings: Vec<ForwardingRequest>,
|
||||||
|
explicit_identity_files: Vec<PathBuf>,
|
||||||
|
use_ssh_agent: Option<bool>,
|
||||||
|
) -> Result<native::NativeUserAuth> {
|
||||||
|
let use_agent = use_ssh_agent.unwrap_or(config.use_ssh_agent);
|
||||||
|
let mut errors = Vec::new();
|
||||||
|
if use_agent {
|
||||||
|
match ssh_agent::sign_user_auth_with_agent(
|
||||||
|
hello,
|
||||||
|
server_hello,
|
||||||
|
requested_forwardings.clone(),
|
||||||
|
) {
|
||||||
|
Ok(auth) => return Ok(auth),
|
||||||
|
Err(err) => errors.push(format!("ssh-agent: {err:#}")),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut paths = explicit_identity_files;
|
||||||
|
if paths.is_empty() {
|
||||||
|
paths.extend(config.identity_files.iter().map(|path| expand_tilde(path)));
|
||||||
|
}
|
||||||
|
if paths.is_empty() {
|
||||||
|
paths.extend(default_identity_paths());
|
||||||
|
}
|
||||||
|
for path in paths {
|
||||||
|
match native::load_native_identity(&path).and_then(|identity| {
|
||||||
|
sign_user_auth_with_private_key(
|
||||||
|
&identity,
|
||||||
|
hello,
|
||||||
|
server_hello,
|
||||||
|
requested_forwardings.clone(),
|
||||||
|
)
|
||||||
|
}) {
|
||||||
|
Ok(auth) => return Ok(auth),
|
||||||
|
Err(err) => errors.push(format!("{}: {err:#}", path.display())),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let _ = host_config;
|
||||||
|
Err(anyhow!(
|
||||||
|
"native auth found no usable identity: {}",
|
||||||
|
errors.join("; ")
|
||||||
|
))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn default_identity_paths() -> Vec<PathBuf> {
|
||||||
|
["~/.ssh/id_ed25519", "~/.ssh/id_ecdsa", "~/.ssh/id_rsa"]
|
||||||
|
.into_iter()
|
||||||
|
.map(expand_tilde)
|
||||||
|
.collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn resolve_addr(host: &str, port: u16) -> Result<SocketAddr> {
|
||||||
|
(host, port)
|
||||||
|
.to_socket_addrs()
|
||||||
|
.with_context(|| format!("resolve UDP target {host}:{port}"))?
|
||||||
|
.next()
|
||||||
|
.ok_or_else(|| anyhow!("no UDP address resolved for {host}:{port}"))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn destination_host(destination: &str) -> String {
|
||||||
|
destination
|
||||||
|
.rsplit('@')
|
||||||
|
.next()
|
||||||
|
.unwrap_or(destination)
|
||||||
|
.split(':')
|
||||||
|
.next()
|
||||||
|
.unwrap_or(destination)
|
||||||
|
.to_string()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn user_from_destination(destination: &str) -> Option<String> {
|
||||||
|
destination
|
||||||
|
.rsplit_once('@')
|
||||||
|
.map(|(user, _)| user.to_string())
|
||||||
|
.filter(|user| !user.is_empty())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn default_sdk_session() -> String {
|
||||||
|
let millis = SystemTime::now()
|
||||||
|
.duration_since(UNIX_EPOCH)
|
||||||
|
.unwrap_or_default()
|
||||||
|
.as_millis();
|
||||||
|
format!("sdk-{millis}-{}", std::process::id())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn parses_user_and_host_from_destination() {
|
||||||
|
assert_eq!(
|
||||||
|
user_from_destination("palav@example.com").as_deref(),
|
||||||
|
Some("palav")
|
||||||
|
);
|
||||||
|
assert_eq!(destination_host("palav@example.com"), "example.com");
|
||||||
|
assert_eq!(destination_host("example.com:2222"), "example.com");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn default_identity_paths_are_expanded() {
|
||||||
|
assert!(
|
||||||
|
default_identity_paths()
|
||||||
|
.iter()
|
||||||
|
.any(|path| path.ends_with(".ssh/id_ed25519"))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -5,6 +5,7 @@ use std::fs;
|
|||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
|
||||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
|
#[serde(default)]
|
||||||
pub struct ServerConfig {
|
pub struct ServerConfig {
|
||||||
pub port: u16,
|
pub port: u16,
|
||||||
pub bind: String,
|
pub bind: String,
|
||||||
@@ -46,6 +47,10 @@ pub struct ServerConfig {
|
|||||||
pub allow_remote_non_loopback_bind: bool,
|
pub allow_remote_non_loopback_bind: bool,
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
pub allow_agent_forwarding: bool,
|
pub allow_agent_forwarding: bool,
|
||||||
|
#[serde(default = "default_true")]
|
||||||
|
pub allow_file_transfer: bool,
|
||||||
|
#[serde(default = "default_true")]
|
||||||
|
pub allow_exec_command: bool,
|
||||||
#[serde(default = "default_accept_env")]
|
#[serde(default = "default_accept_env")]
|
||||||
pub accept_env: Vec<String>,
|
pub accept_env: Vec<String>,
|
||||||
/// Run terminal sessions under per-session holder processes so shells
|
/// Run terminal sessions under per-session holder processes so shells
|
||||||
@@ -82,6 +87,8 @@ impl Default for ServerConfig {
|
|||||||
allow_remote_forwarding: false,
|
allow_remote_forwarding: false,
|
||||||
allow_remote_non_loopback_bind: false,
|
allow_remote_non_loopback_bind: false,
|
||||||
allow_agent_forwarding: false,
|
allow_agent_forwarding: false,
|
||||||
|
allow_file_transfer: true,
|
||||||
|
allow_exec_command: true,
|
||||||
accept_env: default_accept_env(),
|
accept_env: default_accept_env(),
|
||||||
persist_sessions: true,
|
persist_sessions: true,
|
||||||
}
|
}
|
||||||
@@ -89,6 +96,7 @@ impl Default for ServerConfig {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
|
#[serde(default)]
|
||||||
pub struct ClientConfig {
|
pub struct ClientConfig {
|
||||||
pub update_repo: Option<String>,
|
pub update_repo: Option<String>,
|
||||||
pub update_port: Option<u16>,
|
pub update_port: Option<u16>,
|
||||||
@@ -144,6 +152,12 @@ pub struct ClientConfig {
|
|||||||
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
|
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
|
||||||
pub struct HostConfig {
|
pub struct HostConfig {
|
||||||
pub ssh: Option<String>,
|
pub ssh: Option<String>,
|
||||||
|
/// Optional OpenSSH config host name to use for SSH-backed bootstrap,
|
||||||
|
/// trust/setup/doctor checks, and native-auth identity selection. This lets
|
||||||
|
/// a Dosh host keep a separate UDP endpoint while still inheriting
|
||||||
|
/// alias-scoped OpenSSH settings like IdentityFile, ProxyJump, and SendEnv.
|
||||||
|
#[serde(default)]
|
||||||
|
pub ssh_config: Option<String>,
|
||||||
pub user: Option<String>,
|
pub user: Option<String>,
|
||||||
pub dosh_host: Option<String>,
|
pub dosh_host: Option<String>,
|
||||||
pub port: Option<u16>,
|
pub port: Option<u16>,
|
||||||
@@ -318,3 +332,63 @@ pub fn expand_tilde(path: &str) -> PathBuf {
|
|||||||
}
|
}
|
||||||
PathBuf::from(path)
|
PathBuf::from(path)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::{ClientConfig, ServerConfig};
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn old_client_configs_fill_new_fields_from_defaults() {
|
||||||
|
let raw = r#"
|
||||||
|
server = "palav"
|
||||||
|
dosh_port = 50000
|
||||||
|
"#;
|
||||||
|
|
||||||
|
let config: ClientConfig = toml::from_str(raw).expect("parse old client config");
|
||||||
|
|
||||||
|
assert_eq!(config.server, "palav");
|
||||||
|
assert!(config.predict);
|
||||||
|
assert_eq!(config.predict_mode, "experimental");
|
||||||
|
assert_eq!(config.auth_preference, "native,ssh");
|
||||||
|
assert_eq!(config.native_auth_timeout_ms, 700);
|
||||||
|
assert_eq!(config.known_hosts, "~/.config/dosh/known_hosts");
|
||||||
|
assert_eq!(config.identity_files, vec!["~/.ssh/id_ed25519"]);
|
||||||
|
assert!(config.use_ssh_agent);
|
||||||
|
assert!(!config.forward_agent);
|
||||||
|
assert!(config.disconnect_status);
|
||||||
|
assert_eq!(config.escape_key, "^]");
|
||||||
|
assert!(config.send_env.iter().any(|value| value == "TERM"));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn old_server_configs_fill_new_fields_from_defaults() {
|
||||||
|
let raw = r#"
|
||||||
|
port = 50000
|
||||||
|
"#;
|
||||||
|
|
||||||
|
let config: ServerConfig = toml::from_str(raw).expect("parse old server config");
|
||||||
|
|
||||||
|
assert_eq!(config.port, 50000);
|
||||||
|
assert_eq!(config.bind, "0.0.0.0");
|
||||||
|
assert_eq!(config.client_timeout_secs, 2_592_000);
|
||||||
|
assert_eq!(config.output_frame_interval_ms, 16);
|
||||||
|
assert!(config.native_auth);
|
||||||
|
assert_eq!(config.host_key, "~/.config/dosh/host_key");
|
||||||
|
assert!(
|
||||||
|
config
|
||||||
|
.authorized_keys
|
||||||
|
.iter()
|
||||||
|
.any(|path| path == "~/.ssh/authorized_keys")
|
||||||
|
);
|
||||||
|
assert_eq!(config.native_auth_rate_limit_per_minute, 30);
|
||||||
|
assert_eq!(config.rekey_after_packets, 100_000);
|
||||||
|
assert_eq!(config.rekey_after_secs, 3600);
|
||||||
|
assert!(config.allow_tcp_forwarding);
|
||||||
|
assert!(!config.allow_remote_forwarding);
|
||||||
|
assert!(!config.allow_agent_forwarding);
|
||||||
|
assert!(config.allow_file_transfer);
|
||||||
|
assert!(config.allow_exec_command);
|
||||||
|
assert!(config.persist_sessions);
|
||||||
|
assert!(config.accept_env.iter().any(|value| value == "TERM"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -0,0 +1,92 @@
|
|||||||
|
use anyhow::{Context, Result, bail};
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
pub const EXEC_STREAM_SENTINEL: &str = "@dosh-exec";
|
||||||
|
pub const FRAME_MAX_LEN: usize = 1024 * 1024;
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
|
||||||
|
pub struct ExecRequest {
|
||||||
|
pub command: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
|
||||||
|
pub enum ExecResponse {
|
||||||
|
Stdout(Vec<u8>),
|
||||||
|
Stderr(Vec<u8>),
|
||||||
|
Exit { code: Option<i32> },
|
||||||
|
Error { message: String },
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Default)]
|
||||||
|
pub struct FrameDecoder {
|
||||||
|
buf: Vec<u8>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl FrameDecoder {
|
||||||
|
pub fn push(&mut self, bytes: &[u8]) -> Result<Vec<Vec<u8>>> {
|
||||||
|
self.buf.extend_from_slice(bytes);
|
||||||
|
let mut frames = Vec::new();
|
||||||
|
loop {
|
||||||
|
if self.buf.len() < 4 {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
let len = u32::from_be_bytes(self.buf[..4].try_into().unwrap()) as usize;
|
||||||
|
if len > FRAME_MAX_LEN {
|
||||||
|
self.buf.clear();
|
||||||
|
bail!("exec protocol frame too large: {len} bytes");
|
||||||
|
}
|
||||||
|
if self.buf.len() < 4 + len {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
frames.push(self.buf[4..4 + len].to_vec());
|
||||||
|
self.buf.drain(..4 + len);
|
||||||
|
}
|
||||||
|
Ok(frames)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn encode_request(request: &ExecRequest) -> Result<Vec<u8>> {
|
||||||
|
encode_frame(&bincode::serialize(request).context("encode exec request")?)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn encode_response(response: &ExecResponse) -> Result<Vec<u8>> {
|
||||||
|
encode_frame(&bincode::serialize(response).context("encode exec response")?)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn decode_request(frame: &[u8]) -> Result<ExecRequest> {
|
||||||
|
bincode::deserialize(frame).context("decode exec request")
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn decode_response(frame: &[u8]) -> Result<ExecResponse> {
|
||||||
|
bincode::deserialize(frame).context("decode exec response")
|
||||||
|
}
|
||||||
|
|
||||||
|
fn encode_frame(payload: &[u8]) -> Result<Vec<u8>> {
|
||||||
|
if payload.len() > FRAME_MAX_LEN {
|
||||||
|
bail!("exec protocol frame too large: {} bytes", payload.len());
|
||||||
|
}
|
||||||
|
let mut out = Vec::with_capacity(4 + payload.len());
|
||||||
|
out.extend_from_slice(&(payload.len() as u32).to_be_bytes());
|
||||||
|
out.extend_from_slice(payload);
|
||||||
|
Ok(out)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn oversized_frame_clears_decoder_state() {
|
||||||
|
let mut decoder = FrameDecoder::default();
|
||||||
|
let too_large = ((FRAME_MAX_LEN + 1) as u32).to_be_bytes();
|
||||||
|
assert!(decoder.push(&too_large).is_err());
|
||||||
|
|
||||||
|
let valid = encode_response(&ExecResponse::Exit { code: Some(0) }).unwrap();
|
||||||
|
let frames = decoder.push(&valid).unwrap();
|
||||||
|
assert_eq!(frames.len(), 1);
|
||||||
|
assert_eq!(
|
||||||
|
decode_response(&frames[0]).unwrap(),
|
||||||
|
ExecResponse::Exit { code: Some(0) }
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,344 @@
|
|||||||
|
use anyhow::{Context, Result, anyhow, bail};
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use std::path::{Path, PathBuf};
|
||||||
|
|
||||||
|
pub const FILE_STREAM_SENTINEL: &str = "@dosh-file";
|
||||||
|
pub const FRAME_MAX_LEN: usize = 1024 * 1024;
|
||||||
|
pub const CHUNK_SIZE: usize = 32 * 1024;
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
|
||||||
|
pub enum FileRequest {
|
||||||
|
Stat {
|
||||||
|
path: String,
|
||||||
|
},
|
||||||
|
List {
|
||||||
|
path: String,
|
||||||
|
},
|
||||||
|
Mkdir {
|
||||||
|
path: String,
|
||||||
|
mode: Option<u32>,
|
||||||
|
},
|
||||||
|
Readlink {
|
||||||
|
path: String,
|
||||||
|
},
|
||||||
|
Symlink {
|
||||||
|
path: String,
|
||||||
|
target: String,
|
||||||
|
overwrite: bool,
|
||||||
|
},
|
||||||
|
Remove {
|
||||||
|
path: String,
|
||||||
|
recursive: bool,
|
||||||
|
},
|
||||||
|
PutStart {
|
||||||
|
path: String,
|
||||||
|
size: u64,
|
||||||
|
mode: Option<u32>,
|
||||||
|
modified_secs: Option<u64>,
|
||||||
|
overwrite: bool,
|
||||||
|
resume: bool,
|
||||||
|
},
|
||||||
|
PutChunk {
|
||||||
|
offset: u64,
|
||||||
|
bytes: Vec<u8>,
|
||||||
|
},
|
||||||
|
PutFinish {
|
||||||
|
sha256: [u8; 32],
|
||||||
|
},
|
||||||
|
Get {
|
||||||
|
path: String,
|
||||||
|
offset: u64,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
|
||||||
|
pub enum FileResponse {
|
||||||
|
Ok,
|
||||||
|
Resume {
|
||||||
|
offset: u64,
|
||||||
|
prefix_sha256: Option<[u8; 32]>,
|
||||||
|
},
|
||||||
|
Stat {
|
||||||
|
meta: FileMeta,
|
||||||
|
},
|
||||||
|
List {
|
||||||
|
entries: Vec<FileEntry>,
|
||||||
|
},
|
||||||
|
LinkTarget {
|
||||||
|
target: String,
|
||||||
|
},
|
||||||
|
Start {
|
||||||
|
meta: FileMeta,
|
||||||
|
offset: u64,
|
||||||
|
prefix_sha256: Option<[u8; 32]>,
|
||||||
|
},
|
||||||
|
Chunk {
|
||||||
|
offset: u64,
|
||||||
|
bytes: Vec<u8>,
|
||||||
|
},
|
||||||
|
Done {
|
||||||
|
sha256: [u8; 32],
|
||||||
|
bytes: u64,
|
||||||
|
},
|
||||||
|
Error {
|
||||||
|
message: String,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
|
||||||
|
pub struct FileMeta {
|
||||||
|
pub path: String,
|
||||||
|
pub kind: FileKind,
|
||||||
|
pub len: u64,
|
||||||
|
pub mode: Option<u32>,
|
||||||
|
pub modified_secs: Option<u64>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
|
||||||
|
pub enum FileKind {
|
||||||
|
File,
|
||||||
|
Directory,
|
||||||
|
Symlink,
|
||||||
|
Other,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
|
||||||
|
pub struct FileEntry {
|
||||||
|
pub name: String,
|
||||||
|
pub meta: FileMeta,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Default)]
|
||||||
|
pub struct FrameDecoder {
|
||||||
|
buf: Vec<u8>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl FrameDecoder {
|
||||||
|
pub fn push(&mut self, bytes: &[u8]) -> Result<Vec<Vec<u8>>> {
|
||||||
|
self.buf.extend_from_slice(bytes);
|
||||||
|
let mut frames = Vec::new();
|
||||||
|
loop {
|
||||||
|
if self.buf.len() < 4 {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
let len = u32::from_be_bytes(self.buf[..4].try_into().unwrap()) as usize;
|
||||||
|
if len > FRAME_MAX_LEN {
|
||||||
|
self.buf.clear();
|
||||||
|
bail!("file protocol frame too large: {len} bytes");
|
||||||
|
}
|
||||||
|
if self.buf.len() < 4 + len {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
frames.push(self.buf[4..4 + len].to_vec());
|
||||||
|
self.buf.drain(..4 + len);
|
||||||
|
}
|
||||||
|
Ok(frames)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn encode_request(request: &FileRequest) -> Result<Vec<u8>> {
|
||||||
|
encode_frame(&bincode::serialize(request).context("encode file request")?)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn encode_response(response: &FileResponse) -> Result<Vec<u8>> {
|
||||||
|
encode_frame(&bincode::serialize(response).context("encode file response")?)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn decode_request(frame: &[u8]) -> Result<FileRequest> {
|
||||||
|
bincode::deserialize(frame).context("decode file request")
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn decode_response(frame: &[u8]) -> Result<FileResponse> {
|
||||||
|
bincode::deserialize(frame).context("decode file response")
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn encode_frame(payload: &[u8]) -> Result<Vec<u8>> {
|
||||||
|
if payload.len() > FRAME_MAX_LEN {
|
||||||
|
bail!("file protocol frame too large: {} bytes", payload.len());
|
||||||
|
}
|
||||||
|
let mut out = Vec::with_capacity(4 + payload.len());
|
||||||
|
out.extend_from_slice(&(payload.len() as u32).to_be_bytes());
|
||||||
|
out.extend_from_slice(payload);
|
||||||
|
Ok(out)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn parse_copy_endpoint(raw: &str) -> CopyEndpoint {
|
||||||
|
if looks_like_windows_path(raw) {
|
||||||
|
return CopyEndpoint::Local(PathBuf::from(raw));
|
||||||
|
}
|
||||||
|
if let Some((host, path)) = parse_bracketed_remote_endpoint(raw) {
|
||||||
|
return CopyEndpoint::Remote { host, path };
|
||||||
|
}
|
||||||
|
if raw.starts_with('[') {
|
||||||
|
return CopyEndpoint::Local(PathBuf::from(raw));
|
||||||
|
}
|
||||||
|
let Some(index) = raw.find(':') else {
|
||||||
|
return CopyEndpoint::Local(PathBuf::from(raw));
|
||||||
|
};
|
||||||
|
let host = &raw[..index];
|
||||||
|
let path = &raw[index + 1..];
|
||||||
|
if host.is_empty() || host.contains('/') || host.contains('\\') {
|
||||||
|
return CopyEndpoint::Local(PathBuf::from(raw));
|
||||||
|
}
|
||||||
|
CopyEndpoint::Remote {
|
||||||
|
host: host.to_string(),
|
||||||
|
path: if path.is_empty() {
|
||||||
|
".".to_string()
|
||||||
|
} else {
|
||||||
|
path.to_string()
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parse_bracketed_remote_endpoint(raw: &str) -> Option<(String, String)> {
|
||||||
|
let rest = raw.strip_prefix('[')?;
|
||||||
|
let close = rest.find(']')?;
|
||||||
|
let host = &rest[..close];
|
||||||
|
let suffix = &rest[close + 1..];
|
||||||
|
if host.is_empty() || host.contains('/') || host.contains('\\') || !suffix.starts_with(':') {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
let path = &suffix[1..];
|
||||||
|
Some((
|
||||||
|
host.to_string(),
|
||||||
|
if path.is_empty() {
|
||||||
|
".".to_string()
|
||||||
|
} else {
|
||||||
|
path.to_string()
|
||||||
|
},
|
||||||
|
))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||||
|
pub enum CopyEndpoint {
|
||||||
|
Local(PathBuf),
|
||||||
|
Remote { host: String, path: String },
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn remote_join(parent: &str, child: &str) -> String {
|
||||||
|
if parent.is_empty() || parent == "." {
|
||||||
|
return child.to_string();
|
||||||
|
}
|
||||||
|
format!("{}/{}", parent.trim_end_matches('/'), child)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn remote_basename(path: &str) -> String {
|
||||||
|
path.trim_end_matches('/')
|
||||||
|
.rsplit('/')
|
||||||
|
.next()
|
||||||
|
.filter(|value| !value.is_empty())
|
||||||
|
.unwrap_or(path)
|
||||||
|
.to_string()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn clean_remote_path(path: &str, home: &Path) -> Result<PathBuf> {
|
||||||
|
if path.as_bytes().contains(&0) {
|
||||||
|
bail!("remote path contains NUL");
|
||||||
|
}
|
||||||
|
let expanded = if path == "~" {
|
||||||
|
home.to_path_buf()
|
||||||
|
} else if let Some(rest) = path.strip_prefix("~/") {
|
||||||
|
home.join(rest)
|
||||||
|
} else {
|
||||||
|
let raw = Path::new(path);
|
||||||
|
if raw.is_absolute() {
|
||||||
|
raw.to_path_buf()
|
||||||
|
} else {
|
||||||
|
home.join(raw)
|
||||||
|
}
|
||||||
|
};
|
||||||
|
Ok(expanded)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn ensure_relative_child(path: &Path) -> Result<String> {
|
||||||
|
let name = path
|
||||||
|
.file_name()
|
||||||
|
.and_then(|name| name.to_str())
|
||||||
|
.ok_or_else(|| anyhow!("path has no final component: {}", path.display()))?;
|
||||||
|
if name.is_empty() || name == "." || name == ".." {
|
||||||
|
bail!("invalid path final component: {}", path.display());
|
||||||
|
}
|
||||||
|
Ok(name.to_string())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn looks_like_windows_path(raw: &str) -> bool {
|
||||||
|
let bytes = raw.as_bytes();
|
||||||
|
bytes.len() >= 3
|
||||||
|
&& bytes[0].is_ascii_alphabetic()
|
||||||
|
&& bytes[1] == b':'
|
||||||
|
&& (bytes[2] == b'\\' || bytes[2] == b'/')
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn frames_round_trip_incrementally() {
|
||||||
|
let first = encode_response(&FileResponse::Ok).unwrap();
|
||||||
|
let second = encode_response(&FileResponse::Error {
|
||||||
|
message: "nope".to_string(),
|
||||||
|
})
|
||||||
|
.unwrap();
|
||||||
|
let mut decoder = FrameDecoder::default();
|
||||||
|
assert!(decoder.push(&first[..2]).unwrap().is_empty());
|
||||||
|
let frames = decoder.push(&first[2..]).unwrap();
|
||||||
|
assert_eq!(frames.len(), 1);
|
||||||
|
assert_eq!(decode_response(&frames[0]).unwrap(), FileResponse::Ok);
|
||||||
|
let frames = decoder.push(&second).unwrap();
|
||||||
|
assert_eq!(frames.len(), 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn oversized_frame_clears_decoder_state() {
|
||||||
|
let mut decoder = FrameDecoder::default();
|
||||||
|
let too_large = ((FRAME_MAX_LEN + 1) as u32).to_be_bytes();
|
||||||
|
assert!(decoder.push(&too_large).is_err());
|
||||||
|
|
||||||
|
let valid = encode_response(&FileResponse::Ok).unwrap();
|
||||||
|
let frames = decoder.push(&valid).unwrap();
|
||||||
|
assert_eq!(frames.len(), 1);
|
||||||
|
assert_eq!(decode_response(&frames[0]).unwrap(), FileResponse::Ok);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn copy_endpoint_parses_scp_style_but_not_windows_drive() {
|
||||||
|
assert_eq!(
|
||||||
|
parse_copy_endpoint("palav:tmp/file"),
|
||||||
|
CopyEndpoint::Remote {
|
||||||
|
host: "palav".to_string(),
|
||||||
|
path: "tmp/file".to_string()
|
||||||
|
}
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
parse_copy_endpoint("C:\\Users\\palav\\x"),
|
||||||
|
CopyEndpoint::Local(PathBuf::from("C:\\Users\\palav\\x"))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn copy_endpoint_parses_bracketed_ipv6_remote_paths() {
|
||||||
|
assert_eq!(
|
||||||
|
parse_copy_endpoint("[2001:db8::1]:/var/log/syslog"),
|
||||||
|
CopyEndpoint::Remote {
|
||||||
|
host: "2001:db8::1".to_string(),
|
||||||
|
path: "/var/log/syslog".to_string()
|
||||||
|
}
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
parse_copy_endpoint("[::1]:"),
|
||||||
|
CopyEndpoint::Remote {
|
||||||
|
host: "::1".to_string(),
|
||||||
|
path: ".".to_string()
|
||||||
|
}
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
parse_copy_endpoint("[bad/host]:path"),
|
||||||
|
CopyEndpoint::Local(PathBuf::from("[bad/host]:path"))
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
parse_copy_endpoint("[::1]"),
|
||||||
|
CopyEndpoint::Local(PathBuf::from("[::1]"))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
+18
@@ -1,11 +1,29 @@
|
|||||||
|
//! Dosh is an encrypted UDP transport for remote terminals and embeddable Rust
|
||||||
|
//! application streams.
|
||||||
|
//!
|
||||||
|
//! Use [`client::DoshClient`] and [`server::DoshServer`] when you want Dosh to
|
||||||
|
//! handle native authentication, host key checks, roaming, keepalives, reliable
|
||||||
|
//! streams, and service routing. Use [`transport::DoshTransport`] only when an
|
||||||
|
//! application already owns session establishment and wants direct access to the
|
||||||
|
//! encrypted stream transport. The SDK receive loops drive retransmission and
|
||||||
|
//! keepalives while waiting, so simple applications can call `recv().await`
|
||||||
|
//! without building their own maintenance loop.
|
||||||
|
|
||||||
pub mod auth;
|
pub mod auth;
|
||||||
pub mod build_info;
|
pub mod build_info;
|
||||||
|
pub mod client;
|
||||||
pub mod config;
|
pub mod config;
|
||||||
pub mod crypto;
|
pub mod crypto;
|
||||||
|
pub mod exec_service;
|
||||||
|
pub mod file_transfer;
|
||||||
pub mod native;
|
pub mod native;
|
||||||
#[cfg(unix)]
|
#[cfg(unix)]
|
||||||
pub mod persist;
|
pub mod persist;
|
||||||
pub mod protocol;
|
pub mod protocol;
|
||||||
#[cfg(unix)]
|
#[cfg(unix)]
|
||||||
pub mod pty;
|
pub mod pty;
|
||||||
|
pub mod server;
|
||||||
pub mod ssh_agent;
|
pub mod ssh_agent;
|
||||||
|
pub mod trace;
|
||||||
|
pub mod transport;
|
||||||
|
pub mod udp;
|
||||||
|
|||||||
+36
-1
@@ -23,12 +23,14 @@ use std::io::{Read, Write};
|
|||||||
use std::os::fd::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
|
use std::os::fd::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
|
||||||
use std::os::unix::net::{UnixListener, UnixStream};
|
use std::os::unix::net::{UnixListener, UnixStream};
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
|
use std::sync::Mutex;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
/// One-byte commands a server sends to a holder over its control socket after
|
/// One-byte commands a server sends to a holder over its control socket after
|
||||||
/// the holder has handed back the master fd.
|
/// the holder has handed back the master fd.
|
||||||
const HOLDER_CMD_SHUTDOWN: u8 = b'X';
|
const HOLDER_CMD_SHUTDOWN: u8 = b'X';
|
||||||
const HOLDER_STARTUP_TIMEOUT: Duration = Duration::from_millis(750);
|
const HOLDER_STARTUP_TIMEOUT: Duration = Duration::from_millis(750);
|
||||||
|
static SCREEN_WRITE_LOCK: Mutex<()> = Mutex::new(());
|
||||||
|
|
||||||
/// Magic written into a holder's `meta` file, bumped if the on-disk layout
|
/// Magic written into a holder's `meta` file, bumped if the on-disk layout
|
||||||
/// changes so a stale holder from an incompatible build is ignored.
|
/// changes so a stale holder from an incompatible build is ignored.
|
||||||
@@ -140,13 +142,28 @@ pub fn save_screen(
|
|||||||
// No holder runtime for this session (non-persistent): nothing to do.
|
// No holder runtime for this session (non-persistent): nothing to do.
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
let _guard = SCREEN_WRITE_LOCK
|
||||||
|
.lock()
|
||||||
|
.map_err(|_| anyhow!("screen persistence lock poisoned"))?;
|
||||||
|
let path = screen_path(&dir);
|
||||||
|
if existing_screen_output_seq(&path).is_some_and(|existing| existing >= output_seq) {
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
let mut buf = Vec::with_capacity(snapshot.len() + 32);
|
let mut buf = Vec::with_capacity(snapshot.len() + 32);
|
||||||
buf.extend_from_slice(&cols.to_be_bytes());
|
buf.extend_from_slice(&cols.to_be_bytes());
|
||||||
buf.extend_from_slice(&rows.to_be_bytes());
|
buf.extend_from_slice(&rows.to_be_bytes());
|
||||||
buf.extend_from_slice(&output_seq.to_be_bytes());
|
buf.extend_from_slice(&output_seq.to_be_bytes());
|
||||||
buf.extend_from_slice(&(snapshot.len() as u32).to_be_bytes());
|
buf.extend_from_slice(&(snapshot.len() as u32).to_be_bytes());
|
||||||
buf.extend_from_slice(snapshot);
|
buf.extend_from_slice(snapshot);
|
||||||
atomic_write(&screen_path(&dir), &buf)
|
atomic_write(&path, &buf)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn existing_screen_output_seq(path: &Path) -> Option<u64> {
|
||||||
|
let data = std::fs::read(path).ok()?;
|
||||||
|
if data.len() < 12 {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
Some(u64::from_be_bytes(data[4..12].try_into().ok()?))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A restored screen for a re-adopted session.
|
/// A restored screen for a re-adopted session.
|
||||||
@@ -435,6 +452,10 @@ pub fn run_holder(
|
|||||||
let mut cmd = [0u8; 1];
|
let mut cmd = [0u8; 1];
|
||||||
match stream.read(&mut cmd) {
|
match stream.read(&mut cmd) {
|
||||||
Ok(1) if cmd[0] == HOLDER_CMD_SHUTDOWN => {
|
Ok(1) if cmd[0] == HOLDER_CMD_SHUTDOWN => {
|
||||||
|
if shell_pid > 0 {
|
||||||
|
let _ = unsafe { libc::kill(shell_pid, libc::SIGHUP) };
|
||||||
|
let _ = unsafe { libc::kill(shell_pid, libc::SIGTERM) };
|
||||||
|
}
|
||||||
let _ = std::fs::remove_dir_all(runtime_dir);
|
let _ = std::fs::remove_dir_all(runtime_dir);
|
||||||
std::process::exit(0);
|
std::process::exit(0);
|
||||||
}
|
}
|
||||||
@@ -615,6 +636,20 @@ mod tests {
|
|||||||
assert_eq!(loaded.snapshot, snap);
|
assert_eq!(loaded.snapshot, snap);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn save_screen_never_overwrites_newer_snapshot_with_older_seq() {
|
||||||
|
let tmp = tempfile::tempdir().unwrap();
|
||||||
|
let sessions_dir = tmp.path();
|
||||||
|
ensure_runtime_dir(sessions_dir, "work").unwrap();
|
||||||
|
|
||||||
|
save_screen(sessions_dir, "work", 80, 24, 10, b"new-screen").unwrap();
|
||||||
|
save_screen(sessions_dir, "work", 80, 24, 9, b"old-screen").unwrap();
|
||||||
|
|
||||||
|
let loaded = load_screen(sessions_dir, "work").expect("screen restored");
|
||||||
|
assert_eq!(loaded.output_seq, 10);
|
||||||
|
assert_eq!(loaded.snapshot, b"new-screen");
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn load_screen_absent_is_none() {
|
fn load_screen_absent_is_none() {
|
||||||
let tmp = tempfile::tempdir().unwrap();
|
let tmp = tempfile::tempdir().unwrap();
|
||||||
|
|||||||
+16
-8
@@ -5,10 +5,11 @@ use anyhow::{Context, Result, bail};
|
|||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use std::time::{SystemTime, UNIX_EPOCH};
|
use std::time::{SystemTime, UNIX_EPOCH};
|
||||||
|
|
||||||
/// Generate a name for an implicit, ephemeral terminal session — the kind
|
/// Generate a name for an implicit terminal session: the kind created by
|
||||||
/// created by `dosh host` with no `--session`. Single source of truth shared by
|
/// `dosh host` with no `--session`. Single source of truth shared by the client
|
||||||
/// the client (which generates it) and the server (which recognizes it via
|
/// (which generates it) and the server (which recognizes it via
|
||||||
/// [`is_implicit_session_name`] to decide a session is NOT worth persisting).
|
/// [`is_implicit_session_name`] to apply shorter cleanup grace than named
|
||||||
|
/// sessions).
|
||||||
pub fn generate_implicit_session_name() -> String {
|
pub fn generate_implicit_session_name() -> String {
|
||||||
let millis = SystemTime::now()
|
let millis = SystemTime::now()
|
||||||
.duration_since(UNIX_EPOCH)
|
.duration_since(UNIX_EPOCH)
|
||||||
@@ -18,9 +19,9 @@ pub fn generate_implicit_session_name() -> String {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Whether `name` is a client-generated implicit session name
|
/// Whether `name` is a client-generated implicit session name
|
||||||
/// (`term-<digits>-<digits>`). Such sessions are ephemeral: the user can never
|
/// (`term-<digits>-<digits>`). Such sessions are update-survivable while the
|
||||||
/// reattach by name, so the server must not persist them across restarts.
|
/// original client can reconnect with its ticket, but they are reaped sooner
|
||||||
/// Explicitly-named (`--session work`) and prewarmed sessions are not implicit.
|
/// than explicitly-named sessions once no clients remain.
|
||||||
pub fn is_implicit_session_name(name: &str) -> bool {
|
pub fn is_implicit_session_name(name: &str) -> bool {
|
||||||
let Some(rest) = name.strip_prefix("term-") else {
|
let Some(rest) = name.strip_prefix("term-") else {
|
||||||
return false;
|
return false;
|
||||||
@@ -272,6 +273,9 @@ pub fn decode(input: &[u8]) -> Result<Packet> {
|
|||||||
if input.len() < end {
|
if input.len() < end {
|
||||||
bail!("truncated packet body");
|
bail!("truncated packet body");
|
||||||
}
|
}
|
||||||
|
if input.len() > end {
|
||||||
|
bail!("trailing packet bytes");
|
||||||
|
}
|
||||||
Ok(Packet {
|
Ok(Packet {
|
||||||
header,
|
header,
|
||||||
body: input[HEADER_LEN..end].to_vec(),
|
body: input[HEADER_LEN..end].to_vec(),
|
||||||
@@ -280,7 +284,7 @@ pub fn decode(input: &[u8]) -> Result<Packet> {
|
|||||||
|
|
||||||
pub fn decrypt_body(packet: &Packet, key: &[u8; 32], direction: u32) -> Result<Vec<u8>> {
|
pub fn decrypt_body(packet: &Packet, key: &[u8; 32], direction: u32) -> Result<Vec<u8>> {
|
||||||
if packet.header.flags & 1 == 0 {
|
if packet.header.flags & 1 == 0 {
|
||||||
return Ok(packet.body.clone());
|
bail!("packet body is not encrypted");
|
||||||
}
|
}
|
||||||
let nonce = crypto::nonce_from(direction, packet.header.seq);
|
let nonce = crypto::nonce_from(direction, packet.header.seq);
|
||||||
let aad = packet.header.aad();
|
let aad = packet.header.aad();
|
||||||
@@ -357,6 +361,10 @@ pub struct NativeAuthCheckOkBody {
|
|||||||
pub allow_tcp_forwarding: bool,
|
pub allow_tcp_forwarding: bool,
|
||||||
pub allow_remote_forwarding: bool,
|
pub allow_remote_forwarding: bool,
|
||||||
pub allow_agent_forwarding: bool,
|
pub allow_agent_forwarding: bool,
|
||||||
|
#[serde(default)]
|
||||||
|
pub allow_file_transfer: bool,
|
||||||
|
#[serde(default)]
|
||||||
|
pub allow_exec_command: bool,
|
||||||
pub policy_flags: Vec<String>,
|
pub policy_flags: Vec<String>,
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
pub server_version: String,
|
pub server_version: String,
|
||||||
|
|||||||
+807
@@ -0,0 +1,807 @@
|
|||||||
|
use crate::config::{ServerConfig, load_server_config};
|
||||||
|
use crate::crypto;
|
||||||
|
use crate::native::{
|
||||||
|
self, ForwardingKind, ForwardingRequest, NativeAuthOk, NativeServerHello,
|
||||||
|
derive_native_session_key, generate_native_ephemeral, host_public_key, load_or_create_host_key,
|
||||||
|
sign_server_hello, verify_native_user_auth_from_config,
|
||||||
|
};
|
||||||
|
use crate::protocol::{
|
||||||
|
self, AttachReject, CLIENT_TO_SERVER, NativeAuthOkBody, NativeClientHelloBody,
|
||||||
|
NativeServerHelloBody, NativeUserAuthBody, PacketKind, SERVER_TO_CLIENT,
|
||||||
|
};
|
||||||
|
use crate::transport::{
|
||||||
|
ADAPTIVE_RETRANSMIT_MIN, DoshTransport, SessionEvent, SessionRole, SessionTransportConfig,
|
||||||
|
TransportConfig, service_name_from_target,
|
||||||
|
};
|
||||||
|
use crate::udp::{is_transient_udp_error, is_transient_udp_send_error};
|
||||||
|
use anyhow::{Context, Result, anyhow, bail};
|
||||||
|
use ed25519_dalek::SigningKey;
|
||||||
|
use std::collections::{HashMap, HashSet};
|
||||||
|
use std::net::SocketAddr;
|
||||||
|
use std::path::PathBuf;
|
||||||
|
use std::sync::Arc;
|
||||||
|
use std::time::{Duration, Instant};
|
||||||
|
use tokio::net::UdpSocket;
|
||||||
|
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub struct DoshServerConfig {
|
||||||
|
pub server: ServerConfig,
|
||||||
|
pub bind_addr: Option<SocketAddr>,
|
||||||
|
pub services: HashSet<String>,
|
||||||
|
pub transport: TransportConfig,
|
||||||
|
pub require_current_user: bool,
|
||||||
|
pub auth_timeout: Duration,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl DoshServerConfig {
|
||||||
|
pub fn new(server: ServerConfig) -> Self {
|
||||||
|
Self {
|
||||||
|
server,
|
||||||
|
bind_addr: None,
|
||||||
|
services: HashSet::new(),
|
||||||
|
transport: TransportConfig::default(),
|
||||||
|
require_current_user: true,
|
||||||
|
auth_timeout: Duration::from_secs(30),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn bind_addr(mut self, addr: SocketAddr) -> Self {
|
||||||
|
self.bind_addr = Some(addr);
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn service(mut self, name: impl Into<String>) -> Result<Self> {
|
||||||
|
self.services.insert(validate_service_name(name.into())?);
|
||||||
|
Ok(self)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn services(mut self, names: impl IntoIterator<Item = impl Into<String>>) -> Result<Self> {
|
||||||
|
for name in names {
|
||||||
|
self.services.insert(validate_service_name(name.into())?);
|
||||||
|
}
|
||||||
|
Ok(self)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn require_current_user(mut self, value: bool) -> Self {
|
||||||
|
self.require_current_user = value;
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn transport(mut self, transport: TransportConfig) -> Self {
|
||||||
|
self.transport = transport;
|
||||||
|
self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for DoshServerConfig {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self::new(ServerConfig::default())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub struct DoshAccepted {
|
||||||
|
pub conn_id: [u8; 16],
|
||||||
|
pub user: String,
|
||||||
|
pub session: String,
|
||||||
|
pub services: Vec<String>,
|
||||||
|
pub peer_addr: SocketAddr,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub enum DoshServerEvent {
|
||||||
|
Accepted(DoshAccepted),
|
||||||
|
Session {
|
||||||
|
conn_id: [u8; 16],
|
||||||
|
event: SessionEvent,
|
||||||
|
},
|
||||||
|
Ignored,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
struct PendingServerAuth {
|
||||||
|
client: native::NativeClientHello,
|
||||||
|
server: NativeServerHello,
|
||||||
|
session_key: [u8; 32],
|
||||||
|
peer: SocketAddr,
|
||||||
|
created_at: Instant,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct DoshServer {
|
||||||
|
socket: Arc<UdpSocket>,
|
||||||
|
config: DoshServerConfig,
|
||||||
|
host_signing: SigningKey,
|
||||||
|
pending: HashMap<[u8; 16], PendingServerAuth>,
|
||||||
|
transports: HashMap<[u8; 16], DoshTransport>,
|
||||||
|
accepted: HashMap<[u8; 16], DoshAccepted>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl DoshServer {
|
||||||
|
pub async fn load() -> Result<Self> {
|
||||||
|
Self::bind(DoshServerConfig::new(load_server_config(None)?)).await
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn load_from_path(path: Option<PathBuf>) -> Result<Self> {
|
||||||
|
Self::bind(DoshServerConfig::new(load_server_config(path)?)).await
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn bind(config: DoshServerConfig) -> Result<Self> {
|
||||||
|
let bind_addr = match config.bind_addr {
|
||||||
|
Some(addr) => addr,
|
||||||
|
None => format!("{}:{}", config.server.bind, config.server.port)
|
||||||
|
.parse()
|
||||||
|
.with_context(|| {
|
||||||
|
format!(
|
||||||
|
"parse Dosh bind address {}:{}",
|
||||||
|
config.server.bind, config.server.port
|
||||||
|
)
|
||||||
|
})?,
|
||||||
|
};
|
||||||
|
let host_signing = load_or_create_host_key(&config.server)?;
|
||||||
|
let socket = Arc::new(UdpSocket::bind(bind_addr).await?);
|
||||||
|
Ok(Self {
|
||||||
|
socket,
|
||||||
|
config,
|
||||||
|
host_signing,
|
||||||
|
pending: HashMap::new(),
|
||||||
|
transports: HashMap::new(),
|
||||||
|
accepted: HashMap::new(),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn local_addr(&self) -> Result<SocketAddr> {
|
||||||
|
Ok(self.socket.local_addr()?)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn connection(&self, conn_id: &[u8; 16]) -> Option<&DoshAccepted> {
|
||||||
|
self.accepted.get(conn_id)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn transport(&self, conn_id: &[u8; 16]) -> Option<&DoshTransport> {
|
||||||
|
self.transports.get(conn_id)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn transport_mut(&mut self, conn_id: &[u8; 16]) -> Option<&mut DoshTransport> {
|
||||||
|
self.transports.get_mut(conn_id)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn recv(&mut self) -> Result<DoshServerEvent> {
|
||||||
|
let mut buf = vec![0u8; 65535];
|
||||||
|
loop {
|
||||||
|
self.expire_pending();
|
||||||
|
let (n, peer) = match tokio::time::timeout(
|
||||||
|
ADAPTIVE_RETRANSMIT_MIN,
|
||||||
|
self.socket.recv_from(&mut buf),
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
Ok(Ok(value)) => value,
|
||||||
|
Ok(Err(err)) if is_transient_udp_error(&err) => continue,
|
||||||
|
Ok(Err(err)) => return Err(err.into()),
|
||||||
|
Err(_) => {
|
||||||
|
self.maintenance().await?;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
if let Some(event) = self.handle_datagram(peer, &buf[..n]).await? {
|
||||||
|
return Ok(event);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn handle_datagram(
|
||||||
|
&mut self,
|
||||||
|
peer: SocketAddr,
|
||||||
|
datagram: &[u8],
|
||||||
|
) -> Result<Option<DoshServerEvent>> {
|
||||||
|
let packet = match protocol::decode(datagram) {
|
||||||
|
Ok(packet) => packet,
|
||||||
|
Err(_) => return Ok(None),
|
||||||
|
};
|
||||||
|
if packet.header.kind == PacketKind::NativeClientHello {
|
||||||
|
self.handle_client_hello(peer, packet.body).await?;
|
||||||
|
return Ok(Some(DoshServerEvent::Ignored));
|
||||||
|
}
|
||||||
|
if packet.header.kind == PacketKind::NativeUserAuth {
|
||||||
|
return self.handle_user_auth(peer, &packet).await.map(Some);
|
||||||
|
}
|
||||||
|
if let Some(transport) = self.transports.get_mut(&packet.header.conn_id) {
|
||||||
|
let event = transport.handle_datagram(datagram, peer).await?;
|
||||||
|
return Ok(Some(DoshServerEvent::Session {
|
||||||
|
conn_id: packet.header.conn_id,
|
||||||
|
event,
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
self.send_reject(peer, packet.header.conn_id, "unknown Dosh connection")
|
||||||
|
.await?;
|
||||||
|
Ok(Some(DoshServerEvent::Ignored))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn accept_stream(&mut self, conn_id: [u8; 16], stream_id: u64) -> Result<()> {
|
||||||
|
self.transport_mut(&conn_id)
|
||||||
|
.ok_or_else(|| anyhow!("unknown Dosh connection"))?
|
||||||
|
.accept_stream(stream_id)
|
||||||
|
.await
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn reject_stream(
|
||||||
|
&mut self,
|
||||||
|
conn_id: [u8; 16],
|
||||||
|
stream_id: u64,
|
||||||
|
reason: impl Into<String>,
|
||||||
|
) -> Result<()> {
|
||||||
|
self.transport_mut(&conn_id)
|
||||||
|
.ok_or_else(|| anyhow!("unknown Dosh connection"))?
|
||||||
|
.reject_stream(stream_id, reason)
|
||||||
|
.await
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn send(
|
||||||
|
&mut self,
|
||||||
|
conn_id: [u8; 16],
|
||||||
|
stream_id: u64,
|
||||||
|
bytes: impl Into<Vec<u8>>,
|
||||||
|
) -> Result<()> {
|
||||||
|
self.transport_mut(&conn_id)
|
||||||
|
.ok_or_else(|| anyhow!("unknown Dosh connection"))?
|
||||||
|
.send(stream_id, bytes)
|
||||||
|
.await
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn close(&mut self, conn_id: [u8; 16], stream_id: u64) -> Result<()> {
|
||||||
|
self.transport_mut(&conn_id)
|
||||||
|
.ok_or_else(|| anyhow!("unknown Dosh connection"))?
|
||||||
|
.close(stream_id)
|
||||||
|
.await
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn maintenance(&mut self) -> Result<()> {
|
||||||
|
for transport in self.transports.values_mut() {
|
||||||
|
transport.maintenance().await?;
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn handle_client_hello(&mut self, peer: SocketAddr, body: Vec<u8>) -> Result<()> {
|
||||||
|
let req: NativeClientHelloBody = protocol::from_body(&body)?;
|
||||||
|
if let Err(err) =
|
||||||
|
native::check_native_protocol_version(req.hello.protocol_version, "client")
|
||||||
|
{
|
||||||
|
self.send_reject(peer, [0u8; 16], &err.to_string()).await?;
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
let result = self.build_server_hello(req.hello, peer);
|
||||||
|
let (pending_id, hello) = match result {
|
||||||
|
Ok(value) => value,
|
||||||
|
Err(err) => {
|
||||||
|
self.send_reject(peer, [0u8; 16], &err.to_string()).await?;
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
};
|
||||||
|
let body = protocol::to_body(&NativeServerHelloBody { hello })?;
|
||||||
|
let out = protocol::encode_plain(PacketKind::NativeServerHello, pending_id, 1, 0, &body)?;
|
||||||
|
let _ = send_udp(&self.socket, &out, peer).await?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn build_server_hello(
|
||||||
|
&mut self,
|
||||||
|
client: native::NativeClientHello,
|
||||||
|
peer: SocketAddr,
|
||||||
|
) -> Result<([u8; 16], NativeServerHello)> {
|
||||||
|
if !self.config.server.native_auth {
|
||||||
|
bail!("native auth disabled");
|
||||||
|
}
|
||||||
|
if !client
|
||||||
|
.supported_aead
|
||||||
|
.iter()
|
||||||
|
.any(|algorithm| algorithm == "chacha20poly1305")
|
||||||
|
{
|
||||||
|
bail!("native auth requires chacha20poly1305");
|
||||||
|
}
|
||||||
|
if !client
|
||||||
|
.supported_user_key_algorithms
|
||||||
|
.iter()
|
||||||
|
.any(|algorithm| native::is_supported_user_signature_algorithm(algorithm))
|
||||||
|
{
|
||||||
|
bail!("native auth requires a supported user key algorithm");
|
||||||
|
}
|
||||||
|
if self.config.require_current_user {
|
||||||
|
let current_user = std::env::var("USER").unwrap_or_else(|_| "unknown".to_string());
|
||||||
|
if client.requested_user != current_user {
|
||||||
|
bail!("native auth user mismatch");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let (server_secret, server_public) = generate_native_ephemeral();
|
||||||
|
let mut server = NativeServerHello {
|
||||||
|
protocol_version: native::NATIVE_PROTOCOL_VERSION,
|
||||||
|
server_random: crypto::random_32(),
|
||||||
|
server_ephemeral_public: server_public,
|
||||||
|
host_key: host_public_key(&self.host_signing),
|
||||||
|
chosen_aead: "chacha20poly1305".to_string(),
|
||||||
|
server_key_epoch: 1,
|
||||||
|
auth_challenge: crypto::random_32(),
|
||||||
|
rate_limit_remaining: None,
|
||||||
|
host_signature: Vec::new(),
|
||||||
|
};
|
||||||
|
sign_server_hello(&self.host_signing, &client, &mut server)?;
|
||||||
|
let session_key = derive_native_session_key(
|
||||||
|
&server_secret,
|
||||||
|
client.client_ephemeral_public,
|
||||||
|
&client,
|
||||||
|
&server,
|
||||||
|
)?;
|
||||||
|
let mut pending_id = [0u8; 16];
|
||||||
|
pending_id.copy_from_slice(&server.auth_challenge[..16]);
|
||||||
|
self.pending.insert(
|
||||||
|
pending_id,
|
||||||
|
PendingServerAuth {
|
||||||
|
client,
|
||||||
|
server: server.clone(),
|
||||||
|
session_key,
|
||||||
|
peer,
|
||||||
|
created_at: Instant::now(),
|
||||||
|
},
|
||||||
|
);
|
||||||
|
Ok((pending_id, server))
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn handle_user_auth(
|
||||||
|
&mut self,
|
||||||
|
peer: SocketAddr,
|
||||||
|
packet: &protocol::Packet,
|
||||||
|
) -> Result<DoshServerEvent> {
|
||||||
|
let Some(pending) = self.pending.get(&packet.header.conn_id).cloned() else {
|
||||||
|
self.send_reject(peer, packet.header.conn_id, "unknown native auth")
|
||||||
|
.await?;
|
||||||
|
return Ok(DoshServerEvent::Ignored);
|
||||||
|
};
|
||||||
|
if pending.peer != peer {
|
||||||
|
self.send_reject(peer, packet.header.conn_id, "native auth peer changed")
|
||||||
|
.await?;
|
||||||
|
return Ok(DoshServerEvent::Ignored);
|
||||||
|
}
|
||||||
|
if pending.created_at.elapsed() > self.config.auth_timeout {
|
||||||
|
self.pending.remove(&packet.header.conn_id);
|
||||||
|
self.send_reject(peer, packet.header.conn_id, "native auth expired")
|
||||||
|
.await?;
|
||||||
|
return Ok(DoshServerEvent::Ignored);
|
||||||
|
}
|
||||||
|
|
||||||
|
let body = match protocol::decrypt_body(packet, &pending.session_key, CLIENT_TO_SERVER) {
|
||||||
|
Ok(body) => body,
|
||||||
|
Err(_) => {
|
||||||
|
self.send_reject(peer, packet.header.conn_id, "native auth decrypt failed")
|
||||||
|
.await?;
|
||||||
|
return Ok(DoshServerEvent::Ignored);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
let req: NativeUserAuthBody = match protocol::from_body(&body) {
|
||||||
|
Ok(req) => req,
|
||||||
|
Err(_) => {
|
||||||
|
self.send_reject(peer, packet.header.conn_id, "invalid native auth body")
|
||||||
|
.await?;
|
||||||
|
return Ok(DoshServerEvent::Ignored);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
let services = match self.verify_auth_and_services(&pending, &req.auth) {
|
||||||
|
Ok(services) => services,
|
||||||
|
Err(err) => {
|
||||||
|
self.send_reject(peer, packet.header.conn_id, &format!("{err:#}"))
|
||||||
|
.await?;
|
||||||
|
return Ok(DoshServerEvent::Ignored);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
self.pending.remove(&packet.header.conn_id);
|
||||||
|
|
||||||
|
let conn_id = crypto::random_16();
|
||||||
|
let key_id = protocol::session_key_id(&pending.session_key);
|
||||||
|
let ok = NativeAuthOk {
|
||||||
|
client_id: conn_id,
|
||||||
|
session: pending.client.requested_session.clone(),
|
||||||
|
mode: pending.client.requested_mode.clone(),
|
||||||
|
session_key: pending.session_key,
|
||||||
|
session_key_id: key_id,
|
||||||
|
attach_ticket: Vec::new(),
|
||||||
|
attach_ticket_psk: crypto::random_32(),
|
||||||
|
initial_seq: 1,
|
||||||
|
snapshot: Vec::new(),
|
||||||
|
policy_flags: services
|
||||||
|
.iter()
|
||||||
|
.map(|service| format!("service:{service}"))
|
||||||
|
.collect(),
|
||||||
|
};
|
||||||
|
let body = protocol::to_body(&NativeAuthOkBody { ok })?;
|
||||||
|
let out = protocol::encode_encrypted(
|
||||||
|
PacketKind::NativeAuthOk,
|
||||||
|
conn_id,
|
||||||
|
1,
|
||||||
|
packet.header.seq,
|
||||||
|
&pending.session_key,
|
||||||
|
SERVER_TO_CLIENT,
|
||||||
|
&body,
|
||||||
|
)?;
|
||||||
|
let _ = send_udp(&self.socket, &out, peer).await?;
|
||||||
|
|
||||||
|
let transport = DoshTransport::new(
|
||||||
|
Arc::clone(&self.socket),
|
||||||
|
SessionTransportConfig {
|
||||||
|
role: SessionRole::Server,
|
||||||
|
conn_id,
|
||||||
|
session_key: pending.session_key,
|
||||||
|
peer_addr: peer,
|
||||||
|
initial_send_seq: 2,
|
||||||
|
initial_ack: packet.header.seq,
|
||||||
|
stream: self.config.transport.clone(),
|
||||||
|
},
|
||||||
|
);
|
||||||
|
let accepted = DoshAccepted {
|
||||||
|
conn_id,
|
||||||
|
user: pending.client.requested_user,
|
||||||
|
session: pending.client.requested_session,
|
||||||
|
services,
|
||||||
|
peer_addr: peer,
|
||||||
|
};
|
||||||
|
self.transports.insert(conn_id, transport);
|
||||||
|
self.accepted.insert(conn_id, accepted.clone());
|
||||||
|
Ok(DoshServerEvent::Accepted(accepted))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn verify_auth_and_services(
|
||||||
|
&self,
|
||||||
|
pending: &PendingServerAuth,
|
||||||
|
auth: &native::NativeUserAuth,
|
||||||
|
) -> Result<Vec<String>> {
|
||||||
|
verify_native_user_auth_from_config(
|
||||||
|
&self.config.server,
|
||||||
|
&pending.client,
|
||||||
|
&pending.server,
|
||||||
|
auth,
|
||||||
|
Some(pending.peer.ip()),
|
||||||
|
)
|
||||||
|
.context("verify native user auth")?;
|
||||||
|
validate_requested_services(&self.config.services, &auth.requested_forwardings)
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn send_reject(&self, peer: SocketAddr, conn_id: [u8; 16], reason: &str) -> Result<()> {
|
||||||
|
let body = protocol::to_body(&AttachReject {
|
||||||
|
reason: reason.to_string(),
|
||||||
|
})?;
|
||||||
|
let out = protocol::encode_plain(PacketKind::AttachReject, conn_id, 0, 0, &body)?;
|
||||||
|
let _ = send_udp(&self.socket, &out, peer).await?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn expire_pending(&mut self) {
|
||||||
|
let timeout = self.config.auth_timeout;
|
||||||
|
self.pending
|
||||||
|
.retain(|_, pending| pending.created_at.elapsed() <= timeout);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn send_udp(socket: &UdpSocket, packet: &[u8], peer: SocketAddr) -> Result<bool> {
|
||||||
|
match socket.send_to(packet, peer).await {
|
||||||
|
Ok(_) => Ok(true),
|
||||||
|
Err(err) if is_transient_udp_send_error(&err) => Ok(false),
|
||||||
|
Err(err) => Err(err).with_context(|| format!("send UDP packet to {peer}")),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn validate_requested_services(
|
||||||
|
allowed_services: &HashSet<String>,
|
||||||
|
requests: &[ForwardingRequest],
|
||||||
|
) -> Result<Vec<String>> {
|
||||||
|
let mut services = Vec::new();
|
||||||
|
for request in requests {
|
||||||
|
if request.kind != ForwardingKind::Local {
|
||||||
|
bail!("embedded Dosh services only accept local service requests");
|
||||||
|
}
|
||||||
|
if request.listen_port != 0 || request.target_port != Some(0) {
|
||||||
|
bail!("embedded Dosh service requests must use port 0");
|
||||||
|
}
|
||||||
|
let target = request
|
||||||
|
.target_host
|
||||||
|
.as_deref()
|
||||||
|
.ok_or_else(|| anyhow!("embedded Dosh service request is missing target host"))?;
|
||||||
|
let service = service_name_from_target(target)
|
||||||
|
.ok_or_else(|| anyhow!("target {target:?} is not a Dosh service"))?;
|
||||||
|
if !allowed_services.contains(service) {
|
||||||
|
bail!("Dosh service {service:?} is not registered");
|
||||||
|
}
|
||||||
|
if !services.iter().any(|existing| existing == service) {
|
||||||
|
services.push(service.to_string());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(services)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn validate_service_name(name: String) -> Result<String> {
|
||||||
|
crate::transport::service_target(&name)?;
|
||||||
|
Ok(name)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
use crate::client::DoshClient;
|
||||||
|
use crate::config::{ClientConfig, HostsConfig};
|
||||||
|
use crate::protocol::{CLIENT_TO_SERVER, NativeUserAuthBody};
|
||||||
|
use crate::transport::TransportEvent;
|
||||||
|
use ed25519_dalek::SigningKey;
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn sdk_client_and_server_exchange_service_stream() {
|
||||||
|
let dir = tempfile::tempdir().unwrap();
|
||||||
|
let host_key = dir.path().join("host_key");
|
||||||
|
let authorized_keys = dir.path().join("authorized_keys");
|
||||||
|
let identity = dir.path().join("id_ed25519");
|
||||||
|
let known_hosts = dir.path().join("known_hosts");
|
||||||
|
|
||||||
|
let signing = SigningKey::from_bytes(&[91u8; 32]);
|
||||||
|
let keypair = ssh_key::private::Ed25519Keypair::from(&signing);
|
||||||
|
let private =
|
||||||
|
ssh_key::PrivateKey::new(ssh_key::private::KeypairData::from(keypair), "").unwrap();
|
||||||
|
private
|
||||||
|
.write_openssh_file(&identity, ssh_key::LineEnding::LF)
|
||||||
|
.unwrap();
|
||||||
|
std::fs::write(
|
||||||
|
&authorized_keys,
|
||||||
|
format!("{}\n", private.public_key().to_openssh().unwrap()),
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let server_config = ServerConfig {
|
||||||
|
host_key: host_key.to_string_lossy().to_string(),
|
||||||
|
authorized_keys: vec![authorized_keys.to_string_lossy().to_string()],
|
||||||
|
..ServerConfig::default()
|
||||||
|
};
|
||||||
|
let server_config = DoshServerConfig::new(server_config)
|
||||||
|
.bind_addr("127.0.0.1:0".parse().unwrap())
|
||||||
|
.service("echo")
|
||||||
|
.unwrap()
|
||||||
|
.require_current_user(false);
|
||||||
|
let mut server = DoshServer::bind(server_config).await.unwrap();
|
||||||
|
let server_port = server.local_addr().unwrap().port();
|
||||||
|
|
||||||
|
let client_config = ClientConfig {
|
||||||
|
dosh_port: server_port,
|
||||||
|
trust_on_first_use: true,
|
||||||
|
known_hosts: known_hosts.to_string_lossy().to_string(),
|
||||||
|
identity_files: vec![identity.to_string_lossy().to_string()],
|
||||||
|
use_ssh_agent: false,
|
||||||
|
..ClientConfig::default()
|
||||||
|
};
|
||||||
|
let client = DoshClient::with_config(client_config, HostsConfig::default());
|
||||||
|
let connect = client
|
||||||
|
.connect("127.0.0.1")
|
||||||
|
.user("sdk-user")
|
||||||
|
.service("echo")
|
||||||
|
.connect();
|
||||||
|
let accept = async {
|
||||||
|
loop {
|
||||||
|
if let DoshServerEvent::Accepted(accepted) = server.recv().await.unwrap() {
|
||||||
|
break accepted;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
let (connected, accepted) = tokio::join!(connect, accept);
|
||||||
|
let mut client_transport = connected.unwrap().into_transport();
|
||||||
|
let conn_id = accepted.conn_id;
|
||||||
|
assert_eq!(accepted.services, vec!["echo".to_string()]);
|
||||||
|
|
||||||
|
let stream_id = client_transport.open_service("echo").await.unwrap();
|
||||||
|
match server.recv().await.unwrap() {
|
||||||
|
DoshServerEvent::Session {
|
||||||
|
event: SessionEvent::Stream(TransportEvent::Open(open)),
|
||||||
|
..
|
||||||
|
} => {
|
||||||
|
assert_eq!(open.stream_id, stream_id);
|
||||||
|
server.accept_stream(conn_id, open.stream_id).await.unwrap();
|
||||||
|
}
|
||||||
|
other => panic!("unexpected event {other:?}"),
|
||||||
|
}
|
||||||
|
assert!(matches!(
|
||||||
|
client_transport.recv().await.unwrap(),
|
||||||
|
SessionEvent::Stream(TransportEvent::OpenOk { .. })
|
||||||
|
));
|
||||||
|
|
||||||
|
client_transport
|
||||||
|
.send(stream_id, b"ping".to_vec())
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
loop {
|
||||||
|
match server.recv().await.unwrap() {
|
||||||
|
DoshServerEvent::Session {
|
||||||
|
event: SessionEvent::Stream(TransportEvent::Data(data)),
|
||||||
|
..
|
||||||
|
} => {
|
||||||
|
assert_eq!(data.chunks, vec![b"ping".to_vec()]);
|
||||||
|
server
|
||||||
|
.send(conn_id, data.stream_id, b"pong".to_vec())
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
DoshServerEvent::Session { .. } | DoshServerEvent::Ignored => {}
|
||||||
|
other => panic!("unexpected event {other:?}"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
loop {
|
||||||
|
match client_transport.recv().await.unwrap() {
|
||||||
|
SessionEvent::Stream(TransportEvent::Data(data)) => {
|
||||||
|
assert_eq!(data.chunks, vec![b"pong".to_vec()]);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
SessionEvent::Stream(_) | SessionEvent::Ignored => {}
|
||||||
|
other => panic!("unexpected event {other:?}"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn sdk_server_recv_drives_transport_maintenance_while_idle() {
|
||||||
|
let dir = tempfile::tempdir().unwrap();
|
||||||
|
let host_key = dir.path().join("host_key");
|
||||||
|
let authorized_keys = dir.path().join("authorized_keys");
|
||||||
|
let signing = SigningKey::from_bytes(&[94u8; 32]);
|
||||||
|
let keypair = ssh_key::private::Ed25519Keypair::from(&signing);
|
||||||
|
let private =
|
||||||
|
ssh_key::PrivateKey::new(ssh_key::private::KeypairData::from(keypair), "").unwrap();
|
||||||
|
std::fs::write(
|
||||||
|
&authorized_keys,
|
||||||
|
format!("{}\n", private.public_key().to_openssh().unwrap()),
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let server_config = ServerConfig {
|
||||||
|
host_key: host_key.to_string_lossy().to_string(),
|
||||||
|
authorized_keys: vec![authorized_keys.to_string_lossy().to_string()],
|
||||||
|
..ServerConfig::default()
|
||||||
|
};
|
||||||
|
let server_config = DoshServerConfig::new(server_config)
|
||||||
|
.bind_addr("127.0.0.1:0".parse().unwrap())
|
||||||
|
.service("echo")
|
||||||
|
.unwrap()
|
||||||
|
.require_current_user(false)
|
||||||
|
.transport(TransportConfig {
|
||||||
|
retransmit_after: Duration::from_millis(20),
|
||||||
|
keepalive_after: Duration::from_secs(60),
|
||||||
|
..TransportConfig::default()
|
||||||
|
});
|
||||||
|
let mut server = DoshServer::bind(server_config).await.unwrap();
|
||||||
|
let client_socket = UdpSocket::bind("127.0.0.1:0").await.unwrap();
|
||||||
|
let client_addr = client_socket.local_addr().unwrap();
|
||||||
|
let conn_id = [95u8; 16];
|
||||||
|
let session_key = [96u8; 32];
|
||||||
|
let transport = DoshTransport::new(
|
||||||
|
Arc::clone(&server.socket),
|
||||||
|
SessionTransportConfig {
|
||||||
|
role: SessionRole::Server,
|
||||||
|
conn_id,
|
||||||
|
session_key,
|
||||||
|
peer_addr: client_addr,
|
||||||
|
initial_send_seq: 1,
|
||||||
|
initial_ack: 0,
|
||||||
|
stream: server.config.transport.clone(),
|
||||||
|
},
|
||||||
|
);
|
||||||
|
server.transports.insert(conn_id, transport);
|
||||||
|
server
|
||||||
|
.transport_mut(&conn_id)
|
||||||
|
.unwrap()
|
||||||
|
.open_service("echo")
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let mut buf = [0u8; 65535];
|
||||||
|
tokio::time::timeout(
|
||||||
|
Duration::from_millis(200),
|
||||||
|
client_socket.recv_from(&mut buf),
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.unwrap()
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let recv_task = tokio::spawn(async move { server.recv().await });
|
||||||
|
let (n, _) =
|
||||||
|
tokio::time::timeout(Duration::from_secs(1), client_socket.recv_from(&mut buf))
|
||||||
|
.await
|
||||||
|
.unwrap()
|
||||||
|
.unwrap();
|
||||||
|
recv_task.abort();
|
||||||
|
|
||||||
|
let packet = protocol::decode(&buf[..n]).unwrap();
|
||||||
|
assert_eq!(packet.header.kind, PacketKind::StreamOpen);
|
||||||
|
let plain = protocol::decrypt_body(&packet, &session_key, SERVER_TO_CLIENT).unwrap();
|
||||||
|
let open: protocol::StreamOpen = protocol::from_body(&plain).unwrap();
|
||||||
|
assert_eq!(open.target_host, "@dosh-echo");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn bad_native_auth_does_not_consume_pending_challenge() {
|
||||||
|
let dir = tempfile::tempdir().unwrap();
|
||||||
|
let host_key = dir.path().join("host_key");
|
||||||
|
let authorized_keys = dir.path().join("authorized_keys");
|
||||||
|
let signing = SigningKey::from_bytes(&[93u8; 32]);
|
||||||
|
let keypair = ssh_key::private::Ed25519Keypair::from(&signing);
|
||||||
|
let private =
|
||||||
|
ssh_key::PrivateKey::new(ssh_key::private::KeypairData::from(keypair), "").unwrap();
|
||||||
|
std::fs::write(
|
||||||
|
&authorized_keys,
|
||||||
|
format!("{}\n", private.public_key().to_openssh().unwrap()),
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let server_config = ServerConfig {
|
||||||
|
host_key: host_key.to_string_lossy().to_string(),
|
||||||
|
authorized_keys: vec![authorized_keys.to_string_lossy().to_string()],
|
||||||
|
..ServerConfig::default()
|
||||||
|
};
|
||||||
|
let server_config = DoshServerConfig::new(server_config)
|
||||||
|
.bind_addr("127.0.0.1:0".parse().unwrap())
|
||||||
|
.require_current_user(false);
|
||||||
|
let mut server = DoshServer::bind(server_config).await.unwrap();
|
||||||
|
let peer: SocketAddr = "127.0.0.1:9".parse().unwrap();
|
||||||
|
let (client_secret, client_public) = native::generate_native_ephemeral();
|
||||||
|
let hello = native::NativeClientHello {
|
||||||
|
protocol_version: native::NATIVE_PROTOCOL_VERSION,
|
||||||
|
client_random: crypto::random_32(),
|
||||||
|
client_ephemeral_public: client_public,
|
||||||
|
requested_host: "127.0.0.1".to_string(),
|
||||||
|
requested_user: "sdk-user".to_string(),
|
||||||
|
requested_session: "test".to_string(),
|
||||||
|
requested_mode: "forward-only".to_string(),
|
||||||
|
terminal_size: (80, 24),
|
||||||
|
supported_aead: vec!["chacha20poly1305".to_string()],
|
||||||
|
supported_user_key_algorithms: vec!["ssh-ed25519".to_string()],
|
||||||
|
cached_host_key_fingerprint: None,
|
||||||
|
attach_ticket_envelope: None,
|
||||||
|
requested_env: Vec::new(),
|
||||||
|
};
|
||||||
|
let (pending_id, server_hello) = server.build_server_hello(hello.clone(), peer).unwrap();
|
||||||
|
let session_key = native::derive_native_session_key(
|
||||||
|
&client_secret,
|
||||||
|
server_hello.server_ephemeral_public,
|
||||||
|
&hello,
|
||||||
|
&server_hello,
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
let auth = native::sign_user_auth(&signing, &hello, &server_hello, Vec::new()).unwrap();
|
||||||
|
let body = protocol::to_body(&NativeUserAuthBody { auth }).unwrap();
|
||||||
|
let bad = protocol::encode_encrypted(
|
||||||
|
PacketKind::NativeUserAuth,
|
||||||
|
pending_id,
|
||||||
|
2,
|
||||||
|
1,
|
||||||
|
&[7u8; 32],
|
||||||
|
CLIENT_TO_SERVER,
|
||||||
|
&body,
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
let bad = protocol::decode(&bad).unwrap();
|
||||||
|
|
||||||
|
assert!(matches!(
|
||||||
|
server.handle_user_auth(peer, &bad).await.unwrap(),
|
||||||
|
DoshServerEvent::Ignored
|
||||||
|
));
|
||||||
|
assert!(server.pending.contains_key(&pending_id));
|
||||||
|
|
||||||
|
let valid = protocol::encode_encrypted(
|
||||||
|
PacketKind::NativeUserAuth,
|
||||||
|
pending_id,
|
||||||
|
2,
|
||||||
|
1,
|
||||||
|
&session_key,
|
||||||
|
CLIENT_TO_SERVER,
|
||||||
|
&body,
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
let valid = protocol::decode(&valid).unwrap();
|
||||||
|
assert!(matches!(
|
||||||
|
server.handle_user_auth(peer, &valid).await.unwrap(),
|
||||||
|
DoshServerEvent::Accepted(_)
|
||||||
|
));
|
||||||
|
assert!(!server.pending.contains_key(&pending_id));
|
||||||
|
}
|
||||||
|
}
|
||||||
+223
@@ -0,0 +1,223 @@
|
|||||||
|
use std::fs::{File, OpenOptions};
|
||||||
|
use std::io::Write;
|
||||||
|
use std::path::{Path, PathBuf};
|
||||||
|
use std::sync::{Mutex, OnceLock};
|
||||||
|
use std::time::{SystemTime, UNIX_EPOCH};
|
||||||
|
|
||||||
|
static TRACE_FILE: OnceLock<Option<Mutex<File>>> = OnceLock::new();
|
||||||
|
static HEALTH_FILE: OnceLock<Option<Mutex<File>>> = OnceLock::new();
|
||||||
|
static TRACE_BYTES: OnceLock<bool> = OnceLock::new();
|
||||||
|
const HEALTH_LOG_MAX_BYTES: u64 = 1024 * 1024;
|
||||||
|
|
||||||
|
pub fn enabled() -> bool {
|
||||||
|
TRACE_FILE.get_or_init(open_trace_file).is_some()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn bytes_enabled() -> bool {
|
||||||
|
*TRACE_BYTES.get_or_init(|| truthy_env("DOSH_TRACE_BYTES"))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn event(name: &str, fields: &[(&str, String)]) {
|
||||||
|
let Some(file) = TRACE_FILE.get_or_init(open_trace_file) else {
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
write_event(file, name, fields);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn health_event(name: &str, fields: &[(&str, String)]) {
|
||||||
|
let Some(file) = HEALTH_FILE.get_or_init(open_health_file) else {
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
write_event(file, name, fields);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn default_health_log_path() -> PathBuf {
|
||||||
|
trace_root().join("health.log")
|
||||||
|
}
|
||||||
|
|
||||||
|
fn write_event(file: &Mutex<File>, name: &str, fields: &[(&str, String)]) {
|
||||||
|
let now = SystemTime::now()
|
||||||
|
.duration_since(UNIX_EPOCH)
|
||||||
|
.map(|duration| duration.as_millis())
|
||||||
|
.unwrap_or(0);
|
||||||
|
let mut line = format!(
|
||||||
|
"ts_ms={} pid={} event={}",
|
||||||
|
now,
|
||||||
|
std::process::id(),
|
||||||
|
shell_escape(name)
|
||||||
|
);
|
||||||
|
for (key, value) in fields {
|
||||||
|
line.push(' ');
|
||||||
|
line.push_str(key);
|
||||||
|
line.push('=');
|
||||||
|
line.push_str(&shell_escape(value));
|
||||||
|
}
|
||||||
|
line.push('\n');
|
||||||
|
if let Ok(mut file) = file.lock() {
|
||||||
|
let _ = file.write_all(line.as_bytes());
|
||||||
|
let _ = file.flush();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn bytes_summary(bytes: &[u8]) -> String {
|
||||||
|
let mut parts = vec![
|
||||||
|
format!("len={}", bytes.len()),
|
||||||
|
format!(
|
||||||
|
"esc={}",
|
||||||
|
contains_byte(bytes, 0x1b) || contains_byte(bytes, 0x9b)
|
||||||
|
),
|
||||||
|
format!("focus={}", has_focus_report(bytes)),
|
||||||
|
format!("mouseish={}", looks_mouseish(bytes)),
|
||||||
|
format!("printable={}", printable_count(bytes)),
|
||||||
|
];
|
||||||
|
if bytes_enabled() {
|
||||||
|
parts.push(format!("hex={}", hex_prefix(bytes, 160)));
|
||||||
|
}
|
||||||
|
parts.join(",")
|
||||||
|
}
|
||||||
|
|
||||||
|
fn open_trace_file() -> Option<Mutex<File>> {
|
||||||
|
let raw = std::env::var_os("DOSH_TRACE")?;
|
||||||
|
let normalized = raw.to_string_lossy().to_ascii_lowercase();
|
||||||
|
if normalized.is_empty() || matches!(normalized.as_str(), "0" | "false" | "off") {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
let path = if matches!(normalized.as_str(), "1" | "true" | "on") {
|
||||||
|
default_trace_path()
|
||||||
|
} else {
|
||||||
|
PathBuf::from(raw)
|
||||||
|
};
|
||||||
|
if let Some(parent) = path.parent() {
|
||||||
|
let _ = std::fs::create_dir_all(parent);
|
||||||
|
}
|
||||||
|
OpenOptions::new()
|
||||||
|
.create(true)
|
||||||
|
.append(true)
|
||||||
|
.open(&path)
|
||||||
|
.ok()
|
||||||
|
.map(Mutex::new)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn open_health_file() -> Option<Mutex<File>> {
|
||||||
|
let path = health_log_path_from_env()?;
|
||||||
|
rotate_health_log_if_needed(&path);
|
||||||
|
if let Some(parent) = path.parent() {
|
||||||
|
let _ = std::fs::create_dir_all(parent);
|
||||||
|
}
|
||||||
|
OpenOptions::new()
|
||||||
|
.create(true)
|
||||||
|
.append(true)
|
||||||
|
.open(&path)
|
||||||
|
.ok()
|
||||||
|
.map(Mutex::new)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn health_log_path_from_env() -> Option<PathBuf> {
|
||||||
|
match std::env::var_os("DOSH_HEALTH_LOG") {
|
||||||
|
Some(raw) => {
|
||||||
|
let normalized = raw.to_string_lossy().to_ascii_lowercase();
|
||||||
|
if normalized.is_empty() || matches!(normalized.as_str(), "0" | "false" | "off") {
|
||||||
|
None
|
||||||
|
} else if matches!(normalized.as_str(), "1" | "true" | "on") {
|
||||||
|
Some(default_health_log_path())
|
||||||
|
} else {
|
||||||
|
Some(PathBuf::from(raw))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
None => Some(default_health_log_path()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn rotate_health_log_if_needed(path: &Path) {
|
||||||
|
let Ok(metadata) = std::fs::metadata(path) else {
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
if metadata.len() <= HEALTH_LOG_MAX_BYTES {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
let rotated = path.with_extension("log.1");
|
||||||
|
let _ = std::fs::remove_file(&rotated);
|
||||||
|
let _ = std::fs::rename(path, rotated);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn default_trace_path() -> PathBuf {
|
||||||
|
let exe = std::env::current_exe()
|
||||||
|
.ok()
|
||||||
|
.and_then(|path| {
|
||||||
|
path.file_stem()
|
||||||
|
.map(|stem| stem.to_string_lossy().to_string())
|
||||||
|
})
|
||||||
|
.unwrap_or_else(|| "dosh".to_string());
|
||||||
|
trace_root().join(format!("{}-{}.log", exe, std::process::id()))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn trace_root() -> PathBuf {
|
||||||
|
dirs::cache_dir()
|
||||||
|
.unwrap_or_else(std::env::temp_dir)
|
||||||
|
.join("dosh")
|
||||||
|
.join("trace")
|
||||||
|
}
|
||||||
|
|
||||||
|
fn truthy_env(name: &str) -> bool {
|
||||||
|
std::env::var_os(name).is_some_and(|value| {
|
||||||
|
let normalized = value.to_string_lossy().to_ascii_lowercase();
|
||||||
|
!normalized.is_empty() && !matches!(normalized.as_str(), "0" | "false" | "off")
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
fn shell_escape(value: &str) -> String {
|
||||||
|
if value.bytes().all(|byte| {
|
||||||
|
byte.is_ascii_alphanumeric()
|
||||||
|
|| matches!(byte, b'.' | b'-' | b'_' | b':' | b'/' | b',' | b'=')
|
||||||
|
}) {
|
||||||
|
return value.to_string();
|
||||||
|
}
|
||||||
|
let mut out = String::from("'");
|
||||||
|
for ch in value.chars() {
|
||||||
|
if ch == '\'' {
|
||||||
|
out.push_str("'\\''");
|
||||||
|
} else {
|
||||||
|
out.push(ch);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
out.push('\'');
|
||||||
|
out
|
||||||
|
}
|
||||||
|
|
||||||
|
fn contains_byte(bytes: &[u8], needle: u8) -> bool {
|
||||||
|
bytes.contains(&needle)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn has_focus_report(bytes: &[u8]) -> bool {
|
||||||
|
bytes
|
||||||
|
.windows(3)
|
||||||
|
.any(|window| matches!(window, b"\x1b[I" | b"\x1b[O"))
|
||||||
|
|| bytes
|
||||||
|
.windows(2)
|
||||||
|
.any(|window| matches!(window, b"\x9bI" | b"\x9bO"))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn looks_mouseish(bytes: &[u8]) -> bool {
|
||||||
|
bytes.windows(3).any(|window| window == b"\x1b[<")
|
||||||
|
|| bytes.windows(2).any(|window| window == b"\x9b<")
|
||||||
|
|| bytes.iter().filter(|byte| **byte == b';').take(3).count() >= 2
|
||||||
|
}
|
||||||
|
|
||||||
|
fn printable_count(bytes: &[u8]) -> usize {
|
||||||
|
bytes
|
||||||
|
.iter()
|
||||||
|
.filter(|byte| byte.is_ascii_graphic() || **byte == b' ')
|
||||||
|
.count()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn hex_prefix(bytes: &[u8], max: usize) -> String {
|
||||||
|
let mut out = String::with_capacity(bytes.len().min(max) * 2);
|
||||||
|
for byte in bytes.iter().take(max) {
|
||||||
|
use std::fmt::Write as _;
|
||||||
|
let _ = write!(&mut out, "{byte:02x}");
|
||||||
|
}
|
||||||
|
if bytes.len() > max {
|
||||||
|
out.push_str("...");
|
||||||
|
}
|
||||||
|
out
|
||||||
|
}
|
||||||
+2575
File diff suppressed because it is too large
Load Diff
+190
@@ -0,0 +1,190 @@
|
|||||||
|
//! UDP error classification shared by Dosh clients, servers, and embedders.
|
||||||
|
|
||||||
|
use std::net::SocketAddr;
|
||||||
|
use std::time::{Duration, Instant};
|
||||||
|
use tokio::net::UdpSocket;
|
||||||
|
|
||||||
|
pub fn is_transient_udp_error(err: &std::io::Error) -> bool {
|
||||||
|
matches!(
|
||||||
|
err.kind(),
|
||||||
|
std::io::ErrorKind::Interrupted
|
||||||
|
| std::io::ErrorKind::TimedOut
|
||||||
|
| std::io::ErrorKind::WouldBlock
|
||||||
|
) || err.raw_os_error().is_some_and(is_transient_udp_os_error)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn is_transient_udp_send_error(err: &std::io::Error) -> bool {
|
||||||
|
is_transient_udp_error(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn send_udp_retrying_transient(
|
||||||
|
socket: &UdpSocket,
|
||||||
|
packet: &[u8],
|
||||||
|
peer: SocketAddr,
|
||||||
|
wait: Duration,
|
||||||
|
) -> std::io::Result<()> {
|
||||||
|
let deadline = Instant::now() + wait.max(Duration::from_millis(1));
|
||||||
|
loop {
|
||||||
|
match socket.send_to(packet, peer).await {
|
||||||
|
Ok(_) => return Ok(()),
|
||||||
|
Err(err) if is_transient_udp_send_error(&err) && Instant::now() < deadline => {
|
||||||
|
sleep_until_retry(deadline).await;
|
||||||
|
}
|
||||||
|
Err(err) => return Err(err),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn recv_udp_retrying_transient(
|
||||||
|
socket: &UdpSocket,
|
||||||
|
buf: &mut [u8],
|
||||||
|
wait: Duration,
|
||||||
|
) -> std::io::Result<(usize, SocketAddr)> {
|
||||||
|
let deadline = Instant::now() + wait.max(Duration::from_millis(1));
|
||||||
|
loop {
|
||||||
|
let now = Instant::now();
|
||||||
|
if now >= deadline {
|
||||||
|
return Err(std::io::Error::from(std::io::ErrorKind::TimedOut));
|
||||||
|
}
|
||||||
|
match tokio::time::timeout(deadline - now, socket.recv_from(buf)).await {
|
||||||
|
Ok(Ok(value)) => return Ok(value),
|
||||||
|
Ok(Err(err)) if is_transient_udp_error(&err) && Instant::now() < deadline => {
|
||||||
|
sleep_until_retry(deadline).await;
|
||||||
|
}
|
||||||
|
Ok(Err(err)) => return Err(err),
|
||||||
|
Err(_) => return Err(std::io::Error::from(std::io::ErrorKind::TimedOut)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn sleep_until_retry(deadline: Instant) {
|
||||||
|
let now = Instant::now();
|
||||||
|
if now >= deadline {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
tokio::time::sleep((deadline - now).min(Duration::from_millis(20))).await;
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(unix)]
|
||||||
|
fn is_transient_udp_os_error(code: i32) -> bool {
|
||||||
|
matches!(
|
||||||
|
code,
|
||||||
|
libc::EADDRNOTAVAIL
|
||||||
|
| libc::ECONNREFUSED
|
||||||
|
| libc::ECONNRESET
|
||||||
|
| libc::EHOSTDOWN
|
||||||
|
| libc::EHOSTUNREACH
|
||||||
|
| libc::ENETDOWN
|
||||||
|
| libc::ENETRESET
|
||||||
|
| libc::ENETUNREACH
|
||||||
|
| libc::ETIMEDOUT
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(windows)]
|
||||||
|
fn is_transient_udp_os_error(code: i32) -> bool {
|
||||||
|
matches!(
|
||||||
|
code,
|
||||||
|
10049 // WSAEADDRNOTAVAIL
|
||||||
|
| 10050 // WSAENETDOWN
|
||||||
|
| 10051 // WSAENETUNREACH
|
||||||
|
| 10052 // WSAENETRESET
|
||||||
|
| 10054 // WSAECONNRESET
|
||||||
|
| 10060 // WSAETIMEDOUT
|
||||||
|
| 10061 // WSAECONNREFUSED
|
||||||
|
| 10064 // WSAEHOSTDOWN
|
||||||
|
| 10065 // WSAEHOSTUNREACH
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(not(any(unix, windows)))]
|
||||||
|
fn is_transient_udp_os_error(_code: i32) -> bool {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::{
|
||||||
|
is_transient_udp_error, is_transient_udp_send_error, recv_udp_retrying_transient,
|
||||||
|
send_udp_retrying_transient,
|
||||||
|
};
|
||||||
|
use std::time::Duration;
|
||||||
|
use tokio::net::UdpSocket;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn classifies_portable_transient_udp_errors() {
|
||||||
|
for kind in [
|
||||||
|
std::io::ErrorKind::Interrupted,
|
||||||
|
std::io::ErrorKind::TimedOut,
|
||||||
|
std::io::ErrorKind::WouldBlock,
|
||||||
|
] {
|
||||||
|
let err = std::io::Error::from(kind);
|
||||||
|
assert!(is_transient_udp_error(&err));
|
||||||
|
assert!(is_transient_udp_send_error(&err));
|
||||||
|
}
|
||||||
|
let fatal = std::io::Error::from(std::io::ErrorKind::PermissionDenied);
|
||||||
|
assert!(!is_transient_udp_error(&fatal));
|
||||||
|
assert!(!is_transient_udp_send_error(&fatal));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn retrying_udp_send_and_receive_round_trip() {
|
||||||
|
let receiver = UdpSocket::bind("127.0.0.1:0").await.unwrap();
|
||||||
|
let sender = UdpSocket::bind("127.0.0.1:0").await.unwrap();
|
||||||
|
let addr = receiver.local_addr().unwrap();
|
||||||
|
|
||||||
|
send_udp_retrying_transient(&sender, b"hello", addr, Duration::from_millis(50))
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
let mut buf = [0u8; 16];
|
||||||
|
let (n, peer) = recv_udp_retrying_transient(&receiver, &mut buf, Duration::from_millis(50))
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
assert_eq!(&buf[..n], b"hello");
|
||||||
|
assert_eq!(peer, sender.local_addr().unwrap());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn retrying_udp_receive_times_out_cleanly() {
|
||||||
|
let receiver = UdpSocket::bind("127.0.0.1:0").await.unwrap();
|
||||||
|
let mut buf = [0u8; 16];
|
||||||
|
let err = recv_udp_retrying_transient(&receiver, &mut buf, Duration::from_millis(1))
|
||||||
|
.await
|
||||||
|
.unwrap_err();
|
||||||
|
|
||||||
|
assert_eq!(err.kind(), std::io::ErrorKind::TimedOut);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(unix)]
|
||||||
|
#[test]
|
||||||
|
fn classifies_unix_network_churn_as_transient() {
|
||||||
|
for code in [
|
||||||
|
libc::EADDRNOTAVAIL,
|
||||||
|
libc::ECONNREFUSED,
|
||||||
|
libc::ECONNRESET,
|
||||||
|
libc::EHOSTDOWN,
|
||||||
|
libc::EHOSTUNREACH,
|
||||||
|
libc::ENETDOWN,
|
||||||
|
libc::ENETRESET,
|
||||||
|
libc::ENETUNREACH,
|
||||||
|
libc::ETIMEDOUT,
|
||||||
|
] {
|
||||||
|
let err = std::io::Error::from_raw_os_error(code);
|
||||||
|
assert!(is_transient_udp_error(&err));
|
||||||
|
assert!(is_transient_udp_send_error(&err));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(windows)]
|
||||||
|
#[test]
|
||||||
|
fn classifies_windows_network_churn_as_transient() {
|
||||||
|
for code in [
|
||||||
|
10049, 10050, 10051, 10052, 10054, 10060, 10061, 10064, 10065,
|
||||||
|
] {
|
||||||
|
let err = std::io::Error::from_raw_os_error(code);
|
||||||
|
assert!(is_transient_udp_error(&err));
|
||||||
|
assert!(is_transient_udp_send_error(&err));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+155
-12
@@ -29,7 +29,7 @@ use std::io::{Read, Write};
|
|||||||
use std::net::{SocketAddr, TcpListener, UdpSocket};
|
use std::net::{SocketAddr, TcpListener, UdpSocket};
|
||||||
use std::process::{Child, Command, Stdio};
|
use std::process::{Child, Command, Stdio};
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
|
use std::sync::atomic::{AtomicBool, AtomicU32, AtomicU64, Ordering};
|
||||||
use std::sync::mpsc::{Receiver, Sender, channel};
|
use std::sync::mpsc::{Receiver, Sender, channel};
|
||||||
use std::thread;
|
use std::thread;
|
||||||
use std::time::{Duration, Instant};
|
use std::time::{Duration, Instant};
|
||||||
@@ -49,9 +49,20 @@ use ed25519_dalek::{SigningKey, VerifyingKey};
|
|||||||
use rand::rngs::StdRng;
|
use rand::rngs::StdRng;
|
||||||
use rand::{Rng, SeedableRng};
|
use rand::{Rng, SeedableRng};
|
||||||
|
|
||||||
|
const TEST_UDP_PORT_START: u16 = 31_000;
|
||||||
|
const TEST_UDP_PORT_END: u16 = 60_999;
|
||||||
|
static NEXT_UDP_PORT: AtomicU32 = AtomicU32::new(TEST_UDP_PORT_START as u32);
|
||||||
|
|
||||||
fn free_udp_port() -> u16 {
|
fn free_udp_port() -> u16 {
|
||||||
let socket = UdpSocket::bind("127.0.0.1:0").unwrap();
|
let span = u32::from(TEST_UDP_PORT_END - TEST_UDP_PORT_START) + 1;
|
||||||
socket.local_addr().unwrap().port()
|
for _ in TEST_UDP_PORT_START..=TEST_UDP_PORT_END {
|
||||||
|
let offset = NEXT_UDP_PORT.fetch_add(1, Ordering::SeqCst) % span;
|
||||||
|
let port = TEST_UDP_PORT_START + offset as u16;
|
||||||
|
if UdpSocket::bind(("127.0.0.1", port)).is_ok() {
|
||||||
|
return port;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
panic!("no free UDP test port in {TEST_UDP_PORT_START}-{TEST_UDP_PORT_END}");
|
||||||
}
|
}
|
||||||
|
|
||||||
fn write_server_config(dir: &tempfile::TempDir, port: u16) -> std::path::PathBuf {
|
fn write_server_config(dir: &tempfile::TempDir, port: u16) -> std::path::PathBuf {
|
||||||
@@ -258,7 +269,8 @@ fn relay_loop(
|
|||||||
let mut upstream = new_upstream();
|
let mut upstream = new_upstream();
|
||||||
let mut client_addr: Option<SocketAddr> = None;
|
let mut client_addr: Option<SocketAddr> = None;
|
||||||
let mut rng = StdRng::seed_from_u64(seed);
|
let mut rng = StdRng::seed_from_u64(seed);
|
||||||
let mut held: Option<(Vec<u8>, bool)> = None; // (packet, is_c2s) held for reorder
|
let mut held_c2s: Option<Vec<u8>> = None;
|
||||||
|
let mut held_s2c: Option<Vec<u8>> = None;
|
||||||
let mut buf = [0u8; 65535];
|
let mut buf = [0u8; 65535];
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
@@ -288,10 +300,9 @@ fn relay_loop(
|
|||||||
&upstream,
|
&upstream,
|
||||||
server_addr,
|
server_addr,
|
||||||
packet,
|
packet,
|
||||||
true,
|
|
||||||
&controls,
|
&controls,
|
||||||
&mut rng,
|
&mut rng,
|
||||||
&mut held,
|
&mut held_c2s,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -310,7 +321,12 @@ fn relay_loop(
|
|||||||
let drop_pct = controls.drop_s2c_percent.load(Ordering::SeqCst);
|
let drop_pct = controls.drop_s2c_percent.load(Ordering::SeqCst);
|
||||||
if drop_pct == 0 || rng.gen_range(0..100) >= drop_pct {
|
if drop_pct == 0 || rng.gen_range(0..100) >= drop_pct {
|
||||||
forward_with_effects(
|
forward_with_effects(
|
||||||
&front, dst, packet, false, &controls, &mut rng, &mut held,
|
&front,
|
||||||
|
dst,
|
||||||
|
packet,
|
||||||
|
&controls,
|
||||||
|
&mut rng,
|
||||||
|
&mut held_s2c,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -336,23 +352,22 @@ fn forward_with_effects(
|
|||||||
out: &UdpSocket,
|
out: &UdpSocket,
|
||||||
dst: SocketAddr,
|
dst: SocketAddr,
|
||||||
packet: Vec<u8>,
|
packet: Vec<u8>,
|
||||||
is_c2s: bool,
|
|
||||||
controls: &RelayControls,
|
controls: &RelayControls,
|
||||||
rng: &mut StdRng,
|
rng: &mut StdRng,
|
||||||
held: &mut Option<(Vec<u8>, bool)>,
|
held: &mut Option<Vec<u8>>,
|
||||||
) {
|
) {
|
||||||
// Reorder: if armed, hold this packet and release the previously held one
|
// Reorder: if armed, hold this packet and release the previously held one
|
||||||
// afterward (so two consecutive packets swap order).
|
// afterward (so two consecutive packets swap order).
|
||||||
if controls.reorder_next.swap(false, Ordering::SeqCst) {
|
if controls.reorder_next.swap(false, Ordering::SeqCst) {
|
||||||
if let Some((prev, _)) = held.take() {
|
if let Some(prev) = held.take() {
|
||||||
let _ = out.send_to(&packet, dst);
|
let _ = out.send_to(&packet, dst);
|
||||||
let _ = out.send_to(&prev, dst);
|
let _ = out.send_to(&prev, dst);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
*held = Some((packet, is_c2s));
|
*held = Some(packet);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if let Some((prev, _)) = held.take() {
|
if let Some(prev) = held.take() {
|
||||||
let _ = out.send_to(&prev, dst);
|
let _ = out.send_to(&prev, dst);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -767,6 +782,46 @@ fn wait_for_text(socket: &UdpSocket, key: &[u8; 32], needle: &str, millis: u64)
|
|||||||
acc.contains(needle)
|
acc.contains(needle)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn wait_for_text_with_ack(
|
||||||
|
socket: &UdpSocket,
|
||||||
|
relay: &Relay,
|
||||||
|
client_id: [u8; 16],
|
||||||
|
seq: &mut u64,
|
||||||
|
key: &[u8; 32],
|
||||||
|
needle: &str,
|
||||||
|
millis: u64,
|
||||||
|
) -> bool {
|
||||||
|
let prev = socket.read_timeout().unwrap();
|
||||||
|
socket
|
||||||
|
.set_read_timeout(Some(Duration::from_millis(100)))
|
||||||
|
.unwrap();
|
||||||
|
let deadline = Instant::now() + Duration::from_millis(millis);
|
||||||
|
let mut acc = String::new();
|
||||||
|
while Instant::now() < deadline {
|
||||||
|
if let Some((_header, frame)) = recv_frame(socket, key) {
|
||||||
|
acc.push_str(&String::from_utf8_lossy(&frame.bytes));
|
||||||
|
let ack = protocol::encode_encrypted(
|
||||||
|
PacketKind::Ack,
|
||||||
|
client_id,
|
||||||
|
*seq,
|
||||||
|
frame.output_seq,
|
||||||
|
key,
|
||||||
|
CLIENT_TO_SERVER,
|
||||||
|
b"",
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
*seq += 1;
|
||||||
|
socket.send_to(&ack, relay.front_addr()).unwrap();
|
||||||
|
if acc.contains(needle) {
|
||||||
|
socket.set_read_timeout(prev).unwrap();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
socket.set_read_timeout(prev).unwrap();
|
||||||
|
acc.contains(needle)
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn session_survives_packet_loss_and_reorder() {
|
fn session_survives_packet_loss_and_reorder() {
|
||||||
let dir = tempfile::tempdir().unwrap();
|
let dir = tempfile::tempdir().unwrap();
|
||||||
@@ -820,6 +875,94 @@ fn session_survives_packet_loss_and_reorder() {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
#[ignore = "30-minute hostile-network TUI soak; run with `DOSH_BADNET_SOAK_SECONDS=1800 cargo test --test hostile_network bad_network_tui_work_soak_30m -- --ignored --nocapture`"]
|
||||||
|
fn bad_network_tui_work_soak_30m() {
|
||||||
|
let soak_secs = std::env::var("DOSH_BADNET_SOAK_SECONDS")
|
||||||
|
.ok()
|
||||||
|
.and_then(|value| value.parse::<u64>().ok())
|
||||||
|
.unwrap_or(30 * 60);
|
||||||
|
let dir = tempfile::tempdir().unwrap();
|
||||||
|
let port = free_udp_port();
|
||||||
|
let config = write_server_config(&dir, port);
|
||||||
|
let mut server = start_server(&dir, &config);
|
||||||
|
let relay = Relay::spawn(port, 0xBADC0DEu64);
|
||||||
|
let (socket, bootstrap, ok) = attach_through_relay(&config, &relay);
|
||||||
|
|
||||||
|
relay.set_drop_c2s(15);
|
||||||
|
relay.set_drop_s2c(20);
|
||||||
|
relay.set_dup(10);
|
||||||
|
|
||||||
|
let deadline = Instant::now() + Duration::from_secs(soak_secs);
|
||||||
|
let mut seq = 2u64;
|
||||||
|
let mut iteration = 0u64;
|
||||||
|
while Instant::now() < deadline {
|
||||||
|
if iteration.is_multiple_of(2) {
|
||||||
|
relay.arm_reorder();
|
||||||
|
}
|
||||||
|
if iteration > 0 && iteration.is_multiple_of(7) {
|
||||||
|
let _ = relay.rebind_upstream();
|
||||||
|
}
|
||||||
|
if iteration > 0 && iteration.is_multiple_of(11) {
|
||||||
|
relay.set_drop_s2c(100);
|
||||||
|
thread::sleep(Duration::from_millis(750));
|
||||||
|
relay.set_drop_s2c(20);
|
||||||
|
}
|
||||||
|
|
||||||
|
let marker = format!("DOSH_BADNET_TUI_{iteration}");
|
||||||
|
let command = format!(
|
||||||
|
"stty -echo; \
|
||||||
|
printf '\\033[?1049h\\033[?2026h\\033[?25l'; \
|
||||||
|
for i in 1 2 3 4 5 6; do \
|
||||||
|
printf '\\033[%s;4H{} ⠀⠁⠃⠇⡇⣇⣧⣷⣿ █▇▆▅▄▃▂▁ %s\\033[0m' \"$i\" \"$i\"; \
|
||||||
|
done; \
|
||||||
|
printf '\\033[?25h\\033[?2026l\\033[?1049l'\n",
|
||||||
|
marker
|
||||||
|
);
|
||||||
|
|
||||||
|
let step_deadline = Instant::now() + Duration::from_secs(8);
|
||||||
|
let mut seen = false;
|
||||||
|
let mut attempts = 0;
|
||||||
|
while Instant::now() < step_deadline && attempts < 12 {
|
||||||
|
send_input(
|
||||||
|
&socket,
|
||||||
|
&relay,
|
||||||
|
ok.client_id,
|
||||||
|
seq,
|
||||||
|
0,
|
||||||
|
&bootstrap.session_key,
|
||||||
|
command.as_bytes(),
|
||||||
|
);
|
||||||
|
seq += 1;
|
||||||
|
attempts += 1;
|
||||||
|
if wait_for_text_with_ack(
|
||||||
|
&socket,
|
||||||
|
&relay,
|
||||||
|
ok.client_id,
|
||||||
|
&mut seq,
|
||||||
|
&bootstrap.session_key,
|
||||||
|
&marker,
|
||||||
|
500,
|
||||||
|
) {
|
||||||
|
seen = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
assert!(
|
||||||
|
seen,
|
||||||
|
"TUI marker {marker} did not arrive during hostile-network soak"
|
||||||
|
);
|
||||||
|
|
||||||
|
iteration += 1;
|
||||||
|
thread::sleep(Duration::from_millis(500));
|
||||||
|
}
|
||||||
|
|
||||||
|
relay.clear_impairments();
|
||||||
|
drop(relay);
|
||||||
|
let _ = server.kill();
|
||||||
|
let _ = server.wait();
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn duplicated_and_replayed_input_is_applied_at_most_once() {
|
fn duplicated_and_replayed_input_is_applied_at_most_once() {
|
||||||
let dir = tempfile::tempdir().unwrap();
|
let dir = tempfile::tempdir().unwrap();
|
||||||
|
|||||||
+802
-41
File diff suppressed because it is too large
Load Diff
@@ -58,6 +58,34 @@ fn encrypted_packet_round_trips() {
|
|||||||
assert_eq!(plain, b"hello");
|
assert_eq!(plain, b"hello");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn packet_decode_rejects_truncated_or_trailing_datagrams() {
|
||||||
|
let packet =
|
||||||
|
protocol::encode_plain(PacketKind::Ping, crypto::random_16(), 1, 0, b"hello").unwrap();
|
||||||
|
|
||||||
|
let truncated = &packet[..packet.len() - 1];
|
||||||
|
let err = protocol::decode(truncated).unwrap_err();
|
||||||
|
assert!(err.to_string().contains("truncated packet body"));
|
||||||
|
|
||||||
|
let mut trailing = packet.clone();
|
||||||
|
trailing.extend_from_slice(b"junk");
|
||||||
|
let err = protocol::decode(&trailing).unwrap_err();
|
||||||
|
assert!(err.to_string().contains("trailing packet bytes"));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn plaintext_packet_never_decrypts_as_authenticated_body() {
|
||||||
|
let key = crypto::random_32();
|
||||||
|
let mut decoded = protocol::decode(
|
||||||
|
&protocol::encode_plain(PacketKind::Input, crypto::random_16(), 1, 0, b"hello").unwrap(),
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
decoded.header.session_key_id = protocol::session_key_id(&key);
|
||||||
|
|
||||||
|
let err = protocol::decrypt_body(&decoded, &key, CLIENT_TO_SERVER).unwrap_err();
|
||||||
|
assert!(err.to_string().contains("not encrypted"));
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn encrypted_packet_rejects_wrong_session_key_id_before_decrypt() {
|
fn encrypted_packet_rejects_wrong_session_key_id_before_decrypt() {
|
||||||
let key = crypto::random_32();
|
let key = crypto::random_32();
|
||||||
|
|||||||
@@ -0,0 +1,317 @@
|
|||||||
|
#[test]
|
||||||
|
fn quiet_update_keeps_prebuilt_enabled_by_default() {
|
||||||
|
let install = include_str!("../install.sh");
|
||||||
|
assert!(install.contains("use_prebuilt=\"${DOSH_USE_PREBUILT:-1}\""));
|
||||||
|
assert!(
|
||||||
|
!install.contains("use_prebuilt=0"),
|
||||||
|
"quiet updates must not force source builds"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn release_scripts_skip_stale_artifacts_when_auto_discovering() {
|
||||||
|
let upload = include_str!("../scripts/upload-gitea-release.sh");
|
||||||
|
assert!(upload.contains("skipping $artifact: version"));
|
||||||
|
assert!(upload.contains("expected_version"));
|
||||||
|
|
||||||
|
let publish = include_str!("../scripts/publish-gitea-release.sh");
|
||||||
|
assert!(publish.contains("skipping stale artifact"));
|
||||||
|
assert!(publish.contains("actual=\"$(artifact_version \"$artifact\" || true)\""));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn release_upload_refuses_partial_platform_sets_by_default() {
|
||||||
|
let upload = include_str!("../scripts/upload-gitea-release.sh");
|
||||||
|
assert!(upload.contains("require_complete_release_artifacts \"$@\""));
|
||||||
|
assert!(upload.contains("DOSH_RELEASE_ALLOW_PARTIAL=1"));
|
||||||
|
assert!(upload.contains("refusing partial release upload"));
|
||||||
|
for name in [
|
||||||
|
"dosh-linux-x86_64.tar.gz",
|
||||||
|
"dosh-macos-aarch64.tar.gz",
|
||||||
|
"dosh-macos-x86_64.tar.gz",
|
||||||
|
"dosh-windows-x86_64.zip",
|
||||||
|
] {
|
||||||
|
assert!(
|
||||||
|
upload.contains(name),
|
||||||
|
"release upload completeness check must require {name}"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn release_upload_marks_prerelease_tags_as_prereleases() {
|
||||||
|
let upload = include_str!("../scripts/upload-gitea-release.sh");
|
||||||
|
assert!(upload.contains("release_prerelease=false"));
|
||||||
|
assert!(upload.contains("*-*) release_prerelease=true"));
|
||||||
|
assert!(upload.contains("\"prerelease\":%s"));
|
||||||
|
assert!(upload.contains("metadata_payload="));
|
||||||
|
assert!(
|
||||||
|
upload.contains("-X PATCH"),
|
||||||
|
"existing Gitea releases must be corrected when prerelease metadata changes"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn release_gates_test_all_targets() {
|
||||||
|
let makefile = include_str!("../Makefile");
|
||||||
|
let ci = include_str!("../.github/workflows/ci.yml");
|
||||||
|
assert!(makefile.contains("test:\n\tcargo test --all-targets"));
|
||||||
|
assert!(makefile.contains("cargo test --all-targets\n\t$(MAKE) tui-harness"));
|
||||||
|
assert!(ci.contains("run: sh -n install.sh scripts/*.sh"));
|
||||||
|
assert!(ci.contains("run: cargo clippy --all-targets -- -D warnings"));
|
||||||
|
assert!(ci.contains("run: cargo test --all-targets"));
|
||||||
|
assert!(ci.contains("run: sh scripts/tui-harness.sh"));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn one_dot_zero_gates_default_to_full_length_soaks() {
|
||||||
|
let makefile = include_str!("../Makefile");
|
||||||
|
let reconnect_check = makefile
|
||||||
|
.split("reconnect-check:")
|
||||||
|
.nth(1)
|
||||||
|
.and_then(|tail| tail.split("\nhostile-network-check:").next())
|
||||||
|
.expect("reconnect-check target");
|
||||||
|
assert!(reconnect_check.contains("DOSH_SOAK_SECONDS=$${DOSH_SOAK_SECONDS:-1800}"));
|
||||||
|
assert!(!reconnect_check.contains("DOSH_SOAK_SECONDS=$${DOSH_SOAK_SECONDS:-300}"));
|
||||||
|
|
||||||
|
let hostile_check = makefile
|
||||||
|
.split("hostile-network-check:")
|
||||||
|
.nth(1)
|
||||||
|
.and_then(|tail| tail.split("\npersistence-check:").next())
|
||||||
|
.expect("hostile-network-check target");
|
||||||
|
assert!(hostile_check.contains("DOSH_BADNET_SOAK_SECONDS=$${DOSH_BADNET_SOAK_SECONDS:-1800}"));
|
||||||
|
assert!(!hostile_check.contains("DOSH_BADNET_SOAK_SECONDS=$${DOSH_BADNET_SOAK_SECONDS:-300}"));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn installers_skip_stale_latest_release_prebuilts() {
|
||||||
|
let install = include_str!("../install.sh");
|
||||||
|
assert!(install.contains("latest_release_is_stale"));
|
||||||
|
assert!(install.contains("current_source_version"));
|
||||||
|
assert!(install.contains("latest release $latest is older than source $current"));
|
||||||
|
assert!(install.contains("version_prerelease"));
|
||||||
|
assert!(install.contains("prerelease_less_than"));
|
||||||
|
assert!(
|
||||||
|
install.contains("if latest_release_is_stale; then"),
|
||||||
|
"unix installer must skip stale latest before downloading a prebuilt"
|
||||||
|
);
|
||||||
|
|
||||||
|
let ps1 = include_str!("../install.ps1");
|
||||||
|
assert!(ps1.contains("Latest-ReleaseIsStale"));
|
||||||
|
assert!(ps1.contains("Current-SourceVersion"));
|
||||||
|
assert!(ps1.contains("latest release $latestVersion is older than source $current"));
|
||||||
|
assert!(ps1.contains("Version-Prerelease"));
|
||||||
|
assert!(ps1.contains("Compare-Prerelease"));
|
||||||
|
assert!(
|
||||||
|
ps1.contains("if (Latest-ReleaseIsStale)"),
|
||||||
|
"windows installer must skip stale latest before downloading a prebuilt"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn installers_refuse_prebuilts_without_checksums() {
|
||||||
|
let install = include_str!("../install.sh");
|
||||||
|
assert!(install.contains("verify_archive_checksum"));
|
||||||
|
assert!(install.contains("refusing unverified binary for $url"));
|
||||||
|
assert!(
|
||||||
|
!install.contains("continuing without sidecar verification"),
|
||||||
|
"unix installer must not silently install an unverifiable prebuilt"
|
||||||
|
);
|
||||||
|
|
||||||
|
let ps1 = include_str!("../install.ps1");
|
||||||
|
assert!(ps1.contains("Verify-ArchiveChecksum"));
|
||||||
|
assert!(ps1.contains("refusing unverified binary for $Url"));
|
||||||
|
assert!(
|
||||||
|
!ps1.contains("continuing without sidecar verification"),
|
||||||
|
"windows installer must not silently install an unverifiable prebuilt"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn windows_installer_reuses_persistent_source_update_cache() {
|
||||||
|
let ps1 = include_str!("../install.ps1");
|
||||||
|
assert!(ps1.contains("DOSH_UPDATE_CACHE"));
|
||||||
|
assert!(ps1.contains("Assert-NoRelativePathSegments($Path)"));
|
||||||
|
assert!(ps1.contains("Assert-NoRelativePathSegments $Path"));
|
||||||
|
assert!(ps1.contains("$segment -eq \".\" -or $segment -eq \"..\""));
|
||||||
|
assert!(ps1.contains("$sourceCache = Assert-SafeUpdateCache $UpdateCache"));
|
||||||
|
assert!(ps1.contains("git -C $sourceCache fetch --depth 1 origin main"));
|
||||||
|
assert!(ps1.contains("git -C $sourceCache checkout -q -B main FETCH_HEAD"));
|
||||||
|
assert!(ps1.contains("git clone --depth 1 --branch main $Repo $sourceCache"));
|
||||||
|
assert!(
|
||||||
|
!ps1.contains("git clone --depth 1 $Repo $tmp"),
|
||||||
|
"Windows source updates should reuse a persistent cache instead of recloning into temp"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn unix_installer_rejects_unsafe_source_update_cache_paths() {
|
||||||
|
let install = include_str!("../install.sh");
|
||||||
|
assert!(install.contains("normalize_update_cache_path()"));
|
||||||
|
assert!(install.contains("safe_update_cache_path()"));
|
||||||
|
assert!(install.contains("update_cache=\"$(safe_update_cache_path \"$update_cache\")\""));
|
||||||
|
for pattern in [
|
||||||
|
"\"\"|\"/\"|\"$home_norm\"|\"$home_cache_norm\"",
|
||||||
|
"\".\"|\"..\"|./*|../*|*/.|*/..|*/./*|*/../*",
|
||||||
|
] {
|
||||||
|
assert!(
|
||||||
|
install.contains(pattern),
|
||||||
|
"Unix installer must reject unsafe update cache pattern {pattern}"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
assert!(
|
||||||
|
install.contains("rm -rf \"$update_cache\""),
|
||||||
|
"source cache replacement remains the destructive operation guarded by safe_update_cache_path"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn installers_generate_same_native_auth_client_defaults() {
|
||||||
|
let install = include_str!("../install.sh");
|
||||||
|
let ps1 = include_str!("../install.ps1");
|
||||||
|
for setting in [
|
||||||
|
"auth_preference = \"native,ssh\"",
|
||||||
|
"trust_on_first_use = false",
|
||||||
|
"native_auth_timeout_ms = 700",
|
||||||
|
"known_hosts = \"~/.config/dosh/known_hosts\"",
|
||||||
|
"identity_files = [\"~/.ssh/id_ed25519\"]",
|
||||||
|
"use_ssh_agent = true",
|
||||||
|
"forward_agent = false",
|
||||||
|
] {
|
||||||
|
assert!(
|
||||||
|
install.contains(setting),
|
||||||
|
"Unix installer missing client default {setting}"
|
||||||
|
);
|
||||||
|
assert!(
|
||||||
|
ps1.contains(setting),
|
||||||
|
"Windows installer missing client default {setting}"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn windows_installer_generates_hosts_config_like_unix() {
|
||||||
|
let install = include_str!("../install.sh");
|
||||||
|
let ps1 = include_str!("../install.ps1");
|
||||||
|
for setting in [
|
||||||
|
"hosts.toml",
|
||||||
|
"# default_command = \"tm\"",
|
||||||
|
"[default]",
|
||||||
|
"predict = true",
|
||||||
|
] {
|
||||||
|
assert!(
|
||||||
|
install.contains(setting),
|
||||||
|
"Unix installer missing host config setting {setting}"
|
||||||
|
);
|
||||||
|
assert!(
|
||||||
|
ps1.contains(setting),
|
||||||
|
"Windows installer missing host config setting {setting}"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
assert!(ps1.contains("$hostUdp = if ($DoshHost) { $DoshHost } else { $defaultServer }"));
|
||||||
|
assert!(ps1.contains("Set-Content -NoNewline -Encoding utf8 $hostsConfig"));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn package_check_verifies_only_artifacts_it_builds() {
|
||||||
|
let makefile = include_str!("../Makefile");
|
||||||
|
let verify = include_str!("../scripts/verify-release-artifacts.sh");
|
||||||
|
assert!(
|
||||||
|
verify.contains("if [ \"$#\" -gt 0 ]; then"),
|
||||||
|
"release verifier must accept an explicit artifact list"
|
||||||
|
);
|
||||||
|
let package_check = makefile
|
||||||
|
.split("package-check:")
|
||||||
|
.nth(1)
|
||||||
|
.and_then(|tail| tail.split("\ninstall:").next())
|
||||||
|
.expect("package-check target");
|
||||||
|
assert!(package_check.contains("package-release-linux"));
|
||||||
|
assert!(package_check.contains("package-release-windows"));
|
||||||
|
assert!(package_check.contains("dosh-linux-x86_64.tar.gz"));
|
||||||
|
assert!(package_check.contains("dosh-windows-x86_64.zip"));
|
||||||
|
assert!(
|
||||||
|
!package_check.contains("dosh-macos-aarch64.tar.gz"),
|
||||||
|
"Linux package-check does not build macOS artifacts, so it must not depend on one"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn complete_release_verifier_requires_both_macos_architectures() {
|
||||||
|
let verify = include_str!("../scripts/verify-release-artifacts.sh");
|
||||||
|
assert!(verify.contains("dosh-linux-x86_64.tar.gz"));
|
||||||
|
assert!(verify.contains("dosh-macos-aarch64.tar.gz"));
|
||||||
|
assert!(verify.contains("dosh-macos-x86_64.tar.gz"));
|
||||||
|
assert!(verify.contains("dosh-windows-x86_64.zip"));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn release_verifier_checks_checksums_and_expected_binaries() {
|
||||||
|
let verify = include_str!("../scripts/verify-release-artifacts.sh");
|
||||||
|
assert!(verify.contains("verify_checksum()"));
|
||||||
|
assert!(verify.contains("release checksum mismatch"));
|
||||||
|
for member in [
|
||||||
|
"dosh/bin/dosh",
|
||||||
|
"dosh/bin/dosh-client",
|
||||||
|
"dosh/bin/dosh-server",
|
||||||
|
"dosh/bin/dosh-auth",
|
||||||
|
"dosh/bin/dosh-bench",
|
||||||
|
"dosh/bin/dosh.exe",
|
||||||
|
"dosh/bin/dosh-client.exe",
|
||||||
|
"dosh/bin/dosh-bench.exe",
|
||||||
|
] {
|
||||||
|
assert!(
|
||||||
|
verify.contains(member),
|
||||||
|
"release verifier must require archive member {member}"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
assert!(verify.contains("artifact missing expected member"));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn package_release_stamps_git_build_info() {
|
||||||
|
let package = include_str!("../scripts/package-release.sh");
|
||||||
|
let build = include_str!("../build.rs");
|
||||||
|
for name in [
|
||||||
|
"DOSH_BUILD_GIT_HASH",
|
||||||
|
"DOSH_BUILD_COMMIT_DATE",
|
||||||
|
"DOSH_BUILD_GIT_DIRTY",
|
||||||
|
] {
|
||||||
|
assert!(package.contains(name), "package script must export {name}");
|
||||||
|
assert!(build.contains(name), "build.rs must consume {name}");
|
||||||
|
}
|
||||||
|
assert!(package.contains("git rev-parse --short=12 HEAD"));
|
||||||
|
assert!(package.contains("git show -s --format=%cs HEAD"));
|
||||||
|
assert!(build.contains("cargo:rerun-if-env-changed=DOSH_BUILD_GIT_HASH"));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn systemd_service_does_not_sandbox_remote_shells() {
|
||||||
|
let service = include_str!("../packaging/systemd/dosh-server.service");
|
||||||
|
let install = include_str!("../install.sh");
|
||||||
|
for raw in [service, install] {
|
||||||
|
assert!(raw.contains("KillMode=process"));
|
||||||
|
assert!(
|
||||||
|
raw.contains("Restart=always"),
|
||||||
|
"dosh-server should come back after accidental SIGTERM while preserving child shells"
|
||||||
|
);
|
||||||
|
for directive in [
|
||||||
|
"NoNewPrivileges=",
|
||||||
|
"PrivateTmp=",
|
||||||
|
"ProtectSystem=",
|
||||||
|
"ProtectHome=",
|
||||||
|
"RestrictAddressFamilies=",
|
||||||
|
"RestrictRealtime=",
|
||||||
|
"LockPersonality=",
|
||||||
|
"MemoryDenyWriteExecute=",
|
||||||
|
] {
|
||||||
|
assert!(
|
||||||
|
!raw.contains(directive),
|
||||||
|
"dosh sessions are user shells; systemd sandbox directive {directive} changes terminal/app behavior"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
assert!(
|
||||||
|
!raw.contains("ReadWritePaths="),
|
||||||
|
"remote shells must not be restricted to dosh config/data paths"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
# Dosh Remote
|
||||||
|
|
||||||
|
Open VS Code Remote-SSH windows through Dosh.
|
||||||
|
|
||||||
|
The extension writes a managed SSH config entry like:
|
||||||
|
|
||||||
|
```sshconfig
|
||||||
|
Host dosh-palav
|
||||||
|
HostName 127.0.0.1
|
||||||
|
HostKeyAlias palav
|
||||||
|
ProxyCommand dosh proxy-stdio palav %h %p
|
||||||
|
```
|
||||||
|
|
||||||
|
VS Code still uses Remote-SSH, so server install, extensions, terminals, and
|
||||||
|
normal SSH behavior stay intact. Dosh carries the SSH TCP byte stream.
|
||||||
@@ -0,0 +1,170 @@
|
|||||||
|
const vscode = require('vscode');
|
||||||
|
const fs = require('fs');
|
||||||
|
const os = require('os');
|
||||||
|
const path = require('path');
|
||||||
|
|
||||||
|
function activate(context) {
|
||||||
|
context.subscriptions.push(
|
||||||
|
vscode.commands.registerCommand('dosh.openRemote', openRemote),
|
||||||
|
vscode.commands.registerCommand('dosh.configureHost', configureHostCommand),
|
||||||
|
vscode.commands.registerCommand('dosh.showSshConfig', showSshConfig)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
async function openRemote() {
|
||||||
|
const configured = await configureHost();
|
||||||
|
if (!configured) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const remotePath = await vscode.window.showInputBox({
|
||||||
|
title: 'Remote path',
|
||||||
|
prompt: 'Path to open on the remote host',
|
||||||
|
value: '~'
|
||||||
|
});
|
||||||
|
if (remotePath === undefined) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const suffix = remotePath ? `/${remotePath.replace(/^\/+/, '')}` : '';
|
||||||
|
await vscode.commands.executeCommand(
|
||||||
|
'vscode.openFolder',
|
||||||
|
vscode.Uri.parse(`vscode-remote://ssh-remote+${configured.alias}${suffix}`),
|
||||||
|
{ forceNewWindow: true }
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
async function configureHostCommand() {
|
||||||
|
const configured = await configureHost();
|
||||||
|
if (configured) {
|
||||||
|
vscode.window.showInformationMessage(`Dosh Remote-SSH host ready: ${configured.alias}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function configureHost() {
|
||||||
|
const host = await vscode.window.showInputBox({
|
||||||
|
title: 'Dosh host',
|
||||||
|
prompt: 'Dosh host alias, e.g. palav',
|
||||||
|
ignoreFocusOut: true
|
||||||
|
});
|
||||||
|
if (!host) {
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
const user = await vscode.window.showInputBox({
|
||||||
|
title: 'Remote SSH user',
|
||||||
|
prompt: 'Optional. Leave blank to let SSH config decide.',
|
||||||
|
ignoreFocusOut: true
|
||||||
|
});
|
||||||
|
const config = vscode.workspace.getConfiguration('dosh');
|
||||||
|
const alias = `dosh-${safeAlias(host)}`;
|
||||||
|
const block = sshBlock({
|
||||||
|
alias,
|
||||||
|
host,
|
||||||
|
user: user || undefined,
|
||||||
|
executable: config.get('executable') || 'dosh',
|
||||||
|
targetHost: config.get('targetHost') || '127.0.0.1',
|
||||||
|
targetPort: Number(config.get('targetPort') || 22),
|
||||||
|
doshPort: Number(config.get('doshPort') || 0)
|
||||||
|
});
|
||||||
|
const sshDir = path.join(os.homedir(), '.ssh');
|
||||||
|
fs.mkdirSync(sshDir, { recursive: true });
|
||||||
|
const generatedPath = config.get('generatedSshConfig') || path.join(sshDir, 'config.dosh');
|
||||||
|
const mainConfig = path.join(sshDir, 'config');
|
||||||
|
ensureInclude(mainConfig, path.basename(generatedPath));
|
||||||
|
upsertBlock(generatedPath, alias, block);
|
||||||
|
return { alias, generatedPath };
|
||||||
|
}
|
||||||
|
|
||||||
|
function sshBlock(options) {
|
||||||
|
let proxy = shellQuote(options.executable);
|
||||||
|
if (options.doshPort > 0) {
|
||||||
|
proxy += ` --dosh-port ${options.doshPort}`;
|
||||||
|
}
|
||||||
|
proxy += ` proxy-stdio ${shellQuote(options.host)} %h %p`;
|
||||||
|
const lines = [
|
||||||
|
`# BEGIN DOSH ${options.alias}`,
|
||||||
|
`Host ${options.alias}`,
|
||||||
|
` HostName ${options.targetHost}`,
|
||||||
|
` Port ${options.targetPort}`,
|
||||||
|
` HostKeyAlias ${options.host}`,
|
||||||
|
options.user ? ` User ${options.user}` : undefined,
|
||||||
|
' ClearAllForwardings yes',
|
||||||
|
' ServerAliveInterval 15',
|
||||||
|
' ServerAliveCountMax 3',
|
||||||
|
` ProxyCommand ${proxy}`,
|
||||||
|
`# END DOSH ${options.alias}`,
|
||||||
|
''
|
||||||
|
].filter(Boolean);
|
||||||
|
return lines.join('\n');
|
||||||
|
}
|
||||||
|
|
||||||
|
function ensureInclude(configPath, includeFile) {
|
||||||
|
const raw = fs.existsSync(configPath) ? fs.readFileSync(configPath, 'utf8') : '';
|
||||||
|
if (raw.split(/\r?\n/).some((line) => line.trim().toLowerCase() === `include ${includeFile}`.toLowerCase())) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
fs.writeFileSync(configPath, `Include ${includeFile}\n${raw ? `\n${raw}` : ''}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
function upsertBlock(configPath, alias, block) {
|
||||||
|
const raw = fs.existsSync(configPath) ? fs.readFileSync(configPath, 'utf8') : '';
|
||||||
|
const begin = `# BEGIN DOSH ${alias}`;
|
||||||
|
const end = `# END DOSH ${alias}`;
|
||||||
|
const lines = raw.split(/\r?\n/);
|
||||||
|
const out = [];
|
||||||
|
let skipping = false;
|
||||||
|
let replaced = false;
|
||||||
|
for (const line of lines) {
|
||||||
|
if (line.trim() === begin) {
|
||||||
|
out.push(block.trimEnd());
|
||||||
|
skipping = true;
|
||||||
|
replaced = true;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (skipping) {
|
||||||
|
if (line.trim() === end) {
|
||||||
|
skipping = false;
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (line.length > 0 || out.length > 0) {
|
||||||
|
out.push(line);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!replaced) {
|
||||||
|
if (out.length > 0 && out[out.length - 1] !== '') {
|
||||||
|
out.push('');
|
||||||
|
}
|
||||||
|
out.push(block.trimEnd());
|
||||||
|
}
|
||||||
|
fs.writeFileSync(configPath, `${out.join('\n').trimEnd()}\n`);
|
||||||
|
}
|
||||||
|
|
||||||
|
async function showSshConfig() {
|
||||||
|
const config = vscode.workspace.getConfiguration('dosh');
|
||||||
|
const sshDir = path.join(os.homedir(), '.ssh');
|
||||||
|
const generatedPath = config.get('generatedSshConfig') || path.join(sshDir, 'config.dosh');
|
||||||
|
if (!fs.existsSync(generatedPath)) {
|
||||||
|
vscode.window.showWarningMessage('No generated Dosh SSH config yet.');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const doc = await vscode.workspace.openTextDocument(generatedPath);
|
||||||
|
await vscode.window.showTextDocument(doc);
|
||||||
|
}
|
||||||
|
|
||||||
|
function safeAlias(value) {
|
||||||
|
const alias = value.replace(/[^a-zA-Z0-9_-]+/g, '-').replace(/^-+|-+$/g, '');
|
||||||
|
return alias || 'host';
|
||||||
|
}
|
||||||
|
|
||||||
|
function shellQuote(value) {
|
||||||
|
if (/^[a-zA-Z0-9_./:-]+$/.test(value)) {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
return `'${value.replace(/'/g, `'\\''`)}'`;
|
||||||
|
}
|
||||||
|
|
||||||
|
function deactivate() {}
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
activate,
|
||||||
|
deactivate
|
||||||
|
};
|
||||||
@@ -0,0 +1,69 @@
|
|||||||
|
{
|
||||||
|
"name": "dosh-vscode",
|
||||||
|
"displayName": "Dosh Remote",
|
||||||
|
"description": "Open VS Code Remote-SSH windows through Dosh.",
|
||||||
|
"version": "0.1.0",
|
||||||
|
"publisher": "palav",
|
||||||
|
"license": "MIT",
|
||||||
|
"engines": {
|
||||||
|
"vscode": "^1.90.0"
|
||||||
|
},
|
||||||
|
"categories": [
|
||||||
|
"Other"
|
||||||
|
],
|
||||||
|
"extensionDependencies": [
|
||||||
|
"ms-vscode-remote.remote-ssh"
|
||||||
|
],
|
||||||
|
"activationEvents": [
|
||||||
|
"onCommand:dosh.openRemote",
|
||||||
|
"onCommand:dosh.configureHost",
|
||||||
|
"onCommand:dosh.showSshConfig"
|
||||||
|
],
|
||||||
|
"main": "./extension.js",
|
||||||
|
"contributes": {
|
||||||
|
"commands": [
|
||||||
|
{
|
||||||
|
"command": "dosh.openRemote",
|
||||||
|
"title": "Dosh: Open Remote Folder"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"command": "dosh.configureHost",
|
||||||
|
"title": "Dosh: Configure Remote-SSH Host"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"command": "dosh.showSshConfig",
|
||||||
|
"title": "Dosh: Show Generated SSH Config"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"configuration": {
|
||||||
|
"title": "Dosh Remote",
|
||||||
|
"properties": {
|
||||||
|
"dosh.executable": {
|
||||||
|
"type": "string",
|
||||||
|
"default": "dosh",
|
||||||
|
"description": "Path to the local dosh executable."
|
||||||
|
},
|
||||||
|
"dosh.targetHost": {
|
||||||
|
"type": "string",
|
||||||
|
"default": "127.0.0.1",
|
||||||
|
"description": "Host opened from the Dosh server side for VS Code's SSH connection."
|
||||||
|
},
|
||||||
|
"dosh.targetPort": {
|
||||||
|
"type": "number",
|
||||||
|
"default": 22,
|
||||||
|
"description": "SSH port opened from the Dosh server side."
|
||||||
|
},
|
||||||
|
"dosh.doshPort": {
|
||||||
|
"type": "number",
|
||||||
|
"default": 0,
|
||||||
|
"description": "Optional Dosh UDP port override. 0 uses Dosh config."
|
||||||
|
},
|
||||||
|
"dosh.generatedSshConfig": {
|
||||||
|
"type": "string",
|
||||||
|
"default": "",
|
||||||
|
"description": "Path for generated SSH config. Empty means ~/.ssh/config.dosh."
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user