Git Product home page Git Product logo

Comments (1)

Ange-Cesari avatar Ange-Cesari commented on May 23, 2024

Hey,

I figured out the problem.

The answer is related to the way wireguard works with the ips and my personal use case.

I actually mistaken the allowed Ips of the peers and the allowed ips you attribute to the inside the host.

Which means that my configuration was incorrect and I gave /24 Ips to peers which, obviously isn't working because the IP is associated to a public key. And once the Range IP is taken by an ip address, you can't override it with another peer, or even share the ip address.

Which means that in the issue you saw right above, I tried to attribute the range to every of my peers inside the host.

I slightly modified the for loop to match what I was describing above :

use std::{str::FromStr, fmt::format, thread::current};

use defguard_wireguard_rs::{
    host::Peer, key::Key, net::IpAddrMask, InterfaceConfiguration, WGApi, WireguardInterfaceApi,
};
use x25519_dalek::{EphemeralSecret, PublicKey};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Create new api object for interface
    let ifname: String = if cfg!(target_os = "linux") || cfg!(target_os = "freebsd") {
        "wg0".into()
    } else {
        "utun3".into()
    };
    let wgapi = WGApi::new(ifname.clone(), false)?;

    // create interface
    wgapi.create_interface()?;

    // read current interface data
    let host = wgapi.read_interface_data()?;
    println!("WireGuard interface: {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.20.40/24").unwrap();
    peer.allowed_ips.push(addr); 

    println!("WireGuard interface: {host:#?}");

    // 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
    for last_ip_digit in 0..3 {
        

        let secret = EphemeralSecret::random();
        let key = PublicKey::from(&secret);
        let mut peer = Peer::new(key.as_ref().try_into().unwrap());

        let current_ip = format!("10.20.30.{}/32", last_ip_digit);

        let addr = IpAddrMask::from_str(&current_ip).unwrap();
        peer.allowed_ips.push(addr);
        
        wgapi.configure_peer(&peer)?;

        println!("{:?}", peer)
        /* 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(())
}

Here is the output :

interface: wg0
  public key: dk5wF6ddw4IolWSxtwhIrghD753KdQRmg0m+DwkFgDo=
  private key: (hidden)
  listening port: 12345

peer: QXvd74w0SHZ9ZLphF1ZSje2nJwH9V3ufBxjwvJQH5m0=
  allowed ips: 10.20.20.0/24

peer: JGbAfVy+j7FwdDnZo8D9TydQzhacgCKb5uWQdCslxwo=
  allowed ips: 10.20.30.0/32

peer: ytt7LNExuwNMTtR98pFgT8TMdvrDMcRfFXgfh9O8iEY=
  allowed ips: 10.20.30.1/32

peer: K5DYNIftamKRfUI8UEDAQ0eOiJRRygv6/rm9MA3bCxE=
  allowed ips: 10.20.30.2/32

As you can see, all the peers have now an ip address in the allowed ips. As you can see, the allowed ips for the peer is with ip 10.20.20.0/24 (the first peer that is initialized) and all of the others have an ip in /32 because they are not on the same subnet (10.20.30 instead of 10.20.20)

Now let's change the first peer and make it 10.20.30, and let's conserve the /24 subnet and see the result :

(change the prepare peer configuration) :

    // 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); 

Here is the output :

interface: wg0
  public key: dk5wF6ddw4IolWSxtwhIrghD753KdQRmg0m+DwkFgDo=
  private key: (hidden)
  listening port: 12345

peer: qRsB13K9QtZdQv8wor/2eoqJTDShdo1nAv4BjWN//BQ=
  allowed ips: 10.20.30.0/24

peer: EvVpGPgk5YuSLCOKRUEZkiLfl4RYKuBwU7Y3P5UNjhw=
  allowed ips: 10.20.30.0/32

peer: uACPkJNiguIaBdw0gSS+RnkzH5Kb+kPkrz4FM3JHR30=
  allowed ips: 10.20.30.1/32

peer: kuFJ3rrkVF8h4woVhz08eLllTHPpobBaZgYf10OnoEs=
  allowed ips: 10.20.30.2/32
  

And if we change in the for loop the network the /32 with the /24 :

    for last_ip_digit in 0..3 {
      

      let secret = EphemeralSecret::random();
      let key = PublicKey::from(&secret);
      let mut peer = Peer::new(key.as_ref().try_into().unwrap());

      let current_ip = format!("10.20.30.{}/24", last_ip_digit);

      let addr = IpAddrMask::from_str(&current_ip).unwrap();
      peer.allowed_ips.push(addr);
      
      wgapi.configure_peer(&peer)?;

      println!("{:?}", peer)
      /* wgapi.remove_peer(&peer.public_key)?; */
      
  }

Here is the output :

interface: wg0
  public key: dk5wF6ddw4IolWSxtwhIrghD753KdQRmg0m+DwkFgDo=
  private key: (hidden)
  listening port: 12345

peer: v74bxkgpjQxcPSWFhsHJBXuM9BICPnyshVmMYXzUXHQ=
  allowed ips: (none)

peer: WXA2rwOfWXiKODI8+JFf76bfY05kJC+ac4NQ8aY000k=
  allowed ips: (none)

peer: DsqVI4DGryN8gI1AUZVn5YKwO6Zp//9Vy2dC22a0tCI=
  allowed ips: (none)

peer: kPNNDjRNLVOme91Xy1l1B/hbjHz1aFPoNa1ueWl7ljU=
  allowed ips: 10.20.30.0/24

As you can see the allowed ips are written as (none).

If this problem ever happens to you, it seems that it is more likely to be related to your wireguard configuration (and the rules that apply).

It's tricky to understand for a newbie in Wireguard, especially when it's not a configuration file, but I hope with those examples it is more understandable,

Best,
Ange

PS : the for loop might be interesting to add in the server.rs file in the example folder, in order to have an out of the box working file.

from wireguard-rs.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.