add missing allowed-ips

This commit is contained in:
Alex 2023-03-09 11:04:27 +01:00
parent 878414bfb7
commit 84381b5096

View file

@ -83,7 +83,7 @@ fn fasthash(data: &[u8]) -> u64 {
h.digest() h.digest()
} }
fn wg_dump(config: &Config) -> Result<(Pubkey, Vec<(Pubkey, Option<SocketAddr>, u64)>)> { fn wg_dump(config: &Config) -> Result<(Pubkey, u16, Vec<(Pubkey, Option<SocketAddr>, u64)>)> {
let output = Command::new("sudo") let output = Command::new("sudo")
.args(["wg", "show", &config.interface, "dump"]) .args(["wg", "show", &config.interface, "dump"])
.output()?; .output()?;
@ -91,6 +91,7 @@ fn wg_dump(config: &Config) -> Result<(Pubkey, Vec<(Pubkey, Option<SocketAddr>,
let ourself = lines.next().unwrap().split('\t').collect::<Vec<_>>(); let ourself = lines.next().unwrap().split('\t').collect::<Vec<_>>();
let our_pubkey = ourself[1].to_string(); let our_pubkey = ourself[1].to_string();
let listen_port = ourself[2].parse::<u16>()?;
let peers = lines let peers = lines
.filter_map(|line| { .filter_map(|line| {
@ -107,14 +108,15 @@ fn wg_dump(config: &Config) -> Result<(Pubkey, Vec<(Pubkey, Option<SocketAddr>,
}) })
.collect::<Vec<_>>(); .collect::<Vec<_>>();
Ok((our_pubkey, peers)) Ok((our_pubkey, listen_port, peers))
} }
// ============ DAEMON CODE ================= // ============ DAEMON CODE =================
struct Daemon { struct Daemon {
config: Config, config: Config,
ourself: Pubkey, our_pubkey: Pubkey,
listen_port: u16,
socket: UdpSocket, socket: UdpSocket,
state: Mutex<State>, state: Mutex<State>,
} }
@ -137,11 +139,12 @@ enum Gossip {
impl Daemon { impl Daemon {
fn new(config: Config) -> Result<Self> { fn new(config: Config) -> Result<Self> {
let (ourself, _peers) = wg_dump(&config)?; let (our_pubkey, listen_port, _peers) = wg_dump(&config)?;
let socket = UdpSocket::bind(SocketAddr::new("0.0.0.0".parse()?, config.gossip_port))?; let socket = UdpSocket::bind(SocketAddr::new("0.0.0.0".parse()?, config.gossip_port))?;
Ok(Daemon { Ok(Daemon {
config, config,
ourself, our_pubkey,
listen_port,
socket, socket,
state: Mutex::new(State { state: Mutex::new(State {
peers: HashMap::new(), peers: HashMap::new(),
@ -176,7 +179,7 @@ impl Daemon {
} }
fn wg_loop_iter(&self, i: usize) -> Result<()> { fn wg_loop_iter(&self, i: usize) -> Result<()> {
let (_, wg_peers) = wg_dump(&self.config)?; let (_, _, wg_peers) = wg_dump(&self.config)?;
let mut state = self.state.lock().unwrap(); let mut state = self.state.lock().unwrap();
// 1. Update local peers info of peers // 1. Update local peers info of peers
@ -191,7 +194,7 @@ impl Daemon {
Some(x) => x.address, Some(x) => x.address,
None => continue, None => continue,
}; };
let gossip_prio = fasthash(format!("{}-{}", self.ourself, pk).as_bytes()); let gossip_prio = fasthash(format!("{}-{}", self.our_pubkey, pk).as_bytes());
state.peers.insert( state.peers.insert(
pk, pk,
PeerInfo { PeerInfo {
@ -258,6 +261,8 @@ impl Daemon {
&endpoint.0.to_string(), &endpoint.0.to_string(),
"persistent-keepalive", "persistent-keepalive",
"20", "20",
"allowed-ips",
&format!("{}/32", peer.address),
]) ])
.output()?; .output()?;
} }