diff options
author | vantezzen <hello@vantezzen.io> | 2019-10-27 10:17:47 +0100 |
---|---|---|
committer | vantezzen <hello@vantezzen.io> | 2019-10-27 10:17:47 +0100 |
commit | a0ac009c44b210df4d79ffedbdd892fe7302d22e (patch) | |
tree | e74b4aa156a6da0311d5fe82b954ea76723c4919 /src | |
parent | Improve hero picture (diff) | |
download | ferdium-app-a0ac009c44b210df4d79ffedbdd892fe7302d22e.tar.gz ferdium-app-a0ac009c44b210df4d79ffedbdd892fe7302d22e.tar.zst ferdium-app-a0ac009c44b210df4d79ffedbdd892fe7302d22e.zip |
#146 Add possible fix for 1.1.1.1 hack
Diffstat (limited to 'src')
-rw-r--r-- | src/api/apiBase.js | 21 | ||||
-rw-r--r-- | src/api/server/ServerApi.js | 23 | ||||
-rw-r--r-- | src/config.js | 1 | ||||
-rw-r--r-- | src/stores/AppStore.js | 4 | ||||
-rw-r--r-- | src/stores/FeaturesStore.js | 5 | ||||
-rw-r--r-- | src/stores/UserStore.js | 13 | ||||
-rw-r--r-- | src/stores/lib/CachedRequest.js | 4 |
7 files changed, 48 insertions, 23 deletions
diff --git a/src/api/apiBase.js b/src/api/apiBase.js index 561b025f0..85dd0f3df 100644 --- a/src/api/apiBase.js +++ b/src/api/apiBase.js | |||
@@ -6,25 +6,19 @@ import { | |||
6 | } from '../environment'; | 6 | } from '../environment'; |
7 | import { | 7 | import { |
8 | LOCAL_SERVER, | 8 | LOCAL_SERVER, |
9 | SERVER_NOT_LOADED, | ||
9 | } from '../config'; | 10 | } from '../config'; |
10 | 11 | ||
11 | const apiBase = () => { | 12 | const apiBase = (withVersion = true) => { |
12 | let url; | 13 | let url; |
13 | 14 | ||
14 | if (!window.ferdi | 15 | if (!window.ferdi |
15 | || !window.ferdi.stores.settings | 16 | || !window.ferdi.stores.settings |
16 | || !window.ferdi.stores.settings.all | 17 | || !window.ferdi.stores.settings.all |
17 | || !window.ferdi.stores.settings.all.app.server) { | 18 | || !window.ferdi.stores.settings.all.app.server) { |
18 | // Stores have not yet been loaded - send invalid URL to force a retry when stores are loaded | 19 | // Stores have not yet been loaded - return SERVER_NOT_LOADED to force a retry when stores are loaded |
19 | // "Why 1.1.1.1 as the default, invalid URL?" | 20 | return SERVER_NOT_LOADED; |
20 | // 1.1.1.1 is the server for Cloudflare's DNS service and will be the same across most networks. | 21 | } if (window.ferdi.stores.settings.all.app.server === LOCAL_SERVER) { |
21 | // Using a random IP could result in unwanted connections, using localhost could unwantedly | ||
22 | // connect to local develoment servers. | ||
23 | // 1.1.1.1 also sends a status 400 response for invalid routes. Other servers may return status 401 | ||
24 | // on some routes. This would result in Ferdi deleting its current authToken as it thinks it | ||
25 | // has gone invalid. | ||
26 | url = 'https://1.1.1.1'; | ||
27 | } else if (window.ferdi.stores.settings.all.app.server === LOCAL_SERVER) { | ||
28 | // Use URL for local server | 22 | // Use URL for local server |
29 | url = `http://127.0.0.1:${window.ferdi.stores.requests.localServerPort}`; | 23 | url = `http://127.0.0.1:${window.ferdi.stores.requests.localServerPort}`; |
30 | } else { | 24 | } else { |
@@ -32,7 +26,10 @@ const apiBase = () => { | |||
32 | url = window.ferdi.stores.settings.all.app.server; | 26 | url = window.ferdi.stores.settings.all.app.server; |
33 | } | 27 | } |
34 | 28 | ||
35 | return `${url}/${API_VERSION}`; | 29 | if (withVersion) { |
30 | return `${url}/${API_VERSION}`; | ||
31 | } | ||
32 | return url; | ||
36 | }; | 33 | }; |
37 | 34 | ||
38 | export default apiBase; | 35 | export default apiBase; |
diff --git a/src/api/server/ServerApi.js b/src/api/server/ServerApi.js index a5d636b4e..164bc237b 100644 --- a/src/api/server/ServerApi.js +++ b/src/api/server/ServerApi.js | |||
@@ -14,8 +14,7 @@ import OrderModel from '../../models/Order'; | |||
14 | 14 | ||
15 | import { sleep } from '../../helpers/async-helpers'; | 15 | import { sleep } from '../../helpers/async-helpers'; |
16 | 16 | ||
17 | import { API } from '../../environment'; | 17 | import { RECIPES_PATH, SERVER_NOT_LOADED } from '../../config'; |
18 | import { RECIPES_PATH } from '../../config'; | ||
19 | import apiBase from '../apiBase'; | 18 | import apiBase from '../apiBase'; |
20 | import { prepareAuthRequest, sendAuthRequest } from '../utils/auth'; | 19 | import { prepareAuthRequest, sendAuthRequest } from '../utils/auth'; |
21 | 20 | ||
@@ -39,8 +38,6 @@ module.paths.unshift( | |||
39 | const { app } = remote; | 38 | const { app } = remote; |
40 | const { default: fetch } = remote.require('electron-fetch'); | 39 | const { default: fetch } = remote.require('electron-fetch'); |
41 | 40 | ||
42 | const SERVER_URL = API; | ||
43 | |||
44 | export default class ServerApi { | 41 | export default class ServerApi { |
45 | recipePreviews = []; | 42 | recipePreviews = []; |
46 | 43 | ||
@@ -121,6 +118,10 @@ export default class ServerApi { | |||
121 | } | 118 | } |
122 | 119 | ||
123 | async userInfo() { | 120 | async userInfo() { |
121 | if (apiBase() === SERVER_NOT_LOADED) { | ||
122 | throw new Error('Server not loaded'); | ||
123 | } | ||
124 | |||
124 | const request = await sendAuthRequest(`${apiBase()}/me`); | 125 | const request = await sendAuthRequest(`${apiBase()}/me`); |
125 | if (!request.ok) { | 126 | if (!request.ok) { |
126 | throw request; | 127 | throw request; |
@@ -163,6 +164,10 @@ export default class ServerApi { | |||
163 | 164 | ||
164 | // Services | 165 | // Services |
165 | async getServices() { | 166 | async getServices() { |
167 | if (apiBase() === SERVER_NOT_LOADED) { | ||
168 | throw new Error('Server not loaded'); | ||
169 | } | ||
170 | |||
166 | const request = await sendAuthRequest(`${apiBase()}/me/services`); | 171 | const request = await sendAuthRequest(`${apiBase()}/me/services`); |
167 | if (!request.ok) { | 172 | if (!request.ok) { |
168 | throw request; | 173 | throw request; |
@@ -287,6 +292,10 @@ export default class ServerApi { | |||
287 | } | 292 | } |
288 | 293 | ||
289 | async getFeatures() { | 294 | async getFeatures() { |
295 | if (apiBase() === SERVER_NOT_LOADED) { | ||
296 | throw new Error('Server not loaded'); | ||
297 | } | ||
298 | |||
290 | const request = await sendAuthRequest(`${apiBase()}/features`); | 299 | const request = await sendAuthRequest(`${apiBase()}/features`); |
291 | if (!request.ok) { | 300 | if (!request.ok) { |
292 | throw request; | 301 | throw request; |
@@ -466,7 +475,11 @@ export default class ServerApi { | |||
466 | 475 | ||
467 | // Health Check | 476 | // Health Check |
468 | async healthCheck() { | 477 | async healthCheck() { |
469 | const request = await sendAuthRequest(`${SERVER_URL}/health`, { | 478 | if (apiBase() === SERVER_NOT_LOADED) { |
479 | throw new Error('Server not loaded'); | ||
480 | } | ||
481 | |||
482 | const request = await sendAuthRequest(`${apiBase(false)}/health`, { | ||
470 | method: 'GET', | 483 | method: 'GET', |
471 | }, false); | 484 | }, false); |
472 | if (!request.ok) { | 485 | if (!request.ok) { |
diff --git a/src/config.js b/src/config.js index 761d26eea..1db881d35 100644 --- a/src/config.js +++ b/src/config.js | |||
@@ -112,6 +112,7 @@ export const FILE_SYSTEM_SETTINGS_TYPES = [ | |||
112 | ]; | 112 | ]; |
113 | 113 | ||
114 | export const LOCAL_SERVER = 'You are using Ferdi without a server'; | 114 | export const LOCAL_SERVER = 'You are using Ferdi without a server'; |
115 | export const SERVER_NOT_LOADED = 'Ferdi::SERVER_NOT_LOADED'; | ||
115 | 116 | ||
116 | export const SETTINGS_PATH = path.join(app.getPath('userData'), 'config'); | 117 | export const SETTINGS_PATH = path.join(app.getPath('userData'), 'config'); |
117 | 118 | ||
diff --git a/src/stores/AppStore.js b/src/stores/AppStore.js index 894c19347..124c117b0 100644 --- a/src/stores/AppStore.js +++ b/src/stores/AppStore.js | |||
@@ -181,7 +181,9 @@ export default class AppStore extends Store { | |||
181 | 181 | ||
182 | this.locale = this._getDefaultLocale(); | 182 | this.locale = this._getDefaultLocale(); |
183 | 183 | ||
184 | this._healthCheck(); | 184 | setTimeout(() => { |
185 | this._healthCheck(); | ||
186 | }, 1000); | ||
185 | 187 | ||
186 | this.isSystemDarkModeEnabled = systemPreferences.isDarkMode(); | 188 | this.isSystemDarkModeEnabled = systemPreferences.isDarkMode(); |
187 | 189 | ||
diff --git a/src/stores/FeaturesStore.js b/src/stores/FeaturesStore.js index ab5d762c7..780cde3a7 100644 --- a/src/stores/FeaturesStore.js +++ b/src/stores/FeaturesStore.js | |||
@@ -57,7 +57,10 @@ export default class FeaturesStore extends Store { | |||
57 | _updateFeatures = () => { | 57 | _updateFeatures = () => { |
58 | const features = Object.assign({}, DEFAULT_FEATURES_CONFIG); | 58 | const features = Object.assign({}, DEFAULT_FEATURES_CONFIG); |
59 | if (this.stores.user.isLoggedIn) { | 59 | if (this.stores.user.isLoggedIn) { |
60 | const requestResult = this.featuresRequest.execute().result; | 60 | let requestResult = {}; |
61 | try { | ||
62 | requestResult = this.featuresRequest.execute().result; | ||
63 | } catch (e) {} // eslint-disable-line no-empty | ||
61 | Object.assign(features, requestResult); | 64 | Object.assign(features, requestResult); |
62 | } | 65 | } |
63 | runInAction('FeaturesStore::_updateFeatures', () => { | 66 | runInAction('FeaturesStore::_updateFeatures', () => { |
diff --git a/src/stores/UserStore.js b/src/stores/UserStore.js index d6a2e5fde..ec0b0cf8d 100644 --- a/src/stores/UserStore.js +++ b/src/stores/UserStore.js | |||
@@ -348,7 +348,12 @@ export default class UserStore extends Store { | |||
348 | // Reactions | 348 | // Reactions |
349 | async _getUserData() { | 349 | async _getUserData() { |
350 | if (this.isLoggedIn) { | 350 | if (this.isLoggedIn) { |
351 | const data = await this.getUserInfoRequest.execute()._promise; | 351 | let data; |
352 | try { | ||
353 | data = await this.getUserInfoRequest.execute()._promise; | ||
354 | } catch (e) { | ||
355 | return false; | ||
356 | } | ||
352 | 357 | ||
353 | // We need to set the beta flag for the SettingsStore | 358 | // We need to set the beta flag for the SettingsStore |
354 | this.actions.settings.update({ | 359 | this.actions.settings.update({ |
@@ -408,7 +413,11 @@ export default class UserStore extends Store { | |||
408 | } | 413 | } |
409 | 414 | ||
410 | async _migrateUserLocale() { | 415 | async _migrateUserLocale() { |
411 | await this.getUserInfoRequest._promise; | 416 | try { |
417 | await this.getUserInfoRequest._promise; | ||
418 | } catch (e) { | ||
419 | return false; | ||
420 | } | ||
412 | 421 | ||
413 | if (!this.data.locale) { | 422 | if (!this.data.locale) { |
414 | debug('Migrate "locale" to user data'); | 423 | debug('Migrate "locale" to user data'); |
diff --git a/src/stores/lib/CachedRequest.js b/src/stores/lib/CachedRequest.js index ac8b2bd81..31c7ce241 100644 --- a/src/stores/lib/CachedRequest.js +++ b/src/stores/lib/CachedRequest.js | |||
@@ -39,7 +39,7 @@ export default class CachedRequest extends Request { | |||
39 | }), 0); | 39 | }), 0); |
40 | 40 | ||
41 | // Issue api call & save it as promise that is handled to update the results of the operation | 41 | // Issue api call & save it as promise that is handled to update the results of the operation |
42 | this._promise = new Promise((resolve, reject) => { | 42 | this._promise = new Promise((resolve) => { |
43 | this._api[this._method](...callArgs) | 43 | this._api[this._method](...callArgs) |
44 | .then((result) => { | 44 | .then((result) => { |
45 | setTimeout(action(() => { | 45 | setTimeout(action(() => { |
@@ -63,7 +63,7 @@ export default class CachedRequest extends Request { | |||
63 | this.wasExecuted = true; | 63 | this.wasExecuted = true; |
64 | this._isWaitingForResponse = false; | 64 | this._isWaitingForResponse = false; |
65 | this._triggerHooks(); | 65 | this._triggerHooks(); |
66 | reject(error); | 66 | // reject(error); |
67 | }), 1); | 67 | }), 1); |
68 | })); | 68 | })); |
69 | }); | 69 | }); |