mod api; mod auth; mod cli; mod config; mod connect; #[cfg(feature = "gui")] mod gui; mod handler; mod headless; mod mux; mod qr; mod router; mod serve; mod service; mod session; mod shape; mod tcp; mod tls; mod tui; mod tunnel; mod update; use clap::{Parser, Subcommand}; use console::style; use std::path::PathBuf; #[derive(Parser)] #[command(name = "frislit", about = "frislit CLI - expose local ports through your own frislit server", version)] struct Cli { #[command(subcommand)] command: Commands, } #[derive(Subcommand)] enum Commands { Login { link: Option, #[arg(long, value_name = "KEY", help = "Link with an API key instead of the browser")] token: Option, #[arg(short, long)] server: Option, }, Logout, Http { target: String, #[arg(short, long)] name: Option, #[arg(long, value_name = "PATH|SUBDOMAIN", help = "Forwarding style for this tunnel (default: the server setting)")] mode: Option, #[arg(long, value_name = "PREFIX=TARGET")] route: Vec, #[arg(long)] keep_host: bool, #[command(flatten)] rules: RuleArgs, #[arg(long, value_name = "PASSWORD")] password: Option, #[arg(long)] require_login: bool, #[command(flatten)] shape: ShapeArgs, #[arg(long, help = "Print plain log lines instead of the terminal UI")] plain: bool, }, Serve { #[arg(default_value = ".")] dir: String, #[arg(short, long)] name: Option, #[arg(long, value_name = "PATH|SUBDOMAIN", help = "Forwarding style for this tunnel (default: the server setting)")] mode: Option, #[command(flatten)] rules: RuleArgs, #[arg(long, value_name = "PASSWORD")] password: Option, #[arg(long)] require_login: bool, #[command(flatten)] shape: ShapeArgs, #[arg(long, help = "Print plain log lines instead of the terminal UI")] plain: bool, }, Tcp { target: String, #[arg(short, long)] name: Option, #[command(flatten)] rules: RuleArgs, #[command(flatten)] shape: ShapeArgs, #[arg(long, help = "Print plain log lines instead of the terminal UI")] plain: bool, }, Connect { target: String, #[arg(short, long)] port: Option, #[arg(short, long, default_value = "127.0.0.1")] bind: String, #[arg(short, long)] server: Option, }, #[command(alias = "status")] Ls, Start { #[arg(short, long, value_name = "FILE")] config: Option, }, Service { #[command(subcommand)] action: ServiceAction, }, #[cfg(feature = "gui")] Gui { link: Option, #[arg(long)] hidden: bool, }, Config { #[command(subcommand)] action: ConfigAction, }, Update, Links { #[command(subcommand)] action: LinkAction, }, } #[derive(clap::Args)] struct RuleArgs { #[arg(long, value_name = "CIDR")] allow: Vec, #[arg(long, value_name = "CODE")] allow_country: Vec, #[arg(long, value_name = "CIDR")] block_ip: Vec, #[arg(long, value_name = "CODE")] block_country: Vec, #[arg(long, value_name = "CATEGORY", help = "tor, vpn, datacenter or blocklist")] block: Vec, } impl RuleArgs { fn parse(self) -> anyhow::Result { tunnel::Rules::new(self.allow, self.allow_country, self.block_ip, self.block_country, self.block) } } #[derive(clap::Args)] struct ShapeArgs { #[arg(long, value_name = "DURATION")] latency: Option, #[arg(long, value_name = "DURATION")] jitter: Option, #[arg(long, value_name = "RATE")] bandwidth: Option, #[arg(long, value_name = "PERCENT")] loss: Option, } impl ShapeArgs { fn parse(&self) -> anyhow::Result { shape::Shape::parse(self.latency.as_deref(), self.jitter.as_deref(), self.bandwidth.as_deref(), self.loss.as_deref()) } } #[derive(Subcommand)] enum ServiceAction { Install, Uninstall, } #[derive(Subcommand)] enum LinkAction { Register, Forget, } #[derive(Subcommand)] enum ConfigAction { Set { key: String, value: String }, Get { key: String }, Show, } fn main() { tls::install_provider(); if cfg!(windows) { update::cleanup(); } let cli = Cli::parse(); let runtime = tokio::runtime::Builder::new_multi_thread().worker_threads(4).enable_all().build().unwrap_or_else(|err| fail(err.into())); #[cfg(feature = "gui")] if let Commands::Gui { link, hidden } = cli.command { gui::launch(runtime.handle().clone(), link, hidden).unwrap_or_else(|err| fail(err)); return; } runtime.block_on(run(cli)).unwrap_or_else(|err| fail(err)); } fn fail(err: anyhow::Error) -> ! { eprintln!("{} {err:#}", style("✗").red().bold()); std::process::exit(1) } async fn run(cli: Cli) -> anyhow::Result<()> { match cli.command { Commands::Login { link, token, server } => cli::login(link, token, server).await, Commands::Logout => cli::logout(), Commands::Http { target, name, mode, route, keep_host, rules, password, require_login, shape, plain } => cli::tunnel(tunnel::Options::http(tunnel::TargetSpec::with_routes(&target, &route)?, name, keep_host, tunnel::Access::new(rules.parse()?, password, require_login)?).http_mode(mode).shaped(shape.parse()?), plain).await, Commands::Serve { dir, name, mode, rules, password, require_login, shape, plain } => cli::tunnel(tunnel::Options::http(tunnel::TargetSpec::dir(&dir)?, name, false, tunnel::Access::new(rules.parse()?, password, require_login)?).http_mode(mode).shaped(shape.parse()?), plain).await, Commands::Tcp { target, name, rules, shape, plain } => cli::tunnel(tunnel::Options::tcp(tunnel::Target::parse(&target)?, name, tunnel::Access::new(rules.parse()?, None, false)?).shaped(shape.parse()?), plain).await, Commands::Connect { target, port, bind, server } => cli::connect(target, port, bind, server).await, Commands::Ls => cli::list().await, Commands::Start { config } => headless::run(config).await, Commands::Service { action } => match action { ServiceAction::Install => service::install(), ServiceAction::Uninstall => service::uninstall(), }, #[cfg(feature = "gui")] Commands::Gui { .. } => unreachable!("handled before the runtime starts"), Commands::Links { action } => match action { LinkAction::Register => { let where_ = handler::install()?; println!("{} {} links now open in this frislit", style("✓").green().bold(), style(format!("{}://", handler::SCHEME)).cyan().bold()); println!(" {}", style(where_.to_string_lossy()).dim()); println!(" Your browser will ask before it opens one."); Ok(()) } LinkAction::Forget => { handler::remove()?; println!("{} {} links are no longer handled by frislit", style("✓").green().bold(), style(format!("{}://", handler::SCHEME)).bold()); Ok(()) } }, Commands::Update => cli::update().await, Commands::Config { action } => match action { ConfigAction::Set { key, value } => config::set(&key, &value), ConfigAction::Get { key } => config::get(&key), ConfigAction::Show => config::show(), }, } }