Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/UlisesGascon/tor-detect-middleware
Tor detect middleware for express
https://github.com/UlisesGascon/tor-detect-middleware
Last synced: 3 months ago
JSON representation
Tor detect middleware for express
- Host: GitHub
- URL: https://github.com/UlisesGascon/tor-detect-middleware
- Owner: UlisesGascon
- License: gpl-3.0
- Created: 2019-09-07T06:05:44.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2023-12-06T14:31:17.000Z (11 months ago)
- Last Synced: 2024-05-14T00:02:58.094Z (6 months ago)
- Language: JavaScript
- Size: 2.16 MB
- Stars: 14
- Watchers: 3
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
- awesome-nodejs-security - tor-detect-middleware
README
tor-detect-middleware
Tor detect middleware for Express# About
Tor detect middleware for Express.
โค๏ธ Awesome Features:
- Easy to redirect Tor or Surface users. ๐ฅ
- Easy to recognize TorUsers at inside the `req` object, `req.isTorUser` ๐บ
- No infra required, the database is json based using `lowdb` ๐
- The `strictMode` won't allow any request to access until the relays IPs are collected ๐ฆ
- The `purge` allow you to dump the database at startup โฃ๏ธ
- `debug` is supported ๐ช
- Refresh time is customizable ๐ง
- Easy to use and great test coverage โ```js
const express = require('express');
const torUserHandler = require('tor-detect-middleware')
const app = express();app.use(torUserHandler())
app.get('/', (req, res) => {
const ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress;
res.send(`Are you (${ip}) a TOR user? ${req.isTorUser}`);
});app.listen(3000, () => {
console.log('We are in port 3000!');
});
```## Usage
### Install
```bash
npm install tor-detect-middleware
```### simple example
```js
const express = require('express');
const torUserHandler = require('tor-detect-middleware')
const app = express();app.use(torUserHandler())
app.get('/', (req, res) => {
const ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress;
res.send(`Are you (${ip}) a TOR user? ${req.isTorUser}`);
});app.listen(3000, () => {
console.log('We are in port 3000!');
});
```### Redirect Users
In this example we redirect the surface users to `https://www.nytimes.com` and Tor users to `https://www.nytimes3xbfgragh.onion/`. You can use both ways to redirect.
```js
const express = require('express');
const torUserHandler = require('tor-detect-middleware')
const app = express();app.use(torUserHandler({
surface: "https://www.nytimes.com",
tor: "https://www.nytimes3xbfgragh.onion/"
}))app.listen(3000, () => {
console.log('We are in port 3000!');
});
```### Ban users from surface or TOR
```js
const express = require('express');
const torUserHandler = require('tor-detect-middleware')
const app = express();app.use(torUserHandler())
app.get('/surface-only', (req, res) => {
if(req.isTorUser) return res.status(401).send("You can't access from TOR here")
res.send('Welcome surface user!');
});app.get('/tor-only', (req, res) => {
if(!req.isTorUser) return res.status(401).send("You can't access from the surface here")
res.send('Welcome to the dark side. We have cookies!');
});app.listen(3000, () => {
console.log('We are in port 3000!');
});
```### Custom Cron Jobs
By default we refresh the IP list every hour, but you can modify the ms as you wish.
```js
const express = require('express');
const torUserHandler = require('tor-detect-middleware')
const app = express();app.use(torUserHandler({refreshMs: 600000}))
//...
```### Strict Mode
Special behaviour in Strict mode:
- The server will stop at startup if `https://onionoo.torproject.org/details` url is down
- The server will wait until there is a list ready to dispatch requests. (Few seconds)_Note: if there is a list stored you wont surfer any problem, as we start the service from the previous list._
```js
const express = require('express');
const torUserHandler = require('tor-detect-middleware')
const app = express();app.use(torUserHandler({
strictMode: true
}))//...
```### Purge list at start
You can purge the list by default at the startup of the service.
```js
const express = require('express');
const torUserHandler = require('tor-detect-middleware')
const app = express();app.use(torUserHandler({
purge: true
}))//...
```## Built With
Development only:
- [Standard](https://www.npmjs.com/package/standard) - Linting propuses
- [Husky](https://www.npmjs.com/package/husky) - Git Hooks
- [Nodemon](https://www.npmjs.com/package/nodemon) - Reload the processProduction only:
- [debug](https://www.npmjs.com/package/debug) - Debug the app
- [got](https://www.npmjs.com/package/got) - Download TOR infra data
- [ip-regex](https://www.npmjs.com/package/ip-regex) - Validate relays IPs
- [lowdb](https://www.npmjs.com/package/lowdb) - Store and query IPs## Contributing
Please read [CONTRIBUTING.md](CONTRIBUTING.md) for details on our code of conduct, and the process for submitting pull requests to us.
## Versioning
We use [SemVer](http://semver.org/) for versioning. For the versions available, see the [tags on this repository](https://github.com/ulisesGascon/tor-detect-middelware/tags).
## Authors
- **Ulises Gascรณn** - *Initial work- - [@ulisesGascon](https://github.com/ulisesGascon)
See also the list of [contributors](https://github.com/ulisesGascon/tor-detect-middelware/contributors) who participated in this project.
## License
This project is licensed under the GNU AGPL3.0 License - see the [LICENSE.md](LICENSE.md) file for details
## Acknowledgments
- This project is under development, but you can help us to improve it! We :heart: FOSS!