aboutsummaryrefslogtreecommitdiffstats
path: root/src/api
diff options
context:
space:
mode:
authorLibravatar vantezzen <hello@vantezzen.io>2019-10-27 10:17:47 +0100
committerLibravatar vantezzen <hello@vantezzen.io>2019-10-27 10:17:47 +0100
commita0ac009c44b210df4d79ffedbdd892fe7302d22e (patch)
treee74b4aa156a6da0311d5fe82b954ea76723c4919 /src/api
parentImprove hero picture (diff)
downloadferdium-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/api')
-rw-r--r--src/api/apiBase.js21
-rw-r--r--src/api/server/ServerApi.js23
2 files changed, 27 insertions, 17 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';
7import { 7import {
8 LOCAL_SERVER, 8 LOCAL_SERVER,
9 SERVER_NOT_LOADED,
9} from '../config'; 10} from '../config';
10 11
11const apiBase = () => { 12const 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
38export default apiBase; 35export 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
15import { sleep } from '../../helpers/async-helpers'; 15import { sleep } from '../../helpers/async-helpers';
16 16
17import { API } from '../../environment'; 17import { RECIPES_PATH, SERVER_NOT_LOADED } from '../../config';
18import { RECIPES_PATH } from '../../config';
19import apiBase from '../apiBase'; 18import apiBase from '../apiBase';
20import { prepareAuthRequest, sendAuthRequest } from '../utils/auth'; 19import { prepareAuthRequest, sendAuthRequest } from '../utils/auth';
21 20
@@ -39,8 +38,6 @@ module.paths.unshift(
39const { app } = remote; 38const { app } = remote;
40const { default: fetch } = remote.require('electron-fetch'); 39const { default: fetch } = remote.require('electron-fetch');
41 40
42const SERVER_URL = API;
43
44export default class ServerApi { 41export 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) {