Reorganized files

This commit is contained in:
Dreaded_X 2025-04-16 02:55:42 +02:00
parent 4fe64981d0
commit 19ec3714a6
Signed by: Dreaded_X
GPG Key ID: FA5F485356B0D2D4
20 changed files with 118 additions and 114 deletions

View File

@ -1,23 +0,0 @@
use clap::Parser;
/// Quickly create http tunnels for development
#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
pub struct Args {
/// Make all tunnels public by default instead of private
#[arg(long, group = "access")]
public: bool,
#[arg(long, group = "access")]
protected: bool,
}
impl Args {
pub fn make_public(&self) -> bool {
self.public
}
pub fn make_protected(&self) -> bool {
self.protected
}
}

5
src/helper/mod.rs Normal file
View File

@ -0,0 +1,5 @@
mod animals;
mod units;
pub use animals::get_animal_name;
pub use units::Unit;

7
src/io/mod.rs Normal file
View File

@ -0,0 +1,7 @@
mod input;
mod stats;
mod terminal_handle;
pub use input::Input;
pub use stats::{Stats, TrackStats};
pub use terminal_handle::TerminalHandle;

View File

@ -7,23 +7,58 @@ use std::{
use pin_project_lite::pin_project;
use russh::{ChannelStream, server::Msg};
use crate::stats::Stats;
use crate::helper::Unit;
use std::sync::atomic::{AtomicUsize, Ordering};
#[derive(Debug, Default)]
pub struct Stats {
connections: AtomicUsize,
rx: AtomicUsize,
tx: AtomicUsize,
}
impl Stats {
pub fn add_connection(&self) {
self.connections.fetch_add(1, Ordering::Relaxed);
}
pub fn add_rx_bytes(&self, n: usize) {
self.rx.fetch_add(n, Ordering::Relaxed);
}
pub fn add_tx_bytes(&self, n: usize) {
self.tx.fetch_add(n, Ordering::Relaxed);
}
pub fn connections(&self) -> usize {
self.connections.load(Ordering::Relaxed)
}
pub fn rx(&self) -> Unit {
Unit::new(self.rx.load(Ordering::Relaxed), "B")
}
pub fn tx(&self) -> Unit {
Unit::new(self.tx.load(Ordering::Relaxed), "B")
}
}
pin_project! {
pub struct Wrapper {
pub struct TrackStats {
#[pin]
inner: ChannelStream<Msg>,
stats: Arc<Stats>,
}
}
impl Wrapper {
impl TrackStats {
pub fn new(inner: ChannelStream<Msg>, stats: Arc<Stats>) -> Self {
Self { inner, stats }
}
}
impl hyper::rt::Read for Wrapper {
impl hyper::rt::Read for TrackStats {
fn poll_read(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
@ -47,7 +82,7 @@ impl hyper::rt::Read for Wrapper {
}
}
impl hyper::rt::Write for Wrapper {
impl hyper::rt::Write for TrackStats {
fn poll_write(
self: Pin<&mut Self>,
cx: &mut Context<'_>,

View File

@ -1,22 +1,7 @@
#![feature(let_chains)]
mod animals;
pub mod auth;
mod cli;
mod handler;
mod helper;
mod input;
mod io;
mod ldap;
mod server;
mod stats;
mod tui;
mod tunnel;
mod units;
mod web;
mod wrapper;
pub use ldap::Ldap;
pub use server::Server;
pub use tunnel::Registry;
pub use tunnel::Tunnel;
pub use web::Service;
pub mod ldap;
pub mod ssh;
pub mod tunnel;
pub mod web;

View File

@ -8,7 +8,12 @@ use rand::rngs::OsRng;
use tokio::net::TcpListener;
use tracing::{error, info, warn};
use tracing_subscriber::{EnvFilter, layer::SubscriberExt, util::SubscriberInitExt};
use tunnel_rs::{Ldap, Registry, Server, Service, auth::ForwardAuth};
use tunnel_rs::{
ldap::Ldap,
ssh::Server,
tunnel::Registry,
web::{ForwardAuth, Service},
};
#[tokio::main]
async fn main() -> color_eyre::Result<()> {

View File

@ -1,6 +1,6 @@
use std::{cmp::min, io::Write, iter::once};
use clap::Parser as _;
use clap::Parser;
use ratatui::{Terminal, TerminalOptions, Viewport, layout::Rect, prelude::CrosstermBackend};
use russh::{
ChannelId,
@ -10,14 +10,33 @@ use russh::{
use tracing::{debug, trace, warn};
use crate::{
Ldap, cli,
input::Input,
io::TerminalHandle,
ldap::LdapError,
tui::Renderer,
io::{Input, TerminalHandle},
ldap::{Ldap, LdapError},
tunnel::{Registry, Tunnel, TunnelAccess},
};
/// Quickly create http tunnels for development
#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
pub struct Args {
/// Make all tunnels public by default instead of private
#[arg(long, group = "access")]
public: bool,
#[arg(long, group = "access")]
protected: bool,
}
impl Args {
pub fn make_public(&self) -> bool {
self.public
}
pub fn make_protected(&self) -> bool {
self.protected
}
}
#[derive(Debug, thiserror::Error)]
pub enum HandlerError {
#[error(transparent)]
@ -38,7 +57,7 @@ pub struct Handler {
pty_channel: Option<ChannelId>,
terminal: Option<Terminal<CrosstermBackend<TerminalHandle>>>,
renderer: Renderer,
renderer: super::Renderer,
selected: Option<usize>,
rename_buffer: Option<String>,
@ -315,7 +334,7 @@ impl russh::server::Handler for Handler {
trace!(?cmd, "exec_request");
let cmd = once("<ssh command> --").chain(cmd.split_whitespace());
match cli::Args::try_parse_from(cmd) {
match Args::try_parse_from(cmd) {
Ok(args) => {
debug!("{args:?}");
if args.make_public() {

View File

@ -1,10 +1,15 @@
mod handler;
mod renderer;
use std::{net::SocketAddr, sync::Arc, time::Duration};
use russh::{MethodKind, keys::PrivateKey, server::Server as _};
use tokio::net::ToSocketAddrs;
use tracing::{debug, warn};
use crate::{Ldap, handler::Handler, tunnel::Registry};
use crate::{ldap::Ldap, tunnel::Registry};
use handler::Handler;
use renderer::Renderer;
pub struct Server {
ldap: Ldap,

View File

@ -1,36 +0,0 @@
use std::sync::atomic::{AtomicUsize, Ordering};
use crate::units::Unit;
#[derive(Debug, Default)]
pub struct Stats {
connections: AtomicUsize,
rx: AtomicUsize,
tx: AtomicUsize,
}
impl Stats {
pub fn add_connection(&self) {
self.connections.fetch_add(1, Ordering::Relaxed);
}
pub fn add_rx_bytes(&self, n: usize) {
self.rx.fetch_add(n, Ordering::Relaxed);
}
pub fn add_tx_bytes(&self, n: usize) {
self.tx.fetch_add(n, Ordering::Relaxed);
}
pub fn connections(&self) -> usize {
self.connections.load(Ordering::Relaxed)
}
pub fn rx(&self) -> Unit {
Unit::new(self.rx.load(Ordering::Relaxed), "B")
}
pub fn tx(&self) -> Unit {
Unit::new(self.tx.load(Ordering::Relaxed), "B")
}
}

View File

@ -8,19 +8,19 @@ use tracing::trace;
use russh::server::Handle;
use tokio::sync::{RwLock, RwLockReadGuard};
use crate::{stats::Stats, wrapper::Wrapper};
pub use registry::Registry;
use crate::io::{Stats, TrackStats};
#[derive(Debug, Clone)]
pub enum TunnelAccess {
pub(crate) enum TunnelAccess {
Private(String),
Protected,
Public,
}
#[derive(Debug, Clone)]
pub struct TunnelInner {
pub(crate) struct TunnelInner {
handle: Handle,
internal_address: String,
port: u32,
@ -29,7 +29,7 @@ pub struct TunnelInner {
}
impl TunnelInner {
pub async fn open(&self) -> Result<Wrapper, russh::Error> {
pub(crate) async fn open(&self) -> Result<TrackStats, russh::Error> {
trace!("Opening tunnel");
self.stats.add_connection();
let channel = self
@ -42,20 +42,20 @@ impl TunnelInner {
)
.await?;
Ok(Wrapper::new(channel.into_stream(), self.stats.clone()))
Ok(TrackStats::new(channel.into_stream(), self.stats.clone()))
}
pub async fn is_public(&self) -> bool {
pub(crate) async fn is_public(&self) -> bool {
matches!(*self.access.read().await, TunnelAccess::Public)
}
pub async fn get_access(&self) -> RwLockReadGuard<'_, TunnelAccess> {
pub(crate) async fn get_access(&self) -> RwLockReadGuard<'_, TunnelAccess> {
self.access.read().await
}
}
#[derive(Debug)]
pub struct Tunnel {
pub(crate) struct Tunnel {
inner: TunnelInner,
registry: Registry,

View File

@ -6,19 +6,19 @@ use std::{
use tokio::sync::RwLock;
use tracing::trace;
use crate::{Tunnel, animals::get_animal_name};
use crate::{helper::get_animal_name, tunnel::Tunnel};
use super::TunnelInner;
#[derive(Debug)]
pub struct RegistryEntry {
pub(crate) struct RegistryEntry {
registry: Registry,
name: String,
address: Option<String>,
}
impl RegistryEntry {
pub fn new(registry: Registry) -> Self {
pub(crate) fn new(registry: Registry) -> Self {
Self {
registry,
name: Default::default(),
@ -26,11 +26,11 @@ impl RegistryEntry {
}
}
pub fn get_address(&self) -> Option<&String> {
pub(crate) fn get_address(&self) -> Option<&String> {
self.address.as_ref()
}
pub fn get_name(&self) -> &str {
pub(crate) fn get_name(&self) -> &str {
&self.name
}
}
@ -124,7 +124,7 @@ impl Registry {
self.register(tunnel).await;
}
pub async fn get(&self, address: &str) -> Option<TunnelInner> {
pub(crate) async fn get(&self, address: &str) -> Option<TunnelInner> {
self.tunnels.read().await.get(address).cloned()
}
}

View File

@ -1,4 +1,7 @@
use crate::Registry;
mod auth;
mod response;
use crate::tunnel::Registry;
use std::{ops::Deref, pin::Pin};
use bytes::Bytes;
@ -11,11 +14,10 @@ use hyper::{
};
use tracing::{debug, error, trace, warn};
use crate::{
auth::{AuthStatus, ForwardAuth},
helper::response,
tunnel::TunnelAccess,
};
use crate::tunnel::TunnelAccess;
use auth::AuthStatus;
pub use auth::ForwardAuth;
use response::response;
#[derive(Debug, Clone)]
pub struct Service {