diff options
| author | Andrej Mihajlov <and@codeispoetry.ru> | 2017-03-06 19:15:53 +0000 |
|---|---|---|
| committer | Andrej Mihajlov <and@codeispoetry.ru> | 2017-03-06 19:15:53 +0000 |
| commit | aac77666f5d2fc1dacad664dedbb44733550a3f9 (patch) | |
| tree | 6f97fccfeeb088ac274382ccf2a8570dc83eed45 /app/lib/backend.js | |
| parent | 731a9e48bb5cc21c0156b81fa1e027faee86ae0a (diff) | |
| download | mullvadvpn-aac77666f5d2fc1dacad664dedbb44733550a3f9.tar.xz mullvadvpn-aac77666f5d2fc1dacad664dedbb44733550a3f9.zip | |
Fetch user location from freegeoip.net
Diffstat (limited to 'app/lib/backend.js')
| -rw-r--r-- | app/lib/backend.js | 25 |
1 files changed, 24 insertions, 1 deletions
diff --git a/app/lib/backend.js b/app/lib/backend.js index 158828ab62..cf0c4258ae 100644 --- a/app/lib/backend.js +++ b/app/lib/backend.js @@ -4,7 +4,7 @@ import { EventEmitter } from 'events'; import { servers } from '../config'; import { ConnectionState as ReduxConnectionState } from '../enums'; -const EventType = Enum('connect', 'connecting', 'disconnect', 'login', 'logging', 'logout', 'updatedIp'); +const EventType = Enum('connect', 'connecting', 'disconnect', 'login', 'logging', 'logout', 'updatedIp', 'updatedLocation'); const ConnectionState = Enum('disconnected', 'connecting', 'connected'); /** @@ -204,11 +204,34 @@ export default class Backend extends EventEmitter { // @TODO: Add disconnect call } + _fetchLocation() { + return fetch('https://freegeoip.net/json/').then((res) => { + return res.json(); + }); + } + refreshIp() { + if(this._connStatus === ConnectionState.disconnected) { + this._fetchLocation().then((res) => { + console.log('Got location data: ', res); + const data = { + location: [ res.latitude, res.longitude ], // lat, lng + city: res.city, + country: res.country_name + }; + this.emit(EventType.updatedLocation, data); + this.emit(EventType.updatedIp, res.ip); + }).catch((error) => { + console.log('Got error: ', error); + }); + return; + } + let ip = []; for(let i = 0; i < 4; i++) { ip.push(parseInt(Math.random() * 253 + 1)); } + this.emit(EventType.updatedIp, ip.join('.')); } } |
