diff --git a/static/skywire-manager-src/src/app/components/vpn/pages/vpn-status/vpn-status.component.html b/static/skywire-manager-src/src/app/components/vpn/pages/vpn-status/vpn-status.component.html index f610ac3a38..028e64c6e7 100644 --- a/static/skywire-manager-src/src/app/components/vpn/pages/vpn-status/vpn-status.component.html +++ b/static/skywire-manager-src/src/app/components/vpn/pages/vpn-status/vpn-status.component.html @@ -288,9 +288,9 @@
{{ 'vpn.status-page.data.country' | translate }}
{{ ipCountry }} - {{ 'common.unknown' | translate }} - - warning + {{ 'common.unknown' | translate }} + + warning
{{ 'vpn.status-page.data.unavailable' | translate }} diff --git a/static/skywire-manager-src/src/app/components/vpn/pages/vpn-status/vpn-status.component.ts b/static/skywire-manager-src/src/app/components/vpn/pages/vpn-status/vpn-status.component.ts index 8d77a348ca..ee7795442b 100644 --- a/static/skywire-manager-src/src/app/components/vpn/pages/vpn-status/vpn-status.component.ts +++ b/static/skywire-manager-src/src/app/components/vpn/pages/vpn-status/vpn-status.component.ts @@ -73,18 +73,12 @@ export class VpnStatusComponent implements OnInit, OnDestroy { ipInfoAllowed: boolean; // Public IP of the machine running the app. currentIp: string; - // IP the machine running the app had the last time it was checked. - previousIp: string; // Country of the public IP of the machine running the app. ipCountry: string; // If the current IP is being checked. loadingCurrentIp = true; - // If the country of the current IP is being checked. - loadingIpCountry = true; // If there was a problem the last time the code tried to get the current IP. problemGettingIp = false; - // If there was a problem the last time the code tried to get the country of the current IP. - problemGettingIpCountry = false; // Moment in which the IP was refreshed for the last time. private lastIpRefresDate = 0; // Pk of the local visor. @@ -482,7 +476,7 @@ export class VpnStatusComponent implements OnInit, OnDestroy { if (!ignoreTimeCheck) { // Cancel the operation if the IP or its country is already being obtained. - if (this.loadingCurrentIp || this.loadingIpCountry) { + if (this.loadingCurrentIp) { this.snackbarService.showWarning('vpn.status-page.data.ip-refresh-loading-warning'); return; @@ -507,73 +501,26 @@ export class VpnStatusComponent implements OnInit, OnDestroy { // Indicate that the IP and its country are being loaded. this.loadingCurrentIp = true; - this.loadingIpCountry = true; - this.previousIp = this.currentIp; - - // Get the IP. - this.ipSubscription = this.vpnClientService.getIp().subscribe(response => { + // Get the IP and country. + this.ipSubscription = this.vpnClientService.getIpData().subscribe(response => { this.loadingCurrentIp = false; this.lastIpRefresDate = Date.now(); if (response) { - // Update the IP. + // Update the data. this.problemGettingIp = false; - this.currentIp = response; - - // Update the country, if no country has been loaded or the IP changed. - if (!this.ipCountry || this.previousIp !== this.currentIp || this.problemGettingIpCountry) { - this.getIpCountry(); - } else { - this.loadingIpCountry = false; - } + this.currentIp = response[0]; + this.ipCountry = response[1]; } else { // Indicate that there was a problem. this.problemGettingIp = true; - this.problemGettingIpCountry = true; - this.loadingIpCountry = false; } }, () => { // Indicate that there was a problem. this.lastIpRefresDate = Date.now(); this.loadingCurrentIp = false; - this.loadingIpCountry = false; this.problemGettingIp = false; - this.problemGettingIpCountry = true; - }); - } - - /** - * Checks and updates the country of the public IP of the machine running the app. It was made - * to be called from getIp(). - */ - private getIpCountry() { - if (!this.ipInfoAllowed) { - return; - } - - if (this.ipSubscription) { - this.ipSubscription.unsubscribe(); - } - - this.loadingIpCountry = true; - - // Get the country. - this.ipSubscription = this.vpnClientService.getIpCountry(this.currentIp).subscribe(response => { - this.loadingIpCountry = false; - - this.lastIpRefresDate = Date.now(); - - if (response) { - this.problemGettingIpCountry = false; - this.ipCountry = response; - } else { - this.problemGettingIpCountry = true; - } - }, () => { - this.lastIpRefresDate = Date.now(); - this.loadingIpCountry = false; - this.problemGettingIpCountry = true; }); } } diff --git a/static/skywire-manager-src/src/app/services/vpn-client.service.ts b/static/skywire-manager-src/src/app/services/vpn-client.service.ts index c96a01a0ae..4896ec3482 100644 --- a/static/skywire-manager-src/src/app/services/vpn-client.service.ts +++ b/static/skywire-manager-src/src/app/services/vpn-client.service.ts @@ -289,49 +289,30 @@ export class VpnClientService { * Gets the public IP of the machine running this app. If there is an error, it could * return null. */ - getIp(): Observable { + getIpData(): Observable { // Use a test value if in development mode. if (!environment.production && AppConfig.vpn.hardcodedIpWhileDeveloping) { - return of('8.8.8.8 (testing)'); + return of(['8.8.8.8 (testing)', 'United States (testing)']); } - return this.http.request('GET', 'https://api.ipify.org?format=json').pipe( + return this.http.request('GET', window.location.protocol + '//ip.skycoin.com/').pipe( retryWhen(errors => concat(errors.pipe(delay(this.standardWaitTime), take(4)), throwError(''))), map(data => { - if (data && data['ip']) { - return data['ip']; + let ip = ''; + if (data && data['ip_address']) { + ip = data['ip_address']; + } else { + ip = this.translateService.instant('common.unknown'); } - return null; - }) - ); - } - - /** - * Gets the country of the public IP of the machine running this app. If there is an error, - * it could return null. - */ - getIpCountry(ip: string): Observable { - // Use a test value if in development mode. - if (!environment.production && AppConfig.vpn.hardcodedIpWhileDeveloping) { - return of('United States (testing)'); - } - - return this.http.request('GET', 'https://ip2c.org/' + ip, { responseType: 'text' }).pipe( - retryWhen(errors => concat(errors.pipe(delay(2000), take(4)), throwError(''))), - map(data => { - let country: string = null; - - // The name must be the fourth element of the retrieved value. - if (data) { - const dataParts: string[] = data.split(';'); - - if (dataParts.length === 4) { - country = dataParts[3]; - } + let country = ''; + if (data && data['country_name']) { + country = data['country_name']; + } else { + country = this.translateService.instant('common.unknown'); } - return country; + return [ip, country]; }) ); }