Skip to content

lupael/DNS-for-ISP

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

38 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

DNS Server for ISP

A comprehensive DNS server solution similar to AdGuard, designed for ISPs and end-users with advanced filtering, monitoring, and multi-tenant capabilities.

✨ NEW: Now with rate limiting and webhook integration! See Rate Limiting Guide and Webhook Integration.

πŸš€ Features

Core DNS Functionality

  • βœ… Recursive DNS resolver with IPv4 and IPv6 support
  • βœ… DNS-over-HTTPS (DoH) support (RFC 8484)
  • βœ… DNS-over-TLS (DoT) support (RFC 7858)
  • βœ… Intelligent DNS response caching
  • βœ… Blacklist/whitelist domain filtering

Ad-Blocking & Filtering

  • βœ… Automatic blocklist updates from trusted sources
  • βœ… Support for exact match, wildcard, and regex filtering
  • βœ… Custom rules per ISP/customer
  • βœ… Whitelist overrides for trusted domains
  • βœ… Multiple category support (ads, trackers, malware, adult content)

Real-time Monitoring

  • βœ… Complete DNS query logging
  • βœ… Suspicious activity detection (DNS tunneling, malware C2)
  • βœ… Prometheus metrics export
  • βœ… Grafana dashboard integration
  • βœ… Live query statistics and visualizations

Admin Panel & API

  • βœ… Modern React-based web dashboard
  • βœ… Real-time charts with ApexCharts
  • βœ… Role-based access control (super_admin, isp_admin, customer)
  • βœ… RESTful API with FastAPI
  • βœ… Complete API documentation
  • βœ… Dark mode support

Multi-ISP Support

  • βœ… Multi-tenant architecture
  • βœ… ISP-specific configurations and blocklists
  • βœ… API key management for ISP integration
  • βœ… Isolated dashboards per ISP
  • βœ… Subscription tier support

Security & Reliability

  • βœ… JWT-based authentication
  • βœ… TLS encryption for DoH/DoT
  • βœ… Password hashing with bcrypt
  • βœ… Database connection pooling
  • βœ… Docker containerization
  • βœ… Health check endpoints
  • βœ… Rate limiting middleware (NEW)
  • βœ… Webhook signature verification (NEW)

Integration & Extensibility

  • βœ… Webhook support for real-time events (NEW)
  • βœ… Rate limiting with configurable thresholds (NEW)
  • βœ… Event-driven architecture
  • βœ… Complete REST API
  • βœ… Prometheus metrics integration

πŸ“‹ Requirements

  • Docker & Docker Compose
  • Python 3.11+
  • Node.js 18+
  • PostgreSQL 15+
  • Redis 7+

πŸ› οΈ Quick Start

Using Docker Compose (Recommended)

  1. Clone the repository:
git clone https://github.com/i4edubd/DNS-for-ISP.git
cd DNS-for-ISP
  1. Configure SSL certificates (for DoH/DoT):
# For testing with self-signed certificates (default)
echo "SSL_SELF_SIGNED=true" >> backend/.env

# For production, use valid certificates
# SSL_SELF_SIGNED=false
# SSL_CERT_FILE=/path/to/cert.pem
# SSL_KEY_FILE=/path/to/key.pem
  1. Start all services:
cd docker
docker-compose up -d
  1. Access the services:

Manual Setup

Backend Setup

  1. Install dependencies:
cd backend
pip install -r requirements.txt
  1. Configure environment:
cp .env.example .env
# Edit .env with your settings
  1. Initialize database:
# Database will be auto-initialized on first run
  1. Run the server:
python -m uvicorn app.main:app --host 0.0.0.0 --port 8000

Frontend Setup

  1. Install dependencies:
cd frontend
npm install
  1. Configure API URL:
# Create .env file
echo "REACT_APP_API_URL=http://localhost:8000" > .env
  1. Run development server:
npm start

πŸ“– API Documentation

Authentication

Register User

