use anyhow::Result;
use clap::{Parser, Subcommand};
use localtunnel_client::{broadcast, open_tunnel, ClientConfig};
use localtunnel_server::{start, ServerConfig};
use tokio::signal;
mod config;
#[derive(Parser)]
#[clap(author, version, about)]
#[clap(propagate_version = true)]
struct Cli {
#[clap(subcommand)]
command: Command,
}
#[derive(Subcommand)]
enum Command {
Client {
#[clap(long)]
host: String,
#[clap(long)]
subdomain: String,
#[clap(long, default_value = "127.0.0.1")]
local_host: String,
#[clap(short, long)]
port: u16,
#[clap(long, default_value = "10")]
max_conn: u8,
#[clap(long)]
credential: Option<String>,
},
Server {
#[clap(long)]
domain: String,
#[clap(short, long, default_value = "3000")]
port: u16,
#[clap(long)]
secure: bool,
#[clap(long, default_value = "10")]
max_sockets: u8,
#[clap(long, default_value = "3001")]
proxy_port: u16,
#[clap(long)]
require_auth: bool,
},
}
#[tokio::main]
async fn main() -> Result<()> {
config::setup();
log::info!("Run localtunnel CLI!");
let command = Cli::parse().command;
match command {
Command::Client {
host,
subdomain,
local_host,
port,
max_conn,
credential,
} => {
let (notify_shutdown, _) = broadcast::channel(1);
let config = ClientConfig {
server: Some(host),
subdomain: Some(subdomain),
local_host: Some(local_host),
local_port: port,
shutdown_signal: notify_shutdown.clone(),
max_conn,
credential,
};
let result = open_tunnel(config).await?;
log::info!("Tunnel url: {:?}", result);
signal::ctrl_c().await?;
log::info!("Quit");
}
Command::Server {
domain,
port,
secure,
max_sockets,
proxy_port,
require_auth,
} => {
let config = ServerConfig {
domain,
api_port: port,
secure,
max_sockets,
proxy_port,
require_auth,
};
start(config).await?;
}
}
Ok(())
}