6
6
AUTH_API_URL ,
7
7
VPN_API_URL ,
8
8
STAGE_ENV ,
9
+ FORWARDER_DOMAIN ,
9
10
} from '../config' ;
10
11
import { clearFromWrappingQuotes } from '../../common/utils/string' ;
11
12
import { log } from '../../common/logger' ;
@@ -16,8 +17,6 @@ import { authService } from '../authentication/authService';
16
17
import { credentialsService } from '../credentials/credentialsService' ;
17
18
import { FallbackInfo , StorageKey } from '../schema' ;
18
19
19
- export const DEFAULT_CACHE_EXPIRE_TIME_MS = 1000 * 60 * 5 ; // 5 minutes
20
-
21
20
// DNS over https api
22
21
const GOOGLE_DOH_HOSTNAME = 'dns.google' ;
23
22
export const GOOGLE_DOH_URL = `${ GOOGLE_DOH_HOSTNAME } /resolve` ;
@@ -54,14 +53,20 @@ export class FallbackApi {
54
53
*/
55
54
defaultFallbackInfo : FallbackInfo ;
56
55
56
+ /**
57
+ * Fallback info cache expiration time in milliseconds.
58
+ */
59
+ static DEFAULT_CACHE_EXPIRE_TIME_MS = 1000 * 60 * 5 ; // 5 minutes
60
+
57
61
/**
58
62
* Default urls we set already expired,
59
63
* so we need to check bkp url immediately when bkp url is required
60
64
*/
61
- constructor ( vpnApiUrl : string , authApiUrl : string ) {
65
+ constructor ( vpnApiUrl : string , authApiUrl : string , forwarderApiUrl : string ) {
62
66
this . defaultFallbackInfo = {
63
67
vpnApiUrl,
64
68
authApiUrl,
69
+ forwarderApiUrl,
65
70
expiresInMs : Date . now ( ) - 1 ,
66
71
} ;
67
72
}
@@ -84,7 +89,11 @@ export class FallbackApi {
84
89
return fallbackInfo . expiresInMs < Date . now ( ) ;
85
90
}
86
91
87
- private async updateFallbackInfo ( ) {
92
+ /**
93
+ * Updates the fallback info.
94
+ * If backup urls are not received, the default fallback info is set.
95
+ */
96
+ private async updateFallbackInfo ( ) : Promise < void > {
88
97
const [ bkpVpnApiUrl , bkpAuthApiUrl ] = await Promise . all ( [
89
98
this . getBkpVpnApiUrl ( ) ,
90
99
this . getBkpAuthApiUrl ( ) ,
@@ -94,8 +103,13 @@ export class FallbackApi {
94
103
this . fallbackInfo = {
95
104
vpnApiUrl : bkpVpnApiUrl ,
96
105
authApiUrl : bkpAuthApiUrl ,
97
- expiresInMs : Date . now ( ) + DEFAULT_CACHE_EXPIRE_TIME_MS ,
106
+ // use received vpn api url as forwarder api url
107
+ forwarderApiUrl : bkpVpnApiUrl ,
108
+ expiresInMs : Date . now ( ) + FallbackApi . DEFAULT_CACHE_EXPIRE_TIME_MS ,
98
109
} ;
110
+ } else if ( ! this . fallbackInfo ) {
111
+ // set default fallback info if bkp urls are not received and fallback info is not set
112
+ this . fallbackInfo = this . defaultFallbackInfo ;
99
113
}
100
114
}
101
115
@@ -115,6 +129,21 @@ export class FallbackApi {
115
129
return fallbackInfo . vpnApiUrl ;
116
130
} ;
117
131
132
+ /**
133
+ * Returns fallback forwarder API URL. AG-32237.
134
+ *
135
+ * Received VPN API URL is used as a fallback forwarder API URL,
136
+ * but if the default VPN API URL is received, the default forwarder API URL should be returned.
137
+ *
138
+ * @returns Forwarder API URL.
139
+ */
140
+ public getForwarderApiUrl = async ( ) : Promise < string > => {
141
+ const { forwarderApiUrl } = await this . getFallbackInfo ( ) ;
142
+ return forwarderApiUrl === this . defaultFallbackInfo . vpnApiUrl
143
+ ? this . defaultFallbackInfo . forwarderApiUrl
144
+ : forwarderApiUrl ;
145
+ } ;
146
+
118
147
public getAuthApiUrl = async ( ) : Promise < string > => {
119
148
const fallbackInfo = await this . getFallbackInfo ( ) ;
120
149
return fallbackInfo . authApiUrl ;
@@ -284,7 +313,14 @@ export class FallbackApi {
284
313
return `${ basePrefix } .${ UserType . Free } ` ;
285
314
} ;
286
315
287
- getBkpVpnApiUrl = async ( ) => {
316
+ /**
317
+ * Fetches and returns backup VPN API URL.
318
+ *
319
+ * @returns Backup VPN API URL if fetched successfully and not an empty string;
320
+ * if fetched url is an empty string, returns default fallback VPN API URL;
321
+ * OR null if fetching failed.
322
+ */
323
+ getBkpVpnApiUrl = async ( ) : Promise < string | null > => {
288
324
const prefix = await this . getApiHostnamePrefix ( ) ;
289
325
// we use prefix for api hostname to recognize free, premium and not authenticated users
290
326
const hostname = `${ prefix } .${ BKP_API_HOSTNAME_PART } ` ;
@@ -313,4 +349,4 @@ export class FallbackApi {
313
349
}
314
350
}
315
351
316
- export const fallbackApi = new FallbackApi ( VPN_API_URL , AUTH_API_URL ) ;
352
+ export const fallbackApi = new FallbackApi ( VPN_API_URL , AUTH_API_URL , FORWARDER_DOMAIN ) ;
0 commit comments