Skip to content

Commit

Permalink
refactor: move command parsing progress to lib.rs
Browse files Browse the repository at this point in the history
1. move command parsing progress to lib.rs
2. rename some variables
  • Loading branch information
X1r0z committed Dec 21, 2024
1 parent 0dccd99 commit 5380504
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 148 deletions.
28 changes: 14 additions & 14 deletions src/forward.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,24 @@ pub struct Forward {
remote_addrs: Vec<String>,
socket: Option<String>,
udp: bool,
local_ssl_opts: Vec<bool>,
remote_ssl_opts: Vec<bool>,
local_opts: Vec<bool>,
remote_opts: Vec<bool>,
}

impl Forward {
pub fn new(
local_addrs: Vec<String>,
remote_addrs: Vec<String>,
local_ssl_opts: Vec<bool>,
remote_ssl_opts: Vec<bool>,
local_opts: Vec<bool>,
remote_opts: Vec<bool>,
socket: Option<String>,
udp: bool,
) -> Self {
Self {
local_addrs,
remote_addrs,
local_ssl_opts,
remote_ssl_opts,
local_opts,
remote_opts,
socket,
udp,
}
Expand Down Expand Up @@ -81,12 +81,12 @@ impl Forward {
info!("Bind to {} success", listener1.local_addr()?);
info!("Bind to {} success", listener1.local_addr()?);

let acceptor1 = Arc::new(match self.local_ssl_opts[0] {
let acceptor1 = Arc::new(match self.local_opts[0] {
true => Some(crypto::get_tls_acceptor(&self.local_addrs[0])),
false => None,
});

let acceptor2 = Arc::new(match self.local_ssl_opts[1] {
let acceptor2 = Arc::new(match self.local_opts[1] {
true => Some(crypto::get_tls_acceptor(&self.local_addrs[1])),
false => None,
});
Expand Down Expand Up @@ -116,12 +116,12 @@ impl Forward {
let listener = TcpListener::bind(&self.local_addrs[0]).await?;
info!("Bind to {} success", listener.local_addr()?);

let acceptor = Arc::new(match self.local_ssl_opts[0] {
let acceptor = Arc::new(match self.local_opts[0] {
true => Some(crypto::get_tls_acceptor(&self.local_addrs[0])),
false => None,
});

let connector = Arc::new(match self.remote_ssl_opts[0] {
let connector = Arc::new(match self.remote_opts[0] {
true => Some(crypto::get_tls_connector()),
false => None,
});
Expand Down Expand Up @@ -152,12 +152,12 @@ impl Forward {
}

async fn remote_to_remote_tcp(&self) -> Result<()> {
let connector1 = Arc::new(match self.remote_ssl_opts[0] {
let connector1 = Arc::new(match self.remote_opts[0] {
true => Some(crypto::get_tls_connector()),
false => None,
});

let connector2 = Arc::new(match self.remote_ssl_opts[1] {
let connector2 = Arc::new(match self.remote_opts[1] {
true => Some(crypto::get_tls_connector()),
false => None,
});
Expand Down Expand Up @@ -193,7 +193,7 @@ impl Forward {
let local_listener = TcpListener::bind(&self.local_addrs[0]).await?;
info!("Bind to {} success", local_listener.local_addr()?);

let acceptor = Arc::new(match self.local_ssl_opts[0] {
let acceptor = Arc::new(match self.local_opts[0] {
true => Some(crypto::get_tls_acceptor(&self.local_addrs[0])),
false => None,
});
Expand Down Expand Up @@ -221,7 +221,7 @@ impl Forward {
async fn socket_to_remote_tcp(&self) -> Result<()> {
let socket_path = self.socket.as_ref().unwrap();

let connector = Arc::new(match self.remote_ssl_opts[0] {
let connector = Arc::new(match self.remote_opts[0] {
true => Some(crypto::get_tls_connector()),
false => None,
});
Expand Down
119 changes: 119 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,125 @@
use std::io::Result;

use clap::{Parser, Subcommand};
use forward::Forward;
use proxy::Proxy;
use tracing::info;

pub mod crypto;
pub mod forward;
pub mod proxy;
pub mod socks;
pub mod tcp;
pub mod udp;

#[derive(Parser)]
#[command(author, version, about = "Rsproxy: Port-Forwarding and Proxy Tool")]
pub struct Cli {
#[command(subcommand)]
command: Commands,
}

#[derive(Subcommand)]
pub enum Commands {
/// Port forwarding mode
Fwd {
/// Local listen address, format: [+][IP:]PORT
#[arg(short, long)]
local: Vec<String>,

/// Remote connect address, format: [+]IP:PORT
#[arg(short, long)]
remote: Vec<String>,

/// Unix domain socket path
#[arg(short, long)]
socket: Option<String>,

/// Enable UDP forward mode
#[arg(short, long)]
udp: bool,
},

/// Socks proxy mode
Socks {
/// Local listen address, format: [+][IP:]PORT
#[arg(short, long)]
local: Vec<String>,

/// Reverse server address, format: [+]IP:PORT
#[arg(short, long)]
remote: Option<String>,
},
}

pub async fn run(cli: Cli) -> Result<()> {
match cli.command {
Commands::Fwd {
mut local,
mut remote,
socket,
udp,
} => {
info!("Starting forward mode");

if udp {
info!("Using UDP protocol");
} else {
info!("Using TCP protocol");
}

let mut local_opts = Vec::new();
let mut remote_opts = Vec::new();

load_opts(&mut local, &mut local_opts);
load_opts(&mut remote, &mut remote_opts);

format_addrs(&mut local);

let forward = Forward::new(local, remote, local_opts, remote_opts, socket, udp);
forward.start().await?;
}
Commands::Socks { mut local, remote } => {
info!("Starting proxy mode");

let mut local_opts = Vec::new();
let mut remote_opt = false;

load_opts(&mut local, &mut local_opts);

let remote = match remote {
Some(remote) => Some(if remote.starts_with('+') {
remote_opt = true;
remote.replace("+", "")
} else {
remote
}),
None => None,
};

let proxy = Proxy::new(local, remote, local_opts, remote_opt);
proxy.start().await?;
}
}

Ok(())
}

pub fn load_opts(addrs: &mut Vec<String>, opts: &mut Vec<bool>) {
for addr in addrs {
if addr.starts_with('+') {
*addr = addr.replace("+", "");
opts.push(true);
} else {
opts.push(false);
}
}
}

pub fn format_addrs(addrs: &mut Vec<String>) {
for addr in addrs {
if !addr.contains(":") {
*addr = "0.0.0.0:".to_string() + addr;
}
}
}
127 changes: 3 additions & 124 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,132 +1,11 @@
use clap::Parser;
use rsproxy::Cli;
use std::io::Result;

use clap::{Parser, Subcommand};
use rsproxy::{forward::Forward, proxy::Proxy};
use tracing::info;

#[derive(Parser)]
#[command(author, version, about = "Rsproxy: Port-Forwarding and Proxy Tool")]
struct Cli {
#[command(subcommand)]
command: Commands,
}

#[derive(Subcommand)]
enum Commands {
/// Port forwarding mode
Fwd {
/// Local listen address, format: [+][IP:]PORT
#[arg(short, long)]
local: Vec<String>,

/// Remote connect address, format: [+]IP:PORT
#[arg(short, long)]
remote: Vec<String>,

/// Unix domain socket path
#[arg(short, long)]
socket: Option<String>,

/// Enable UDP forward mode
#[arg(short, long)]
udp: bool,
},

/// Socks proxy mode
Socks {
/// Local listen address, format: [+][IP:]PORT
#[arg(short, long)]
local: Vec<String>,

/// Reverse server address, format: [+]IP:PORT
#[arg(short, long)]
remote: Option<String>,
},
}
#[tokio::main]
async fn main() -> Result<()> {
tracing_subscriber::fmt::init();

let cli = Cli::parse();

match cli.command {
Commands::Fwd {
mut local,
mut remote,
socket,
udp,
} => {
info!("Starting forward mode");

if udp {
info!("Using UDP protocol");
} else {
info!("Using TCP protocol");
}

let mut local_ssl_opts = Vec::new();
let mut remote_ssl_opts = Vec::new();

for addr in &mut local {
if addr.starts_with('+') {
*addr = addr.replace("+", "");
local_ssl_opts.push(true);
} else {
local_ssl_opts.push(false);
}

if !addr.contains(":") {
*addr = "0.0.0.0:".to_string() + addr;
}
}

for addr in &mut remote {
if addr.starts_with('+') {
*addr = addr.replace("+", "");
remote_ssl_opts.push(true);
} else {
remote_ssl_opts.push(false);
}
}

let forward = Forward::new(local, remote, local_ssl_opts, remote_ssl_opts, socket, udp);
forward.start().await?;
}
Commands::Socks { mut local, remote } => {
info!("Starting proxy mode");

let mut local_ssl_opts = Vec::new();
let mut remote_ssl_opt = false;

for addr in &mut local {
if addr.starts_with('+') {
*addr = addr.replace("+", "");
local_ssl_opts.push(true);
} else {
local_ssl_opts.push(false);
}

if !addr.contains(":") {
*addr = "0.0.0.0:".to_string() + addr;
}
}

let remote = match remote {
Some(remote) => {
if remote.starts_with('+') {
remote_ssl_opt = true;
Some(remote.replace("+", ""))
} else {
Some(remote)
}
}
None => None,
};

let proxy = Proxy::new(local, remote, local_ssl_opts, remote_ssl_opt);
proxy.start().await?;
}
}

Ok(())
rsproxy::run(cli).await
}
Loading

0 comments on commit 5380504

Please sign in to comment.