Skip to content

Commit

Permalink
generate_ssr_url function
Browse files Browse the repository at this point in the history
  • Loading branch information
ssrlive committed May 22, 2024
1 parent 266ade4 commit 638f2ee
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
8 changes: 7 additions & 1 deletion src/bin/overtls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,13 @@ fn main() -> Result<(), BoxError> {
config.set_cache_dns(opt.cache_dns);

if opt.qrcode {
let qrcode = config.generate_ssr_qrcode()?;
if let Some(ref ca) = config.certificate_content() {
if !opt.qrcode_cert {
eprintln!("Certificate content:");
eprint!("{}", ca);
}
}
let qrcode = config.generate_ssr_url(opt.qrcode_cert)?;
println!("{}", qrcode);
return Ok(());
}
Expand Down
4 changes: 4 additions & 0 deletions src/cmdopt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ pub struct CmdOpt {
#[arg(short, long)]
pub qrcode: bool,

/// QR code contains certificate content.
#[arg(long)]
pub qrcode_cert: bool,

/// Use C API for client.
#[arg(long)]
pub c_api: bool,
Expand Down
12 changes: 7 additions & 5 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ impl Config {
Ok(config)
}

pub fn generate_ssr_qrcode(&self) -> Result<String> {
pub fn generate_ssr_url(&self, include_ca_file: bool) -> Result<String> {
let client = self.client.as_ref().ok_or(Error::from("client is not set"))?;
let engine = crate::Base64Engine::UrlSafeNoPad;
let method = self.method.as_ref().map_or("none".to_string(), |m| m.clone());
Expand All @@ -467,9 +467,11 @@ impl Config {
let mut url = format!("{host}:{port}:origin:{method}:plain:{password}/?remarks={remarks}&ot_enable=1");
url.push_str(&format!("&ot_domain={domain}&ot_path={tunnel_path}"));

if let Some(ref ca) = client.certificate_content() {
let ca = crate::base64_encode(ca.as_bytes(), engine);
url.push_str(&format!("&ot_cert={}", ca));
if include_ca_file {
if let Some(ref ca) = client.certificate_content() {
let ca = crate::base64_encode(ca.as_bytes(), engine);
url.push_str(&format!("&ot_cert={}", ca));
}
}

Ok(format!("ssr://{}", crate::base64_encode(url.as_bytes(), engine)))
Expand All @@ -494,7 +496,7 @@ fn test_config() {

config.check_correctness(false).unwrap();

let qrcode = config.generate_ssr_qrcode().unwrap();
let qrcode = config.generate_ssr_url(true).unwrap();
println!("{:?}", qrcode);

let config = Config::from_ssr_url(&qrcode).unwrap();
Expand Down

0 comments on commit 638f2ee

Please sign in to comment.