mod http;
mod ssh;
use std::ffi::OsStr;
use std::fmt;
use std::time::Duration;
use crate::error::{Error, Result};
use crate::session::http::Endpoint as HttpEndpoint;
use crate::session::{
PullSession, PushSession, SessionOptions, StreamSession, StreamSessionOptions,
};
pub(crate) use ssh::Transport;
pub use ssh::{SshAddress, SshOptions, without_userinfo};
const PENDING_READ_LIMIT: Duration = Duration::from_secs(5);
#[derive(Debug, Clone)]
pub enum Endpoint {
Ssh {
address: SshAddress,
options: SshOptions,
},
Http(ostrya_fetch::FetcherOptions),
}
impl Endpoint {
pub fn parse(address: &str) -> Result<Endpoint> {
parse_endpoint(address, cfg!(windows))
}
}
fn parse_endpoint(address: &str, windows: bool) -> Result<Endpoint> {
if address.starts_with("http://") || address.starts_with("https://") {
ostrya_fetch::check_base_url(address)
.map_err(|e| Error::InvalidInput(format!("address: {e}")))?;
Ok(Endpoint::Http(ostrya_fetch::FetcherOptions::new(address)))
} else {
Ok(Endpoint::Ssh {
address: SshAddress::parse_with(address, windows)?,
options: SshOptions::default(),
})
}
}
impl PushSession {
pub async fn connect(
endpoint: Endpoint,
refs: &[String],
opts: SessionOptions,
) -> Result<PushSession> {
PushSession::prepare(endpoint).await?.open(refs, opts).await
}
pub async fn prepare(endpoint: Endpoint) -> Result<PreparedSession> {
let env = std::env::var_os("OSTRYA_SSH_COMMAND");
let inner = prepare_with(endpoint, env.as_deref()).await?;
Ok(PreparedSession { inner })
}
}
pub struct PreparedSession {
inner: Prepared,
}
impl PreparedSession {
pub async fn open(self, refs: &[String], opts: SessionOptions) -> Result<PushSession> {
self.inner.open(refs, opts).await
}
}
impl fmt::Debug for PreparedSession {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut out = f.debug_struct("PreparedSession");
match &self.inner {
Prepared::Ssh(_) => out.field("transport", &"ssh"),
Prepared::Http(endpoint) => out.field("transport", &"http").field("url", &endpoint.url),
};
out.finish_non_exhaustive()
}
}
pub(crate) enum Prepared {
Ssh(Vec<String>),
Http(HttpEndpoint),
}
async fn prepare_with(endpoint: Endpoint, env: Option<&OsStr>) -> Result<Prepared> {
match endpoint {
Endpoint::Ssh { address, options } => {
let program = options.program(env)?;
let receive = ssh::receive_command(options.remote_command.as_deref())?;
Ok(Prepared::Ssh(address.command_line(program, receive)))
}
Endpoint::Http(http) => Ok(Prepared::Http(Box::pin(http::prepare(http)).await?)),
}
}
impl Prepared {
async fn open(self, refs: &[String], opts: SessionOptions) -> Result<PushSession> {
self.open_with(refs, opts, PENDING_READ_LIMIT).await
}
async fn open_with(
self,
refs: &[String],
opts: SessionOptions,
limit: Duration,
) -> Result<PushSession> {
match self {
Prepared::Ssh(argv) => spawn_and_open(&argv, refs, opts, limit).await,
Prepared::Http(endpoint) => {
Box::pin(PushSession::open_http(endpoint, refs, opts)).await
}
}
}
}
impl StreamSession {
pub async fn connect(
address: &SshAddress,
options: &SshOptions,
session: StreamSessionOptions,
) -> Result<StreamSession> {
let env = std::env::var_os("OSTRYA_SSH_COMMAND");
let argv = pull_command_line(address, options, env.as_deref())?;
spawn_and_open_pull(&argv, session, PENDING_READ_LIMIT).await
}
pub fn check_command_line(address: &SshAddress, options: &SshOptions) -> Result<()> {
let env = std::env::var_os("OSTRYA_SSH_COMMAND");
pull_command_line(address, options, env.as_deref()).map(drop)
}
}
impl PullSession {
pub async fn connect(endpoint: Endpoint, opts: StreamSessionOptions) -> Result<PullSession> {
match endpoint {
Endpoint::Ssh { address, options } => StreamSession::connect(&address, &options, opts)
.await
.map(PullSession::Stream),
Endpoint::Http(http) => Ok(PullSession::Http(
Box::pin(http::prepare_pull(http, opts)).await?,
)),
}
}
}
fn pull_command_line(
address: &SshAddress,
options: &SshOptions,
env: Option<&OsStr>,
) -> Result<Vec<String>> {
let program = options.program(env)?;
let send = ssh::send_command(options.remote_command.as_deref())?;
Ok(address.command_line(program, send))
}
async fn spawn_and_open_pull(
argv: &[String],
opts: StreamSessionOptions,
limit: Duration,
) -> Result<StreamSession> {
let (input, output, transport) = Transport::spawn(argv, limit)?;
match StreamSession::open(input, output, opts, Some(limit)).await {
Ok(session) => Ok(session.with_transport(transport)),
Err(e) => transport.finish(Err(e)).await,
}
}
#[cfg(test)]
async fn connect_pull_with(
address: &SshAddress,
options: &SshOptions,
env: Option<&OsStr>,
opts: StreamSessionOptions,
limit: Duration,
) -> Result<StreamSession> {
let argv = pull_command_line(address, options, env)?;
spawn_and_open_pull(&argv, opts, limit).await
}
#[cfg(test)]
async fn connect_with(
endpoint: Endpoint,
env: Option<&OsStr>,
refs: &[String],
opts: SessionOptions,
limit: Duration,
) -> Result<PushSession> {
prepare_with(endpoint, env)
.await?
.open_with(refs, opts, limit)
.await
}
async fn spawn_and_open(
argv: &[String],
refs: &[String],
opts: SessionOptions,
limit: Duration,
) -> Result<PushSession> {
let (input, output, transport) = Transport::spawn(argv, limit)?;
match PushSession::open(input, output, refs, opts, Some(limit)).await {
Ok(session) => Ok(session.with_transport(transport)),
Err(e) => transport.finish(Err(e)).await,
}
}
#[cfg(test)]
mod tests;