summaryrefslogtreecommitdiffhomepage
path: root/app/lib
diff options
context:
space:
mode:
authorAndrej Mihajlov <and@codeispoetry.ru>2017-02-22 17:16:54 +0000
committerAndrej Mihajlov <and@codeispoetry.ru>2017-02-22 17:16:54 +0000
commit95e62eecce28cc4d387c3314211ef3454444b821 (patch)
tree3a9ad50d6b4be1351e52fe43e6cd6015d9cf2da9 /app/lib
parent512f02f109024675a6b27d002c51ce511423c7b7 (diff)
downloadmullvadvpn-95e62eecce28cc4d387c3314211ef3454444b821.tar.xz
mullvadvpn-95e62eecce28cc4d387c3314211ef3454444b821.zip
- Add cancellation logic
- Simulate IP address change - Switch buttons in footer when connecting
Diffstat (limited to 'app/lib')
-rw-r--r--app/lib/backend.js38
1 files changed, 36 insertions, 2 deletions
diff --git a/app/lib/backend.js b/app/lib/backend.js
index 496194d7d4..095f1a9eba 100644
--- a/app/lib/backend.js
+++ b/app/lib/backend.js
@@ -1,7 +1,7 @@
import Enum from './enum';
import { EventEmitter } from 'events';
-const EventType = Enum('connect', 'connecting', 'disconnect', 'login', 'logging', 'logout');
+const EventType = Enum('connect', 'connecting', 'disconnect', 'login', 'logging', 'logout', 'updatedIp');
/**
* Backend implementation
@@ -17,6 +17,10 @@ export default class Backend extends EventEmitter {
this._account = null;
this._loggedIn = false;
this._serverAddress = null;
+ this._cancellationHandler = null;
+
+ // update IP in background
+ setTimeout(::this.refreshIp, 0);
}
// Accessors
@@ -60,22 +64,52 @@ export default class Backend extends EventEmitter {
this.emit(EventType.connecting, addr);
// @TODO: Add connect call
- setTimeout(() => {
+ let timer = null;
+
+ timer = setTimeout(() => {
let err;
if(!/se\d+\.mullvad\.net/.test(addr)) {
err = new Error('Server is unreachable');
}
// emit: connect
this.emit(EventType.connect, addr, err);
+ this.refreshIp();
+
+ // reset timer
+ timer = null;
+ this._cancellationHandler = null;
}, 5000);
+
+ this._cancellationHandler = () => {
+ if(timer !== null) {
+ clearTimeout(timer);
+ this._timer = null;
+ }
+ this._cancellationHandler = null;
+ }
}
disconnect() {
this._serverAddress = null;
+ // cancel ongoing connection attempt
+ if(this._cancellationHandler) {
+ this._cancellationHandler();
+ } else {
+ this.refreshIp();
+ }
+
// emit: disconnect
this.emit(EventType.disconnect);
// @TODO: Add disconnect call
}
+
+ refreshIp() {
+ let ip = [];
+ for(let i = 0; i < 4; i++) {
+ ip.push(parseInt(Math.random() * 254));
+ }
+ this.emit(EventType.updatedIp, ip.join('.'));
+ }
}