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 ++++++++++++++++++----- 2 files changed, 27 insertions(+), 17 deletions(-) (limited to 'src/api') 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) { -- cgit v1.2.3-54-g00ecf