nginx_mail_auth

command module
v0.0.0-...-49cf50a Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 9, 2026 License: MIT Imports: 16 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README ΒΆ

Mail Auth Proxy for Nginx (IMAP/SMTP)

A lightweight Go authentication backend designed specifically for the Nginx mail proxy module.

It validates user credentials against IMAP and/or SMTP servers and enforces rate limiting and temporary blocking to protect against brute-force attacks.


🎯 Purpose

This service is built to act as an auth_http backend for Nginx mail proxy:

  • Nginx handles client connections (IMAP/SMTP)
  • This service validates credentials
  • Returns upstream mail server to Nginx

✨ Features

  • πŸ” IMAP & SMTP authentication backend
  • πŸ” Optional IMAP-only mode (use IMAP for SMTP auth)
  • 🚫 Brute-force protection (per IP + per user)
  • ⏱️ Expiring blocks with configurable TTL
  • ⚑ Fast in-memory HTTP auth service
  • 🧩 Drop-in replacement for Nginx auth_http

πŸ—οΈ Architecture

Mail Client
    ↓
IMAP / SMTP
    ↓
Nginx mail proxy
    ↓
auth_http request
    ↓
Mail Auth Proxy (this service)
    ↓
IMAP / SMTP servers

πŸš€ Build & Run

git clone <repo>
cd <repo>
go build -o nginx_mail_auth

./nginx_mail_auth   --imap 127.0.0.1:143,127.0.0.2:143   --smtp 127.0.0.1:25   --port 9143

βš™οΈ Nginx Configuration

IMAP example
mail {
    server_name mail.example.com;

    auth_http 127.0.0.1:9143;

    imap {
        listen 143;
        protocol imap;
    }
}

SMTP example
mail {
    server_name mail.example.com;

    auth_http 127.0.0.1:9143;

    smtp {
        listen 25;
        protocol smtp;
    }
}

Full setup (IMAP + SMTP)
mail {
    server_name mail.example.com;
    auth_http   nginx_mail_auth:9143/auth;

    proxy_pass_error_message on;

    ssl                 on;
    ssl_certificate     /certificates/example.com.crt;
    ssl_certificate_key /certificates/example.com.key;
    ssl_protocols       TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers         HIGH:!aNULL:!MD5;
    ssl_session_cache   shared:SSL:10m;
    ssl_session_timeout 10m;

    server {
        listen          587;
        protocol        smtp;
        proxy_smtp_auth on;
        smtp_auth       login plain cram-md5;
    }

     server {
        listen   993;
        protocol imap;
    }
}

πŸ“‘ Nginx β†’ Auth Backend Headers

Header Description
Auth-Method Authentication method (must be plain)
Auth-User Username
Auth-Pass Password
Auth-Protocol imap or smtp
Auth-Login-Attempt Login attempt counter
Client-IP Client IP address

βœ… Responses

Success
Auth-Status: OK
Auth-Server: <backend server>
Auth-Port: <port>

Failure
Auth-Status: Invalid credentials
Auth-Wait: <seconds>

SMTP temporary error
Auth-Status: Temporary server problem, try again later
Auth-Error-Code: <smtp error>

πŸ›‘οΈ Rate Limiting

  • Per-IP tracking
  • Per-user tracking
  • Automatic expiration

Behavior:

  • Exceed max attempts β†’ temporary block
  • Each failure increases penalty
  • TTL-based cleanup

βš™οΈ CLI Flags

--imap host:port
--smtp host:port
--port 9143
--maxinvalidattempts 5
--invalidduration 5m
--useimaponly

mail {
    auth_http_timeout 10s;
    proxy_pass_error_message on;
    max_errors 3;
}

πŸ”’ TLS Notes

This service does NOT handle TLS.

Use Nginx for:

  • IMAPS (993)
  • SMTPS (465)

Keep service internal only.


⚠️ Limitations

  • SMTP only AUTH LOGIN
  • No TLS backend support
  • No persistent storage
  • No clustering

πŸ§ͺ Test Request

curl -i http://127.0.0.1:9143 \
  -H "Auth-Method: plain" \
  -H "Auth-User: user@example.com" \
  -H "Auth-Pass: password" \
  -H "Auth-Protocol: imap" \
  -H "Auth-Login-Attempt: 1" \
  -H "Client-IP: 127.0.0.1"

πŸ“Œ Production Tips

  • Bind to localhost/private interface
  • Firewall restrict access
  • Monitor logs for brute force
  • Use Redis if scaling

πŸ“„ License

MIT

Documentation ΒΆ

The Go Gopher

There is no documentation for this package.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
⌂ 捒网址