curl -X POST http://localhost:8000/api/auth/register \
  -H "Content-Type: application/json" \
  -d '{
    "email": "user@example.com",
    "username": "newuser",
    "password": "securepassword",
    "full_name": "New User"
  }'

Note: Registration creates a customer role by default. Admin users must be created by super admins through privileged flows.

Login

curl -X POST http://localhost:8000/api/auth/login \
  -H "Content-Type: application/x-www-form-urlencoded" \
  -d "username=admin&password=securepassword"

DNS Operations

Query DNS

curl -X POST http://localhost:8000/api/dns/query \
  -H "Authorization: Bearer YOUR_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "domain": "example.com",
    "query_type": "A"
  }'

Get Query Logs

curl http://localhost:8000/api/dns/queries?limit=100 \
  -H "Authorization: Bearer YOUR_TOKEN"

Get Statistics

curl http://localhost:8000/api/dns/stats?hours=24 \
  -H "Authorization: Bearer YOUR_TOKEN"

Blocklist Management

Add Domain to Blocklist

curl -X POST http://localhost:8000/api/blocklist/add \
  -H "Authorization: Bearer YOUR_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "domain": "malicious-site.com",
    "rule_type": "exact",
    "category": "malware"
  }'

Update Blocklists

curl -X POST http://localhost:8000/api/blocklist/update \
  -H "Authorization: Bearer YOUR_TOKEN"

ISP Management

Create ISP

curl -X POST http://localhost:8000/api/isp/create \
  -H "Authorization: Bearer YOUR_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "name": "My ISP",
    "domain": "myisp.com",
    "subscription_tier": "premium"
  }'

Webhook Management

Register Webhook

curl -X POST http://localhost:8000/api/webhooks/register \
  -H "Authorization: Bearer YOUR_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "url": "https://your-server.com/webhook",
    "events": ["dns.blocked", "alert.suspicious_activity"],
    "secret": "your-webhook-secret"
  }'

List Webhooks

curl http://localhost:8000/api/webhooks/list \
  -H "Authorization: Bearer YOUR_TOKEN"

See Webhook Integration Guide for complete documentation.

Rate Limiting

Rate limiting is automatically enabled with default settings:

  • 100 requests per 60 seconds per IP
  • Configurable via environment variables

See Rate Limiting Guide for details.

DNS-over-HTTPS (DoH) and DNS-over-TLS (DoT)

The server now supports encrypted DNS protocols:

Using DoH

# Query using curl (POST method)
curl -H 'Content-Type: application/dns-message' \
     --data-binary @query.bin \
     https://localhost:443/dns-query

# Configure in browsers
# Firefox/Chrome: Settings β†’ DNS over HTTPS β†’ https://localhost:443/dns-query

Using DoT

# Query using kdig
kdig -d @localhost +tls example.com

# Configure systemd-resolved
echo "[Resolve]
DNS=your-server-address
DNSOverTLS=yes" | sudo tee -a /etc/systemd/resolved.conf
sudo systemctl restart systemd-resolved

See DoH/DoT Guide for complete documentation.

πŸ—οΈ Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   React Frontend β”‚
β”‚   (Port 3000)    β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
         β”‚
         β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”      β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   FastAPI       │◄────►│  PostgreSQL   β”‚
β”‚   Backend       β”‚      β”‚  Database     β”‚
β”‚   (Port 8000)   β”‚      β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
         β”‚                β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
         β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Ίβ”‚    Redis      β”‚
         β”‚                β”‚    Cache      β”‚
         β”‚                β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
         β”‚
         β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”      β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   Prometheus    │◄────►│   Grafana     β”‚
β”‚   Metrics       β”‚      β”‚   Dashboard   β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜      β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸ”§ Configuration

Environment Variables

Create a .env file in the backend directory:

# Application
APP_NAME="DNS Server for ISP"
DEBUG=false

# Server
HOST=0.0.0.0
PORT=8000

# DNS
DNS_HOST=0.0.0.0
DNS_PORT=53
DNS_CACHE_TTL=3600
DNS_UPSTREAM_SERVERS=["8.8.8.8", "8.8.4.4", "1.1.1.1"]

