1
1
// Copyright 2024 - Nym Technologies SA <[email protected] >
2
2
// SPDX-License-Identifier: GPL-3.0-only
3
3
4
- use std:: { fmt, net:: IpAddr , str:: FromStr } ;
5
-
6
4
use itertools:: Itertools ;
7
5
use nym_sdk:: mixnet:: NodeIdentity ;
6
+ use nym_topology:: { NodeId , RoutingNode } ;
8
7
use nym_vpn_api_client:: types:: Percent ;
9
8
use rand:: seq:: IteratorRandom ;
9
+ use std:: { fmt, net:: IpAddr } ;
10
10
use tracing:: error;
11
11
12
12
use crate :: { error:: Result , AuthAddress , Country , Error , IpPacketRouterAddress } ;
13
13
14
+ pub type NymNode = Gateway ;
15
+
14
16
#[ derive( Clone ) ]
15
17
pub struct Gateway {
18
+ pub node_id : NodeId ,
16
19
pub identity : NodeIdentity ,
17
20
pub location : Option < Location > ,
18
21
pub ipr_address : Option < IpPacketRouterAddress > ,
19
22
pub authenticator_address : Option < AuthAddress > ,
20
23
pub last_probe : Option < Probe > ,
21
- pub host : Option < nym_topology:: NetworkAddress > ,
24
+ pub ips : Vec < IpAddr > ,
25
+ pub host : Option < String > ,
22
26
pub clients_ws_port : Option < u16 > ,
23
27
pub clients_wss_port : Option < u16 > ,
24
28
pub mixnet_performance : Option < Percent > ,
@@ -28,6 +32,7 @@ pub struct Gateway {
28
32
impl fmt:: Debug for Gateway {
29
33
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
30
34
f. debug_struct ( "Gateway" )
35
+ . field ( "node_id" , & self . node_id )
31
36
. field ( "identity" , & self . identity . to_base58_string ( ) )
32
37
. field ( "location" , & self . location )
33
38
. field ( "ipr_address" , & self . ipr_address )
@@ -42,8 +47,12 @@ impl fmt::Debug for Gateway {
42
47
}
43
48
44
49
impl Gateway {
45
- pub fn identity ( & self ) -> & NodeIdentity {
46
- & self . identity
50
+ pub fn node_id ( & self ) -> NodeId {
51
+ self . node_id
52
+ }
53
+
54
+ pub fn identity ( & self ) -> NodeIdentity {
55
+ self . identity
47
56
}
48
57
49
58
pub fn two_letter_iso_country_code ( & self ) -> Option < & str > {
@@ -64,17 +73,12 @@ impl Gateway {
64
73
self . authenticator_address . is_some ( )
65
74
}
66
75
67
- pub fn host ( & self ) -> Option < & nym_topology :: NetworkAddress > {
76
+ pub fn host ( & self ) -> Option < & String > {
68
77
self . host . as_ref ( )
69
78
}
70
79
71
- pub async fn lookup_ip ( & self ) -> Option < IpAddr > {
72
- match self . host . clone ( ) ? {
73
- nym_topology:: NetworkAddress :: IpAddr ( ip) => Some ( ip) ,
74
- nym_topology:: NetworkAddress :: Hostname ( hostname) => {
75
- crate :: helpers:: try_resolve_hostname ( & hostname) . await . ok ( )
76
- }
77
- }
80
+ pub fn lookup_ip ( & self ) -> Option < IpAddr > {
81
+ self . ips . first ( ) . copied ( )
78
82
}
79
83
80
84
pub fn clients_address_no_tls ( & self ) -> Option < String > {
@@ -218,23 +222,22 @@ impl TryFrom<nym_vpn_api_client::response::NymDirectoryGateway> for Gateway {
218
222
. authenticator
219
223
. and_then ( |auth| AuthAddress :: try_from_base58_string ( & auth. address ) . ok ( ) ) ;
220
224
221
- let hostname = gateway
222
- . entry
223
- . hostname
224
- . map ( nym_topology:: NetworkAddress :: Hostname ) ;
225
- let first_ip_address = gateway. ip_addresses . first ( ) . cloned ( ) . and_then ( |ip| {
226
- IpAddr :: from_str ( & ip)
227
- . ok ( )
228
- . map ( nym_topology:: NetworkAddress :: IpAddr )
229
- } ) ;
225
+ let hostname = gateway. entry . hostname ;
226
+ let first_ip_address = gateway
227
+ . ip_addresses
228
+ . first ( )
229
+ . cloned ( )
230
+ . map ( |ip| ip. to_string ( ) ) ;
230
231
let host = hostname. or ( first_ip_address) ;
231
232
232
233
Ok ( Gateway {
234
+ node_id : gateway. node_id ,
233
235
identity,
234
236
location : Some ( gateway. location . into ( ) ) ,
235
237
ipr_address,
236
238
authenticator_address,
237
239
last_probe : gateway. last_probe . map ( Probe :: from) ,
240
+ ips : gateway. ip_addresses ,
238
241
host,
239
242
clients_ws_port : Some ( gateway. entry . ws_port ) ,
240
243
clients_wss_port : gateway. entry . wss_port ,
@@ -287,19 +290,24 @@ impl TryFrom<nym_validator_client::models::NymNodeDescription> for Gateway {
287
290
} else {
288
291
nym_validator_client:: nym_nodes:: NodeRole :: Inactive
289
292
} ;
290
- let gateway = nym_topology:: gateway:: LegacyNode :: try_from (
291
- & node_description. to_skimmed_node ( role, Default :: default ( ) ) ,
292
- )
293
- . ok ( ) ;
294
- let host = gateway. clone ( ) . map ( |g| g. host ) ;
295
- let clients_ws_port = gateway. as_ref ( ) . map ( |g| g. clients_ws_port ) ;
296
- let clients_wss_port = gateway. and_then ( |g| g. clients_wss_port ) ;
293
+
294
+ let gateway =
295
+ RoutingNode :: try_from ( & node_description. to_skimmed_node ( role, Default :: default ( ) ) )
296
+ . map_err ( |_| Error :: MalformedGateway ) ?;
297
+
298
+ let host = gateway. ws_entry_address ( false ) ;
299
+ let entry_info = & gateway. entry ;
300
+ let clients_ws_port = entry_info. as_ref ( ) . map ( |g| g. clients_ws_port ) ;
301
+ let clients_wss_port = entry_info. as_ref ( ) . and_then ( |g| g. clients_wss_port ) ;
302
+ let ips = node_description. description . host_information . ip_address ;
297
303
Ok ( Gateway {
304
+ node_id : gateway. node_id ,
298
305
identity,
299
306
location,
300
307
ipr_address,
301
308
authenticator_address,
302
309
last_probe : None ,
310
+ ips,
303
311
host,
304
312
clients_ws_port,
305
313
clients_wss_port,
@@ -309,6 +317,8 @@ impl TryFrom<nym_validator_client::models::NymNodeDescription> for Gateway {
309
317
}
310
318
}
311
319
320
+ pub type NymNodeList = GatewayList ;
321
+
312
322
#[ derive( Debug , Clone ) ]
313
323
pub struct GatewayList {
314
324
gateways : Vec < Gateway > ,
@@ -341,10 +351,14 @@ impl GatewayList {
341
351
. collect ( )
342
352
}
343
353
344
- pub fn gateway_with_identity ( & self , identity : & NodeIdentity ) -> Option < & Gateway > {
354
+ pub fn node_with_identity ( & self , identity : & NodeIdentity ) -> Option < & NymNode > {
345
355
self . gateways
346
356
. iter ( )
347
- . find ( |gateway| gateway. identity ( ) == identity)
357
+ . find ( |node| & node. identity ( ) == identity)
358
+ }
359
+
360
+ pub fn gateway_with_identity ( & self , identity : & NodeIdentity ) -> Option < & Gateway > {
361
+ self . node_with_identity ( identity)
348
362
}
349
363
350
364
pub fn gateways_located_at ( & self , code : String ) -> impl Iterator < Item = & Gateway > {
@@ -425,16 +439,22 @@ impl IntoIterator for GatewayList {
425
439
}
426
440
427
441
impl nym_client_core:: init:: helpers:: ConnectableGateway for Gateway {
428
- fn identity ( & self ) -> & nym_sdk:: mixnet:: NodeIdentity {
442
+ fn node_id ( & self ) -> NodeId {
443
+ self . node_id ( )
444
+ }
445
+
446
+ fn identity ( & self ) -> nym_sdk:: mixnet:: NodeIdentity {
429
447
self . identity ( )
430
448
}
431
449
432
- fn clients_address ( & self ) -> String {
450
+ fn clients_address ( & self , _prefer_ipv6 : bool ) -> Option < String > {
433
451
// This is a bit of a sharp edge, but temporary until we can remove Option from host
434
452
// and tls port when we add these to the vpn API endpoints.
435
- self . clients_address_tls ( )
436
- . or ( self . clients_address_no_tls ( ) )
437
- . unwrap_or ( "ws://" . to_string ( ) )
453
+ Some (
454
+ self . clients_address_tls ( )
455
+ . or ( self . clients_address_no_tls ( ) )
456
+ . unwrap_or ( "ws://" . to_string ( ) ) ,
457
+ )
438
458
}
439
459
440
460
fn is_wss ( & self ) -> bool {
0 commit comments