From a0ac009c44b210df4d79ffedbdd892fe7302d22e Mon Sep 17 00:00:00 2001 From: vantezzen Date: Sun, 27 Oct 2019 10:17:47 +0100 Subject: #146 Add possible fix for 1.1.1.1 hack --- src/api/apiBase.js | 21 +++++++++------------ src/api/server/ServerApi.js | 23 ++++++++++++++++++----- src/config.js | 1 + src/stores/AppStore.js | 4 +++- src/stores/FeaturesStore.js | 5 ++++- src/stores/UserStore.js | 13 +++++++++++-- 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 { } from '../environment'; import { LOCAL_SERVER, + SERVER_NOT_LOADED, } from '../config'; -const apiBase = () => { +const apiBase = (withVersion = true) => { let url; if (!window.ferdi || !window.ferdi.stores.settings || !window.ferdi.stores.settings.all || !window.ferdi.stores.settings.all.app.server) { - // Stores have not yet been loaded - send invalid URL to force a retry when stores are loaded - // "Why 1.1.1.1 as the default, invalid URL?" - // 1.1.1.1 is the server for Cloudflare's DNS service and will be the same across most networks. - // Using a random IP could result in unwanted connections, using localhost could unwantedly - // connect to local develoment servers. - // 1.1.1.1 also sends a status 400 response for invalid routes. Other servers may return status 401 - // on some routes. This would result in Ferdi deleting its current authToken as it thinks it - // has gone invalid. - url = 'https://1.1.1.1'; - } else if (window.ferdi.stores.settings.all.app.server === LOCAL_SERVER) { + // Stores have not yet been loaded - return SERVER_NOT_LOADED to force a retry when stores are loaded + return SERVER_NOT_LOADED; + } if (window.ferdi.stores.settings.all.app.server === LOCAL_SERVER) { // Use URL for local server url = `http://127.0.0.1:${window.ferdi.stores.requests.localServerPort}`; } else { @@ -32,7 +26,10 @@ const apiBase = () => { url = window.ferdi.stores.settings.all.app.server; } - return `${url}/${API_VERSION}`; + if (withVersion) { + return `${url}/${API_VERSION}`; + } + return url; }; 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'; import { sleep } from '../../helpers/async-helpers'; -import { API } from '../../environment'; -import { RECIPES_PATH } from '../../config'; +import { RECIPES_PATH, SERVER_NOT_LOADED } from '../../config'; import apiBase from '../apiBase'; import { prepareAuthRequest, sendAuthRequest } from '../utils/auth'; @@ -39,8 +38,6 @@ module.paths.unshift( const { app } = remote; const { default: fetch } = remote.require('electron-fetch'); -const SERVER_URL = API; - export default class ServerApi { recipePreviews = []; @@ -121,6 +118,10 @@ export default class ServerApi { } async userInfo() { + if (apiBase() === SERVER_NOT_LOADED) { + throw new Error('Server not loaded'); + } + const request = await sendAuthRequest(`${apiBase()}/me`); if (!request.ok) { throw request; @@ -163,6 +164,10 @@ export default class ServerApi { // Services async getServices() { + if (apiBase() === SERVER_NOT_LOADED) { + throw new Error('Server not loaded'); + } + const request = await sendAuthRequest(`${apiBase()}/me/services`); if (!request.ok) { throw request; @@ -287,6 +292,10 @@ export default class ServerApi { } async getFeatures() { + if (apiBase() === SERVER_NOT_LOADED) { + throw new Error('Server not loaded'); + } + const request = await sendAuthRequest(`${apiBase()}/features`); if (!request.ok) { throw request; @@ -466,7 +475,11 @@ export default class ServerApi { // Health Check async healthCheck() { - const request = await sendAuthRequest(`${SERVER_URL}/health`, { + if (apiBase() === SERVER_NOT_LOADED) { + throw new Error('Server not loaded'); + } + + const request = await sendAuthRequest(`${apiBase(false)}/health`, { method: 'GET', }, false); 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 = [ ]; export const LOCAL_SERVER = 'You are using Ferdi without a server'; +export const SERVER_NOT_LOADED = 'Ferdi::SERVER_NOT_LOADED'; export const SETTINGS_PATH = path.join(app.getPath('userData'), 'config'); 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 { this.locale = this._getDefaultLocale(); - this._healthCheck(); + setTimeout(() => { + this._healthCheck(); + }, 1000); this.isSystemDarkModeEnabled = systemPreferences.isDarkMode(); 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 { _updateFeatures = () => { const features = Object.assign({}, DEFAULT_FEATURES_CONFIG); if (this.stores.user.isLoggedIn) { - const requestResult = this.featuresRequest.execute().result; + let requestResult = {}; + try { + requestResult = this.featuresRequest.execute().result; + } catch (e) {} // eslint-disable-line no-empty Object.assign(features, requestResult); } 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 { // Reactions async _getUserData() { if (this.isLoggedIn) { - const data = await this.getUserInfoRequest.execute()._promise; + let data; + try { + data = await this.getUserInfoRequest.execute()._promise; + } catch (e) { + return false; + } // We need to set the beta flag for the SettingsStore this.actions.settings.update({ @@ -408,7 +413,11 @@ export default class UserStore extends Store { } async _migrateUserLocale() { - await this.getUserInfoRequest._promise; + try { + await this.getUserInfoRequest._promise; + } catch (e) { + return false; + } if (!this.data.locale) { 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 { }), 0); // Issue api call & save it as promise that is handled to update the results of the operation - this._promise = new Promise((resolve, reject) => { + this._promise = new Promise((resolve) => { this._api[this._method](...callArgs) .then((result) => { setTimeout(action(() => { @@ -63,7 +63,7 @@ export default class CachedRequest extends Request { this.wasExecuted = true; this._isWaitingForResponse = false; this._triggerHooks(); - reject(error); + // reject(error); }), 1); })); }); -- cgit v1.2.3-54-g00ecf