-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathresponse.rs
More file actions
870 lines (754 loc) · 25.5 KB
/
response.rs
File metadata and controls
870 lines (754 loc) · 25.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
// Copyright 2024 - Nym Technologies SA <contact@nymtech.net>
// SPDX-License-Identifier: GPL-3.0-only
use std::{
collections::HashSet,
fmt,
net::{IpAddr, SocketAddr},
};
use crate::{error::VpnApiClientError, network_compatibility::NetworkCompatibility};
use itertools::Itertools;
use nym_contracts_common::Percent;
use nym_credential_proxy_requests::api::v1::ticketbook::models::TicketbookWalletSharesResponse;
pub use nym_credential_proxy_requests::api::v1::ticketbook::models::UpgradeModeAttestation;
use nym_validator_client::models::LewesProtocolDetailsV1;
use serde::{Deserialize, Serialize};
use time::{OffsetDateTime, UtcDateTime, format_description::well_known::Iso8601};
const MAX_PROBE_RESULT_AGE_MINUTES: i64 = 60;
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
pub struct NymVpnRegisterAccountResponse {
pub created_on_utc: String,
pub last_updated_utc: String,
pub account_addr: String,
pub status: NymVpnRegisterAccountStatusResponse,
pub account_token: String,
}
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum NymVpnRegisterAccountStatusResponse {
Active,
}
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
pub struct NymVpnAccountResponse {
pub created_on_utc: String,
pub last_updated_utc: String,
pub account_addr: String,
pub status: NymVpnAccountStatusResponse,
pub canonical_account_addr: Option<String>,
pub auth_methods: Vec<NymVpnAccountAuthMethodResponse>,
}
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq, strum_macros::Display)]
#[serde(rename_all = "snake_case")]
pub enum NymVpnAccountStatusResponse {
Active,
Inactive,
DeleteMe,
}
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub struct NymVpnAccountAuthMethodResponse {
pub id: String,
pub pubkey: String,
pub kind: String,
pub label: String,
pub status: NymVpnAccountStatusResponse,
pub created: String,
}
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct NymVpnAccountSummaryResponse {
pub account: NymVpnAccountResponse,
pub subscription: NymVpnAccountSummarySubscription,
pub devices: NymVpnAccountSummaryDevices,
pub fair_usage: NymVpnAccountSummaryFairUsage,
}
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct NymVpnAccountSummaryWithDeviceResponse {
#[serde(flatten)]
pub account_summary: NymVpnAccountSummaryResponse,
pub active_device: Option<NymVpnDevice>,
}
impl NymVpnAccountSummaryWithDeviceResponse {
pub fn account_active(&self) -> bool {
self.account_summary.account.status == NymVpnAccountStatusResponse::Active
}
pub fn subscription_active(&self) -> bool {
self.account_summary.subscription.is_active
}
pub fn subscription_pending(&self) -> bool {
!self.account_summary.subscription.is_active
&& self.account_summary.subscription.pending.is_some()
}
pub fn bandwidth_limit(&self) -> u64 {
self.account_summary.fair_usage.limitGB
}
pub fn used_bandwidth(&self) -> u64 {
self.account_summary.fair_usage.usedGB
}
pub fn remaining_devices(&self) -> u64 {
self.account_summary.devices.remaining
}
}
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct NymVpnAccountSummarySubscription {
pub is_active: bool,
pub active: Option<NymVpnSubscription>,
pub pending: Option<NymVpnSubscription>,
#[serde(default)]
pub is_stacked: bool,
}
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct NymVpnAccountSummaryDevices {
pub active: u64,
pub max: u64,
pub remaining: u64,
}
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
#[allow(non_snake_case)]
// These fields have the substring 'GB' in them, meaning we can't use `rename_all = "camelCase"`
// like for the other structs
pub struct NymVpnAccountSummaryFairUsage {
pub usedGB: u64,
pub limitGB: u64,
pub resetsOnUtc: Option<String>,
// Absent in older API responses - treat as false (data available) during rollout.
#[serde(default, rename = "dataUnavailable")]
pub data_unavailable: bool,
}
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
pub struct NymVpnCanonicalAccountIdentityResponse {
pub canonical_account_addr: String,
}
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct NymVpnHealthResponse {
pub status: String,
#[serde(with = "time::serde::rfc3339")]
pub timestamp_utc: OffsetDateTime,
}
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
pub struct NymVpnDevice {
pub created_on_utc: String,
pub last_updated_utc: String,
pub device_identity_key: String,
pub status: NymVpnDeviceStatus,
}
#[derive(Copy, Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum NymVpnDeviceStatus {
Active,
Inactive,
DeleteMe,
}
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct NymVpnDevicesResponse {
pub total_items: u64,
pub page: u64,
pub page_size: u64,
pub items: Vec<NymVpnDevice>,
}
impl fmt::Display for NymVpnDevicesResponse {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"{}",
self.items
.iter()
.format_with(", ", |item, f| f(&format_args!("{item:?}")))
)
}
}
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct NymVpnRefundsResponse {
pub total_items: u64,
pub page: u64,
pub page_size: u64,
pub items: Vec<NymVpnRefund>,
}
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
pub struct NymVpnRefund {
pub created_on_utc: String,
pub last_updated_utc: String,
pub subscription_invoice: String,
pub status: NymVpnRefundStatus,
pub user_reason: NymVpnRefundUserReason,
pub data: Option<String>,
}
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum NymVpnRefundStatus {
Pending,
Complete,
Rejected,
}
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum NymVpnRefundUserReason {
SubscriptionInError,
PoorPerformance,
Other,
}
// Legacy type, because the blinded_shares response for the POST seems to be different than the GET
// Remove once it's not needed anymore
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct NymVpnZkNymPost {
pub created_on_utc: String,
pub last_updated_utc: String,
pub id: String,
pub ticketbook_type: String,
pub valid_until_utc: String,
pub valid_from_utc: String,
pub issued_bandwidth_in_gb: f64,
pub blinded_shares: Option<Vec<Option<TicketbookWalletSharesResponse>>>,
pub status: NymVpnZkNymStatus,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct UpgradeModeResponseData {
pub upgrade_mode_attestation: UpgradeModeAttestation,
pub upgrade_mode_jwt: String,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct NymVpnZkNym {
pub created_on_utc: String,
pub last_updated_utc: String,
pub id: String,
pub ticketbook_type: String,
pub valid_until_utc: String,
pub valid_from_utc: String,
pub issued_bandwidth_in_gb: f64,
pub blinded_shares: Option<TicketbookWalletSharesResponse>,
pub status: NymVpnZkNymStatus,
pub upgrade_mode: Option<UpgradeModeResponseData>,
}
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq, strum::Display)]
#[serde(rename_all = "snake_case")]
pub enum NymVpnZkNymStatus {
Pending,
Active,
Revoking,
Revoked,
Error,
UpgradeMode,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct NymVpnZkNymResponse {
pub total_items: u64,
pub page: u64,
pub page_size: u64,
pub items: Vec<NymVpnZkNym>,
}
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
pub struct NymVpnSubscription {
pub created_on_utc: String,
pub last_updated_utc: String,
pub id: String,
pub valid_until_utc: String,
pub valid_from_utc: String,
pub status: NymVpnSubscriptionStatus,
pub kind: NymVpnSubscriptionKind,
#[serde(default, rename = "isRecurring")]
pub is_recurring: bool,
}
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum NymVpnSubscriptionStatus {
Pending,
Complete,
Active,
}
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum NymVpnSubscriptionKind {
OneMonth,
OneYear,
TwoYears,
Freepass,
#[serde(untagged)]
Other(String),
}
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct NymVpnSubscriptionResponse {
pub is_subscription_active: bool,
pub subscription: Option<NymVpnSubscription>,
pub remaining_allowance_in_gb: f64,
}
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct NymVpnSubscriptionsResponse {
pub total_items: u64,
pub page: u64,
pub page_size: u64,
pub items: Vec<NymVpnSubscription>,
}
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct NymVpnUsagesResponse {
pub total_items: u64,
pub page: u64,
pub page_size: u64,
pub items: Vec<NymVpnUsage>,
}
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq)]
pub struct NymVpnUsage {
pub created_on_utc: String,
pub last_updated_utc: String,
pub id: String,
pub subscription_id: String,
pub valid_until_utc: String,
pub valid_from_utc: String,
pub bandwidth_allowance_gb: f64,
pub bandwidth_used_gb: f64,
}
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct NymDirectoryGatewaysResponse(Vec<NymDirectoryGateway>);
impl NymDirectoryGatewaysResponse {
pub fn into_inner(self) -> Vec<NymDirectoryGateway> {
self.0
}
}
impl IntoIterator for NymDirectoryGatewaysResponse {
type Item = NymDirectoryGateway;
type IntoIter = std::vec::IntoIter<Self::Item>;
fn into_iter(self) -> Self::IntoIter {
self.0.into_iter()
}
}
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum ScoreValue {
Offline,
Low,
Medium,
High,
}
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct DVpnGatewayPerformance {
pub last_updated_utc: String,
pub score: ScoreValue,
pub mixnet_score: ScoreValue,
pub load: ScoreValue,
pub uptime_percentage_last_24_hours: f32,
}
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct NodeStaking {
// delegations + bond
pub total_stake: u128,
pub total_delegations: u128,
pub total_bond: u128,
// number of delegations
pub delegations: usize,
}
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct NodeFamily {
pub id: u32,
pub name: String,
pub description: String,
// in unym
pub family_stake: u128,
pub members: usize,
}
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct NymDirectoryGateway {
pub identity_key: String,
pub name: String,
pub description: Option<String>,
pub ip_packet_router: Option<IpPacketRouter>,
pub authenticator: Option<Authenticator>,
pub location: Location,
pub last_probe: Option<Probe>,
pub ip_addresses: Vec<IpAddr>,
pub mix_port: u16,
pub role: Role,
pub entry: EntryInformation,
pub bridges: Option<BridgeInformation>,
// The performance data here originates from the nym-api, and is effectively mixnet performance
// at the time of writing this
pub performance: Percent,
// Node performance information needed by the NymVPN UI / Explorer to show more information
// about the node in a user-friendly way
pub performance_v2: Option<DVpnGatewayPerformance>,
pub build_information: Option<BuildInformation>,
pub lewes_protocol_details: Option<LewesProtocolDetailsV1>,
pub node_staking: Option<NodeStaking>,
pub node_family: Option<NodeFamily>,
}
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct EntryInformation {
pub hostname: Option<String>,
pub ws_port: u16,
pub wss_port: Option<u16>,
}
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct UxScore {
pub max_score: u8,
pub current_score: u8,
pub color: String,
}
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct IpPacketRouter {
pub address: String,
}
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct Authenticator {
pub address: String,
}
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct BridgeInformation {
pub version: String,
pub transports: Vec<BridgeParameters>,
}
impl BridgeInformation {
pub fn get_addrs(&self) -> Vec<SocketAddr> {
let mut addrs = Vec::new();
for transport in &self.transports {
match transport {
BridgeParameters::QuicPlain(params) => addrs.extend(¶ms.addresses),
}
}
addrs
}
}
#[derive(Debug, Clone, Deserialize, Serialize)]
#[serde(tag = "transport_type", content = "args")]
#[serde(rename_all = "snake_case")]
pub enum BridgeParameters {
QuicPlain(QuicClientOptions),
}
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct QuicClientOptions {
/// Address describing the remote transport server. This is a vec to support multiple addresses
/// so as to support both IPv4 and IPv6. These addresses are meant to describe a single bridge
/// as the key material should not be used across multiple instances.
pub addresses: Vec<std::net::SocketAddr>,
/// Override hostname used for certificate verification
pub host: Option<String>,
/// Use identity public key to verify server self signed certificate
pub id_pubkey: String,
}
#[derive(Debug, Clone, Deserialize, Serialize)]
pub enum Role {
// a properly active mixnode
Mixnode {
layer: u8,
},
#[serde(alias = "entry", alias = "gateway")]
EntryGateway,
#[serde(alias = "exit")]
ExitGateway,
// equivalent of node that's in rewarded set but not in the inactive set
Standby,
Inactive,
}
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct BuildInformation {
pub build_version: String,
pub commit_branch: String,
pub commit_sha: String,
}
impl NymDirectoryGateway {
pub fn is_fully_operational_entry(&self) -> bool {
self.last_probe
.as_ref()
.map(|probe| probe.is_fully_operational_entry())
.unwrap_or(false)
}
pub fn is_fully_operational_exit(&self) -> bool {
self.last_probe
.as_ref()
.map(|probe| probe.is_fully_operational_exit())
.unwrap_or(false)
}
}
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum AsnKind {
Residential,
Other,
}
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct Asn {
pub asn: String,
pub name: String,
pub kind: AsnKind,
}
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct Location {
pub two_letter_iso_country_code: String,
pub latitude: f64,
pub longitude: f64,
pub city: String,
pub region: String,
pub asn: Option<Asn>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Probe {
pub last_updated_utc: String,
pub outcome: ProbeOutcome,
}
impl Probe {
pub fn is_fully_operational_entry(&self) -> bool {
self.is_recently_updated() && self.outcome.is_fully_operational_entry()
}
pub fn is_fully_operational_exit(&self) -> bool {
self.is_recently_updated() && self.outcome.is_fully_operational_exit()
}
fn is_recently_updated(&self) -> bool {
UtcDateTime::parse(&self.last_updated_utc, &Iso8601::DEFAULT)
.map(|last_updated| {
let now = UtcDateTime::now();
let duration = now - last_updated;
duration.whole_minutes() < MAX_PROBE_RESULT_AGE_MINUTES
})
.unwrap_or_default()
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProbeOutcome {
pub as_entry: Entry,
pub as_exit: Option<Exit>,
pub wg: Option<WgProbeResults>,
pub socks5: Option<Socks5>,
pub lp: Option<Lp>,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Socks5 {
pub can_proxy_https: bool,
pub score: Option<ScoreValue>,
pub errors: Option<Vec<String>>,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Lp {
pub can_connect: bool,
pub can_handshake: bool,
pub can_register: bool,
pub error: Option<String>,
}
impl ProbeOutcome {
pub fn is_fully_operational_entry(&self) -> bool {
self.as_entry.can_connect && self.as_entry.can_route
}
pub fn is_fully_operational_exit(&self) -> bool {
self.as_entry.can_connect
&& self.as_entry.can_route
&& self.as_exit.as_ref().is_some_and(|exit| {
exit.can_connect
&& exit.can_route_ip_v4
&& exit.can_route_ip_external_v4
&& exit.can_route_ip_v6
&& exit.can_route_ip_external_v6
})
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Entry {
pub can_connect: bool,
pub can_route: bool,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Exit {
pub can_connect: bool,
pub can_route_ip_v4: bool,
pub can_route_ip_external_v4: bool,
pub can_route_ip_v6: bool,
pub can_route_ip_external_v6: bool,
pub socks5: Option<Socks5>,
}
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
#[serde(rename = "wg")]
pub struct WgProbeResults {
pub can_register: bool,
pub can_handshake: bool,
pub can_resolve_dns: bool,
#[serde(default)]
pub can_query_metadata_v4: Option<bool>,
pub ping_hosts_performance: f32,
pub ping_ips_performance: f32,
}
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct NymDirectoryGatewayCountriesResponse(Vec<NymDirectoryCountry>);
impl NymDirectoryGatewayCountriesResponse {
pub fn into_inner(self) -> Vec<NymDirectoryCountry> {
self.0
}
}
impl IntoIterator for NymDirectoryGatewayCountriesResponse {
type Item = NymDirectoryCountry;
type IntoIter = std::vec::IntoIter<Self::Item>;
fn into_iter(self) -> Self::IntoIter {
self.0.into_iter()
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NymDirectoryCountry(String);
impl NymDirectoryCountry {
pub fn iso_code(&self) -> &str {
&self.0
}
pub fn into_string(self) -> String {
self.0
}
}
impl From<String> for NymDirectoryCountry {
fn from(s: String) -> Self {
Self(s)
}
}
#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct NymErrorResponse {
pub message: String,
pub message_id: Option<String>,
pub code_reference_id: Option<String>,
pub status: String,
}
impl fmt::Display for NymErrorResponse {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let fields = [
Some(format!("message: {}", self.message)),
self.message_id
.as_deref()
.map(|x| format!("message_id: {x}")),
self.code_reference_id
.as_deref()
.map(|x| format!("code_reference_id: {x}")),
Some(format!("status: {}", self.status)),
]
.iter()
.filter_map(|x| x.clone())
.collect::<Vec<_>>();
write!(f, "{}", fields.join(", "))
}
}
#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct UnexpectedError {
pub message: String,
}
impl fmt::Display for UnexpectedError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.message)
}
}
#[derive(Debug, Serialize, Deserialize)]
pub struct StatusOk {
pub status: String,
}
impl fmt::Display for StatusOk {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.status)
}
}
pub fn extract_error_response(err: &VpnApiClientError) -> Option<NymErrorResponse> {
// Try to extract the HttpClientError and parse structured error response
if let Some(nym_http_api_client::HttpClientError::EndpointFailure { error, .. }) =
err.http_client_error()
{
// Try to parse the error string as NymErrorResponse
if let Ok(parsed) = serde_json::from_str::<NymErrorResponse>(error) {
return Some(parsed);
}
}
None
}
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize, PartialEq, Eq)]
pub struct ApiUrl {
pub url: String,
pub fronts: Option<Vec<String>>,
}
// The response type we fetch from the discovery endpoint
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
pub struct NymWellknownDiscoveryItemResponse {
pub network_name: String,
pub nym_api_url: String,
pub nym_api_urls: Vec<ApiUrl>,
pub nym_vpn_api_url: String,
pub nym_vpn_api_urls: Vec<ApiUrl>,
pub account_management: Option<AccountManagementResponse>,
pub feature_flags: Option<serde_json::Value>,
pub system_messages: Option<Vec<SystemMessageResponse>>,
pub system_configuration: Option<SystemConfigurationResponse>,
}
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize, PartialEq, Eq)]
pub struct AccountManagementResponse {
pub url: String,
pub paths: AccountManagementPathsResponse,
}
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize, PartialEq, Eq)]
pub struct AccountManagementPathsResponse {
pub sign_up: String,
pub sign_in: String,
pub account: String,
pub privy: AccountManagementPrivyPathsResponse,
pub autologin: AccountManagementAutologinPathsResponse,
pub pricing: String,
}
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize, PartialEq, Eq)]
pub struct AccountManagementAutologinPathsResponse {
pub mobile: String,
pub desktop: String,
pub web: String,
}
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize, PartialEq, Eq)]
pub struct AccountManagementPrivyPathsResponse {
pub mobile: String,
pub desktop: String,
pub web: String,
}
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct SystemMessageResponse {
pub name: String,
pub display_from: String,
pub display_until: String,
pub message: String,
pub properties: serde_json::Value,
}
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize, PartialEq, Eq)]
pub struct SystemConfigurationResponse {
pub mix_thresholds: ScoreThresholdsResponse,
pub wg_thresholds: ScoreThresholdsResponse,
pub statistics_api: Option<String>,
pub min_supported_app_versions: Option<NetworkCompatibility>,
}
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize, PartialEq, Eq)]
pub struct NetworkCompatibilityResponse {
pub core: String,
pub macos: String,
pub ios: String,
pub tauri: String,
pub android: String,
}
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize, PartialEq, Eq)]
pub struct ScoreThresholdsResponse {
pub high: u8,
pub medium: u8,
pub low: u8,
}
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
pub struct NymWellknownDiscoveryItem {
pub network_name: String,
pub nym_api_url: String,
pub nym_vpn_api_url: String,
}
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
pub struct NymUserGeoIpLocationResponse {
pub ip: String,
pub location: Location,
}
pub type RegisteredNetworksResponse = HashSet<String>;
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn nym_vpn_zk_nym_upgrade_mode_response_parsing() {
let raw_response = r#"{"id":"dj2e1yzc9jj9s8n","status":"upgrade_mode","ticketbook_type":"v1-mixnet-entry","last_updated_utc":"2025-11-13 15:40:31.166Z","created_on_utc":"2025-11-13 15:40:30.921Z","valid_until_utc":"2025-11-13 23:00:00.000Z","valid_from_utc":"2025-11-13 15:40:30.916Z","issued_bandwidth_in_gb":25,"upgrade_mode":{"upgrade_mode_attestation":{"attester_public_key":"6sfL7xcCzmcsxA1uXtnExcpA7KWypCcsUbs7SzUADxng","authorised_jwt_issuers":["Ddfd2WDCWbW28hmrrGV24GxQxBuGmg1Ra41k3TGn3B4Z"],"signature":"3UR5cL9XzDCFVHt9WVmJ4KemcftbfpCa63TBa9wd6w5dW4uq7u4BwS5UsBfwMB49StH5wDCbSpN1borZbfMnitxn","starting_time":"2025-11-10T09:15:18.967542Z","type":"upgrade_mode"},"upgrade_mode_jwt":"eyJhbGciOiJFZERTQSIsInR5cCI6IkpXVCIsImp3ayI6IkRkZmQyV0RDV2JXMjhobXJyR1YyNEd4UXhCdUdtZzFSYTQxazNUR24zQjRaIn0.eyJpYXQiOjE3NjMwNDc5NzYsImV4cCI6MTc2MzA1MTU3NiwibmJmIjoxNzYzMDQ3OTc2LCJpc3MiOiJueW0tY3JlZGVudGlhbC1wcm94eSIsIm5vbmNlIjoiR2ZtYzJYY2c2NUdkc0JaOWJ3RFg2OGxONmNfR2V5NUwiLCJ0eXBlIjoidXBncmFkZV9tb2RlIiwic3RhcnRpbmdfdGltZSI6IjIwMjUtMTEtMTBUMDk6MTU6MTguOTY3NTQyWiIsImF0dGVzdGVyX3B1YmxpY19rZXkiOiI2c2ZMN3hjQ3ptY3N4QTF1WHRuRXhjcEE3S1d5cENjc1ViczdTelVBRHhuZyIsImF1dGhvcmlzZWRfand0X2lzc3VlcnMiOlsiRGRmZDJXRENXYlcyOGhtcnJHVjI0R3hReEJ1R21nMVJhNDFrM1RHbjNCNFoiXSwic2lnbmF0dXJlIjoiM1VSNWNMOVh6RENGVkh0OVdWbUo0S2VtY2Z0YmZwQ2E2M1RCYTl3ZDZ3NWRXNHVxN3U0QndTNVVzQmZ3TUI0OVN0SDV3RENiU3BOMWJvclpiZk1uaXR4biJ9.u66O2q-xoEMtyr-6OBEKL8BEdhoo6xLo6dHkVxDmzB3I6CZ1pFIwzgZUKNA5DT_O6bFzcLv8DyLmOuzdAe-lDw"}}"#;
let res = serde_json::from_str::<NymVpnZkNym>(raw_response);
assert!(res.is_ok());
}
}