Skip to content

zMarcosDantas/python-firewall

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 

Repository files navigation

Python Firewall

A lightweight command-line utility to block and unblock domains at the OS level by manipulating the system hosts file. Works on Windows and Linux.

How It Works

Blocking a domain adds entries to your system's hosts file that redirect both the bare domain and the www. variant to 127.0.0.1 (localhost), preventing your machine from reaching the real server. Unblocking removes those entries. On Windows, the DNS cache is flushed automatically after each operation.

Requirements

  • Python 3.x (no external dependencies — standard library only)
  • Administrator / root privileges to modify the hosts file

Usage

python firewall.py --d <domain(s)> [--b | --u]
Argument Description
--d, --domains One or more domains to act on (required)
--b Block the specified domains
--u Unblock the specified domains

--b and --u are mutually exclusive — only one can be used per invocation.

Examples

# Block a single domain
python firewall.py --d example.com --b

# Block multiple domains at once
python firewall.py --d facebook.com twitter.com youtube.com --b

# Unblock a domain
python firewall.py --d example.com --u

Running with Elevated Privileges

Windows — open PowerShell as Administrator, then run the script:

python firewall.py --d example.com --b

Linux:

sudo python3 firewall.py --d example.com --b

Hosts File Entries

For each domain, two entries are written:

127.0.0.1    example.com
127.0.0.1    www.example.com
OS Hosts file location
Windows C:\Windows\System32\drivers\etc\hosts
Linux /etc/hosts

Project Structure

python-firewall/
└── firewall.py   # Single-file implementation

Limitations

  • Does not block subdomains — only the bare domain and its www. variant are blocked. Subdomains like mail.example.com or api.example.com will still be accessible.
  • DNS cache flushing (ipconfig /flushdns) is Windows-only; on Linux you may need to flush manually or restart the browser.
  • Unblocking uses substring matching, so unblocking mail.com could affect any hosts entry that contains that string.
  • No validation that input strings are well-formed domain names.
  • Does not check whether a domain is already blocked before blocking it.

About

A CLI tool to block and unblock domains at the OS level by editing the system hosts file. Supports Windows and Linux with automatic DNS cache flushing on Windows.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages