1- use std:: { net:: { IpAddr , SocketAddr } , time:: Duration } ;
1+ use std:: {
2+ net:: { IpAddr , SocketAddr } ,
3+ time:: Duration ,
4+ } ;
25
3- use tokio:: net:: UdpSocket ;
4- use tokio_graceful_shutdown:: SubsystemHandle ;
56use futures:: future:: join_all;
67use tokio:: io:: Error ;
8+ use tokio:: net:: UdpSocket ;
9+ use tokio:: time:: timeout;
10+ use tokio_graceful_shutdown:: SubsystemHandle ;
711use tracing:: instrument;
812use tracing_unwrap:: ResultExt ;
913use wirespider:: WireguardKey ;
10- use tokio:: time:: timeout;
1114
12- const MESSAGE : & str = "wirespider" ;
15+ const MESSAGE : & str = "wirespider" ;
1316
14- pub async fn local_ip_detection_service ( subsys : SubsystemHandle , key : WireguardKey ) -> Result < ( ) , Error > {
17+ pub async fn local_ip_detection_service (
18+ subsys : SubsystemHandle ,
19+ key : WireguardKey ,
20+ ) -> Result < ( ) , Error > {
1521 let socket = UdpSocket :: bind ( "0.0.0.0:27212" ) . await . unwrap_or_log ( ) ;
1622 let mut buf = [ 0u8 ; MESSAGE . len ( ) ] ;
17- loop {
23+ loop {
1824 tokio:: select! {
1925 recv = socket. recv_from( & mut buf) => {
2026 let ( length, from) = recv. unwrap_or_log( ) ;
@@ -31,34 +37,29 @@ pub async fn local_ip_detection_service(subsys: SubsystemHandle, key: WireguardK
3137}
3238
3339#[ instrument]
34- pub async fn check_local_ips ( ips : & [ IpAddr ] , key : WireguardKey ) -> Result < Option < IpAddr > , Error > {
35- let results = timeout ( Duration :: from_millis ( 100 ) , join_all ( ips. iter ( ) . map ( |x| check_ip ( * x, key) ) ) ) . await ;
36- match results {
37- Ok ( results) => {
38- for result in results {
39- match result? {
40- Some ( ip) => return Ok ( Some ( ip) ) ,
41- None => continue ,
42- } ;
43- }
44- } ,
45- Err ( _) => return Ok ( None )
40+ pub async fn check_local_ips ( ips : & [ IpAddr ] , key : WireguardKey ) -> Result < Option < IpAddr > , Error > {
41+ let results = join_all ( ips. iter ( ) . map ( |x| check_ip ( * x, key) ) ) . await ;
42+ for result in results {
43+ match result? {
44+ Some ( ip) => return Ok ( Some ( ip) ) ,
45+ None => continue ,
46+ } ;
4647 }
4748 Ok ( None )
4849}
4950
5051#[ instrument]
51- async fn check_ip ( ip : IpAddr , key : WireguardKey ) -> Result < Option < IpAddr > , Error > {
52+ async fn check_ip ( ip : IpAddr , key : WireguardKey ) -> Result < Option < IpAddr > , Error > {
5253 if ip. is_ipv6 ( ) {
5354 return Ok ( None ) ; // not supported for now
5455 }
5556 let socket = UdpSocket :: bind ( "0.0.0.0:0" ) . await ?;
5657 socket. connect ( SocketAddr :: from ( ( ip, 27212 ) ) ) . await ?;
5758 socket. send ( MESSAGE . as_bytes ( ) ) . await ?;
58- let mut buffer : WireguardKey = [ 0 ; 32 ] ;
59+ let mut buffer: WireguardKey = [ 0 ; 32 ] ;
5960
60- match socket. recv ( & mut buffer) . await {
61- Ok ( size) if size == 32 && buffer == key => Ok ( Some ( ip) ) ,
62- _ => Ok ( None )
61+ match timeout ( Duration :: from_millis ( 100 ) , socket. recv ( & mut buffer) ) . await {
62+ Ok ( Ok ( size) ) if size == 32 && buffer == key => Ok ( Some ( ip) ) ,
63+ _ => Ok ( None ) ,
6364 }
64- }
65+ }
0 commit comments