OnionGuard is a pure Go HTTP admission control and Layer 7 defense engine designed specifically for Tor v3 Onion Services. Built from the ground up for Tor Browser in Safest mode, where traditional WAFs, CAPTCHAs, and IP rate limiters fail completely.
$go get github.com/ihatemyfcklife/onionguard
Standard web application firewalls (Cloudflare, AWS WAF, Fail2ban) assume every visitor has a unique client IP and can execute complex JavaScript challenges. On Tor, these assumptions destroy your service.
All traffic exits locally from the Tor daemon (127.0.0.1 or a Unix domain socket). Any IP-based rate limiting or banning accidentally blacklists your entire userbase at once.
Security-conscious visitors use Tor Browser at the Safest security level where JavaScript is strictly disabled. Cloudflare Turnstile, reCAPTCHA, and JS crypto puzzles render your site completely unreachable.
Canvas inspection, audio API probes, and browser fingerprinting actively violate the anonymity promise of the Tor network and trigger Tor Browser anti-tracking defenses.
CI/CD static AST analysis rigorously ensures that no middleware or handler ever accesses RemoteAddr or X-Forwarded-For headers, preventing catastrophic global IP bans.
Generates memory-buffered PNG visual challenges with sinusoidal wave distortions and stochastic noise. 100% pure Go standard library: zero CGO, zero external FreeType dependencies.
A fair admission queue powered entirely by standard HTML <meta http-equiv="refresh"> pacing. No JavaScript needed to throttle aggressive Layer 7 traffic floods.
Single-key atomic Lua scripts compatible with standalone Redis and Redis Cluster provide lightning-fast distributed token buckets and non-blocking state synchronization.
A modular, zero-allocation-conscious pipeline built to throttle automated scrapers, slow-loris bursts, and volumetric HTTP floods before requests ever touch your application layer.
Classifies inbound requests deterministically into distinct privilege tiers without relying on client network addresses: Authenticated Principal > API Bearer Token > Active Valid Session > New Anonymous Visitor.
Generates 256-bit high-entropy CSPRNG tokens via crypto/rand with atomic rotation under distributed locks. Immediate, clean cookie revocation (MaxAge: -1) on expired or challenged sessions.
Independent, isolated quota scopes for anonymous traffic, authenticated sessions, API credentials, challenge attempts, and first-contact handshakes with microsecond replenishment.
Adaptive penalty escalation: visitors exceeding MaxAttempts on bitmap CAPTCHAs are immediately expelled to the Proof-of-Patience Wait Room with quadratic backoff delays.
Integrate OnionGuard with your standard Go net/http server in fewer than 20 lines of code. Works seamlessly with any Go framework (Chi, Gin, Echo, or standard Mux).
package main
import (
"fmt"
"net/http"
"time"
og "github.com/ihatemyfcklife/onionguard/pkg/onionguard"
middleware "github.com/ihatemyfcklife/onionguard/pkg/middleware"
)
func main() {
// 1. Load baseline config with Wait Room & pure Go CAPTCHA enabled
cfg := og.DefaultConfig()
cfg.WaitRoom.Enabled = true
cfg.Captcha.Enabled = true
cfg.Captcha.MaxAttempts = 3
cfg.RateLimit.AnonymousRate = 10 // req/min per anonymous circuit
// 2. Initialize the OnionGuard defense engine
engine, err := og.New(cfg)
if err != nil {
panic(fmt.Sprintf("failed to initialize onionguard: %v", err))
}
defer engine.Close()
// 3. Define protected application handlers
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
fmt.Fprintf(w, "<h1>Protected Onion Service</h1><p>Zero-JS, Zero-IP verified.</p>")
})
// 4. Wrap your router with OnionGuard admission control middleware
protectedHandler := middleware.Middleware(engine)(mux)
server := &http.Server{
Addr: "127.0.0.1:8080", // Local Tor reverse proxy binding
Handler: protectedHandler,
ReadTimeout: 5 * time.Second,
WriteTimeout: 10 * time.Second,
}
println("[OnionGuard] Engine active. Listening on 127.0.0.1:8080")
if err := server.ListenAndServe(); err != nil {
panic(err)
}
}
Route traffic from the Tor network to your local OnionGuard instance by configuring your daemon's torrc file.
# ===================================================
# Onion Service v3 Configuration
# ===================================================
HiddenServiceDir /var/lib/tor/my_onion_service/
HiddenServicePort 80 127.0.0.1:8080
HiddenServiceVersion 3
Ensure your Go server only listens on 127.0.0.1 or an internal Unix domain socket. Never expose OnionGuard directly to public IPv4/IPv6 interfaces.
Tor requires strict permissions on the keys directory: chmod 700 /var/lib/tor/my_onion_service/. The onion address will be stored in hostname.