Skip to content

Commit

Permalink
Fix connection issues
Browse files Browse the repository at this point in the history
  • Loading branch information
undyingwraith committed May 28, 2024
1 parent a58cd78 commit 61fc804
Show file tree
Hide file tree
Showing 15 changed files with 82 additions and 64 deletions.
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"@mui/icons-material": "^5.15.15",
"@mui/material": "^5.15.15",
"@preact/signals-react": "^2.0.1",
"helia": "^4.2.1",
"helia": "^4.2.2",
"i18next": "^23.11.2",
"minidenticons": "^4.2.1",
"multiformats": "^13.1.0",
Expand Down
13 changes: 5 additions & 8 deletions packages/core/src/components/pages/AppContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { ProfileSelector } from "../molecules/ProfileSelector";
import { AppBar } from "../organisms/AppBar";
import { LoadScreen } from "../molecules/LoadScreen";
import { SimpleProfileManager } from '../../service/ProfileManager/SimpleProfileManager';
import { IProfileManager } from '../../service/ProfileManager';
import { IProfileManager, RemoteProfileManager } from '../../service/ProfileManager';
import { useTranslation } from '../../hooks/useTranslation';

export interface IAppContext {
Expand Down Expand Up @@ -43,7 +43,6 @@ export function AppContextProvider(props: PropsWithChildren<IAppInit>) {
const _t = useTranslation();

const profileManager = useSignal<IProfileManager | undefined>(undefined);
const node = useSignal<IIpfsService | undefined>(undefined);
const state = useSignal<LoadState>(LoadState.Idle);
const darkMode = useSignal<boolean>(true);

Expand All @@ -59,14 +58,12 @@ export function AppContextProvider(props: PropsWithChildren<IAppInit>) {
state.value = LoadState.Starting;

if (isRemoteProfile(currentProfile)) {
node.value = await props.nodeService.createRemote(currentProfile.url);
profileManager.value = new RemoteProfileManager(currentProfile);
}
if (isInternalProfile(currentProfile)) {
node.value = await props.nodeService.create(currentProfile);
profileManager.value = new SimpleProfileManager(await props.nodeService.create(currentProfile), currentProfile);
}
const manager = new SimpleProfileManager(node.value!, currentProfile);
profileManager.value = manager;
await manager.start();
await profileManager.value!.start();

state.value = LoadState.Ready;
} catch (ex) {
Expand All @@ -81,7 +78,6 @@ export function AppContextProvider(props: PropsWithChildren<IAppInit>) {
state.value = LoadState.Stopping;
try {
await profileManager.value!.stop();
node.value = undefined;
profileManager.value = undefined;
} catch (ex) {
console.error(ex);
Expand Down Expand Up @@ -130,6 +126,7 @@ export function AppContextProvider(props: PropsWithChildren<IAppInit>) {
});

const profile = useComputed(() => profileManager.value?.profile);
const node = useComputed(() => profileManager.value?.ipfs);

return useComputed(() => (
<ThemeProvider theme={theme.value}>
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/createRemoteIpfsService.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { IFileInfo, IIpfsService } from './service';
import { create } from 'kubo-rpc-client';

export async function createRemoteIpfsService(url: string): Promise<IIpfsService> {
export async function createRemoteIpfsService(url?: string): Promise<IIpfsService> {
const node = create({ url: url });
const connString = (await node.config.get('Addresses.Gateway')) as string;
const port = connString.substring(connString.lastIndexOf('/') + 1);
Expand Down
6 changes: 0 additions & 6 deletions packages/core/src/service/INodeService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,4 @@ export interface INodeService {
* @param profile profile to start with.
*/
create(profile?: IInternalProfile): Promise<IIpfsService>;

/**
* Connects to a remote IIpfsService.
* @param url url to connect to.
*/
createRemote(url?: string): Promise<IIpfsService>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import { IProfile } from '../Profile/IProfile';
import { MovieIndexFetcher, SeriesIndexFetcher } from '../indexer';
import { IProfileManager, ProfileManagerState } from "./IProfileManager";

export abstract class BaseProfileManager implements IProfileManager {
protected constructor(public readonly profile: IProfile) {
export abstract class BaseProfileManager<TProfile extends IProfile> implements IProfileManager {
protected constructor(public readonly profile: TProfile) {
for (const lib of this.profile.libraries) {
this.libraries.set(lib.name, new Signal<ILibrary>(lib));
}
Expand Down Expand Up @@ -44,7 +44,7 @@ export abstract class BaseProfileManager implements IProfileManager {

protected abstract stopNode(): Promise<void>;

protected ipfs: IIpfsService | undefined;
public ipfs: IIpfsService | undefined;


private triggerUpdate(): void {
Expand Down
6 changes: 6 additions & 0 deletions packages/core/src/service/ProfileManager/IProfileManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { ReadonlySignal } from '@preact/signals-react';
import { ILibrary } from '../Library';
import { IProfile } from '../Profile';
import { ITask } from '../ITask';
import { IIpfsService } from '../IIpfsService';

/**
* Manages a profile.
Expand Down Expand Up @@ -36,6 +37,11 @@ export interface IProfileManager {
* The profile of the service.
*/
profile: IProfile;

/**
* Ipfs service of the profile if started.
*/
ipfs: undefined | IIpfsService;
}

export enum ProfileManagerState {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import { createRemoteIpfsService } from '../../createRemoteIpfsService';
import { IRemoteProfile } from "../Profile/IRemoteProfile";
import { BaseProfileManager } from "./BaseProfileManager";

export class RemoteProfileManager extends BaseProfileManager {
export class RemoteProfileManager extends BaseProfileManager<IRemoteProfile> {
constructor(profile: IRemoteProfile) {
super(profile);
}

protected startNode() {
return Promise.reject();
protected async startNode() {
this.ipfs = await createRemoteIpfsService(this.profile.url);
}

protected stopNode() {
return Promise.reject();
return Promise.resolve();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { IIpfsService } from '../IIpfsService';
import { IProfile } from '../Profile/IProfile';
import { BaseProfileManager } from './BaseProfileManager';

export class SimpleProfileManager extends BaseProfileManager {
export class SimpleProfileManager extends BaseProfileManager<IProfile> {
constructor(ipfs: IIpfsService, profile: IProfile) {
super(profile);
this.ipfs = ipfs;
Expand Down
8 changes: 4 additions & 4 deletions packages/desktop/electron.vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { resolve } from 'path'
import { defineConfig, externalizeDepsPlugin } from 'electron-vite'
import react from '@vitejs/plugin-react'
import { resolve } from 'path';
import { defineConfig, externalizeDepsPlugin } from 'electron-vite';
import react from '@vitejs/plugin-react';

export default defineConfig({
main: {
Expand Down Expand Up @@ -47,4 +47,4 @@ export default defineConfig({
}),
],
}
})
});
6 changes: 3 additions & 3 deletions packages/desktop/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,18 +58,18 @@
"@preact/signals-react-transform": "^0.3.1",
"@rollup/plugin-commonjs": "^25.0.7",
"@types/express": "^4",
"@types/node": "^20.12.10",
"@types/node": "^20.12.12",
"@types/react": "^18.3.1",
"@types/react-dom": "^18.3.0",
"@vitejs/plugin-react": "^4.2.1",
"electron": "30.0.7",
"electron-builder": "^24.13.3",
"electron-vite": "^2.1.0",
"electron-vite": "^2.2.0",
"eslint": "^8.57.0",
"eslint-plugin-react": "^7.34.1",
"prettier": "^3.2.5",
"typescript": "^5.4.5",
"vite": "^5.0.0",
"vite": "^5.2.11",
"vite-plugin-commonjs": "^0.10.1"
}
}
20 changes: 10 additions & 10 deletions packages/desktop/src/main/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { app, shell, BrowserWindow } from 'electron'
import { join } from 'path'
import { electronApp, optimizer, is } from '@electron-toolkit/utils'
import icon from '../../resources/icon.png?asset'
import { app, shell, BrowserWindow } from 'electron';
import { join } from 'path';
import { electronApp, optimizer, is } from '@electron-toolkit/utils';
import icon from '../../resources/icon.png?asset';
//import { fileURLToPath } from 'url'

process.on('uncaughtException', function (error) {
process.stdout.write(error.name + ': ' + error.message + '\n' + (error.stack != undefined ? error.stack + '\n' : ''));
})
});

function createWindow(): void {
// Create the browser window.
Expand All @@ -19,7 +19,7 @@ function createWindow(): void {
webPreferences: {
nodeIntegration: false,
contextIsolation: true,
preload: join(__dirname, '../preload/index.mjs'),
preload: join(__dirname, '../preload/index.mjs'),
//preload: fileURLToPath(new URL('../preload/index.cjs', import.meta.url)),
sandbox: false, // https://www.electronjs.org/docs/latest/tutorial/esm#summary-esm-support-matrix
},
Expand Down Expand Up @@ -60,7 +60,7 @@ app.whenReady().then(() => {
// see https://github.com/alex8088/electron-toolkit/tree/master/packages/utils
app.on('browser-window-created', (_, window) => {
optimizer.watchWindowShortcuts(window);
})
});

createWindow();

Expand All @@ -69,16 +69,16 @@ app.whenReady().then(() => {
// dock icon is clicked and there are no other windows open.
if (BrowserWindow.getAllWindows().length === 0) createWindow();
});
})
});

// Quit when all windows are closed, except on macOS. There, it's common
// for applications and their menu bar to stay active until the user quits
// explicitly with Cmd + Q.
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit()
app.quit();
}
})
});

// In this file you can include the rest of your app"s specific main process
// code. You can also put them in separate files and require them here.
38 changes: 29 additions & 9 deletions packages/desktop/src/preload/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { noise } from '@chainsafe/libp2p-noise';
import { noise, pureJsCrypto } from '@chainsafe/libp2p-noise';
import { yamux } from '@chainsafe/libp2p-yamux';
import { mplex } from '@libp2p/mplex';
import { bitswap, trustlessGateway } from '@helia/block-brokers';
Expand All @@ -23,13 +23,23 @@ import { CID } from 'multiformats';
import { gossipsub } from '@chainsafe/libp2p-gossipsub';
import { pubsubPeerDiscovery } from '@libp2p/pubsub-peer-discovery';
import { mdns } from '@libp2p/mdns';
import { identify, identifyPush } from '@libp2p/identify';
import { keychain } from '@libp2p/keychain';
import { ping } from '@libp2p/ping';
import { dcutr } from '@libp2p/dcutr';
import { autoNAT } from '@libp2p/autonat';
import { ipnsSelector } from 'ipns/selector';
import { ipnsValidator } from 'ipns/validator';
import { uPnPNAT } from '@libp2p/upnp-nat';
import * as libp2pInfo from 'libp2p/version';

function getProfileFolder(name: string): string {
return `./profiles/${name}`;
}

const nodeService: INodeService = {
async create(profile: IInternalProfile): Promise<IIpfsService> {
const agentVersion = `helia/2.0.0 ${libp2pInfo.name}/${libp2pInfo.version} UserAgent=${process.version}`;
const datastore = new LevelDatastore(`${getProfileFolder(profile.name)}/data`);
await datastore.open();
const blockstore = new FsBlockstore(`${getProfileFolder(profile.name)}/blocks`);
Expand Down Expand Up @@ -74,23 +84,36 @@ const nodeService: INodeService = {
mdns(),
],
connectionEncryption: [
noise(),
noise({
crypto: pureJsCrypto
}),
],
streamMuxers: [
yamux(),
mplex(),
],
services: {
relay: circuitRelayServer({
advertise: true
advertise: true,
}),
dht: kadDHT({
protocol: '/ipfs/kad/1.0.0',
peerInfoMapper: removePrivateAddressesMapper,
allowQueryWithZeroPeers: true,
validators: {
ipns: ipnsValidator
},
selectors: {
ipns: ipnsSelector
}
}),
identify: identify({ agentVersion }),
identifyPush: identifyPush({ agentVersion }),
keychain: keychain(),
ping: ping(),
autoNAT: autoNAT(),
dcutr: dcutr(),
upnp: uPnPNAT(),
pubsub: gossipsub({
allowPublishToZeroTopicPeers: true,
allowPublishToZeroTopicPeers: true,
canRelayMessage: true,
}),
},
Expand All @@ -102,9 +125,6 @@ const nodeService: INodeService = {
});

console.log(helia.libp2p.getMultiaddrs().map(a => a.toString()));
helia.libp2p.addEventListener('peer:connect', console.log);
helia.libp2p.addEventListener('peer:disconnect', console.log);
helia.libp2p.addEventListener('peer:discovery', console.log);

const fs = unixfs(helia);

Expand Down
2 changes: 1 addition & 1 deletion packages/tools/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@
"@types/cli-progress": "^3.11.5",
"@types/fluent-ffmpeg": "^2.1.24",
"@types/yargs": "^17.0.32",
"vite-node": "^1.5.0"
"vite-node": "^1.6.0"
}
}
4 changes: 2 additions & 2 deletions packages/webui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"crypto": "^1.0.1",
"crypto-browserify": "^3.12.0",
"datastore-idb": "^2.1.9",
"helia": "^4.2.1",
"helia": "^4.2.2",
"i18next": "^23.11.2",
"i18next-browser-languagedetector": "^7.2.1",
"ipmc-core": "workspace:^",
Expand All @@ -36,7 +36,7 @@
"@types/react": "^18.3.1",
"@types/react-dom": "^18.3.0",
"typescript": "^5.4.5",
"vite": "^5.0.0",
"vite": "^5.2.11",
"vite-plugin-node-polyfills": "^0.21.0"
},
"packageManager": "yarn@4.0.2"
Expand Down
Loading

0 comments on commit 61fc804

Please sign in to comment.