Controlling Internet Access with OpenWRT Firewall: Blocking Websites and Disabling Internet
Introduction⌗
In today’s connected world, managing internet access within a home or small office network has become increasingly important. Whether you’re implementing parental controls, limiting non-productive internet usage during specific hours, or securing access to network resources, OpenWRT provides powerful tools to accomplish these tasks.
This article demonstrates several practical scripts for controlling internet access using an OpenWRT router. We’ll cover:
- Switching between different proxy modes (using OpenClash)
- Restricting access to NAS resources for specific devices
- Completely disabling internet access for selected devices
- Blocking specific websites using filtering rules
- Automating all of the above with scheduled cron jobs
Prerequisites⌗
- An OpenWRT router with shell access
- Basic familiarity with Linux command line
- OpenClash installed (for the proxy mode script)
- AdGuard Home installed (for the website filtering script)
Script 1: Controlling Proxy Mode with OpenClash⌗
OpenClash is a popular proxy tool for OpenWRT that allows traffic routing through different rules. This script toggles between “Rule” mode (selective proxy) and “Direct” mode (bypass proxy):
Usage:
./clashlimit.sh enable
- Switches to Rule mode./clashlimit.sh disable
- Switches to Direct mode
Note: Replace the Authorization token with your own OpenClash API token.
Script 2: Restricting Access to NAS Resources⌗
This script allows you to control which devices can access your NAS by MAC address:
Usage:
./naslimit.sh disable
- Blocks specified devices from accessing the NAS./naslimit.sh enable
- Allows specified devices to access the NAS
Note: Replace the MAC addresses with those of the devices you want to control.
Script 3: Controlling Internet Access by MAC Address⌗
This script enables or disables internet access completely for specified devices:
Usage:
./netlimit.sh disable
- Blocks internet access for specified devices./netlimit.sh enable
- Enables internet access for specified devices
Script 4: Blocking Specific Websites with AdGuard Home⌗
This script manages filtering rules in AdGuard Home to block or allow access to specific websites:
Usage:
./weblimit100_1.sh disable
- Blocks additional websites (like YouTube)./weblimit100_1.sh enable
- Removes additional blocks
Note: Replace the username, password, and cookie with your AdGuard Home credentials.
Scheduling with Cron Jobs⌗
To automate these scripts based on time schedules, you can use cron jobs on your OpenWRT router. Here are some examples:
Example 1: Block Games and Social Media During Study Hours⌗
# Block access to entertainment sites on weekdays during school hours
0 8 * * 1-5 /root/weblimit100_1.sh disable
0 17 * * 1-5 /root/weblimit100_1.sh enable
Example 2: Disable Internet Access at Night for Children’s Devices⌗
# Disable internet for kids' devices at night
0 22 * * * /root/netlimit.sh disable
0 7 * * * /root/netlimit.sh enable
Example 3: Restrict NAS Access During Work Hours⌗
# Block NAS access during work hours
0 9 * * 1-5 /root/naslimit.sh disable
0 18 * * 1-5 /root/naslimit.sh enable
Example 4: Switch Proxy Mode Based on Time of Day⌗
# Use Rule mode during daytime, Direct mode at night
0 8 * * * /root/clashlimit.sh enable
0 23 * * * /root/clashlimit.sh disable
Adding Cron Jobs in OpenWRT⌗
To add these schedules to your OpenWRT router:
- SSH into your router
- Edit the crontab:
crontab -e
- Add your desired schedules using the format shown above
- Save and exit
- Restart the cron service:
/etc/init.d/cron restart
You can also add cron jobs through the LuCI web interface under System → Scheduled Tasks.
Security Considerations⌗
When implementing these scripts, consider the following security aspects:
- Credential Protection: Replace all API tokens, usernames, and passwords with secure credentials.
- Script Permissions: Ensure your scripts are only executable by root or appropriate system users.
- IP and MAC Validation: Validate all IPs and MACs to prevent script injection attacks.
- Logging: Implement proper logging to monitor script execution and troubleshoot issues.
Conclusion⌗
OpenWRT provides powerful capabilities for controlling internet access on your network. By combining firewall rules, AdGuard Home filtering, and scheduled cron jobs, you can implement sophisticated access control policies tailored to your specific needs.
These scripts offer a starting point that you can customize for your particular network environment and requirements. Whether you’re implementing parental controls, boosting productivity, or securing network resources, these tools provide an effective solution.