diff --git a/.env.template b/.env.template deleted file mode 100644 index 7bc34ca..0000000 --- a/.env.template +++ /dev/null @@ -1,3 +0,0 @@ -PEER_ID_CONFIG_PATH="peerIdJson.json" -RELAY_ADDRESS="" -SAVED_DATA_DIR_PATH="" \ No newline at end of file diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml new file mode 100644 index 0000000..9108218 --- /dev/null +++ b/.github/workflows/deploy.yml @@ -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 + + + + + diff --git a/Dockerfile b/Dockerfile index ba639c2..18c1dc2 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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 \ No newline at end of file diff --git a/README.md b/README.md index 12e3bf4..eee93c6 100644 --- a/README.md +++ b/README.md @@ -22,10 +22,9 @@ 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= -t proxy:v0.0.1 . +docker build -t proxy:v0.0.1 . ``` Launch: @@ -33,7 +32,7 @@ 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:: -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: @@ -51,7 +50,7 @@ docker stop libp2p-proxy --- Requirements: -1. Node v.18.16.1 +1. Node v.20.10.0 Installation: @@ -67,7 +66,6 @@ Launch: Specify the port number on which the WebSocket server will be launched. ``` -export PORT= node src/index.js ``` Then you can connect a websocket client and libp2p nodes to proxy messages between them. \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 4f9baa2..49a1cd9 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,5 +1,5 @@ { - "name": "libp2p-ws-proxy", + "name": "ha-libp2p", "lockfileVersion": 3, "requires": true, "packages": { diff --git a/src/index.js b/src/index.js index c0d991a..0b74c0d 100644 --- a/src/index.js +++ b/src/index.js @@ -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); diff --git a/src/libp2pManager.js b/src/libp2pManager.js index 82c3ec3..9afa1a4 100644 --- a/src/libp2pManager.js +++ b/src/libp2pManager.js @@ -9,11 +9,8 @@ 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. @@ -21,7 +18,7 @@ dotenv.config(); 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; } diff --git a/src/messageHandler.js b/src/messageHandler.js index 057f866..4ad8c44 100644 --- a/src/messageHandler.js +++ b/src/messageHandler.js @@ -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. @@ -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) => { diff --git a/src/wsManager.js b/src/wsManager.js index afe1220..4f6518e 100644 --- a/src/wsManager.js +++ b/src/wsManager.js @@ -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 @@ -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}`); }); diff --git a/utils/configurationManager.js b/utils/configurationManager.js index badbbf2..b12647e 100644 --- a/utils/configurationManager.js +++ b/utils/configurationManager.js @@ -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; } diff --git a/utils/saveData.js b/utils/saveData.js index f41e616..5bf26e1 100644 --- a/utils/saveData.js +++ b/utils/saveData.js @@ -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.