Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add TCP and Websockets transport #172

Merged
merged 2 commits into from
May 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion apps/box/config/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
"key_path": "",
"listen": [
"/dns4/wrtc-star1.par.dwebops.pub/tcp/443/wss/p2p-webrtc-star",
"/dns4/wrtc-star2.sjc.dwebops.pub/tcp/443/wss/p2p-webrtc-star"
"/dns4/wrtc-star2.sjc.dwebops.pub/tcp/443/wss/p2p-webrtc-star",
"/ip4/0.0.0.0/tcp/4002",
"/ip4/0.0.0.0/tcp/4003/ws"
]
},
"ipfs": {
Expand Down
1 change: 1 addition & 0 deletions apps/box/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"libp2p-webrtc-direct": "~0.7.1",
"libp2p-webrtc-star": "~0.25.0",
"libp2p-websockets": "~0.16.2",
"libp2p-tcp": "^0.17.2",
"multiformats": "~9.6.4",
"openpgp": "~5.2.0",
"orbit-db": "~0.28.3",
Expand Down
4 changes: 3 additions & 1 deletion apps/box/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { resolveLater } from 'async-later';
import debug from 'debug';
import {registerFile} from "./file";
import {libConfig, ipfsConfig} from "./config";
import {registerGraph} from "./graph";
import {registerGraph, getOrbitDb} from "./graph";
import {IPFS_PATH, IPFS_HTTP} from "./const";
import config from "config";
import {create} from "ipfs-http-client";
Expand Down Expand Up @@ -77,6 +77,8 @@ export async function graceful() {
debug('\nStopping server...');
const ipfs = await getIPFS();
const libp2p = await getLibp2p();
const otbitDb = await getOrbitDb();
await otbitDb.stop();
if(!IPFS_HTTP)
await ipfs.stop();
await libp2p.stop()
Expand Down
12 changes: 5 additions & 7 deletions apps/box/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ import config from "config"
import * as fs from "fs"
import peerId from "peer-id"
import {LIBP2P_PATH, FULA_NODES, IPFS_HTTP} from "./const";

import TCP from 'libp2p-tcp'
import WS from 'libp2p-websockets'

const getPeerId = async () => {
if (fs.existsSync(LIBP2P_PATH + '/identity.json')) {
let identity = JSON.parse(fs.readFileSync(LIBP2P_PATH + '/identity.json'));
const identity = JSON.parse(fs.readFileSync(LIBP2P_PATH + '/identity.json'));
return await peerId.createFromJSON(identity)
} else {
let identity = await peerId.create()
Expand Down Expand Up @@ -42,7 +43,7 @@ export const libConfig = async (config: Partial<Libp2pOptions>): Promise<Libp2pO
listen
},
modules: {
transport: [WebRTCStar],
transport: [WebRTCStar, TCP, WS],
streamMuxer: [Mplex],
connEncryption: [NOISE],
peerDiscovery: [Bootstrap],
Expand Down Expand Up @@ -79,10 +80,7 @@ export const libConfig = async (config: Partial<Libp2pOptions>): Promise<Libp2pO

export const ipfsConfig = () => ({
Addresses: {
Swarm: [
'/ip4/0.0.0.0/tcp/4002',
'/ip4/127.0.0.1/tcp/4003/ws'
],
Swarm: listen,
Announce: [],
NoAnnounce: [],
API: '/ip4/127.0.0.1/tcp/5002',
Expand Down
2 changes: 2 additions & 0 deletions libraries/fula-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
"libp2p": "~0.36.2",
"libp2p-mplex": "~0.10.7",
"libp2p-webrtc-star": "~0.25.0",
"libp2p-tcp": "^0.17.2",
"libp2p-websockets": "~0.16.2",
"@chainsafe/libp2p-noise": "~5.0.2",
"events": "~3.3.0"
},
Expand Down
6 changes: 4 additions & 2 deletions libraries/fula-client/scripts/build-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ await build({
'libp2p-webrtc-star',
'libp2p-bootstrap',
'libp2p-mplex',
'libp2p-websockets'
'libp2p-websockets',
'libp2p-tcp'
]
});

Expand Down Expand Up @@ -66,7 +67,8 @@ await build({
'libp2p-bootstrap',
'libp2p-mplex',
'libp2p-websockets',
'wrtc'
'wrtc',
'libp2p-tcp'
]
});

2 changes: 1 addition & 1 deletion libraries/fula-client/scripts/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ await build({
bundle: true,
sourcemap: false,
outfile: 'dist/node/index.cjs',
external: ['wrtc','libp2p-webrtc-star','libp2p-mplex','libp2p','@chainsafe/libp2p-noise']
external: ['wrtc','libp2p-webrtc-star','libp2p-mplex','libp2p','@chainsafe/libp2p-noise','libp2p-tcp','libp2p-websockets']
});


Expand Down
5 changes: 4 additions & 1 deletion libraries/fula-client/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import { NOISE, Noise } from '@chainsafe/libp2p-noise';
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
import Mplex from 'libp2p-mplex';
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
import WS from 'libp2p-websockets'
import { constructorOptions, Libp2pOptions } from 'libp2p';
import { SIG_SERVER } from './constant';
import Protector from "libp2p/src/pnet/index.js"
Expand All @@ -26,7 +29,7 @@ export async function configure(option?:Option): Promise<Libp2pOptions & Partial
listen: SIG_SERVER
},
modules: {
transport: [WebRTCStar],
transport: [WebRTCStar,WS],
streamMuxer: [Mplex],
connEncryption: [NOISE],
connProtector: option?.netSecret?new Protector(option.netSecret):undefined
Expand Down
2 changes: 1 addition & 1 deletion libraries/fula-client/src/constant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Multiaddr } from 'multiaddr';

export const SIG_SERVER = [
'/dns4/wrtc-star1.par.dwebops.pub/tcp/443/wss/p2p-webrtc-star',
'/dns4/wrtc-star2.sjc.dwebops.pub/tcp/443/wss/p2p-webrtc-star'
'/dns4/wrtc-star2.sjc.dwebops.pub/tcp/443/wss/p2p-webrtc-star',
];

export const SIG_MULTIADDRS = SIG_SERVER.map((item) => {
Expand Down
2 changes: 1 addition & 1 deletion libraries/fula-client/tests/fula.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {createClient} from '../src';
import {Status} from "../src/connection";
import {File} from '@web-std/file'

const serverIds = ['12D3KooWPRpVw8od6xtsGTjzoKkQMN2xdEnAssQi8mQeK1VTizKa'];
const serverIds = ['12D3KooWQnEmxzocejZTHHSP2VxWs9z93rzAjkiWo2xhXgaWcBbK'];

const testFile = new File(['test'], 'test', {
lastModified: 1639579330347,
Expand Down