tun2proxy/src/main.rs

66 lines
1.8 KiB
Rust
Raw Normal View History

2023-03-22 01:02:27 +01:00
use std::net::SocketAddr;
2023-03-20 14:13:06 +08:00
use clap::{Parser, ValueEnum};
2022-08-16 16:18:25 +02:00
use env_logger::Env;
2023-03-22 01:02:27 +01:00
use tun2proxy::tun2proxy::Credentials;
2023-03-20 17:56:54 +01:00
use tun2proxy::{main_entry, ProxyType};
2023-03-20 14:13:06 +08:00
/// Tunnel interface to proxy
#[derive(Parser)]
#[command(author, version, about = "Tunnel interface to proxy.", long_about = None)]
struct Args {
/// Name of the tun interface
2023-03-20 17:27:28 +08:00
#[arg(short, long, value_name = "name")]
2023-03-20 14:13:06 +08:00
tun: String,
/// What proxy type to run
2023-03-20 17:27:28 +08:00
#[arg(short, long = "proxy", value_name = "type", value_enum)]
2023-03-20 17:52:15 +08:00
proxy_type: ArgProxyType,
2023-03-20 14:13:06 +08:00
2023-03-20 17:27:28 +08:00
/// Server address with format ip:port
#[clap(short, long, value_name = "ip:port")]
2023-03-20 14:13:06 +08:00
addr: SocketAddr,
2023-03-22 01:02:27 +01:00
/// Username for authentication
#[clap(long, value_name = "username")]
username: Option<String>,
2023-03-22 10:12:32 +01:00
/// Password for authentication
2023-03-22 01:02:27 +01:00
#[clap(long, value_name = "password")]
password: Option<String>,
2023-03-20 14:13:06 +08:00
}
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, ValueEnum)]
2023-03-20 17:52:15 +08:00
enum ArgProxyType {
2023-03-20 14:13:06 +08:00
/// SOCKS5 server to use
Socks5,
/// HTTP server to use
Http,
}
2021-09-02 11:30:23 +02:00
fn main() {
2022-08-16 16:18:25 +02:00
env_logger::Builder::from_env(Env::default().default_filter_or("info")).init();
2023-03-20 14:13:06 +08:00
let args = Args::parse();
2021-09-02 11:30:23 +02:00
2023-03-22 01:02:27 +01:00
let credentials = if args.username.is_some() || args.password.is_some() {
Credentials::new(
args.username.unwrap_or(String::from("")),
args.password.unwrap_or(String::from("")),
)
} else {
Credentials::none()
};
2023-03-20 14:13:06 +08:00
match args.proxy_type {
2023-03-20 17:52:15 +08:00
ArgProxyType::Socks5 => {
2023-03-20 14:13:06 +08:00
log::info!("SOCKS5 server: {}", args.addr);
2023-03-22 01:02:27 +01:00
main_entry(&args.tun, args.addr, ProxyType::Socks5, credentials);
2021-09-02 11:30:23 +02:00
}
2023-03-20 17:52:15 +08:00
ArgProxyType::Http => {
2023-03-20 14:13:06 +08:00
log::info!("HTTP server: {}", args.addr);
2023-03-22 01:02:27 +01:00
main_entry(&args.tun, args.addr, ProxyType::Http, credentials);
2021-09-02 11:30:23 +02:00
}
}
}