Skip to content

Commit 4ac6d54

Browse files
authored
Merge pull request #239 from Dstack-TEE/fix-rev-net
gw: Fix reserved ip allocation
2 parents 14db101 + 0ddba89 commit 4ac6d54

1 file changed

Lines changed: 25 additions & 10 deletions

File tree

gateway/src/main_service.rs

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -269,19 +269,29 @@ fn start_sync_task(proxy: Proxy) {
269269
}
270270

271271
impl ProxyState {
272+
fn valid_ip(&self, ip: Ipv4Addr) -> bool {
273+
if self.config.wg.ip.broadcast() == ip {
274+
return false;
275+
}
276+
if self.config.wg.ip.addr() == ip {
277+
return false;
278+
}
279+
if self
280+
.config
281+
.wg
282+
.reserved_net
283+
.iter()
284+
.any(|net| net.contains(&ip))
285+
{
286+
return false;
287+
}
288+
true
289+
}
272290
fn alloc_ip(&mut self) -> Option<Ipv4Addr> {
273291
for ip in self.config.wg.client_ip_range.hosts() {
274-
if self.config.wg.ip.broadcast() == ip {
292+
if !self.valid_ip(ip) {
275293
continue;
276294
}
277-
if self.config.wg.ip.addr() == ip {
278-
continue;
279-
}
280-
for net in &self.config.wg.reserved_net {
281-
if net.contains(&ip) {
282-
continue;
283-
}
284-
}
285295
if self.state.allocated_addresses.contains(&ip) {
286296
continue;
287297
}
@@ -305,7 +315,12 @@ impl ProxyState {
305315
info!("public key changed for instance {id}, new key: {public_key}");
306316
existing.public_key = public_key.to_string();
307317
}
308-
return Some(existing.clone());
318+
let existing = existing.clone();
319+
if self.valid_ip(existing.ip) {
320+
return Some(existing);
321+
}
322+
info!("ip {} is invalid, removing", existing.ip);
323+
self.state.allocated_addresses.remove(&existing.ip);
309324
}
310325
let ip = self.alloc_ip()?;
311326
let host_info = InstanceInfo {

0 commit comments

Comments
 (0)