Skip to content

Commit

Permalink
fix stun examples
Browse files Browse the repository at this point in the history
  • Loading branch information
yngrtc committed Mar 2, 2024
1 parent 98775b5 commit ed0faab
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 52 deletions.
2 changes: 1 addition & 1 deletion stun/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ md-5 = "0.10.1"
thiserror = "1.0"

[dev-dependencies]
clap = "3.2.6"
clap = "4.5"
criterion = "0.5"

[[bench]]
Expand Down
39 changes: 13 additions & 26 deletions stun/examples/stun_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,24 @@ use stun::client::*;
use stun::message::*;
use stun::xoraddr::*;

use clap::{App, Arg};
use clap::Parser;
use shared::error::Error;
use std::net::UdpSocket;

fn main() -> Result<(), Error> {
let mut app = App::new("STUN Client")
.version("0.1.0")
.author("Rain Liu <yliu@webrtc.rs>")
.about("An example of STUN Client")
.arg(
Arg::with_name("FULLHELP")
.help("Prints more detailed help information")
.long("fullhelp"),
)
.arg(
Arg::with_name("server")
.required_unless("FULLHELP")
.takes_value(true)
.default_value("stun.l.google.com:19302")
.long("server")
.help("STUN Server"),
);

let matches = app.clone().get_matches();
#[derive(Parser)]
#[command(name = "STUN Client")]
#[command(author = "Rusty Rain <y@ngr.tc>")]
#[command(version = "0.1.0")]
#[command(about = "An example of STUN Client", long_about = None)]
struct Cli {
#[arg(long, default_value_t = format!("stun.l.google.com:19302"))]
server: String,
}

if matches.is_present("FULLHELP") {
app.print_long_help().unwrap();
std::process::exit(0);
}
fn main() -> Result<(), Error> {
let cli = Cli::parse();

let server = matches.value_of("server").unwrap();
let server = cli.server;

let conn = UdpSocket::bind("0:0")?;
println!("Local address: {}", conn.local_addr()?);
Expand Down
39 changes: 14 additions & 25 deletions stun/examples/stun_decode.rs
Original file line number Diff line number Diff line change
@@ -1,33 +1,22 @@
use clap::{App, Arg};
use clap::Parser;

use stun::message::Message;

fn main() {
let mut app = App::new("STUN decode")
.version("0.1.0")
.author("Jtplouffe <jtplouffe@gmail.com>")
.about("An example of STUN decode")
.arg(
Arg::with_name("FULLHELP")
.help("Prints more detailed help information")
.long("fullhelp"),
)
.arg(
Arg::with_name("data")
.required_unless("FULLHELP")
.takes_value(true)
.index(1)
.help("base64 encoded message, e.g. 'AAEAHCESpEJML0JTQWsyVXkwcmGALwAWaHR0cDovL2xvY2FsaG9zdDozMDAwLwAA'"),
);

let matches = app.clone().get_matches();
#[derive(Parser)]
#[command(name = "STUN decode")]
#[command(author = "Rusty Rain <y@ngr.tc>")]
#[command(version = "0.1.0")]
#[command(about = "An example of STUN decode", long_about = None)]
struct Cli {
/// base64 encoded message, e.g. 'AAEAHCESpEJML0JTQWsyVXkwcmGALwAWaHR0cDovL2xvY2FsaG9zdDozMDAwLwAA'"
#[arg(long)]
data: String,
}

if matches.is_present("FULLHELP") {
app.print_long_help().unwrap();
std::process::exit(0);
}
fn main() {
let cli = Cli::parse();

let encoded_data = matches.value_of("data").unwrap();
let encoded_data = cli.data;
let decoded_data = match base64::decode(encoded_data) {
Ok(d) => d,
Err(e) => panic!("Unable to decode base64 value: {e}"),
Expand Down

0 comments on commit ed0faab

Please sign in to comment.