1
1
use crate :: country:: Country ;
2
+
2
3
use anyhow:: { Result , anyhow} ;
3
4
use nym_vpn_proto:: proto as p;
4
5
use serde:: { Deserialize , Serialize } ;
@@ -26,6 +27,46 @@ pub enum Score {
26
27
High ,
27
28
}
28
29
30
+ #[ derive( Serialize , Deserialize , Clone , Debug , PartialEq , Eq , PartialOrd , Ord , TS , Default ) ]
31
+ #[ ts( export) ]
32
+ #[ serde( rename_all = "kebab-case" ) ]
33
+ pub enum AsnType {
34
+ #[ default]
35
+ Other ,
36
+ Residential ,
37
+ }
38
+
39
+ #[ derive( Serialize , Deserialize , Clone , Debug , TS ) ]
40
+ #[ ts( export) ]
41
+ #[ serde( rename_all = "camelCase" ) ]
42
+ pub struct Asn {
43
+ pub asn : String ,
44
+ pub name : String ,
45
+ #[ serde( rename = "type" ) ]
46
+ pub kind : AsnType ,
47
+ }
48
+
49
+ #[ derive( Serialize , Deserialize , Clone , Debug , TS ) ]
50
+ #[ ts( export) ]
51
+ #[ serde( rename_all = "camelCase" ) ]
52
+ pub struct Location {
53
+ pub latitude : f64 ,
54
+ pub longitude : f64 ,
55
+ pub city : String ,
56
+ pub region : String ,
57
+ }
58
+
59
+ #[ derive( Serialize , Deserialize , Clone , Debug , TS ) ]
60
+ #[ ts( export) ]
61
+ #[ serde( rename_all = "camelCase" ) ]
62
+ pub struct Performance {
63
+ pub score : Score ,
64
+ pub load : Score ,
65
+ pub last_updated_utc : String ,
66
+ /// uptime percentage on the last 24 hours
67
+ pub uptime_24h : f32 ,
68
+ }
69
+
29
70
#[ derive( Serialize , Deserialize , Clone , Debug , TS ) ]
30
71
#[ ts( export) ]
31
72
#[ serde( rename_all = "camelCase" ) ]
@@ -35,8 +76,14 @@ pub struct Gateway {
35
76
pub kind : GatewayType ,
36
77
pub name : String ,
37
78
pub country : Country ,
79
+ pub location : Location ,
80
+ pub asn : Option < Asn > ,
38
81
pub mx_score : Score ,
39
82
pub wg_score : Score ,
83
+ pub wg_performance : Option < Performance > ,
84
+ pub exit_ipv4 : Option < String > ,
85
+ pub exit_ipv6 : Option < String > ,
86
+ pub build_version : Option < String > ,
40
87
}
41
88
42
89
impl Gateway {
@@ -62,20 +109,31 @@ impl Gateway {
62
109
63
110
let wg_score = gateway
64
111
. wg_performance
112
+ . as_ref ( )
65
113
. map ( |s| {
66
114
p:: Score :: try_from ( s. score )
67
115
. inspect_err ( |e| error ! ( "failed to parse proto gw wireguard score: {}" , e) )
68
116
} )
69
117
. transpose ( ) ?
70
118
. unwrap_or ( p:: Score :: Offline ) ;
71
119
120
+ let asn = location. asn . clone ( ) . map ( |a| a. into ( ) ) ;
121
+ let exit_ipv4 = gateway. exit_ipv4s . first ( ) . cloned ( ) ;
122
+ let exit_ipv6 = gateway. exit_ipv6s . first ( ) . cloned ( ) ;
123
+
72
124
Ok ( Self {
73
125
id : id. id ,
74
126
kind : gw_type,
75
127
name : gateway. moniker ,
76
128
country : Country :: try_from ( & location) ?,
129
+ location : location. into ( ) ,
130
+ asn,
77
131
mx_score : Score :: from ( mx_score) ,
78
132
wg_score : Score :: from ( wg_score) ,
133
+ wg_performance : gateway. wg_performance . map ( |p| p. into ( ) ) ,
134
+ exit_ipv4,
135
+ exit_ipv6,
136
+ build_version : gateway. build_version ,
79
137
} )
80
138
}
81
139
}
@@ -111,6 +169,42 @@ impl From<GatewayType> for p::GatewayType {
111
169
}
112
170
}
113
171
172
+ impl From < p:: RichLocation > for Location {
173
+ fn from ( proto : p:: RichLocation ) -> Self {
174
+ Location {
175
+ latitude : proto. latitude ,
176
+ longitude : proto. longitude ,
177
+ city : proto. city ,
178
+ region : proto. region ,
179
+ }
180
+ }
181
+ }
182
+
183
+ impl From < p:: Asn > for Asn {
184
+ fn from ( proto : p:: Asn ) -> Self {
185
+ let asn_kind = & proto. kind ( ) ;
186
+ Asn {
187
+ asn : proto. asn ,
188
+ name : proto. name ,
189
+ kind : match asn_kind {
190
+ p:: AsnKind :: Residential => AsnType :: Residential ,
191
+ p:: AsnKind :: Other => AsnType :: Other ,
192
+ } ,
193
+ }
194
+ }
195
+ }
196
+
197
+ impl From < p:: Performance > for Performance {
198
+ fn from ( proto : p:: Performance ) -> Self {
199
+ Performance {
200
+ score : Score :: from ( proto. score ( ) ) ,
201
+ load : Score :: from ( proto. load ( ) ) ,
202
+ last_updated_utc : proto. last_updated_utc ,
203
+ uptime_24h : proto. uptime_percentage_last_24_hours ,
204
+ }
205
+ }
206
+ }
207
+
114
208
impl fmt:: Display for Gateway {
115
209
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
116
210
write ! ( f, "[{}] ({}) {}" , self . id, self . name, self . country)
0 commit comments