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

merge main into dev #23

Merged
merged 2 commits into from
May 21, 2024
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
3 changes: 0 additions & 3 deletions .env.template

This file was deleted.

43 changes: 43 additions & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
name: "Deploy"

env:
CONTAINER_NAME: libp2p-ws-proxy

on:
push:
branches:
- main

jobs:
publish:
name: Publish
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
steps:
- name: Checkout the repository
uses: actions/checkout@v3
- name: Set up QEMU
uses: docker/setup-qemu-action@v3

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Login to ghcr.io
uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{github.actor}}
password: ${{secrets.GITHUB_TOKEN}}
- name: Build and Push Proxy Image
uses: docker/build-push-action@v5
with:
context: .
platforms: linux/amd64,linux/arm64
push: true
tags: ghcr.io/pinoutltd/${{ env.CONTAINER_NAME }}:latest





6 changes: 2 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
FROM node:18-alpine AS dependencies
FROM node:20-alpine
WORKDIR /proxy
RUN chown -R node:node /proxy
RUN chmod -R 777 /proxy
COPY package.json ./
COPY package-lock.json ./
RUN npm ci --only=production
RUN npm ci --omit=dev
COPY . .
ARG PORT=8888
ENV PORT=${PORT}
EXPOSE ${PORT}
CMD [ -d "node_modules" ] && npm run start || npm ci --only=production && npm run start
EXPOSE 9999
USER node
10 changes: 4 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,17 @@ Set the configuration file by specifying the relay address and the directory nam

Build:

Specify the port number on which the WebSocket server will be launched. The `PORT` variable specifying the port number in the Docker container. It can be any variable except `9999`.

Specify the port number on which the WebSocket server will be launched.
```
docker build --build-arg PORT=<port> -t proxy:v0.0.1 .
docker build -t proxy:v0.0.1 .
```

Launch:

To enable access to the WebSocket server from another application, it is essential to establish a port mapping between the host and the container. While not mandatory, it is advisable to align the port numbers in the container and on the host. So use the same port number as you did during build:

```
docker run --name libp2p-proxy --detach -p 127.0.0.1:<port>:<port> -p 127.0.0.1:9999:9999 -v ${PWD}:/proxy proxy:v0.0.1
docker run --name libp2p-proxy --detach -p 127.0.0.1:8888:8888 -p 127.0.0.1:9999:9999 proxy:v0.0.1
```

To see logs:
Expand All @@ -51,7 +50,7 @@ docker stop libp2p-proxy
---

Requirements:
1. Node v.18.16.1
1. Node v.20.10.0

Installation:

Expand All @@ -67,7 +66,6 @@ Launch:

Specify the port number on which the WebSocket server will be launched.
```
export PORT=<port>
node src/index.js
```
Then you can connect a websocket client and libp2p nodes to proxy messages between them.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import { multiaddr } from '@multiformats/multiaddr';
import dotenv from 'dotenv';
import { Libp2pManager } from './libp2pManager.js';
import { WebSocketManager } from './wsManager.js';
import { MessageHandler } from './messageHandler.js';
import { createDir4SavedData } from '../utils/saveData.js';
import { Logger } from '../utils/logger.js';

dotenv.config();

async function run() {
const logger = new Logger();
await createDir4SavedData(logger);
Expand Down
5 changes: 1 addition & 4 deletions src/libp2pManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,16 @@ import { toString as uint8ArrayToString } from 'uint8arrays/to-string';
import { circuitRelayTransport } from 'libp2p/circuit-relay';
import { identifyService } from 'libp2p/identify';
import { multiaddr } from '@multiformats/multiaddr';
import dotenv from 'dotenv';
import { ConfigurationManager } from '../utils/configurationManager.js';

dotenv.config();

/**
* Libp2p manager. Contains all methods that are used for libp2p communication.
* @param logger Instance of the Logger class.
*/
export class Libp2pManager {
constructor(logger) {
this.configuration = new ConfigurationManager(logger);
this.realayAddress = process.env.RELAY_ADDRESS;
this.realayAddress = '/dns4/libp2p-relay-1.robonomics.network/tcp/443/wss/p2p/12D3KooWEMFXXvpZUjAuj1eKR11HuzZTCQ5HmYG9MNPtsnqPSERD';
this.logger = logger;
}

Expand Down
5 changes: 1 addition & 4 deletions src/messageHandler.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import WebSocket from 'ws';
import fs from 'fs/promises';
import dotenv from 'dotenv';
import { saveMsg2File } from '../utils/saveData.js';

dotenv.config();

/**
* Handler for messsages from libp2p and websocket channels. It proxes msgs from
* libp2p to websocket clients and msgs from websocket clients to libp2p nodes.
Expand Down Expand Up @@ -70,7 +67,7 @@ export class MessageHandler {
* @param node Instance of the libp2p node.
*/
async sendSavedMsg(connectedPeerId, node) {
const directoryPath = process.env.SAVED_DATA_DIR_PATH;
const directoryPath = 'saved_data';
try {
const files = await fs.readdir(directoryPath);
files.forEach(async (file) => {
Expand Down
5 changes: 1 addition & 4 deletions src/wsManager.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import { WebSocketServer } from 'ws';
import dotenv from 'dotenv';

dotenv.config();

/**
* Handler for messsages from libp2p and websocket channels. It proxes msgs from
Expand All @@ -23,7 +20,7 @@ export class WebSocketManager {
* @returns Instance of the server.
*/
#createWebsocketServer() {
const port = Number(process.env.PORT);
const port = 8888;
const wss = new WebSocketServer({ port }, () => {
this.logger.INFO(`WebSocket server listening on port ${port}`);
});
Expand Down
2 changes: 1 addition & 1 deletion utils/configurationManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { toString as uint8ArrayToString } from 'uint8arrays/to-string';
*/
export class ConfigurationManager {
constructor(logger) {
this.filePath = process.env.PEER_ID_CONFIG_PATH;
this.filePath = 'peerIdJson.json';
this.logger = logger;
}

Expand Down
5 changes: 1 addition & 4 deletions utils/saveData.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import fs from 'fs/promises';
import dotenv from 'dotenv';

dotenv.config();

const directoryPath = process.env.SAVED_DATA_DIR_PATH;
const directoryPath = 'saved_data';

/**
* Creates directory for saving data if it doesn't exists.
Expand Down
Loading