MicroExitPolicy

Struct MicroExitPolicy 

Source
pub struct MicroExitPolicy {
    pub is_accept: bool,
    pub ports: Vec<PortRange>,
}
Expand description

A compact exit policy used in microdescriptors.

Microdescriptor exit policies are a simplified form of exit policy that only specify ports, not addresses. They are used in Tor’s microdescriptor format to provide a compact representation of a relay’s exit policy.

§Format

Micro exit policies have the format:

accept|reject port[,port...]

Where each port can be a single port or a range (e.g., 80-443).

§Matching Semantics

  • accept policies allow traffic to the listed ports
  • reject policies block traffic to the listed ports (allowing all others)

Since micro policies don’t include address information, clients can only guess whether a relay will accept their traffic. If the guess is wrong, the relay will return an end-reason-exit-policy error.

§Example

use stem_rs::exit_policy::MicroExitPolicy;

// Accept only web ports
let policy = MicroExitPolicy::parse("accept 80,443").unwrap();
assert!(policy.can_exit_to(80));
assert!(policy.can_exit_to(443));
assert!(!policy.can_exit_to(22));

// Reject privileged ports
let policy = MicroExitPolicy::parse("reject 1-1024").unwrap();
assert!(!policy.can_exit_to(80));
assert!(policy.can_exit_to(8080));

§See Also

Fields§

§is_accept: bool

Whether this policy accepts (true) or rejects (false) the listed ports.

§ports: Vec<PortRange>

The port ranges this policy applies to.

Implementations§

Source§

impl MicroExitPolicy

Source

pub fn parse(content: &str) -> Result<Self, Error>

Parses a micro exit policy from a string.

The string must follow the microdescriptor policy format:

accept|reject port[,port...]
§Arguments
  • content - The policy string to parse
§Supported Formats
  • Single port: accept 80
  • Multiple ports: accept 80,443
  • Port range: reject 1-1024
  • Mixed: accept 80,443,8080-8090
  • Wildcard: accept * (all ports)
§Errors

Returns Error::Parse if:

  • The policy doesn’t start with accept or reject
  • A port number is invalid
  • A port range is invalid (min > max)
§Example
use stem_rs::exit_policy::MicroExitPolicy;

let policy = MicroExitPolicy::parse("accept 80,443").unwrap();
assert!(policy.is_accept);
assert_eq!(policy.ports.len(), 2);

let policy = MicroExitPolicy::parse("reject 1-1024").unwrap();
assert!(!policy.is_accept);

// Invalid policies
assert!(MicroExitPolicy::parse("allow 80").is_err());
assert!(MicroExitPolicy::parse("80,443").is_err());
Source

pub fn can_exit_to(&self, port: u16) -> bool

Checks if traffic can exit to a specific port.

For accept policies, returns true if the port is in the list. For reject policies, returns true if the port is NOT in the list.

§Arguments
  • port - The destination port to check
§Returns

true if traffic to this port is allowed, false otherwise.

§Example
use stem_rs::exit_policy::MicroExitPolicy;

// Accept policy: only listed ports are allowed
let policy = MicroExitPolicy::parse("accept 80,443").unwrap();
assert!(policy.can_exit_to(80));
assert!(policy.can_exit_to(443));
assert!(!policy.can_exit_to(22));

// Reject policy: listed ports are blocked, others allowed
let policy = MicroExitPolicy::parse("reject 1-1024").unwrap();
assert!(!policy.can_exit_to(80));
assert!(!policy.can_exit_to(443));
assert!(policy.can_exit_to(8080));

Trait Implementations§

Source§

impl Clone for MicroExitPolicy

Source§

fn clone(&self) -> MicroExitPolicy

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for MicroExitPolicy

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for MicroExitPolicy

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl FromStr for MicroExitPolicy

Source§

type Err = Error

The associated error which can be returned from parsing.
Source§

fn from_str(s: &str) -> Result<Self, Self::Err>

Parses a string s to return a value of this type. Read more
Source§

impl PartialEq for MicroExitPolicy

Source§

fn eq(&self, other: &MicroExitPolicy) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Eq for MicroExitPolicy

Source§

impl StructuralPartialEq for MicroExitPolicy

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.