Skip to content

Commit

Permalink
minor changes for examples
Browse files Browse the repository at this point in the history
  • Loading branch information
ssrlive committed Nov 19, 2023
1 parent 1531655 commit af5b71b
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 17 deletions.
3 changes: 2 additions & 1 deletion examples/dns-query.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
mod dns;
mod util;

use socks5_impl::{client, protocol::UserKey, Result};
use std::{net::SocketAddr, time::Duration};
Expand All @@ -7,6 +7,7 @@ use tokio::{
net::TcpStream,
};
use trust_dns_proto::rr::record_type::RecordType;
use util::dns;

/// DNS query through socks5 proxy.
#[derive(clap::Parser, Debug, Clone, PartialEq, Eq)]
Expand Down
24 changes: 17 additions & 7 deletions examples/dns2socks.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
mod dns;
mod util;

use moka::future::Cache;
use socks5_impl::{
Expand All @@ -12,6 +12,7 @@ use tokio::{
net::{TcpListener, TcpStream, ToSocketAddrs, UdpSocket},
};
use trust_dns_proto::op::{Message, Query};
use util::dns;

const MAX_BUFFER_SIZE: usize = 4096;

Expand Down Expand Up @@ -47,27 +48,36 @@ pub struct CmdOpt {
#[clap(short, long)]
cache_records: bool,

/// Verbose mode.
#[clap(short, long)]
verbose: bool,
/// Verbosity level
#[arg(short, long, value_name = "level", value_enum, default_value = "info")]
verbosity: ArgVerbosity,

/// Timeout for DNS query
#[clap(short, long, value_name = "seconds", default_value = "5")]
timeout: u64,
}

#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, clap::ValueEnum)]
enum ArgVerbosity {
Off,
Error,
Warn,
Info,
Debug,
Trace,
}

#[tokio::main]
async fn main() -> Result<()> {
let opt: CmdOpt = clap::Parser::parse();

dotenvy::dotenv().ok();

let level = if opt.verbose { "trace" } else { "info" };
let default = format!("off,{}={}", module_path!(), level);
let default = format!("{}={:?}", module_path!(), opt.verbosity);
env_logger::Builder::from_env(env_logger::Env::default().default_filter_or(default)).init();

let user_key = match (&opt.username, &opt.password) {
(Some(username), Some(password)) => Some(UserKey::new(username, password)),
(Some(username), password) => Some(UserKey::new(username, password.clone().unwrap_or_default())),
_ => None,
};

Expand Down
22 changes: 16 additions & 6 deletions examples/simple-server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,19 @@ pub struct CmdOpt {
#[clap(short, long, value_name = "password")]
password: Option<String>,

/// Verbose mode.
#[clap(short, long, default_value = "false")]
verbose: bool,
/// Verbosity level
#[arg(short, long, value_name = "level", value_enum, default_value = "info")]
verbosity: ArgVerbosity,
}

#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, clap::ValueEnum)]
enum ArgVerbosity {
Off,
Error,
Warn,
Info,
Debug,
Trace,
}

pub(crate) static MAX_UDP_RELAY_PACKET_SIZE: usize = 1500;
Expand All @@ -42,12 +52,12 @@ async fn main() -> Result<()> {

dotenvy::dotenv().ok();

let level = if opt.verbose { "trace" } else { "info" };
let default = format!("off,{}={}", module_path!(), level);
let default = format!("{}={:?}", module_path!(), opt.verbosity);
env_logger::Builder::from_env(env_logger::Env::default().default_filter_or(default)).init();

match (opt.username, opt.password) {
(Some(username), Some(password)) => {
(Some(username), password) => {
let password = password.unwrap_or_default();
let auth = Arc::new(auth::UserKeyAuth::new(&username, &password));
main_loop(auth, opt.listen_addr).await?;
}
Expand Down
2 changes: 1 addition & 1 deletion examples/udp-client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ async fn main() -> Result<()> {
opt.validate()?;

let user_key = match (opt.username, opt.password) {
(Some(username), Some(password)) => Some(UserKey::new(username, password)),
(Some(username), password) => Some(UserKey::new(username, password.unwrap_or_default())),
_ => None,
};
let timeout = Duration::from_secs(opt.timeout);
Expand Down
2 changes: 0 additions & 2 deletions examples/dns.rs → examples/util/dns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,3 @@ pub fn parse_data_to_dns_message(data: &[u8], used_by_tcp: bool) -> Result<Messa
let message = Message::from_vec(data).map_err(|e| e.to_string())?;
Ok(message)
}

fn main() {}
1 change: 1 addition & 0 deletions examples/util/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod dns;

0 comments on commit af5b71b

Please sign in to comment.