make rate-limiting and auto-restart optional via flag and modify readme

This commit is contained in:
schlagmichdoch 2023-01-11 14:21:54 +01:00
parent f12872bf5c
commit 57851902f9
6 changed files with 101 additions and 35 deletions

4
.github/FUNDING.yml vendored
View file

@ -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

8
CONTRIBUTING.md Normal file
View file

@ -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.

View file

@ -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).

View file

@ -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
```

View file

@ -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'));

View file

@ -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": "",