1+ use std:: collections:: HashMap ;
12use std:: net:: { IpAddr , SocketAddr } ;
23use std:: str:: FromStr ;
34use std:: time:: Duration ;
@@ -12,31 +13,32 @@ use crate::device::script_run::Scripts;
1213//WireGuard Manager
1314// rewrite boring/Device, mainly change thread pool to tokio.
1415pub struct WRManager {
15- device : Option < Device > ,
16+ devices : HashMap < String , Device > ,
1617}
1718
1819impl WRManager {
1920 pub fn new ( ) -> Self {
2021 WRManager {
21- device : None ,
22+ devices : HashMap :: new ( ) ,
2223 }
2324 }
2425
25- pub async fn remove_peer ( & mut self , public_key : & x25519_dalek:: PublicKey ) {
26- if let Some ( device) = & mut self . device {
26+ pub async fn remove_peer ( & mut self , network_token_id : & str , public_key : & x25519_dalek:: PublicKey ) {
27+ if let Some ( device) = self . devices . get_mut ( network_token_id ) {
2728 device. remove_peer ( public_key) . await ;
2829 } else {
29- tracing:: warn!( "there's no active device when remove peer" )
30+ tracing:: warn!( "there's no active device in {network_token_id} when remove peer" )
3031 }
3132 }
3233
3334 pub async fn add_peer ( & mut self ,
35+ network_token_id : & str ,
3436 pub_key : x25519_dalek:: PublicKey ,
3537 endpoint : Option < SocketAddr > ,
3638 allowed_ips : & [ AllowedIP ] ,
3739 ip : IpAddr ,
3840 keepalive : Option < u16 > ) {
39- if let Some ( device) = & mut self . device {
41+ if let Some ( device) = & mut self . devices . get_mut ( network_token_id ) {
4042 device. update_peer (
4143 pub_key,
4244 false ,
@@ -64,12 +66,12 @@ impl WRManager {
6466 //TODO: check if need restart
6567 // if interface not equal, restart
6668 // check peers, remove or add new ones.
67- let has_alive = self . is_alive ( ) ;
69+ let has_alive = self . is_alive ( & network_token_id ) ;
6870 if has_alive {
69- let node_type = self . device . as_ref ( ) . map ( |x|x. node_type ) . unwrap_or ( NodeType :: NodeClient ) ;
70- tracing:: info!( "close device" ) ;
71- self . close ( ) . await ;
72- let sleep_time = if node_type == NodeType :: NodeRelay { 10 } else { 20 } ;
71+ let node_type = self . devices . get ( & network_token_id ) . map ( |x|x. node_type ) . unwrap_or ( NodeType :: NodeClient ) ;
72+ tracing:: info!( "close {} device" , network_token_id ) ;
73+ self . close ( & network_token_id ) . await ;
74+ let sleep_time = if node_type == NodeType :: NodeRelay { 10 } else { 20 } ;
7375 tokio:: time:: sleep ( Duration :: from_secs ( sleep_time) ) . await ;
7476 }
7577
@@ -107,7 +109,7 @@ impl WRManager {
107109 } else {
108110 server_config. info . push ( NetworkInfo {
109111 tun_name : Some ( wr_interface. name . clone ( ) ) ,
110- network_id : network_token_id
112+ network_id : network_token_id. clone ( )
111113 } ) ;
112114 need_save = true ;
113115 }
@@ -117,13 +119,14 @@ impl WRManager {
117119 }
118120 }
119121
120- self . device = Some ( wr_interface) ;
122+ self . devices . insert ( network_token_id . clone ( ) , wr_interface) ;
121123 for peer in wr_config. peers {
122124 let ( x_pub_key, _) = Identity :: get_pub_identity_from_base64 ( & peer. public_key ) ?;
123125 let endpoint = peer. endpoint . map ( |v| SocketAddr :: from_str ( & v) . unwrap ( ) ) ;
124126 let allowed_ip: Vec < AllowedIP > = peer. allowed_ip . into_iter ( ) . map ( |ip| AllowedIP :: from_str ( & ip) . unwrap ( ) ) . collect ( ) ;
125127 let ip: IpAddr = peer. address . first ( ) . unwrap ( ) . parse ( ) . unwrap ( ) ;
126128 self . add_peer (
129+ & network_token_id,
127130 x_pub_key,
128131 endpoint,
129132 allowed_ip. as_slice ( ) ,
@@ -135,20 +138,20 @@ impl WRManager {
135138 Ok ( ( ) )
136139 }
137140
138- pub fn is_alive ( & self ) -> bool { self . device . is_some ( ) }
141+ pub fn is_alive ( & self , network_token_id : & str ) -> bool { self . devices . contains_key ( network_token_id ) }
139142
140- pub async fn close ( & mut self ) {
141- if let Some ( ref mut device) = self . device . take ( ) {
142- device. close ( ) . await
143+ pub async fn close ( & mut self , network_token_id : & str ) {
144+ if let Some ( device) = self . devices . get_mut ( network_token_id) {
145+ device. close ( ) . await ;
146+ self . devices . remove ( network_token_id) ;
143147 }
148+
144149 }
145150
146151 pub fn device_info ( & self ) -> Vec < DeviceInfoResp > {
147- self . device . as_ref ( ) . map_or ( vec ! [ ] , |device| {
148- vec ! [ DeviceInfoResp {
149- name: device. name. clone( )
150- } ]
151- } )
152+ self . devices . values ( ) . map ( |device| DeviceInfoResp {
153+ name : device. name . clone ( )
154+ } ) . collect ( )
152155 }
153156}
154157
0 commit comments