make OnionPath a singleton and build path on app Start only

pull/1495/head
Audric Ackermann 4 years ago
parent 7a3a12ccdc
commit 5ab3680903

@ -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());

@ -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();
})();

@ -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}`

@ -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');

@ -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<Props> {
// 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);

@ -12,7 +12,9 @@ interface SnodePath {
bad: boolean;
}
class OnionPaths {
export class OnionPaths {
private static instance: OnionPaths | null;
private onionPaths: Array<SnodePath> = [];
// 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<Snode> = [];
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();

@ -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

Loading…
Cancel
Save