Skip to content

Commit

Permalink
fix(ssurl): make outline url optional
Browse files Browse the repository at this point in the history
ref #1287
  • Loading branch information
zonyitoo committed Sep 11, 2023
1 parent e8ea40c commit c0ba7f0
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 14 deletions.
5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ server = ["shadowsocks-service/server"]
# Enable manager server
manager = ["shadowsocks-service/manager"]
# Enable utility
utility = ["qrcode", "reqwest"]
utility = ["qrcode"]
# Enable service
service = ["local", "server", "manager"]

Expand Down Expand Up @@ -114,6 +114,9 @@ local-socks4 = ["local", "shadowsocks-service/local-socks4"]
# Enable Tun interface protocol for sslocal
local-tun = ["local", "shadowsocks-service/local-tun", "ipnet"]

# ssurl support outline (ssconf) URL
utility-url-outline = ["reqwest"]

# Enable jemalloc for binaries
jemalloc = ["jemallocator"]
# Enable bundled tcmalloc
Expand Down
42 changes: 29 additions & 13 deletions bin/ssurl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
//! SS-URI = "ss://" userinfo "@" hostname ":" port [ "/" ] [ "?" plugin ] [ "#" tag ]
//! userinfo = websafe-base64-encode-utf8(method ":" password)

use std::process::ExitCode;

use clap::{Arg, ArgAction, Command, ValueHint};
use qrcode::{types::Color, QrCode};

Expand Down Expand Up @@ -72,6 +74,7 @@ fn decode(encoded: &str, need_qrcode: bool) {
}
}

#[cfg(feature = "utility-url-outline")]
fn decode_outline(remote: &str, need_qrcode: bool) {
// Protect from using http and other non-ssconf links in reqwest call
if !remote.starts_with("ssconf") {
Expand All @@ -92,8 +95,8 @@ fn decode_outline(remote: &str, need_qrcode: bool) {
}
}

fn main() {
let app = Command::new("ssurl")
fn main() -> ExitCode {
let mut app = Command::new("ssurl")
.version(VERSION)
.about("Encode and decode ShadowSocks URL")
.arg(
Expand All @@ -116,31 +119,44 @@ fn main() {
.help("Decode the server configuration from the provided ShadowSocks URL"),
)
.arg(
Arg::new("QRCODE")
.short('c')
.long("qrcode")
.action(ArgAction::SetTrue)
.help("Generate the QRCode with the provided configuration"),
);

if cfg!(feature = "utility-url-outline") {
app = app.arg(
Arg::new("OUTLINE_CONFIG_URL")
.short('o')
.long("outline")
.value_hint(ValueHint::Url)
.required_unless_present_any(["ENCODE_CONFIG_PATH", "DECODE_CONFIG_PATH"])
.help("Fetch and decode config from ssconf URL used by Outline"),
)
.arg(
Arg::new("QRCODE")
.short('c')
.long("qrcode")
.action(ArgAction::SetTrue)
.help("Generate the QRCode with the provided configuration"),
);
}

let matches = app.get_matches();

let need_qrcode = matches.get_flag("QRCODE");

if let Some(file) = matches.get_one::<String>("ENCODE_CONFIG_PATH") {
encode(file, need_qrcode);
} else if let Some(encoded) = matches.get_one::<String>("DECODE_CONFIG_PATH") {
return ExitCode::SUCCESS;
}

if let Some(encoded) = matches.get_one::<String>("DECODE_CONFIG_PATH") {
decode(encoded, need_qrcode);
} else if let Some(remote) = matches.get_one::<String>("OUTLINE_CONFIG_URL") {
return ExitCode::SUCCESS;
}

#[cfg(feature = "utility-url-outline")]
if let Some(remote) = matches.get_one::<String>("OUTLINE_CONFIG_URL") {
decode_outline(remote, need_qrcode);
} else {
println!("Use -h for more detail");
return ExitCode::SUCCESS;
}

println!("Use -h for more detail");
return ExitCode::FAILURE;

Check warning on line 161 in bin/ssurl.rs

View workflow job for this annotation

GitHub Actions / clippy-check (ubuntu-latest)

unneeded `return` statement

warning: unneeded `return` statement --> bin/ssurl.rs:161:5 | 161 | return ExitCode::FAILURE; | ^^^^^^^^^^^^^^^^^^^^^^^^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_return = note: `#[warn(clippy::needless_return)]` on by default help: remove `return` | 161 - return ExitCode::FAILURE; 161 + ExitCode::FAILURE |

Check warning on line 161 in bin/ssurl.rs

View workflow job for this annotation

GitHub Actions / clippy-check (macos-latest)

unneeded `return` statement

warning: unneeded `return` statement --> bin/ssurl.rs:161:5 | 161 | return ExitCode::FAILURE; | ^^^^^^^^^^^^^^^^^^^^^^^^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_return = note: `#[warn(clippy::needless_return)]` on by default help: remove `return` | 161 - return ExitCode::FAILURE; 161 + ExitCode::FAILURE |
}

0 comments on commit c0ba7f0

Please sign in to comment.