A Node.js Express server that acts as an intelligent proxy to block advertisements, trackers, and malicious content. This lightweight solution provides enterprise-grade ad blocking capabilities with comprehensive APIs for management and monitoring.
- Intelligent Ad Blocking: Blocks ads using domain blacklists and pattern matching
- Real-time Proxy: Acts as a transparent proxy for web requests
- Multiple Blocklist Sources: Integrates with EasyList and custom blocklists
- Whitelist Support: Allow specific domains to bypass blocking
- Statistics & Analytics: Track blocked vs allowed requests with detailed metrics
- RESTful API: Complete API for blocklist management and monitoring
- Production Ready: Includes logging, rate limiting, and error handling
- Performance Optimized: In-memory caching for fast blocklist lookups
graph TD
A[Client Request] --> B[Express Server]
B --> C[Rate Limiting]
C --> D[Ad Blocker Middleware]
D --> E{Check Blocklists}
E -->|Blocked| F[Return 204 No Content]
E -->|Allowed| G[Proxy Middleware]
G --> H[Forward to Target]
H --> I[Return Response]
J[Blocklist Manager] --> K[Local Blocklists]
J --> L[Remote Sources - EasyList]
J --> M[Whitelist]
J --> E
-
Request Flow:
- Client sends request with target URL
- Server applies rate limiting and security checks
- Ad blocker middleware checks URL against blocklists
- If blocked: returns 204 No Content
- If allowed: proxies request to target and returns response
-
Blocklist Management:
- Loads local blocklists on startup
- Periodically updates from remote sources (EasyList)
- Maintains in-memory cache for performance
- Supports custom domain and pattern rules
-
Statistics Tracking:
- Real-time counters for blocked/allowed requests
- Daily statistics with historical data
- Top blocked domains tracking
- Performance metrics and uptime
- Node.js 16+
- npm or yarn
# Clone the repository
git clone https://github.com/drealdumore/ads-blocker.git
cd ads-blocker
# Install dependencies
npm install
# Copy environment configuration
cp .env.example .env
# Start the server
npm start
# For development with auto-reload
npm run devCreate a .env file based on .env.example:
PORT=3000
NODE_ENV=production
ALLOWED_ORIGINS=*
LOG_LEVEL=info
RATE_LIMIT_WINDOW_MS=900000
RATE_LIMIT_MAX_REQUESTS=1000
BLOCKLIST_UPDATE_INTERVAL=24
PROXY_TIMEOUT=30000Place custom blocklists in src/data/blocklists/:
domains.txt- Domain-based blocking rulespatterns.txt- Regex patterns for URL matchingwhitelist.txt- Domains to never block
Send requests through the proxy by providing the target URL:
# Using query parameter
curl "http://localhost:3000/?url=https://example.com"
# Using header
curl -H "X-Target-URL: https://example.com" http://localhost:3000/# Check blocklist status
GET /api/blocklist/status
# Check if URL would be blocked
POST /api/blocklist/check
{
"url": "https://example.com",
"userAgent": "Mozilla/5.0...",
"referer": "https://source.com"
}
# Add domain to blocklist
POST /api/blocklist/domain
{
"domain": "ads.example.com"
}
# Remove domain from blocklist
DELETE /api/blocklist/domain
{
"domain": "ads.example.com"
}
# Add domain to whitelist
POST /api/blocklist/whitelist
{
"domain": "trusted.com"
}
# Update remote blocklists
POST /api/blocklist/update# Get overall statistics
GET /api/stats
# Get daily statistics (last 7 days)
GET /api/stats/daily?days=7
# Get top blocked domains
GET /api/stats/top-blocked?limit=10
# Get recent blocked requests
GET /api/stats/recent?limit=50
# Reset statistics
POST /api/stats/reset# Server health status
GET /health{
"success": true,
"data": {
"blocked": 1250,
"allowed": 8750,
"totalRequests": 10000,
"blockRate": "12.50%",
"uptime": 86400000,
"requestsPerMinute": 6.94,
"topBlockedDomains": [
{ "domain": "doubleclick.net", "count": 342 },
{ "domain": "googlesyndication.com", "count": 201 }
]
}
}{
"success": true,
"data": {
"url": "https://ads.example.com/banner.js",
"blocked": true,
"reason": "Matches blocklist criteria"
}
}- Rate Limiting: Configurable request limits per IP
- CORS Protection: Configurable origin restrictions
- Header Security: Helmet.js for security headers
- Input Validation: URL validation and sanitization
- Error Handling: Graceful error responses
- Logging: Comprehensive request and error logging
logs/combined.log- All application logslogs/error.log- Error-level logs only
error- Critical errorswarn- Warning messagesinfo- General informationdebug- Detailed debugging (development)
- Request counts (blocked/allowed)
- Response times
- Top blocked domains
- Daily/hourly statistics
- Error rates
- Uptime statistics
## π§ͺ Testing
```bash
# Run tests
npm test
# Run linting
npm run lint
# Fix linting issues
npm run lint:fix
- Memory Usage: ~50MB baseline + blocklist size
- Response Time: <10ms for blocklist checks
- Throughput: 1000+ requests/second
- Blocklist Size: 50,000+ domains from EasyList
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Run linting and tests
- Submit a pull request
- Documentation: Check this README
- Issues: GitHub Issues
- Performance: Check
/healthand/api/statsendpoints