-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: move command parsing progress to lib.rs
1. move command parsing progress to lib.rs 2. rename some variables
- Loading branch information
Showing
4 changed files
with
146 additions
and
148 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.