// sandhole: Expose HTTP/SSH/TCP services through SSH port forwarding
// Copyright (C) 2024-2026 Eric Rodrigues Pires
//
// This program is free software: you can redistribute it and/or modify it under
// the terms of the GNU Affero General Public License as published by the Free
// Software Foundation, either version 3 of the License, or (at your option)
// any later version.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
// more details.
//
// You should have received a copy of the GNU Affero General Public License along
// with this program. If not, see .
#[cfg_attr(not(feature = "prometheus"), allow(unused_imports))]
use std::time::Duration;
use std::{net::IpAddr, sync::Arc};
#[cfg_attr(not(feature = "prometheus"), allow(unused_imports))]
use bytes::Bytes;
#[cfg_attr(not(feature = "prometheus"), allow(unused_imports))]
use http::{HeaderValue, Request, Response, StatusCode, header::CONTENT_TYPE};
#[cfg_attr(not(feature = "prometheus"), allow(unused_imports))]
use http_body_util::Full;
#[cfg_attr(not(feature = "prometheus"), allow(unused_imports))]
use hyper::body::Incoming;
#[cfg_attr(not(feature = "prometheus"), allow(unused_imports))]
use hyper_util::{
rt::{TokioExecutor, TokioIo},
server::conn::auto,
};
#[cfg(feature = "prometheus")]
use metrics_exporter_prometheus::PrometheusHandle;
use russh::keys::ssh_key::Fingerprint;
use tokio::io::DuplexStream;
#[cfg_attr(not(feature = "prometheus"), allow(unused_imports))]
use tokio::time::timeout;
use crate::{
connection_handler::{ConnectionHandler, ConnectionHttpData},
error::ServerError,
ssh::ServerHandlerSender,
};
#[derive(Clone)]
pub(crate) struct AdminAliasHandler {
pub(crate) handler: Arc DuplexStream + Send + Sync + 'static>,
}
impl ConnectionHandler for AdminAliasHandler {
fn log_channel(&self) -> ServerHandlerSender {
ServerHandlerSender(None)
}
async fn tunneling_channel(
&self,
_ip: IpAddr,
_port: u16,
) -> Result {
Err(ServerError::TunnelingUnavailable)
}
fn can_alias(&self, _ip: IpAddr, _port: u16, _fingerprint: Option<&'_ Fingerprint>) -> bool {
// We can return true due to a previous check that an admin key is being used
true
}
async fn aliasing_channel(
&self,
_ip: IpAddr,
_port: u16,
_fingerprint: Option<&'_ Fingerprint>,
) -> Result {
Ok((self.handler)())
}
fn http_data(&self) -> Option {
None
}
}
#[cfg(feature = "prometheus")]
pub(crate) fn get_prometheus_service(
handle: PrometheusHandle,
tcp_connection_timeout: Option,
buffer_size: usize,
) -> DuplexStream {
let service = hyper::service::service_fn(move |req: Request| {
let handle_clone = handle.clone();
async move {
if req.uri().path() == "/" {
let mut response = Response::new(Full::new(Bytes::from(
tokio::task::spawn_blocking(move || handle_clone.render())
.await
.expect("task runs to completion"),
)));
response
.headers_mut()
.append(CONTENT_TYPE, HeaderValue::from_static("text/plain"));
Ok(response)
} else {
Response::builder()
.status(StatusCode::NOT_FOUND)
.body(Full::default())
}
}
});
let (server, client) = tokio::io::duplex(buffer_size);
let io = TokioIo::new(server);
tokio::spawn(async move {
let server = auto::Builder::new(TokioExecutor::new());
let conn = server.serve_connection_with_upgrades(io, service);
match tcp_connection_timeout {
Some(duration) => {
let _ = timeout(duration, conn).await;
}
None => {
let _ = conn.await;
}
}
});
client
}