pub struct PeerConfigBuilder { /* private fields */ }
Expand description
Builds and represents a single peer in a WireGuard interface configuration.
Note that if a peer with that public key already exists on the interface, the settings specified here will be applied on top of the existing settings, similarly to interface-wide settings.
If this is not what you want, use DeviceConfigBuilder::replace_peers
to replace all peer settings on the interface, or use
DeviceConfigBuilder::remove_peer_by_key
first
to remove the peer from the interface, and then apply a second configuration to re-add it.
§Example
let peer_keypair = KeyPair::generate();
// create a new peer and allow it to connect from 192.168.1.2
let peer = PeerConfigBuilder::new(&peer_keypair.public)
.replace_allowed_ips()
.add_allowed_ip("192.168.1.2".parse()?, 32);
// update our existing configuration with the new peer
DeviceUpdate::new().add_peer(peer).apply(&"wg-example".parse().unwrap(), Backend::Userspace);
println!("Send these keys to your peer: {:#?}", peer_keypair);
Implementations§
Source§impl PeerConfigBuilder
impl PeerConfigBuilder
Sourcepub fn new(public_key: &Key) -> Self
pub fn new(public_key: &Key) -> Self
Creates a new PeerConfigBuilder
that does nothing when applied.
pub fn into_peer_config(self) -> PeerConfig
Sourcepub fn public_key(&self) -> &Key
pub fn public_key(&self) -> &Key
The public key used in this builder.
Sourcepub fn from_peer_config(config: PeerConfig) -> Self
pub fn from_peer_config(config: PeerConfig) -> Self
Creates a PeerConfigBuilder
from a PeerConfig
.
This is mostly a convenience method for cases when you want to copy some or most of the existing peer configuration to a new configuration.
This returns a PeerConfigBuilder
, so you can still call any methods
you need to override the imported settings.
Specifies a preshared key to be set for this peer.
Specifies that this peer’s preshared key should be unset.
Sourcepub fn set_endpoint(self, address: SocketAddr) -> Self
pub fn set_endpoint(self, address: SocketAddr) -> Self
Specifies an exact endpoint that this peer should be allowed to connect from.
Sourcepub fn set_persistent_keepalive_interval(self, interval: u16) -> Self
pub fn set_persistent_keepalive_interval(self, interval: u16) -> Self
Specifies the interval between keepalive packets to be sent to this peer.
Sourcepub fn unset_persistent_keepalive(self) -> Self
pub fn unset_persistent_keepalive(self) -> Self
Specifies that this peer does not require keepalive packets.
Sourcepub fn add_allowed_ip(self, address: IpAddr, cidr: u8) -> Self
pub fn add_allowed_ip(self, address: IpAddr, cidr: u8) -> Self
Specifies an IP address this peer will be allowed to connect from/to.
See AllowedIp
for details. This method can be called
more than once, and all IP addresses will be added to the configuration.
Sourcepub fn add_allowed_ips(self, ips: &[AllowedIp]) -> Self
pub fn add_allowed_ips(self, ips: &[AllowedIp]) -> Self
Specifies multiple IP addresses this peer will be allowed to connect from/to.
See AllowedIp
for details. This method can be called
more than once, and all IP addresses will be added to the configuration.
Sourcepub fn allow_all_ips(self) -> Self
pub fn allow_all_ips(self) -> Self
Specifies this peer should be allowed to connect to all IP addresses.
This is a convenience method for cases when you want to connect to a server that all traffic should be routed through.
Sourcepub fn replace_allowed_ips(self) -> Self
pub fn replace_allowed_ips(self) -> Self
Specifies that the allowed IP addresses in this configuration should replace the existing configuration of the interface, not be appended to it.
Trait Implementations§
Source§impl Clone for PeerConfigBuilder
impl Clone for PeerConfigBuilder
Source§fn clone(&self) -> PeerConfigBuilder
fn clone(&self) -> PeerConfigBuilder
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more