use std::path::PathBuf;
#[non_exhaustive]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub enum ForwardDirection {
Local,
Remote,
}
#[non_exhaustive]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub struct TcpEndpoint {
host: String,
port: u16,
}
impl TcpEndpoint {
pub fn new(host: impl Into<String>, port: u16) -> Self {
Self {
host: host.into(),
port,
}
}
pub fn host(&self) -> &str {
&self.host
}
pub fn port(&self) -> u16 {
self.port
}
}
impl From<(&str, u16)> for TcpEndpoint {
fn from((host, port): (&str, u16)) -> Self {
Self::new(host, port)
}
}
impl From<(String, u16)> for TcpEndpoint {
fn from((host, port): (String, u16)) -> Self {
Self::new(host, port)
}
}
#[non_exhaustive]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub struct StreamLocalSpec {
path: PathBuf,
}
impl StreamLocalSpec {
pub fn new(path: impl Into<PathBuf>) -> Self {
Self { path: path.into() }
}
pub fn path(&self) -> &std::path::Path {
&self.path
}
}
impl From<&str> for StreamLocalSpec {
fn from(path: &str) -> Self {
Self::new(path)
}
}
impl From<String> for StreamLocalSpec {
fn from(path: String) -> Self {
Self::new(path)
}
}
impl From<PathBuf> for StreamLocalSpec {
fn from(path: PathBuf) -> Self {
Self { path }
}
}
#[non_exhaustive]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum ForwardSpec {
Tcp {
direction: ForwardDirection,
bind: TcpEndpoint,
target: TcpEndpoint,
},
StreamLocal {
direction: ForwardDirection,
bind: StreamLocalSpec,
target: StreamLocalSpec,
},
}
impl ForwardSpec {
pub fn local_tcp(bind: impl Into<TcpEndpoint>, target: impl Into<TcpEndpoint>) -> Self {
Self::Tcp {
direction: ForwardDirection::Local,
bind: bind.into(),
target: target.into(),
}
}
pub fn remote_tcp(bind: impl Into<TcpEndpoint>, target: impl Into<TcpEndpoint>) -> Self {
Self::Tcp {
direction: ForwardDirection::Remote,
bind: bind.into(),
target: target.into(),
}
}
pub fn local_streamlocal(
bind: impl Into<StreamLocalSpec>,
target: impl Into<StreamLocalSpec>,
) -> Self {
Self::StreamLocal {
direction: ForwardDirection::Local,
bind: bind.into(),
target: target.into(),
}
}
pub fn remote_streamlocal(
bind: impl Into<StreamLocalSpec>,
target: impl Into<StreamLocalSpec>,
) -> Self {
Self::StreamLocal {
direction: ForwardDirection::Remote,
bind: bind.into(),
target: target.into(),
}
}
}