Additional stuff

This commit is contained in:
Feuerhamster 2021-06-08 19:12:12 +02:00
parent 31e2c67521
commit 1c3ece87fd
4 changed files with 24 additions and 4 deletions

View file

@ -2,7 +2,7 @@
> The minimalistic email relay for contact forms and more! > The minimalistic email relay for contact forms and more!
This is basically a minimal self-hosted open source alternative to [Formspree](https://formspree.io/) and [SendGrid](https://sendgrid.com/). This is basically a minimal self-hosted open source alternative to [Formspree](https://formspree.io/) and [SendGrid](https://sendgrid.com/).
Unlike other mail services (that often gives you an API key), this self-hosted mail service is designed to be accessed directly from a frontend. Unlike other mail services (that often gives you an API key for backends), this self-hosted mail service is designed to be accessed directly from a frontend.
### Features ### Features
- Access via API or HTML form + redirect - Access via API or HTML form + redirect
@ -29,6 +29,7 @@ cd mailform
docker build -t Feuerhamster/mailform . docker build -t Feuerhamster/mailform .
docker run Feuerhamster/mailform docker run Feuerhamster/mailform
-e PORT=3000 -e PORT=3000
-e PROXY=true
-v /your/custom/path /app/targets -v /your/custom/path /app/targets
``` ```
@ -45,8 +46,12 @@ npm run start
## ⚙Configuration ## ⚙Configuration
### Application ### Application
To configure the port, simply set the environment variable `PORT` to the port you want to have. MailForm can be configured using environment variables.
If you want to have your targets in a different directory, you can change that in the `TARGETS_DIR` environment variable.
**Environment variables:**
- `PORT` The port on which the application starts. If not provided, a random port will be selected.
- `TARGETS_DIR` Path to the directory with your target files. Default is `/targets` of the project root.
- `PROXY` A boolean that enables the "trust proxy" option of Express. **Enable this if you're using MailForm behind a reverse proxy like NGINX!** Default value is false.
### Targets ### Targets
Targets are your different endpoints each with its own rate limits and smtp provider. Targets are your different endpoints each with its own rate limits and smtp provider.

View file

@ -6,8 +6,13 @@ import routes from "./router";
// Start express app // Start express app
const port = process.env.PORT || 0; const port = process.env.PORT || 0;
const trustProxy: boolean = (process.env.PROXY === "true");
const app: Application = express(); const app: Application = express();
app.set("trust proxy", trustProxy);
app.set("x-powered-by", false);
// Middlewares // Middlewares
app.use(express.json()); app.use(express.json());
app.use(express.urlencoded({ extended: true })); app.use(express.urlencoded({ extended: true }));

View file

@ -7,7 +7,7 @@ export class EmailService {
private static targetTransports: Map<string, Transporter> = new Map<string, Transporter>(); private static targetTransports: Map<string, Transporter> = new Map<string, Transporter>();
/** /**
* Register a mail target in the email service * Register a target in the email service
* @param targetName (file-)name of an existing and loaded target * @param targetName (file-)name of an existing and loaded target
* @param smtpURL A valid SMTP(S) URL * @param smtpURL A valid SMTP(S) URL
*/ */

View file

@ -5,6 +5,11 @@ export class RateLimiter {
private static limiters: Map<string, RateLimiterMemory> = new Map<string, RateLimiterMemory>(); private static limiters: Map<string, RateLimiterMemory> = new Map<string, RateLimiterMemory>();
/**
* Register a target in the rate limiter service
* @param targetName Name of the target
* @param limits Rate limit configuration for target
*/
public static registerTarget(targetName: string, limits: TargetRateLimit): void { public static registerTarget(targetName: string, limits: TargetRateLimit): void {
this.limiters.set(targetName, new RateLimiterMemory({ this.limiters.set(targetName, new RateLimiterMemory({
@ -14,6 +19,11 @@ export class RateLimiter {
} }
/**
* Consume one point of a target's rate limiter
* @param target (file-)name of a target
* @param identifier Rate limiter identifier (ip address)
*/
public static async consume(target, identifier): Promise<boolean> { public static async consume(target, identifier): Promise<boolean> {
let rateLimiter: RateLimiterMemory = this.limiters.get(target); let rateLimiter: RateLimiterMemory = this.limiters.get(target);