Skip to content

Commit

Permalink
Stop using error_chain! in examples
Browse files Browse the repository at this point in the history
  • Loading branch information
faern committed May 29, 2024
1 parent 8548959 commit f5f8fe4
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 81 deletions.
16 changes: 4 additions & 12 deletions examples/add_anchor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,15 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#[macro_use]
extern crate error_chain;

use pfctl::PfCtl;
use std::env;

error_chain! {}
quick_main!(run);

fn run() -> Result<()> {
let mut pf = PfCtl::new().chain_err(|| "Unable to connect to PF")?;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut pf = PfCtl::new()?;

for anchor_name in env::args().skip(1) {
pf.try_add_anchor(&anchor_name, pfctl::AnchorKind::Filter)
.chain_err(|| "Unable to add filter anchor")?;
pf.try_add_anchor(&anchor_name, pfctl::AnchorKind::Redirect)
.chain_err(|| "Unable to add redirect anchor")?;
pf.try_add_anchor(&anchor_name, pfctl::AnchorKind::Filter)?;
pf.try_add_anchor(&anchor_name, pfctl::AnchorKind::Redirect)?;

println!("Added {} as both a redirect and filter anchor", anchor_name);
}
Expand Down
43 changes: 13 additions & 30 deletions examples/add_rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,15 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#[macro_use]
extern crate error_chain;

use pfctl::{ipnetwork, FilterRuleBuilder, PfCtl, RedirectRuleBuilder};
use std::net::Ipv4Addr;

error_chain! {}
quick_main!(run);

static ANCHOR_NAME: &str = "test.anchor";

fn run() -> Result<()> {
let mut pf = PfCtl::new().chain_err(|| "Unable to connect to PF")?;
pf.try_add_anchor(ANCHOR_NAME, pfctl::AnchorKind::Filter)
.chain_err(|| "Unable to add test filter anchor")?;
pf.try_add_anchor(ANCHOR_NAME, pfctl::AnchorKind::Redirect)
.chain_err(|| "Unable to add test redirect anchor")?;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut pf = PfCtl::new()?;
pf.try_add_anchor(ANCHOR_NAME, pfctl::AnchorKind::Filter)?;
pf.try_add_anchor(ANCHOR_NAME, pfctl::AnchorKind::Redirect)?;

// Create the firewall rule instances
let pass_all_rule = FilterRuleBuilder::default()
Expand Down Expand Up @@ -94,24 +86,15 @@ fn run() -> Result<()> {
.unwrap();

// Add the rules to the test anchor
pf.add_rule(ANCHOR_NAME, &pass_all_rule)
.chain_err(|| "Unable to add rule")?;
pf.add_rule(ANCHOR_NAME, &pass_all_ipv4_quick_rule)
.chain_err(|| "Unable to add rule")?;
pf.add_rule(ANCHOR_NAME, &pass_all_ipv6_on_utun0_rule)
.chain_err(|| "Unable to add rule")?;
pf.add_rule(ANCHOR_NAME, &block_a_private_net_rule)
.chain_err(|| "Unable to add rule")?;
pf.add_rule(ANCHOR_NAME, &pass_all_icmp_echo_req)
.chain_err(|| "Unable to add rule")?;
pf.add_rule(ANCHOR_NAME, &pass_all_icmp_port_unreach)
.chain_err(|| "Unable to add rule")?;
pf.add_rule(ANCHOR_NAME, &pass_all_icmp_timex_transit)
.chain_err(|| "Unable to add rule")?;
pf.add_rule(ANCHOR_NAME, &pass_all_icmp_timex_reassembly)
.chain_err(|| "Unable to add rule")?;
pf.add_redirect_rule(ANCHOR_NAME, &redirect_incoming_tcp_from_port_3000_to_4000)
.chain_err(|| "Unable to add redirect rule")?;
pf.add_rule(ANCHOR_NAME, &pass_all_rule)?;
pf.add_rule(ANCHOR_NAME, &pass_all_ipv4_quick_rule)?;
pf.add_rule(ANCHOR_NAME, &pass_all_ipv6_on_utun0_rule)?;
pf.add_rule(ANCHOR_NAME, &block_a_private_net_rule)?;
pf.add_rule(ANCHOR_NAME, &pass_all_icmp_echo_req)?;
pf.add_rule(ANCHOR_NAME, &pass_all_icmp_port_unreach)?;
pf.add_rule(ANCHOR_NAME, &pass_all_icmp_timex_transit)?;
pf.add_rule(ANCHOR_NAME, &pass_all_icmp_timex_reassembly)?;
pf.add_redirect_rule(ANCHOR_NAME, &redirect_incoming_tcp_from_port_3000_to_4000)?;

println!("Added a bunch of rules to the {} anchor.", ANCHOR_NAME);
println!("Run this command to remove them:");
Expand Down
12 changes: 3 additions & 9 deletions examples/enable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,17 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#[macro_use]
extern crate error_chain;

use pfctl::PfCtl;

error_chain! {}
quick_main!(run);

fn run() -> Result<()> {
fn main() -> Result<(), Box<dyn std::error::Error>> {
// Create a handle to the firewall. This opens the file /dev/pf and requires root.
let mut pf = PfCtl::new().chain_err(|| "Unable to connect to PF")?;
let mut pf = PfCtl::new()?;

// Try to enable the firewall. Equivalent to the CLI command "pfctl -e".
match pf.enable() {
Ok(_) => println!("Enabled PF"),
Err(pfctl::Error(pfctl::ErrorKind::StateAlreadyActive, _)) => (),
err => err.chain_err(|| "Unable to enable PF")?,
err => err?,
}
Ok(())
}
22 changes: 6 additions & 16 deletions examples/flush_rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,17 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#[macro_use]
extern crate error_chain;

use pfctl::PfCtl;
use std::env;

error_chain! {}
quick_main!(run);

fn run() -> Result<()> {
let mut pf = PfCtl::new().chain_err(|| "Unable to connect to PF")?;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut pf = PfCtl::new()?;

for anchor_name in env::args().skip(1) {
match pf.flush_rules(&anchor_name, pfctl::RulesetKind::Filter) {
Ok(_) => println!("Flushed filter rules under anchor {}", anchor_name),
err => err.chain_err(|| "Unable to flush filter rules")?,
}
match pf.flush_rules(&anchor_name, pfctl::RulesetKind::Redirect) {
Ok(_) => println!("Flushed redirect rules under anchor {}", anchor_name),
err => err.chain_err(|| "Unable to flush redirect rules")?,
}
pf.flush_rules(&anchor_name, pfctl::RulesetKind::Filter)?;
println!("Flushed filter rules under anchor {}", anchor_name);
pf.flush_rules(&anchor_name, pfctl::RulesetKind::Redirect)?;
println!("Flushed redirect rules under anchor {}", anchor_name);
}
Ok(())
}
19 changes: 5 additions & 14 deletions examples/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,15 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#[macro_use]
extern crate error_chain;

use pfctl::PfCtl;
use std::net::Ipv4Addr;

error_chain! {}
quick_main!(run);

static ANCHOR_NAME: &str = "test.anchor";

fn run() -> Result<()> {
let mut pf = PfCtl::new().chain_err(|| "Unable to connect to PF")?;
pf.try_add_anchor(ANCHOR_NAME, pfctl::AnchorKind::Filter)
.chain_err(|| "Unable to add test filter anchor")?;
pf.try_add_anchor(ANCHOR_NAME, pfctl::AnchorKind::Redirect)
.chain_err(|| "Unable to add test redirect anchor")?;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut pf = PfCtl::new()?;
pf.try_add_anchor(ANCHOR_NAME, pfctl::AnchorKind::Filter)?;
pf.try_add_anchor(ANCHOR_NAME, pfctl::AnchorKind::Redirect)?;

// Create some firewall rules that we want to set in one atomic transaction.
let trans_rule1 = pfctl::FilterRuleBuilder::default()
Expand Down Expand Up @@ -50,8 +42,7 @@ fn run() -> Result<()> {

// Execute the transaction. This will OVERWRITE any existing rules under this anchor as it's
// a set operation, not an add operation.
pf.set_rules(ANCHOR_NAME, trans_change)
.chain_err(|| "Unable to set rules")?;
pf.set_rules(ANCHOR_NAME, trans_change)?;

println!("Added a bunch of rules to the {} anchor.", ANCHOR_NAME);
println!("Run this command to remove them:");
Expand Down

0 comments on commit f5f8fe4

Please sign in to comment.