Skip to content

Commit

Permalink
Peer to Peer integration with gateway
Browse files Browse the repository at this point in the history
Pusher protocols
Server abstractions
  • Loading branch information
rennokki committed Mar 24, 2024
1 parent 7f3de02 commit 84726bc
Show file tree
Hide file tree
Showing 93 changed files with 4,192 additions and 583 deletions.
3 changes: 3 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
dist/*
node_modules/*
.nx/*
2 changes: 2 additions & 0 deletions .ipfs/cluster/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*
!.gitignore
2 changes: 2 additions & 0 deletions .ipfs/http-gateway/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*
!.gitignore
4 changes: 4 additions & 0 deletions .ipfs/kubo/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
data/
ipfs/
ipns/
!.gitignore
16 changes: 16 additions & 0 deletions .ipfs/kubo/init.d/001-initialize-kubo.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/sh
set -ex

ipfs config --json Ipns.UsePubsub true
ipfs config --json Pubsub.Enabled true

ipfs config --json Experimental.GatewayOverLibp2p true
ipfs config --json Experimental.Libp2pStreamMounting true
ipfs config --json Experimental.P2pHttpProxy true

ipfs config --json Swarm.RelayClient.Enabled true
ipfs config --json Swarm.RelayService.Enabled true
ipfs config --json Swarm.Transports.Network.Websocket true
ipfs config --bool Swarm.EnableAutoNATService true

ipfs bootstrap rm all
4 changes: 3 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
{
"typescript.tsdk": "node_modules/typescript/lib"
"typescript.tsdk": "node_modules/typescript/lib",
"Lua.diagnostics.globals": ["redis"],
"Lua.diagnostics.disable": ["deprecated"]
}
23 changes: 23 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
FROM --platform=$BUILDPLATFORM node:20-bullseye as build

COPY . /tmp/build

WORKDIR /tmp/build

RUN corepack enable ; \
pnpm install --frozen-lockfile ; \
npx nx run-many --target=lint,test,build --all

FROM --platform=$TARGETPLATFORM node:20-bullseye-slim

COPY --from=build /tmp/build/dist /app/dist/
COPY --from=build /tmp/build/node_modules /app/node_modules/
COPY --from=build /tmp/build/package.json /tmp/build/pnpm-lock.yaml /app/

WORKDIR /app

ENTRYPOINT ["node", "--no-warnings", "--experimental-modules", "--es-module-specifier-resolution=node", "/app/dist/packages/cli/index.js"]

CMD ["ipfs", "start"]

EXPOSE 7001 7001/udp 8001 8001/udp 4001 4001/udp 5001 5001/udp
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,13 @@ soketipfs is a monorepo, and it's composed of the following packages:
- [connections](./packages/connections): Base implementation for Websocket connections handling
- [gossiper](./packages/gossiper): Integration for the Gossip protocol, used to broadcast messages to all nodes in the network
- [p2p](./packages/p2p): libp2p networking implementation using Helia
- [protocols](./packages/protocols): implementation of various protocols, such as Pusher, Ably or MQTT
- [routing](./packages/routing): Unified implementation for HTTP routing or handling custom events in the network
- [server](./packages/server): Ready-to-go & customizable server implemenations for soketipfs
- [utils](./packages/utils): Utility functions used across the packages

#### Pusher

- [pusher-apps](./packages/pusher-apps): Pusher Apps implementation to juggle with the apps definitions
- [pusher-channels](./packages/pusher-channels): Pusher Channels implementation to handle channels and events

Each abstraction has its own place in the soketipfs ecosystem, and they can be used independently or together.
127 changes: 127 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
version: '3'

networks:
libp2p:
driver: bridge

services:
soketi:
build:
context: .
dockerfile: Dockerfile
networks:
- libp2p
ulimits:
nproc: 65535
nofile:
soft: 20000
hard: 40000
environment:
DEBUG: 'libp2p:*'
SOKETI_P2P_SWARM_KEY: |-
/key/swarm/psk/1.0.0/
/base64/
eWPS6sIlS74fXQJgrxtzvBF+zqo7UUg61BYLfAUyJ1CM4/3GHHxCwO9tojUnaqgfMRuGo/n0Km18rtLXeAy5HA==
entrypoint:
- 'node'
- '--no-warnings'
- '--experimental-modules'
- '--es-module-specifier-resolution=node'
- '/app/dist/packages/cli/index.js'
- ipfs
- start
scale: 3
ports:
- 7001-7003:7001

kubo:
image: ipfs/kubo:release
container_name: kubo
volumes:
- ./.ipfs/kubo/data/ipfs:/data/ipfs
- ./.ipfs/kubo/ipfs:/ipfs
- ./.ipfs/kubo/ipns:/ipns
- ./.ipfs/kubo/init.d/001-initialize-kubo.sh:/container-init.d/001-initialize-kubo.sh
networks:
- libp2p
ulimits:
nproc: 65535
nofile:
soft: 20000
hard: 40000
environment:
DEBUG: '*'
IPFS_PATH: /data/ipfs
IPFS_FD_MAX: 10000
# IPFS_HTTP_ROUTERS: 'http://soketi:7001'
IPFS_PROFILE: 'local-discovery'
LIBP2P_FORCE_PNET: '1'
IPFS_SWARM_KEY: |-
/key/swarm/psk/1.0.0/
/base64/
eWPS6sIlS74fXQJgrxtzvBF+zqo7UUg61BYLfAUyJ1CM4/3GHHxCwO9tojUnaqgfMRuGo/n0Km18rtLXeAy5HA==
ports:
- 4001:4001
- 4001:4001/udp
- 5001:5001
- 8080:8080
- 8081:8081

cluster:
image: ipfs/ipfs-cluster:latest
container_name: cluster
depends_on:
- kubo
networks:
- libp2p
ulimits:
nproc: 65535
nofile:
soft: 20000
hard: 40000
environment:
DEBUG: '*'
CLUSTER_PEERNAME: cluster
CLUSTER_SECRET: 'c2b82fedbd69cc89b9fb691b4b4cc9b1966f5ec34717568673b6c19f8fa336b7'
CLUSTER_IPFSHTTP_NODEMULTIADDRESS: /dns4/kubo/tcp/5001
CLUSTER_CRDT_TRUSTEDPEERS: '*' # Trust all peers in Cluster
CLUSTER_RESTAPI_HTTPLISTENMULTIADDRESS: /ip4/0.0.0.0/tcp/9094 # Expose API
CLUSTER_MONITORPINGINTERVAL: 2s # Speed up peer discovery
CLUSTER_ENABLERELAYHOP: 'true'
ports:
# Open API port (allows ipfs-cluster-ctl usage on host)
- '127.0.0.1:9094:9094'
# The cluster swarm port would need to be exposed if this container
# was to connect to cluster peers on other hosts.
# But this is just a testing cluster.
- '9095:9095' # Cluster IPFS Proxy endpoint
- '9096:9096' # Cluster swarm endpoint
volumes:
- ./.ipfs/cluster/data:/data/ipfs-cluster

http_gateway:
image: ghcr.io/ipfs/helia-http-gateway:latest
container_name: http_gateway
networks:
- libp2p
ulimits:
nproc: 65535
nofile:
soft: 20000
hard: 40000
ports:
- 9090:9090
volumes:
- ./.ipfs/http-gateway/data:/data
environment:
DEBUG: '*'
PORT: '9090'
USE_BITSWAP: true
USE_LIBP2P: true
USE_TRUSTLESS_GATEWAYS: true
USE_DELEGATED_ROUTING: true
TRUSTLESS_GATEWAYS: 'http://127.0.0.1:9090'
DELEGATED_ROUTING_V1_HOST: 'http://127.0.0.1:9090'
FILE_BLCOKSTORE_PATH: '/data/blockstore'
FILE_DATASTORE_PATH: '/data/datastore'
ECHO_HEADERS: true
18 changes: 15 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
{
"name": "@soketi/monorepo",
"version": "0.1.1",
"version": "0.1.4",
"license": "MIT",
"scripts": {},
"scripts": {
"cli": "./dist/packages/cli/index.js",
"dc:build": "docker-compose build"
},
"private": true,
"type": "module",
"engines": {
"node": ">=20.0.0 <21.0.0"
},
"optionalDependencies": {
"@nx/nx-linux-arm64-gnu": "17.3.0",
"@nx/nx-linux-arm64-musl": "17.3.0",
"@nx/nx-linux-x64-gnu": "17.3.0",
"@nx/nx-linux-x64-musl": "17.3.0"
},
"dependencies": {
"@chainsafe/libp2p-gossipsub": "^11.1.0",
"@chainsafe/libp2p-noise": "^14.1.0",
Expand All @@ -30,9 +39,10 @@
"blockstore-core": "^4.3.10",
"datastore-core": "^9.2.7",
"helia": "^3.0.1",
"itty-router": "^4.0.27",
"libp2p": "^1.2.1",
"tslib": "^2.6.2",
"uWebSockets.js": "github:uNetworking/uWebSockets.js#v20.39.0",
"uWebSockets.js": "github:uNetworking/uWebSockets.js#v20.41.0",
"uint8arrays": "4.0.10",
"yargs": "^17.7.2"
},
Expand All @@ -46,9 +56,11 @@
"@nx/js": "17.3.0",
"@nx/node": "17.3.0",
"@nx/vite": "17.3.0",
"@nx/web": "17.3.0",
"@nx/workspace": "17.3.0",
"@swc-node/register": "~1.6.8",
"@swc/core": "~1.3.107",
"@swc/helpers": "~0.5.2",
"@types/jest": "^29.5.11",
"@types/node": "18.19.10",
"@types/yargs": "^17.0.32",
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "0.1.4",
"dependencies": {
"@soketi/server": "0.1.4",
"yargs": "*"
"yargs": "^17.7.2"
},
"type": "module",
"main": "./index.js",
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
"main": "packages/cli/src/index.ts",
"tsConfig": "packages/cli/tsconfig.lib.json",
"assets": [],
"external": ["libp2p"],
"format": ["esm", "cjs"],
"external": [],
"format": ["esm"],
"platform": "node",
"bundle": true,
"thirdParty": false,
Expand Down
5 changes: 4 additions & 1 deletion packages/cli/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#!/usr/bin/env node --no-warnings --experimental-modules --es-module-specifier-resolution=node

import('./lib/cli');
process.title = 'soketi';

import { cli } from './lib/cli';
cli();
18 changes: 10 additions & 8 deletions packages/cli/src/lib/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ import yargs from 'yargs';

import ipfsCommand from './commands/ipfs.command';

yargs(process.argv.slice(1))
.demandCommand()
.usage('Usage: $0 <command> [options]')
.showVersion('log')
.showHelpOnFail(true, 'Use --help for available options')
.recommendCommands()
.help()
.command(ipfsCommand.command, ipfsCommand.describe, ipfsCommand.builder).argv;
export const cli = async () =>
yargs(process.argv.slice(2))
.demandCommand()
.usage('Usage: $0 <command> [options]')
.showHelpOnFail(true, 'Use --help for available options')
.recommendCommands()
.help()
.env('SOKETIPFS_')
.command(ipfsCommand.command, ipfsCommand.describe, ipfsCommand.builder)
.argv;
12 changes: 10 additions & 2 deletions packages/cli/src/lib/commands/ipfs.command.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
import { CommandModule } from 'yargs';

import start from './ipfs/start.command';
import generateSwarmKey from './ipfs/generate-swarm-key.command';

export const subcommands = [start, generateSwarmKey];

export const command: CommandModule['command'] = 'ipfs';

export const describe: CommandModule['describe'] =
'Interact with the IPFS system built into the CLI.';

export const builder: CommandModule['builder'] = (cli) =>
cli.command(start.command, start.describe, start.builder, start.handler);
export const builder: CommandModule['builder'] = (cli) => {
for (const { command, describe, builder, handler } of subcommands) {
cli.command(command, describe, builder, handler);
}

return cli;
};

export default {
command,
Expand Down
22 changes: 22 additions & 0 deletions packages/cli/src/lib/commands/ipfs/generate-swarm-key.command.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { CommandModule } from 'yargs';
import { generateSwarmKey } from '@soketi/p2p';

export type StartCommandModule = CommandModule<unknown>;

export const command: StartCommandModule['command'] = 'generate:swarm-key';

export const describe: StartCommandModule['describe'] =
'Generate the Swarm Key for the cluster.';

export const builder: CommandModule['builder'] = (cli) => cli;

export const handler: StartCommandModule['handler'] = async () => {
process.stdout.write(generateSwarmKey());
};

export default {
command,
describe,
builder,
handler,
};
Loading

0 comments on commit 84726bc

Please # to comment.