diff --git a/js/background.js b/js/background.js index a36ef16c8..158ba5155 100644 --- a/js/background.js +++ b/js/background.js @@ -181,14 +181,6 @@ // Update zoom window.updateZoomFactor(); - if ( - window.lokiFeatureFlags.useOnionRequests || - window.lokiFeatureFlags.useFileOnionRequests - ) { - // Initialize paths for onion requests - window.OnionAPI.buildNewOnionPaths(); - } - const currentPoWDifficulty = storage.get('PoWDifficulty', null); if (!currentPoWDifficulty) { storage.put('PoWDifficulty', window.getDefaultPoWDifficulty()); diff --git a/js/expire.js b/js/expire.js index b7f3992c0..23345bdf0 100644 --- a/js/expire.js +++ b/js/expire.js @@ -17,9 +17,23 @@ let nextWaitSeconds = 5; const checkForUpgrades = async () => { + try { + window.libsession.Utils.UserUtils.getOurPubKeyStrFromCache(); + } catch (e) { + // give it a minute + log.warn( + 'Could not check to see if newer version is available cause our pubkey is not set' + ); + nextWaitSeconds = 60; + setTimeout(async () => { + await checkForUpgrades(); + }, nextWaitSeconds * 1000); // wait a minute + return; + } const result = await window.tokenlessFileServerAdnAPI.serverRequest( 'loki/v1/version/client/desktop' ); + if ( result && result.response && @@ -133,5 +147,4 @@ window.clientClockSynced = Math.abs(timeDifferential) < maxTimeDifferential; return window.clientClockSynced; }; - window.setClockParams(); })(); diff --git a/js/modules/loki_app_dot_net_api.js b/js/modules/loki_app_dot_net_api.js index 3479c097c..942b850a0 100644 --- a/js/modules/loki_app_dot_net_api.js +++ b/js/modules/loki_app_dot_net_api.js @@ -64,7 +64,7 @@ const sendViaOnion = async (srvPubKey, url, fetchOptions, options = {}) => { // eslint-disable-next-line no-param-reassign options.retry = 0; // eslint-disable-next-line no-param-reassign - options.requestNumber = window.OnionAPI.assignOnionRequestNumber(); + options.requestNumber = window.OnionPaths.getInstance().assignOnionRequestNumber(); } const payloadObj = { @@ -97,7 +97,7 @@ const sendViaOnion = async (srvPubKey, url, fetchOptions, options = {}) => { let pathNodes = []; try { - pathNodes = await window.OnionAPI.getOnionPath(); + pathNodes = await window.OnionPaths.getInstance().getOnionPath(); } catch (e) { log.error( `loki_app_dot_net:::sendViaOnion #${options.requestNumber} - getOnionPath Error ${e.code} ${e.message}` diff --git a/preload.js b/preload.js index d2842f020..78e302266 100644 --- a/preload.js +++ b/preload.js @@ -395,7 +395,7 @@ window.clipboard = clipboard; window.seedNodeList = JSON.parse(config.seedNodeList); -const { OnionAPI } = require('./ts/session/onions'); +const { OnionPaths } = require('./ts/session/onions'); const { locale } = config; window.i18n = i18n.setup(locale, localeMessages); @@ -413,7 +413,7 @@ window.moment.updateLocale(localeForMoment, { }); window.moment.locale(localeForMoment); -window.OnionAPI = OnionAPI; +window.OnionPaths = OnionPaths; window.libsession = require('./ts/session'); window.models = require('./ts/models'); diff --git a/ts/components/session/ActionsPanel.tsx b/ts/components/session/ActionsPanel.tsx index e5b23b4ff..c7a72cefa 100644 --- a/ts/components/session/ActionsPanel.tsx +++ b/ts/components/session/ActionsPanel.tsx @@ -19,6 +19,7 @@ import { } from '../../session/utils/syncUtils'; import { DAYS } from '../../session/utils/Number'; import { removeItemById } from '../../data/data'; +import { OnionPaths } from '../../session/onions'; // tslint:disable-next-line: no-import-side-effect no-submodule-imports export enum SectionType { @@ -54,6 +55,15 @@ class ActionsPanelPrivate extends React.Component { // fetch the user saved theme from the db, and apply it on mount. public componentDidMount() { + void window.setClockParams(); + if ( + window.lokiFeatureFlags.useOnionRequests || + window.lokiFeatureFlags.useFileOnionRequests + ) { + // Initialize paths for onion requests + void OnionPaths.getInstance().buildNewOnionPaths(); + } + const theme = window.Events.getThemeSetting(); window.setTheme(theme); diff --git a/ts/session/onions/index.ts b/ts/session/onions/index.ts index 1895ae0aa..a3b5b6e4a 100644 --- a/ts/session/onions/index.ts +++ b/ts/session/onions/index.ts @@ -12,7 +12,9 @@ interface SnodePath { bad: boolean; } -class OnionPaths { +export class OnionPaths { + private static instance: OnionPaths | null; + private onionPaths: Array = []; // This array is meant to store nodes will full info, @@ -20,6 +22,15 @@ class OnionPaths { // some naming issue here it seems) private guardNodes: Array = []; private onionRequestCounter = 0; // Request index for debugging + private constructor() {} + + public static getInstance() { + if (OnionPaths.instance) { + return OnionPaths.instance; + } + OnionPaths.instance = new OnionPaths(); + return OnionPaths.instance; + } public async buildNewOnionPaths() { // this function may be called concurrently make sure we only have one inflight @@ -304,5 +315,3 @@ class OnionPaths { log.info(`Built ${this.onionPaths.length} onion paths`, this.onionPaths); } } - -export const OnionAPI = new OnionPaths(); diff --git a/ts/session/snode_api/onions.ts b/ts/session/snode_api/onions.ts index 34d6df983..49efb13bc 100644 --- a/ts/session/snode_api/onions.ts +++ b/ts/session/snode_api/onions.ts @@ -4,7 +4,7 @@ import https from 'https'; import { Snode } from './snodePool'; import ByteBuffer from 'bytebuffer'; import { StringUtils } from '../utils'; -import { OnionAPI } from '../onions'; +import { OnionPaths } from '../onions'; let onionPayload = 0; @@ -522,8 +522,8 @@ export async function lokiOnionFetch( while (true) { // Get a path excluding `targetNode`: // eslint-disable-next-line no-await-in-loop - const path = await OnionAPI.getOnionPath(targetNode); - const thisIdx = OnionAPI.assignOnionRequestNumber(); + const path = await OnionPaths.getInstance().getOnionPath(targetNode); + const thisIdx = OnionPaths.getInstance().assignOnionRequestNumber(); // At this point I only care about BAD_PATH @@ -541,7 +541,7 @@ export async function lokiOnionFetch( targetNode.port }` ); - OnionAPI.markPathAsBad(path); + OnionPaths.getInstance().markPathAsBad(path); return false; } else if (result === RequestError.OTHER) { // could mean, fail to parse results