Skip to content

Commit 13dabbb

Browse files
authored
Merge pull request #1 from fanatid/nat-local-ip-range
Add `local-ip-range` argument
2 parents eac6071 + f8c749a commit 13dabbb

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

client/src/main.rs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -567,7 +567,7 @@ fn fetch(
567567
let State { peers, cidrs } = api.http("GET", "/user/state")?;
568568

569569
let device = Device::get(interface, opts.network.backend)?;
570-
let modifications = device.diff(&peers);
570+
let mut modifications = device.diff(&peers);
571571

572572
let updates = modifications
573573
.iter()
@@ -621,6 +621,34 @@ fn fetch(
621621
if nat.no_nat_traversal {
622622
log::debug!("NAT traversal explicitly disabled, not attempting.");
623623
} else {
624+
let mut peers = Vec::with_capacity(modifications.len());
625+
if let Some(local_ip_range) = nat.local_ip_range {
626+
for diff in &modifications {
627+
peers.push(
628+
diff.new
629+
.cloned()
630+
.map(|mut peer| {
631+
let mut candidates = vec![];
632+
let mut rest = vec![];
633+
for endpoint in peer.candidates.drain(..) {
634+
let addr = endpoint.resolve().with_str(endpoint.to_string())?;
635+
if local_ip_range.contains(&addr.ip()) {
636+
candidates.push(endpoint);
637+
} else {
638+
rest.push(endpoint);
639+
}
640+
}
641+
candidates.append(&mut rest);
642+
peer.candidates = candidates;
643+
Ok::<_, anyhow::Error>(peer)
644+
})
645+
.transpose()?,
646+
);
647+
}
648+
for (i, mut diff) in modifications.iter_mut().enumerate() {
649+
diff.new = peers.get(i).expect("same length").as_ref();
650+
}
651+
}
624652
let mut nat_traverse = NatTraverse::new(interface, opts.network.backend, &modifications)?;
625653

626654
// Give time for handshakes with recently changed endpoints to complete before attempting traversal.

shared/src/types.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,11 @@ pub struct NatOpts {
437437
/// Don't report any candidates to coordinating server.
438438
/// Shorthand for --exclude-nat-candidates '0.0.0.0/0'.
439439
pub no_nat_candidates: bool,
440+
441+
#[clap(long)]
442+
/// Priorities candidads from given IP range.
443+
/// ex. --local-ip-range '192.168.10.0/24'
444+
pub local_ip_range: Option<IpNet>,
440445
}
441446

442447
impl NatOpts {
@@ -445,6 +450,7 @@ impl NatOpts {
445450
no_nat_traversal: true,
446451
exclude_nat_candidates: vec![],
447452
no_nat_candidates: true,
453+
local_ip_range: None,
448454
}
449455
}
450456

0 commit comments

Comments
 (0)