Skip to content

Commit

Permalink
support opp:// address (the OmniP Protocol)
Browse files Browse the repository at this point in the history
  • Loading branch information
neevek committed May 21, 2024
1 parent 15cbea7 commit 1ed2840
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 13 deletions.
15 changes: 11 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "omnip"
version = "0.4.21"
version = "0.4.22"
edition = "2021"

[lib]
Expand Down Expand Up @@ -34,6 +34,7 @@ http = "0.2"
http-body = "0.4"
mime_guess = "2.0"
monolithica = { git = "https://github.com/neevek/monolithica" }
base64 = "0.22"

[dev-dependencies]
jni = "0.19"
Expand Down
77 changes: 70 additions & 7 deletions src/bin/omnip.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
use anyhow::Result;
use anyhow::{bail, Context, Result};
use base64::prelude::*;
use clap::builder::TypedValueParser as _;
use clap::{builder::PossibleValuesParser, Parser};
use log::error;
use omnip::*;
use rs_utilities::log_and_bail;
use std::env;
use url::Url;

extern crate pretty_env_logger;

fn main() -> Result<()> {
let args = OmnipArgs::parse();
let args = parse_args()?;
if print_args_as_base64(&args) {
return Ok(());
}

let log_filter = format!(
"omnip={},rstun={},rs_utilities={}",
args.loglevel, args.loglevel, args.loglevel
Expand Down Expand Up @@ -38,6 +47,54 @@ fn main() -> Result<()> {
server.run()
}

fn parse_args() -> Result<OmnipArgs> {
let args = OmnipArgs::parse();
if args.addr.starts_with("opp://") {
match Url::parse(args.addr.as_str()) {
Ok(url) => {
let base64_args = url.host().context("invalid opp args")?.to_string();
let space_sep_args = String::from_utf8(
BASE64_STANDARD
.decode(base64_args)
.context("invalid base64")?,
)?;
let parts: Vec<String> = space_sep_args
.split_whitespace()
.map(String::from)
.collect();
let mut vec_args = vec![String::from("")]; // empty string as the first arg (the programm name)
vec_args.extend(parts);

return Ok(OmnipArgs::parse_from(vec_args));
}
_ => {
log_and_bail!("invalid addr: {}", args.addr);
}
};
}
Ok(args)
}

fn print_args_as_base64(args: &OmnipArgs) -> bool {
if args.print_base64 {
let space_sep_args = env::args_os()
.skip(1)
.filter(|arg| arg != "-P" && arg != "--print-base64")
.map(|arg| {
arg.into_string()
.unwrap_or_else(|os_str| os_str.to_string_lossy().into_owned())
})
.collect::<Vec<String>>()
.join(" ");

let base64_args = BASE64_STANDARD.encode(space_sep_args.as_bytes());
println!("opp://{base64_args}");
true
} else {
false
}
}

#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)]
struct OmnipArgs {
Expand All @@ -46,8 +103,8 @@ struct OmnipArgs {
#[arg(short = 'a', long, required = true)]
addr: String,

/// upstream which the proxy server will relay traffic to based on proxy rules, [<http|socks5|socks4>://]ip:port
/// for example: http://127.0.0.1:8000, http+quic://127.0.0.1:8000
/// upstream which the proxy server will relay traffic to based on proxy rules,
/// [<http|socks5|socks4>://]ip:port for example: http://127.0.0.1:8000, http+quic://127.0.0.1:8000
#[arg(short = 'u', long, default_value = "")]
upstream: String,

Expand All @@ -63,7 +120,8 @@ struct OmnipArgs {
#[arg(long, default_value = "")]
dot_server: String,

/// comma saprated domain servers (E.g. 1.1.1.1,8.8.8.8), which will be used if no dot_server is specified, or system default if empty
/// comma saprated domain servers (E.g. 1.1.1.1,8.8.8.8), which will be used
/// if no dot_server is specified, or system default if empty
#[arg(long, default_value = "")]
name_servers: String,

Expand Down Expand Up @@ -99,11 +157,11 @@ struct OmnipArgs {
#[arg(short = 'R', long, default_value = "5000")]
retry_interval_ms: u64,

/// set TCP_NODELAY
/// Set TCP_NODELAY
#[arg(long, action)]
tcp_nodelay: bool,

/// reload proxy rules if updated
/// Reload proxy rules if updated
#[arg(short = 'w', long, action)]
watch_proxy_rules_change: bool,

Expand All @@ -118,4 +176,9 @@ struct OmnipArgs {
_ => "info",
}.to_string()))]
loglevel: String,

/// Print the args as base64 string to be used in opp:// address, will be ignored if passing in
/// as an opp:// address, which can combine all args as a single base64 string
#[arg(short = 'P', long, action)]
print_base64: bool,
}
2 changes: 1 addition & 1 deletion src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ impl Server {

async fn run_internal(self: &mut Arc<Self>) -> Result<()> {
info!(
"tcp_nodelay:{}, threads:%{}",
"tcp_nodelay:{}, threads:{}",
self.config.tcp_nodelay, self.config.threads
);
self.set_and_post_server_state(ServerState::Preparing);
Expand Down

0 comments on commit 1ed2840

Please sign in to comment.