# DoH/DoT
DOH_ENABLED=true
DOH_PORT=443
DOT_ENABLED=true
DOT_PORT=853

# SSL/TLS Configuration for DoH/DoT
SSL_CERT_FILE=/etc/ssl/certs/dns_cert.pem
SSL_KEY_FILE=/etc/ssl/private/dns_key.pem
SSL_SELF_SIGNED=true  # Use false in production with valid certs

# Database
DATABASE_URL=postgresql+asyncpg://dns_user:dns_pass@localhost:5432/dns_db

# Redis
REDIS_HOST=localhost
REDIS_PORT=6379
REDIS_DB=0

# Security
SECRET_KEY=your-secret-key-change-in-production
ALGORITHM=HS256
ACCESS_TOKEN_EXPIRE_MINUTES=30

# CORS
CORS_ORIGINS=["http://localhost:3000"]

# Monitoring
PROMETHEUS_ENABLED=true

πŸ“Š Monitoring

Prometheus Metrics

The system exposes the following metrics:

  • dns_queries_total - Total DNS queries
  • dns_blocked_queries_total - Total blocked queries
  • dns_response_time_seconds - DNS response time histogram
  • dns_cache_hits_total - Cache hit counter
  • dns_cache_misses_total - Cache miss counter

Grafana Dashboards

Access Grafana at http://localhost:3001 with:

  • Username: admin
  • Password: admin

Import the provided dashboard from config/grafana/dashboards/

πŸ”’ Security

Best Practices

  1. Change Default Credentials: Update all default passwords
  2. Use Strong Secret Keys: Generate secure SECRET_KEY
  3. Enable HTTPS: Use TLS certificates for production
  4. Regular Updates: Keep blocklists updated
  5. Monitor Logs: Check for suspicious activity
  6. Backup Database: Regular PostgreSQL backups
  7. Limit API Access: Use firewall rules
  8. Rate Limiting: Implement rate limits for API endpoints

Role-Based Access Control

  • super_admin: Full system access, manage all ISPs
  • isp_admin: Manage their ISP's settings and users
  • customer: View their ISP's dashboard and queries

πŸš€ Deployment

Kubernetes Deployment

# Apply Kubernetes manifests
kubectl apply -f k8s/

Production Checklist

  • Change all default passwords
  • Generate secure SECRET_KEY
  • Configure TLS certificates
  • Set up database backups
  • Configure monitoring alerts
  • Set up log aggregation
  • Configure firewall rules
  • Enable rate limiting
  • Set up CDN for frontend
  • Configure DNS load balancing

πŸ“ Testing

Run Backend Tests

cd backend
pytest tests/ -v

Run Frontend Tests

cd frontend
npm test

🀝 Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Commit your changes
  4. Push to the branch
  5. Create a Pull Request

πŸ“„ License

This project is licensed under the MIT License.

πŸ™ Acknowledgments

  • Built with FastAPI, React, and PostgreSQL
  • Blocklists from StevenBlack and AdAway
  • Charts powered by ApexCharts
  • Monitoring with Prometheus and Grafana

πŸ“ž Support

For support, email: support@example.com

πŸ—ΊοΈ Roadmap

Recently Completed βœ…

  • Rate limiting middleware
  • Webhook integration system
  • Grafana dashboard configuration
  • Comprehensive documentation

Future Enhancements

  • WebSocket real-time updates
  • Mobile application
  • Machine learning-based threat detection
  • IPv6 full support
  • DNSSEC validation
  • Custom DNS response manipulation
  • Advanced analytics and reporting
  • Billing integration (Stripe/PayPal)
  • Multi-language support
  • Kubernetes operator

About

Integrated DNS-based ad and tracker blocking for ISP networks. Implements high-performance sinkholing to filter advertising and malicious domains at the network level, reducing bandwidth consumption and enhancing privacy for all subscribers.

Topics

Resources

License

Contributing

Security policy

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors