Skip to content

Commit

Permalink
More i2p implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
everoddandeven committed Feb 12, 2025
1 parent 1a18e61 commit 74b0d66
Show file tree
Hide file tree
Showing 36 changed files with 1,854 additions and 685 deletions.
54 changes: 44 additions & 10 deletions app/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ import { app, BrowserWindow, ipcMain, screen, dialog, Tray, Menu, MenuItemConstr
import * as path from 'path';
import * as fs from 'fs';
import * as os from 'os';
import axios, { AxiosRequestConfig } from 'axios';
import axios, { AxiosError, AxiosRequestConfig } from 'axios';
import { AppMainProcess, I2pdProcess, MonerodProcess, PrivateTestnet } from './process';
import { BatteryUtils, FileUtils, NetworkUtils } from './utils';
import { MoneroI2pdProcess } from './process/I2pdProcess';

app.setName('Monero Daemon');

Expand Down Expand Up @@ -395,6 +396,13 @@ async function monitorMonerod(): Promise<void> {

// #region Download Utils

async function detectInstallation(program: string): Promise<any> {
if (program === 'i2pd') {
return await I2pdProcess.detectInstalled();
}
else return undefined;
}

async function downloadAndVerifyHash(hashUrl: string, fileName: string, filePath: string): Promise<boolean> {
const hashFileName = await FileUtils.downloadFile(hashUrl, app.getPath('temp'), () => {});
const hashFilePath = `${app.getPath('temp')}/${hashFileName}`;
Expand Down Expand Up @@ -529,8 +537,14 @@ try {

// #endregion

ipcMain.handle('start-i2pd', async (event: IpcMainInvokeEvent, params: { eventId: string; path: string, flags: string[] }) => {
const { eventId, path, flags } = params;
ipcMain.handle('detect-installation', async (event: IpcMainInvokeEvent, params: { eventId: string; program: string }) => {
const { eventId, program } = params;

win?.webContents.send(eventId, await detectInstallation(program));
});

ipcMain.handle('start-i2pd', async (event: IpcMainInvokeEvent, params: { eventId: string; path: string }) => {
const { eventId, path } = params;

let error: string | undefined = undefined;

Expand All @@ -542,7 +556,8 @@ try {
}
else {
try {
i2pdProcess = new I2pdProcess({ i2pdPath: path, flags, isExe: true });
//i2pdProcess = new I2pdProcess({ i2pdPath: path, flags, isExe: true });
i2pdProcess = MoneroI2pdProcess.createSimple(path);
await i2pdProcess.start();
i2pdProcess.onStdOut((out: string) => win?.webContents.send('on-ip2d-stdout', out));
i2pdProcess.onStdErr((out: string) => win?.webContents.send('on-ip2d-stderr', out));
Expand Down Expand Up @@ -578,7 +593,11 @@ try {

ipcMain.handle('is-on-battery-power', async (event: IpcMainInvokeEvent, params: { eventId: string; }) => {
const onBattery = await BatteryUtils.isOnBatteryPower();
win?.webContents.send(params.eventId, onBattery);
if (!win) {
console.warn("is-on-battery-power: window not set");
return;
}
win.webContents.send(params.eventId, onBattery);
});

powerMonitor.on('on-ac', () => win?.webContents.send('on-ac'));
Expand All @@ -603,6 +622,8 @@ try {
}
else await monerodProcess.stop();
monerodProcess = null;

if (i2pdProcess && i2pdProcess.running) await i2pdProcess.stop();
}
}
catch (error: any) {
Expand Down Expand Up @@ -903,10 +924,16 @@ try {
win?.webContents.send(eventId, { data: result.data, code: result.status, status: result.statusText });
}
catch (error: any) {
console.error("post(): ", error);
win?.webContents.send(eventId, { error: `${error}` });
}
let msg: string;
if (error instanceof AxiosError) {
msg = error.message
}
else msg = `${error}`;

console.error(msg);

win?.webContents.send(eventId, { error: msg });
}
});

ipcMain.handle('http-get', async (event: IpcMainInvokeEvent, params: { id: string; url: string; config?: AxiosRequestConfig<any> }) => {
Expand All @@ -918,8 +945,15 @@ try {
win?.webContents.send(eventId, { data: result.data, code: result.status, status: result.statusText });
}
catch (error: any) {
console.error("get(): ", error);
win?.webContents.send(eventId, { error: `${error}` });
let msg: string;
if (error instanceof AxiosError) {
msg = error.message
}
else msg = `${error}`;

console.error(msg);

win?.webContents.send(eventId, { error: msg });
}

});
Expand Down
43 changes: 23 additions & 20 deletions app/preload.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,42 @@ function newId() {
}

contextBridge.exposeInMainWorld('electronAPI', {
detectInstallation: (program, callback) => {
const eventId = `on-detect-installation-${newId()}`;

const handler = (event, result) => {
callback(result);
};

ipcRenderer.once(eventId, handler);
ipcRenderer.invoke('detect-installation', { eventId, program });
},
checkValidI2pdPath: (path, callback) => {
const eventId = `on-check-valid-i2pd-path-${newId()}`;

const handler = (event, result) => {
ipcRenderer.off(eventId, handler);
callback(result);
};

ipcRenderer.on(eventId, handler);
ipcRenderer.once(eventId, handler);
ipcRenderer.invoke('check-valid-i2pd-path', { eventId, path });
},
startI2pd: (path, flags, callback) => {
startI2pd: (path, callback) => {
const eventId = `on-start-i2pd-${newId()}`;
const handler = (event, result) => {
ipcRenderer.off(eventId, handler);
callback(result);
};

ipcRenderer.on(eventId, handler);
ipcRenderer.invoke('start-i2pd', { eventId, path, flags });
ipcRenderer.once(eventId, handler);
ipcRenderer.invoke('start-i2pd', { eventId, path });
},
stopI2pd: (callback) => {
const eventId = `on-stop-i2pd-${newId()}`;
const handler = (event, result) => {
ipcRenderer.off(eventId, handler);
callback(result);
};

ipcRenderer.on(eventId, handler);
ipcRenderer.once(eventId, handler);
ipcRenderer.invoke('stop-i2pd', { eventId });
},
onI2pdOutput: (callback) => {
Expand All @@ -59,11 +66,10 @@ contextBridge.exposeInMainWorld('electronAPI', {
const eventId = `on-http-post-result-${id}`;

const handler = (event, result) => {
ipcRenderer.off(eventId, handler);
callback(result);
};

ipcRenderer.on(eventId, handler);
ipcRenderer.once(eventId, handler);
ipcRenderer.invoke('http-post', params);
},
httpGet: (params, callback) => {
Expand All @@ -73,30 +79,28 @@ contextBridge.exposeInMainWorld('electronAPI', {
const eventId = `on-http-get-result-${id}`;

const handler = (event, result) => {
ipcRenderer.off(eventId, handler);
callback(result);
};

ipcRenderer.on(eventId, handler);
ipcRenderer.once(eventId, handler);
ipcRenderer.invoke('http-get', params);
},
getBatteryLevel: (callback) => {
const eventId = `on-get-battery-level-${newId()};`
const eventId = `on-get-battery-level-${newId()}`;
const handler = (event, result) => {
ipcRenderer.off(eventId, handler);
callback(result);
};
ipcRenderer.on(eventId, handler);
ipcRenderer.once(eventId, handler);
ipcRenderer.invoke('get-battery-level', { eventId });
},
isOnBatteryPower: (callback) => {
const eventId = `on-is-on-battery-power-${newId()};`
//const eventId = `on-is-on-battery-power-${newId()}`;
const eventId = 'on-is-on-battery-power';
const handler = (event, result) => {
ipcRenderer.off(eventId);
callback(result);
};

ipcRenderer.on('on-is-on-battery-power', handler);
ipcRenderer.once(eventId, handler);
ipcRenderer.invoke('is-on-battery-power', { eventId });
},
onAc: (callback) => {
Expand Down Expand Up @@ -272,11 +276,10 @@ contextBridge.exposeInMainWorld('electronAPI', {
},
quit: (callback) => {
const handler = (event, result) => {
ipcRenderer.off('on-quit', handler);
callback(result);
};

ipcRenderer.on('on-quit', handler);
ipcRenderer.once('on-quit', handler);
ipcRenderer.invoke('quit');
},
enableAutoLaunch: (minimized) => {
Expand Down
Loading

0 comments on commit 74b0d66

Please sign in to comment.