Skip to content

Commit

Permalink
Allow specifying proxy, residential or mobile connections
Browse files Browse the repository at this point in the history
  • Loading branch information
Firaenix committed Nov 30, 2024
1 parent 97be6f3 commit ab60ba2
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,14 @@ async fn main() -> eyre::Result<()> {
hostnames: vec![endpoint.to_string()],
isp_regex: None,
city: None,
mobile: PerformIcmpBodyMobile::from_str("ALLOWED")?,
mobile: PerformIcmpBodyMobile::from_str(
&APP_CONFIG.mobile.to_string(),
)?,
node_id: None,
proxy: PerformIcmpBodyProxy::from_str("ALLOWED")?,
residential: PerformIcmpBodyResidential::from_str("ALLOWED")?,
proxy: PerformIcmpBodyProxy::from_str(&APP_CONFIG.proxy.to_string())?,
residential: PerformIcmpBodyResidential::from_str(
&APP_CONFIG.residential.to_string(),
)?,
})
.await
.context("Failed to send job");
Expand Down
53 changes: 53 additions & 0 deletions src/options/opts.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,36 @@
use std::fmt::Display;

use bpaf::{long, OptionParser, Parser};
use color_eyre::eyre;
use keshvar::Continent;

#[derive(Debug, Clone)]
pub enum NetworkPolicy {
Allowed,
Denied,
Required,
}

impl From<Option<bool>> for NetworkPolicy {
fn from(_value: Option<bool>) -> Self {
match _value {
None => NetworkPolicy::Allowed,
Some(true) => NetworkPolicy::Required,
Some(false) => NetworkPolicy::Denied,
}
}
}

impl Display for NetworkPolicy {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
NetworkPolicy::Allowed => write!(f, "ALLOWED"),
NetworkPolicy::Denied => write!(f, "DENIED"),
NetworkPolicy::Required => write!(f, "REQUIRED"),
}
}
}

/// This doc string acts as a help message when the user runs '--help'
/// as do all doc strings on fields
#[derive(Debug, Clone)]
Expand All @@ -12,6 +41,9 @@ pub struct Opts {
pub attempts: usize,
pub api_key: String,
pub concurrency: usize,
pub residential: NetworkPolicy,
pub mobile: NetworkPolicy,
pub proxy: NetworkPolicy,
}

impl Opts {
Expand Down Expand Up @@ -53,12 +85,33 @@ impl Opts {
.argument::<usize>("concurrency")
.fallback(100);

let residential = bpaf::long("residential")
.help("Control residential network usage. --residential=true to require, --residential=false to deny, omit to allow.")
.argument::<bool>("residential")
.optional()
.map(NetworkPolicy::from);

let mobile = bpaf::long("mobile")
.help("Control mobile network usage. --mobile=true to require, --mobile=false to deny, omit to allow.")
.argument::<bool>("mobile")
.optional()
.map(NetworkPolicy::from);

let proxy = bpaf::long("proxy")
.help("Control proxy network usage. --proxy=true to require, --proxy=false to deny, omit to allow.")
.argument::<bool>("proxy")
.optional()
.map(NetworkPolicy::from);

bpaf::construct!(Opts {
regions,
count,
attempts,
concurrency,
api_key,
residential,
mobile,
proxy,
endpoint,
})
.to_options()
Expand Down

0 comments on commit ab60ba2

Please sign in to comment.