Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change the API for getting the VPN IP #1077

Merged
merged 1 commit into from
Feb 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -288,9 +288,9 @@
<div class="title">{{ 'vpn.status-page.data.country' | translate }}</div>
<div class="big-text" *ngIf="ipInfoAllowed">
<ng-container *ngIf="ipCountry">{{ ipCountry }}</ng-container>
<ng-container *ngIf="!ipCountry && !loadingIpCountry">{{ 'common.unknown' | translate }}</ng-container>
<mat-spinner *ngIf="loadingIpCountry" [diameter]="20"></mat-spinner>
<mat-icon *ngIf="problemGettingIpCountry" class="small-icon blinking" [inline]="true" [matTooltip]="'vpn.status-page.data.ip-country-problem-info' | translate">warning</mat-icon>
<ng-container *ngIf="!ipCountry && !loadingCurrentIp">{{ 'common.unknown' | translate }}</ng-container>
<mat-spinner *ngIf="loadingCurrentIp" [diameter]="20"></mat-spinner>
<mat-icon *ngIf="problemGettingIp" class="small-icon blinking" [inline]="true" [matTooltip]="'vpn.status-page.data.ip-country-problem-info' | translate">warning</mat-icon>
</div>
<div class="big-text" *ngIf="!ipInfoAllowed">
{{ 'vpn.status-page.data.unavailable' | translate }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
Expand All @@ -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;
});
}
}
47 changes: 14 additions & 33 deletions static/skywire-manager-src/src/app/services/vpn-client.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> {
getIpData(): Observable<string[]> {
// 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<string> {
// 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];
})
);
}
Expand Down