Skip to content

Commit

Permalink
node_url parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
ssrlive committed Jun 3, 2024
1 parent f5427bb commit 52bc9bf
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 14 deletions.
15 changes: 13 additions & 2 deletions src/bin/overtls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ fn main() -> Result<(), BoxError> {
return Err("C API is not supported for server".into());
}

// TODO: Using opt.node_url to generate a config file for client is not supported yet.
let cfg = opt.config.as_ref().ok_or("Config file is required for client")?;

// Test the C API usage
let config_path_str = opt.config.as_path().to_string_lossy().into_owned();
let config_path_str = cfg.as_path().to_string_lossy().into_owned();
let c_string = std::ffi::CString::new(config_path_str)?;
let config_path: *const std::os::raw::c_char = c_string.as_ptr();

Expand Down Expand Up @@ -40,7 +43,15 @@ fn main() -> Result<(), BoxError> {

let is_server = opt.is_server();

let mut config = Config::from_config_file(&opt.config)?;
let mut config = if let Some(file) = opt.config {
Config::from_config_file(file)?
} else if let Some(ref node_url) = opt.node_url {
let mut cfg = Config::from_ssr_url(node_url)?;
cfg.set_listen_addr(opt.listen_addr.ok_or("Listen address is required")?);
cfg
} else {
return Err("Config file or node URL is required".into());
};
config.set_cache_dns(opt.cache_dns);

if opt.generate_url {
Expand Down
40 changes: 37 additions & 3 deletions src/cmdopt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,16 @@ pub struct CmdOpt {
pub role: Role,

/// Config file path
#[arg(short, long, value_name = "file path")]
pub config: std::path::PathBuf,
#[arg(short, long, value_name = "file path", conflicts_with = "node_url")]
pub config: Option<std::path::PathBuf>,

/// URL of the server node used by client
#[arg(short, long, value_name = "url", conflicts_with = "config")]
pub node_url: Option<String>,

/// Local listening address associated with the URL
#[arg(short, long, value_name = "addr:port", requires = "node_url", conflicts_with = "config")]
pub listen_addr: Option<std::net::SocketAddr>,

/// Cache DNS Query result
#[arg(long)]
Expand Down Expand Up @@ -112,6 +120,32 @@ impl CmdOpt {
}

pub fn parse_cmd() -> CmdOpt {
clap::Parser::parse()
fn output_error_and_exit<T: std::fmt::Display>(msg: T) -> ! {
eprintln!("{}", msg);
std::process::exit(1);
}

let args: CmdOpt = clap::Parser::parse();
if args.role == Role::Server {
if args.config.is_none() {
output_error_and_exit("Config file is required for server");
}
if args.c_api {
output_error_and_exit("C API is not supported for server");
}
if args.generate_url {
output_error_and_exit("Generate URL is not supported for server");
}
if args.listen_addr.is_some() {
output_error_and_exit("Listen address is not supported for server");
}
if args.node_url.is_some() {
output_error_and_exit("Node URL is not supported for server");
}
}
if args.role == Role::Client && args.config.is_none() && args.node_url.is_none() {
output_error_and_exit("Config file or node URL is required for client");
}
args
}
}
35 changes: 26 additions & 9 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,18 @@ impl Config {
}
}

pub fn set_listen_addr(&mut self, addr: std::net::SocketAddr) {
if self.is_server {
if let Some(s) = &mut self.server {
s.listen_host = addr.ip().to_string();
s.listen_port = addr.port();
}
} else if let Some(c) = &mut self.client {
c.listen_host = addr.ip().to_string();
c.listen_port = addr.port();
}
}

pub fn disable_tls(&self) -> bool {
if self.is_server {
if let Some(s) = &self.server {
Expand All @@ -290,13 +302,16 @@ impl Config {
pub fn check_correctness(&mut self, is_server: bool) -> Result<()> {
self.is_server = is_server;
if self.is_server {
if self.server.is_none() {
return Err("Configuration needs server settings".into());
}
self.client = None;
} else {
if self.client.is_none() {
return Err("Configuration needs client settings".into());
}
self.server = None;
}
if let (None, None) = (&self.server, &self.client) {
return Err("Need server or client settings".into());
}

if self.tunnel_path.is_empty() {
self.tunnel_path = TunnelPath::default();
Expand Down Expand Up @@ -495,12 +510,14 @@ fn test_config() {
config.method = Some("none".to_string());
config.password = Some("password".to_string());

let mut client = Client::default();
client.server_host = "baidu.com".to_string();
client.server_port = 443;
client.listen_host = "127.0.0.1".to_string();
client.listen_port = 0;
// client.server_domain = Some("baidu.com".to_string());
let client = Client {
server_host: "baidu.com".to_string(),
server_port: 443,
listen_host: "127.0.0.1".to_string(),
listen_port: 0,
// server_domain: Some("baidu.com".to_string()),
..Client::default()
};
config.client = Some(client);

config.check_correctness(false).unwrap();
Expand Down

0 comments on commit 52bc9bf

Please sign in to comment.