destination map
This commit is contained in:
1 parent
3659482ac0
commit
df66b2ba84
4 files changed
+80
-46
No files matched your search
+2
-1
@@ -6,7 +6,8 @@ license = "MIT"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
clap = { version= "4.*", features= ["derive"] }
|
||||
cidr = { version = "0.3.*", features = ["serde"] }
|
||||
clap = { version = "4.*", features = ["derive"] }
|
||||
ed25519-dalek = { version = "2.*", features = ["pem", "pkcs8"] }
|
||||
env_logger = "0.11.*"
|
||||
etherparse = "0.19.*"
|
||||
|
||||
+3
-3
@@ -1,7 +1,7 @@
|
||||
vpn_ip = "10.0.0.1/24"
|
||||
network = "10.20.0.0/16"
|
||||
|
||||
[peers]
|
||||
"10.0.0.2" = "xxx"
|
||||
peers = [
|
||||
]
|
||||
|
||||
[kaonic_radio_config]
|
||||
module = 0
|
||||
|
||||
+49
-23
@@ -1,17 +1,19 @@
|
||||
use std::collections::BTreeMap;
|
||||
use std::net::IpAddr;
|
||||
use std::net::{IpAddr, Ipv4Addr};
|
||||
use std::sync::Arc;
|
||||
|
||||
use cidr;
|
||||
use etherparse;
|
||||
use ipnet::IpNet;
|
||||
use log;
|
||||
use riptun::TokioTun;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use tokio;
|
||||
use tokio::sync::Mutex;
|
||||
|
||||
use reticulum::destination::DestinationName;
|
||||
use reticulum::destination::SingleInputDestination;
|
||||
use reticulum::destination::link::{LinkEvent, LinkId};
|
||||
use reticulum::hash::AddressHash;
|
||||
use reticulum::identity::PrivateIdentity;
|
||||
use reticulum::transport::Transport;
|
||||
|
||||
// TODO: config?
|
||||
@@ -22,15 +24,16 @@ const fn default_announce_freq_secs() -> u32 { 1 }
|
||||
|
||||
#[derive(Deserialize, Serialize)]
|
||||
pub struct Config {
|
||||
pub vpn_ip: IpNet,
|
||||
/// Map of (IP, destination hash)
|
||||
pub peers: BTreeMap<IpAddr, String>,
|
||||
pub network: cidr::Ipv4Cidr,
|
||||
/// List of peer destination hashes
|
||||
pub peers: Vec<String>,
|
||||
#[serde(default = "default_announce_freq_secs")]
|
||||
pub announce_freq_secs: u32
|
||||
}
|
||||
|
||||
pub struct Client {
|
||||
config: Config,
|
||||
in_destination: Arc<Mutex<SingleInputDestination>>,
|
||||
tun: Tun
|
||||
}
|
||||
|
||||
@@ -56,28 +59,26 @@ struct Tun {
|
||||
}
|
||||
|
||||
impl Client {
|
||||
pub fn new(config: Config) -> Result<Self, CreateClientError> {
|
||||
if config.peers.contains_key(&config.vpn_ip.addr()) {
|
||||
log::error!("configured VPN IP ({}) conflicts with peer IPs: {:?}",
|
||||
config.vpn_ip, config.peers);
|
||||
pub async fn new(config: Config, in_destination: Arc<Mutex<SingleInputDestination>>)
|
||||
-> Result<Self, CreateClientError>
|
||||
{
|
||||
if config.peers.is_empty() {
|
||||
log::warn!("no peers configured");
|
||||
return Err(CreateClientError::ConfigError(
|
||||
"configured VPN IP exists in peer IPs".to_owned()))
|
||||
"no peers: add at least one peer destination to configuration".to_string()))
|
||||
}
|
||||
let tun = Tun::new(config.vpn_ip)?;
|
||||
Ok(Client { config, tun })
|
||||
let destination_hash = in_destination.lock().await.desc.address_hash;
|
||||
let vpn_ip = destination_to_ip(destination_hash, config.network);
|
||||
let tun = Tun::new(vpn_ip)?;
|
||||
Ok(Client { config, in_destination, tun })
|
||||
}
|
||||
|
||||
pub async fn run(&self, mut transport: Transport, id: PrivateIdentity) {
|
||||
// create in destination
|
||||
let in_destination = transport
|
||||
.add_destination(id, DestinationName::new("rns_vpn", "client")).await;
|
||||
let in_destination_hash = in_destination.lock().await.desc.address_hash;
|
||||
log::info!("created destination: {}",
|
||||
format!("{}", in_destination_hash).trim_matches('/'));
|
||||
pub async fn run(&self, transport: Transport) {
|
||||
let in_destination_hash = self.in_destination.lock().await.desc.address_hash;
|
||||
// set up peer map
|
||||
let peer_map = {
|
||||
let mut peer_map = BTreeMap::<IpAddr, Peer>::new();
|
||||
for (ip, dest) in self.config.peers.iter().map(|(k,v)| (k.clone(), v.clone())) {
|
||||
for dest in self.config.peers.iter() {
|
||||
let dest = match AddressHash::new_from_hex_string(dest.as_str()) {
|
||||
Ok(dest) => dest,
|
||||
Err(err) => {
|
||||
@@ -86,13 +87,14 @@ impl Client {
|
||||
}
|
||||
};
|
||||
let peer = Peer { dest, link_id: None, link_active: false };
|
||||
assert!(peer_map.insert(ip, peer).is_none());
|
||||
let ip = destination_to_ip(dest, self.config.network);
|
||||
assert!(peer_map.insert(ip.addr(), peer).is_none());
|
||||
}
|
||||
tokio::sync::Mutex::new(peer_map)
|
||||
};
|
||||
// send announces
|
||||
let announce_loop = async || loop {
|
||||
transport.send_announce(&in_destination, None).await;
|
||||
transport.send_announce(&self.in_destination, None).await;
|
||||
tokio::time::sleep(
|
||||
std::time::Duration::from_secs(self.config.announce_freq_secs as u64)
|
||||
).await;
|
||||
@@ -264,3 +266,27 @@ impl Tun {
|
||||
self.tun.send(datagram).await
|
||||
}
|
||||
}
|
||||
|
||||
fn destination_to_ip(destination: AddressHash, prefix: cidr::Ipv4Cidr) -> IpNet {
|
||||
let n = u32::from_be_bytes((&destination.as_slice()[12..16]).try_into().unwrap());
|
||||
let network_bits = prefix.mask().to_bits();
|
||||
let host_bits = (!network_bits) & n;
|
||||
let addr = Ipv4Addr::from_bits(prefix.first_address().to_bits() | host_bits);
|
||||
IpNet::new(IpAddr::V4(addr), network_bits.count_ones() as u8).unwrap()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use reticulum::hash::AddressHash;
|
||||
use super::*;
|
||||
#[test]
|
||||
fn dest_to_ip() {
|
||||
use std::str::FromStr;
|
||||
let destination =
|
||||
AddressHash::new_from_hex_string("fb08aff16ec6f5ccf0d3eb179028e9c3").unwrap();
|
||||
// 0xe9 = 233, 0xc3 = 195
|
||||
let prefix = cidr::Ipv4Cidr::from_str("10.1.0.0/16").unwrap();
|
||||
let ip = destination_to_ip(destination, prefix);
|
||||
assert_eq!(ip.addr(), std::net::IpAddr::from_str("10.1.233.195").unwrap());
|
||||
}
|
||||
}
|
||||
+26
-19
@@ -7,6 +7,7 @@ use ed25519_dalek;
|
||||
use env_logger;
|
||||
use log;
|
||||
use pem;
|
||||
use reticulum::destination::DestinationName;
|
||||
use reticulum::identity::PrivateIdentity;
|
||||
use reticulum::iface::kaonic::kaonic_grpc::KaonicGrpc;
|
||||
use reticulum::iface::kaonic::RadioConfig;
|
||||
@@ -65,23 +66,6 @@ async fn main() -> Result<(), process::ExitCode> {
|
||||
// init logging
|
||||
env_logger::Builder::new().filter_level(log::LevelFilter::Info).parse_default_env()
|
||||
.init();
|
||||
// client
|
||||
let client = match rns_vpn::Client::new(config.vpn_config) {
|
||||
Ok(client) => client,
|
||||
Err(err) => match err {
|
||||
rns_vpn::CreateClientError::RiptunError(riptun::Error::Unix {
|
||||
source: nix::errno::Errno::EPERM
|
||||
}) => {
|
||||
log::error!("EPERM error creating TUN interface: \
|
||||
need to run with root permissions");
|
||||
return Err(process::ExitCode::FAILURE)
|
||||
}
|
||||
_ => {
|
||||
log::error!("error creating VPN client: {:?}", err);
|
||||
return Err(process::ExitCode::FAILURE)
|
||||
}
|
||||
}
|
||||
};
|
||||
// start reticulum
|
||||
log::info!("starting reticulum");
|
||||
let id = if let Some(name) = cmd.id_string {
|
||||
@@ -124,7 +108,7 @@ async fn main() -> Result<(), process::ExitCode> {
|
||||
};
|
||||
PrivateIdentity::new(private_key, sign_key)
|
||||
};
|
||||
let transport = Transport::new(TransportConfig::new("server", &id, true));
|
||||
let mut transport = Transport::new(TransportConfig::new("server", &id, true));
|
||||
if let Some (port) = cmd.udp_listen_port {
|
||||
// udp
|
||||
let forward = cmd.udp_forward_address.unwrap();
|
||||
@@ -144,8 +128,31 @@ async fn main() -> Result<(), process::ExitCode> {
|
||||
let _ = transport.iface_manager().lock().await.spawn(
|
||||
KaonicGrpc::new(address, radio_config, None), KaonicGrpc::spawn);
|
||||
}
|
||||
// create in destination
|
||||
let in_destination = transport
|
||||
.add_destination(id, DestinationName::new("rns_vpn", "client")).await;
|
||||
let in_destination_hash = in_destination.lock().await.desc.address_hash;
|
||||
log::info!("created destination: {}",
|
||||
format!("{}", in_destination_hash).trim_matches('/'));
|
||||
// client
|
||||
let client = match rns_vpn::Client::new(config.vpn_config, in_destination).await {
|
||||
Ok(client) => client,
|
||||
Err(err) => match err {
|
||||
rns_vpn::CreateClientError::RiptunError(riptun::Error::Unix {
|
||||
source: nix::errno::Errno::EPERM
|
||||
}) => {
|
||||
log::error!("EPERM error creating TUN interface: \
|
||||
need to run with root permissions");
|
||||
return Err(process::ExitCode::FAILURE)
|
||||
}
|
||||
_ => {
|
||||
log::error!("error creating VPN client: {:?}", err);
|
||||
return Err(process::ExitCode::FAILURE)
|
||||
}
|
||||
}
|
||||
};
|
||||
// run
|
||||
client.run(transport, id).await;
|
||||
client.run(transport).await;
|
||||
log::info!("server exit");
|
||||
Ok(())
|
||||
}
|
||||
Reference in new issue
Block a user