A FastAPI-based web service that provides WHOIS and GeoIP information about IP addresses and domain names. This service performs WHOIS lookups, fetches geographical information based on IP, logs the incoming requests, and keeps the GeoIP database up to date.
- WHOIS lookup for IP addresses and domain names
- Geographical information retrieval using GeoIP
- Reverse DNS lookup for IP addresses
- Automatic browser/API detection (HTML or JSON response)
- Background task to update GeoIP database every 3 days
- IP Banning: Persistent ban list with TTL support and automatic cleanup
- Rate Limiting: Sliding window rate limiter (60 req/min, 10 req/sec per IP)
- Geographic Blocking: Country/region-based access control with allowlist/blocklist modes
- Suspicious Request Detection: Automatic detection and blocking of malicious patterns (
.env,.php, admin paths, etc.) - Request Whitelisting: Protection for legitimate static file requests
- Secure Admin API: API key-protected endpoints for security management
- Comprehensive Logging: All security events logged with country information
π See SECURITY.md for detailed security documentation, configuration, and API usage.
- Python 3.10+
- Poetry (dependency management)
- Docker (optional, for containerized deployment)
- FastAPI - Web framework
- uvicorn - ASGI server
- python-whois - WHOIS lookups
- geoip2fast - GeoIP database
- dnspython - DNS resolution
- python-dotenv - Environment configuration
- APScheduler - Background tasks
See pyproject.toml for complete dependency list.
git clone https://github.com/1kko/whatismyip.git
cd whatismyip# Copy environment template
cp .env.example .env
# Generate a secure API key
python -c "import secrets; print(secrets.token_urlsafe(32))"
# Edit .env and paste your generated API key
nano .env # or vim/codeImportant: Replace ADMIN_API_KEY=CHANGE_ME_TO_SECURE_RANDOM_STRING with your generated key!
poetry shell
poetry installmakeThe service is configured via environment variables in .env file:
# Admin API authentication (REQUIRED for admin endpoints)
ADMIN_API_KEY=your-secure-random-key-here
# Rate limiting
RATE_LIMIT_REQUESTS_PER_MINUTE=60 # Max requests per minute per IP
RATE_LIMIT_REQUESTS_PER_SECOND=10 # Burst protection
# Ban durations (in seconds)
BAN_DURATION_RATE_LIMIT=3600 # 1 hour for rate limit violations
BAN_DURATION_SUSPICIOUS=86400 # 24 hours for suspicious requests
# Geographic blocking (optional)
# GEO_MODE=disabled # disabled, allowlist, or blocklist
# GEO_BLOCKED_COUNTRIES=CN,RU,KP # Comma-separated country codes
# GEO_ALLOWED_COUNTRIES=US,CA,GB # For allowlist modeSee .env.example for all available options.
make serve
uvicorn main:app --host 0.0.0.0 --port 8000
- WHOIS and GeoIP lookup for your current IP:
GET http://localhost:8000/
WHOIS and GeoIP lookup for a specific domain or IP:
GET http://localhost:8000/{domain_or_ip}
Returns WHOIS and GeoIP information for the client's IP address.
Response: JSON or HTML (based on User-Agent)
Returns WHOIS and GeoIP information for the provided domain or IP address.
Response Example:
{
"address": "8.8.8.8",
"datetime": "2024-09-24T06:55:45.597769Z",
"location": {
"ip": "8.8.8.8",
"country_code": "US",
"country_name": "United States",
"city": {
"name": "",
"subdivision_code": "",
"subdivision_name": "",
"latitude": null,
"longitude": null
},
"cidr": "8.8.8.0/23",
"hostname": "",
"asn_name": "GOOGLE",
"asn_cidr": "8.8.8.0/24",
"is_private": false
},
"whois": {
"domain_name": ["GOOGLE.COM", "google.com"],
"registrar": "MARKMONITOR INC.",
"whois_server": "whois.markmonitor.com",
...
},
"headers": {
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36 OPR/112.0.0.0",
"accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
...
}
}All admin endpoints require the api-key header with your ADMIN_API_KEY.
GET /admin/bans- List all banned IPsPOST /admin/ban/{ip}?duration=3600- Manually ban an IPDELETE /admin/ban/{ip}- Unban an IP
GET /admin/geo/rules- Get current geo-blocking configurationPUT /admin/geo/rules- Update geo-blocking configurationPOST /admin/geo/block/country/{code}- Block a countryDELETE /admin/geo/block/country/{code}- Unblock a countryPOST /admin/geo/allow/country/{code}- Add country to allowlistDELETE /admin/geo/allow/country/{code}- Remove from allowlistGET /admin/geo/lookup/{ip}- Get geographic info for an IPGET /admin/geo/countries- List available country codes
GET /admin/stats- Get security statistics
Example:
# Set your API key
export API_KEY="your-api-key-from-env"
# Get all banned IPs
curl -H "api-key: $API_KEY" http://localhost:8000/admin/bans
# Block China
curl -X POST -H "api-key: $API_KEY" \
http://localhost:8000/admin/geo/block/country/CN
# Get statistics
curl -H "api-key: $API_KEY" http://localhost:8000/admin/statsπ See SECURITY.md for complete API documentation and examples.
The service implements multiple security layers:
- IP Ban Check - Blocked banned IPs immediately (403)
- Geographic Filtering - Country/region-based access control (403)
- Request Whitelist - Allow legitimate static files and main endpoints
- Suspicious Pattern Detection - Auto-ban malicious requests (403 + ban)
- Rate Limiting - Prevent abuse (429 + ban)
IPs are automatically banned when:
- Rate limit exceeded: 60 requests/minute or 10 requests/second β 1 hour ban
- Suspicious request detected: Requests for
.env,.php,/admin, etc. β 24 hour ban
The following patterns are automatically detected and banned:
- Environment files:
.env - Script files:
.php,.asp,.aspx - Data files:
.json,.xml,.sql - Backup files:
.bak,.log,.conf,.ini - Admin paths:
/admin,/wp-*,/cgi-bin/ - Hidden files:
/.*(dotfiles) - Git repository:
/.git/
Disabled (default):
GEO_MODE=disabledBlocklist (recommended):
GEO_MODE=blocklist
GEO_BLOCKED_COUNTRIES=CN,RU,KP,IRAllowlist (high security):
GEO_MODE=allowlist
GEO_ALLOWED_COUNTRIES=US,CA,GB,DE,JP
GEO_BLOCK_UNKNOWN=trueSecurity data is stored in the data/ directory:
data/banned_ips.json- Banned IPs with expiration timesdata/geo_rules.json- Geographic blocking configuration
These files persist across service restarts.
Logs are written to console and to a file service.log with rotation every day, keeping the last 7 days of logs.
All security events are logged with the format:
2025-11-05 10:30:00 - main.py:805 - security_middleware - SECURITY: Banned 192.168.1.100 (CN) for suspicious request: /.env
View security logs:
# Watch live security events
tail -f service.log | grep SECURITY
# Count security events
grep SECURITY service.log | wc -l# Set your API key (required for all admin commands)
export API_KEY="your-api-key-from-env"
# View all banned IPs
curl -H "api-key: $API_KEY" http://localhost:8000/admin/bans
# Ban an IP manually
curl -X POST -H "api-key: $API_KEY" \
http://localhost:8000/admin/ban/192.168.1.100
# Unban an IP
curl -X DELETE -H "api-key: $API_KEY" \
http://localhost:8000/admin/ban/192.168.1.100
# Block a country (China)
curl -X POST -H "api-key: $API_KEY" \
http://localhost:8000/admin/geo/block/country/CN
# Enable blocklist mode
curl -X PUT -H "api-key: $API_KEY" \
-H "Content-Type: application/json" \
-d '{"mode": "blocklist"}' \
http://localhost:8000/admin/geo/rules
# Get security statistics
curl -H "api-key: $API_KEY" http://localhost:8000/admin/stats
# Lookup IP geographic info
curl -H "api-key: $API_KEY" \
http://localhost:8000/admin/geo/lookup/8.8.8.8200- Success403- Forbidden (banned IP, geo-blocked, or suspicious request)404- Not Found (endpoint doesn't exist, or invalid admin API key for security)429- Too Many Requests (rate limit exceeded)
- Configuration:
.env - Ban list:
data/banned_ips.json - Geo rules:
data/geo_rules.json - Logs:
service.log(rotated daily, 7-day retention) - GeoIP database: Auto-downloaded and updated every 3 days
- Fork the repository.
- Create a new branch (git checkout -b feature-branch).
- Make your changes.
- Commit your changes (git commit -am 'Add new feature').
- Push to the branch (git push origin feature-branch).
- Create a new Pull Request.
MIT License. See LICENSE file for details.
π Security: For detailed security documentation, configuration options, and troubleshooting, see SECURITY.md
