From 57851902f91b155dcc06eda6b1cd75af7b2ee6fb Mon Sep 17 00:00:00 2001 From: schlagmichdoch Date: Wed, 11 Jan 2023 14:21:54 +0100 Subject: [PATCH] make rate-limiting and auto-restart optional via flag and modify readme --- .github/FUNDING.yml | 4 +-- CONTRIBUTING.md | 8 ++++++ README.md | 7 +++++ docs/host-your-own.md | 50 ++++++++++++++++++++++++++++++-- index.js | 66 +++++++++++++++++++++++-------------------- package.json | 1 + 6 files changed, 101 insertions(+), 35 deletions(-) create mode 100644 CONTRIBUTING.md diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml index d0fb2a6..5a0ce1e 100644 --- a/.github/FUNDING.yml +++ b/.github/FUNDING.yml @@ -1,6 +1,6 @@ # These are supported funding model platforms -github: robinlinus +github: schlagmichdoch patreon: # Replace with a single Patreon username open_collective: # Replace with a single Open Collective username ko_fi: # Replace with a single Ko-fi username @@ -9,4 +9,4 @@ community_bridge: # Replace with a single Community Bridge project-name e.g., cl liberapay: # Replace with a single Liberapay username issuehunt: # Replace with a single IssueHunt username otechie: # Replace with a single Otechie username -custom: https://www.paypal.com/donate/?hosted_button_id=MG8GV7YCYT352 +custom: https://buymeacoffee.com/pairdrop diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 0000000..fba1262 --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,8 @@ +# Contributing guidelines + +Make sure to follow these guidelines before opening an [issue](https://github.com/schlagmichdoch/pairdrop/issues/new/choose) or a [pull request](https://github.com/schlagmichdoch/pairdrop/pulls): + +- An issue is for a bug or a feature request, if you have any question or something similar, please use [Discussions](https://github.com/schlagmichdoch/pairdrop/discussions). +- Before opening an issue of a pull request, please check if the issue or the pull request already exists. +- Pull requests for packages updates are not allowed since there is [Dependabot](https://github.com/schlagmichdoch/pairdrop/blob/master/.github/dependabot.yml) that checks them automatically. +- If you don't know how to contribute, also if you don't know JavaScript or Node.js, you can always help others on [Discussions](https://github.com/schlagmichdoch/pairdrop/discussions), debug the application, share your awesome ideas with a new issue (feature request) and check the whole project for misspellings, too. diff --git a/README.md b/README.md index bdb7895..cdd044a 100644 --- a/README.md +++ b/README.md @@ -47,3 +47,10 @@ If you want to support me and my work you can [buy me a coffee](https://www.buym To support the original Snapdrop and its creator go to [his GitHub page](https://github.com/RobinLinus/snapdrop). Thanks a lot for supporting free and open software! + + +## How to contribute + +Feel free to [open an issue](https://github.com/schlagmichdoch/pairdrop/issues/new/choose) or a +[pull request](https://github.com/schlagmichdoch/pairdrop/pulls) but follow +[Contributing Guidelines](/CONTRIBUTING.md). diff --git a/docs/host-your-own.md b/docs/host-your-own.md index 1632da3..f480452 100644 --- a/docs/host-your-own.md +++ b/docs/host-your-own.md @@ -45,9 +45,55 @@ The client expects the server at http(s)://your.domain/server. When serving the node server behind a proxy, the `X-Forwarded-For` header has to be set by the proxy. Otherwise, all clients that are served by the proxy will be mutually visible. ## Deployment with node -By default, the node server listens on port 3000. -Use nginx or apache to set the header correctly: +```bash +git clone https://github.com/Bellisario/node-snapdrop.git && cd node-snapdrop +``` + +Install all dependencies with NPM: + +```bash +npm install +``` + +Start the server with: + +```bash +npm start +``` + +### Public Run + +If you want to run in your public "sharable" IP instead of locally, you can use this command: + +```bash +node index.js public +``` +or +```bash +npm start +``` + +> Remember to check your IP Address using your OS command to see where you can access the server. + +> By default, the node server listens on port 3000. + +#### Automatic restart on error +```bash +npm start -- --auto-restart +``` +#### Rate limiting requests: +```bash +npm start -- --rate-limit +``` + +#### Production (autostart and rate-limit) +```bash +npm start:prod +``` + +## HTTP-Server +You must use nginx or apache to set the x-forwarded-for header correctly. Otherwise, all clients will be mutually visible. ### Using nginx ``` diff --git a/index.js b/index.js index 2f96761..8c887b6 100644 --- a/index.js +++ b/index.js @@ -1,7 +1,7 @@ -var process = require('process') -var crypto = require('crypto') -var {spawn} = require('child_process') -var net = require('net') +const process = require('process') +const crypto = require('crypto') +const {spawn} = require('child_process') +const net = require('net') // Handle SIGINT process.on('SIGINT', () => { @@ -29,41 +29,45 @@ process.on('unhandledRejection', (reason, promise) => { console.log(reason) }) -process.on( - 'uncaughtException', - () => { - process.once( - 'exit', - () => spawn( - process.argv.shift(), - process.argv, - { - cwd: process.cwd(), - detached: true, - stdio: 'inherit' - } - ) - ); - process.exit(); - } -); +if (process.argv.includes('--auto-restart')) { + process.on( + 'uncaughtException', + () => { + process.once( + 'exit', + () => spawn( + process.argv.shift(), + process.argv, + { + cwd: process.cwd(), + detached: true, + stdio: 'inherit' + } + ) + ); + process.exit(); + } + ); +} const express = require('express'); const RateLimit = require('express-rate-limit'); const http = require('http'); -const limiter = RateLimit({ - windowMs: 5 * 60 * 1000, // 5 minutes - max: 1000, // Limit each IP to 100 requests per `window` (here, per 5 minutes) - message: 'Too many requests from this IP Address, please try again after 5 minutes.', - standardHeaders: true, // Return rate limit info in the `RateLimit-*` headers - legacyHeaders: false, // Disable the `X-RateLimit-*` headers -}) - const app = express(); const port = process.env.PORT || 3000; -app.use(limiter); +if (process.argv.includes('--rate-limit')) { + const limiter = RateLimit({ + windowMs: 5 * 60 * 1000, // 5 minutes + max: 1000, // Limit each IP to 100 requests per `window` (here, per 5 minutes) + message: 'Too many requests from this IP Address, please try again after 5 minutes.', + standardHeaders: true, // Return rate limit info in the `RateLimit-*` headers + legacyHeaders: false, // Disable the `X-RateLimit-*` headers + }) + + app.use(limiter); +} app.use(express.static('public')); diff --git a/package.json b/package.json index 3d4d12f..a0a21b3 100644 --- a/package.json +++ b/package.json @@ -5,6 +5,7 @@ "main": "index.js", "scripts": { "start": "node index.js public", + "start:prod": "node index.js public --rate-limit --auto-restart", "test": "echo \"Error: no test specified\" && exit 1" }, "author": "",