Skip to content

Commit

Permalink
fix: create interface in server example (#16)
Browse files Browse the repository at this point in the history
* update examples

* bump version

* update runner selectors

---------

Co-authored-by: Maciej Wójcik <[email protected]>
  • Loading branch information
wojcik91 and Maciej Wójcik authored Oct 3, 2023
1 parent df5fcc2 commit d38e147
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ env:

jobs:
test:
runs-on: self-hosted
runs-on: [self-hosted, Linux]
container: rust:1.72

steps:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ on:

jobs:
create-release:
runs-on: self-hosted
runs-on: [self-hosted, Linux]
steps:
- name: Checkout
uses: actions/checkout@v3
Expand Down
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "defguard_wireguard_rs"
version = "0.2.0"
version = "0.2.1"
edition = "2021"
description = "A unified multi-platform high-level API for managing WireGuard interfaces"
license = "Apache-2.0"
Expand Down
7 changes: 5 additions & 2 deletions examples/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
};
let wgapi = WGApi::new(ifname.clone(), false)?;

// create interface
wgapi.create_interface()?;

// Peer configuration
let secret = EphemeralSecret::random();
let key = PublicKey::from(&secret);
Expand All @@ -22,8 +25,8 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut peer = Peer::new(peer_key.clone());

log::info!("endpoint");
// Your wireguard server endpoint which peer connects too
let endpoint: SocketAddr = "<server_ip>:<server_port>".parse().unwrap();
// Your wireguard server endpoint which client connects too
let endpoint: SocketAddr = "10.10.10.10:55001".parse().unwrap();
// Peer endpoint and interval
peer.endpoint = Some(endpoint);
peer.persistent_keepalive_interval = Some(25);
Expand Down
19 changes: 15 additions & 4 deletions examples/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,30 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
"utun3".into()
};
let wgapi = WGApi::new(ifname.clone(), false)?;

// create interface
wgapi.create_interface()?;

// read current interface data
let host = wgapi.read_interface_data()?;
log::debug!("{host:#?}");
println!("WireGuard interface: {host:#?}");

// host
// prepare peer configuration
let secret = EphemeralSecret::random();
let key = PublicKey::from(&secret);
let peer_key: Key = key.as_ref().try_into().unwrap();
let mut peer = Peer::new(peer_key.clone());
let addr = IpAddrMask::from_str("10.20.30.40/24").unwrap();
peer.allowed_ips.push(addr);

// Create host interfaces
// Configure host interface
let interface_config = InterfaceConfiguration {
name: ifname.clone(),
prvkey: "AAECAwQFBgcICQoLDA0OD/Dh0sO0pZaHeGlaSzwtHg8=".to_string(),
address: "10.6.0.30".to_string(),
port: 12345,
peers: vec![peer],
};

wgapi.configure_interface(&interface_config)?;

// Create peers
Expand All @@ -44,5 +48,12 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
wgapi.remove_peer(&peer.public_key)?;
}

// read current interface data
let host = wgapi.read_interface_data()?;
println!("WireGuard interface: {host:#?}");

// remove interface
wgapi.remove_interface()?;

Ok(())
}
3 changes: 3 additions & 0 deletions examples/userspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
};
let api = WireguardApiUserspace::new(ifname.clone())?;

// create interface
api.create_interface()?;

// Peer configuration
let secret = EphemeralSecret::random();
let key = PublicKey::from(&secret);
Expand Down

0 comments on commit d38e147

Please sign in to comment.