@@ -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.
0 commit comments