This document provides a detailed explanation of the Bash script that manages VPN and Tor services. It includes descriptions of functions, processes, and overall workflow to help understand how each component works.
This Bash script manages VPN and Tor connections, ensuring that the network configurations are dynamically adjusted based on the status of each service. It also monitors the VPN connection and Tor service stability and re-establishes them when necessary.
The script uses DNS configurations and specific service management commands to ensure network privacy and connectivity. It automates the process of connecting, disconnecting, and monitoring the VPN and Tor services.
- Description: Resets DNS settings and switches back to DHCP configuration.
- Process:
- Unsets the immutable attribute on the
/etc/resolv.conf
file. - Sets the DNS to a local fallback DNS address.
- Re-enables DHCP client to refresh IP and DNS configuration.
- Makes
/etc/resolv.conf
immutable again to prevent unauthorized changes.
- Unsets the immutable attribute on the
sudo chattr -i /etc/resolv.conf
echo "nameserver $local_dns" | sudo tee /etc/resolv.conf
sudo dhclient -r && sudo dhclient
sudo chattr +i /etc/resolv.conf
- Description: Configures DNS settings for use with VPN or Tor.
- Process:
- Makes
/etc/resolv.conf
file writable. - Sets the DNS to the preferred DNS address.
- Sets
/etc/resolv.conf
back to immutable.
- Makes
sudo chattr -i /etc/resolv.conf
echo "nameserver $desired_dns" | sudo tee /etc/resolv.conf
sudo chattr +i /etc/resolv.conf
- Description: Checks if the VPN is currently running by looking for the
tun0
network interface. - Process: Uses
ip a | grep tun0
to determine if the VPN is active.
vpn_interface=$(ip a | grep tun0)
- Description: Terminates any existing OpenVPN sessions.
- Process:
- Stops the OpenVPN service if it is managed by systemd.
- Kills all existing OpenVPN processes manually.
- Verifies that no OpenVPN processes are running.
sudo systemctl stop openvpn
sudo pkill -9 -f openvpn
- Description: Starts the OpenVPN client using a specified configuration file and credentials.
- Process:
- Checks if OpenVPN is already running.
- Starts the OpenVPN client.
- Verifies VPN connectivity by checking for the
tun0
interface.
sudo openvpn --config "$vpn_config" --auth-user-pass "$vpn_credentials" &
- Description: Restarts the Tor service to ensure connectivity.
- Process: Uses
sudo systemctl restart tor
and allows time for the service to stabilize.
sudo systemctl restart tor
- Description: Validates the Tor connection by using
proxychains
andcurl
to check the Tor project's connection verification page. - Process:
- Uses
proxychains curl -s https://check.torproject.org
. - Looks for the "Congratulations" message to verify a successful connection.
- Restarts Tor if the connection is not established.
- Uses
proxychains curl -s https://check.torproject.org
- Description: Stops both the VPN and Tor services and resets DNS settings.
- Process:
- Terminates VPN and Tor services.
- Kills any proxychains processes.
- Resets DNS to local settings.
terminate_vpn
sudo killall proxychains
sudo systemctl stop tor
reset_to_dhcp
- Description: Monitors VPN and Tor connections and re-establishes them if needed.
- Process:
- Periodically checks the status of the VPN and Tor services.
- Restarts VPN and Tor services if connectivity is lost.
vpn_up=$(ping -c 1 -W 3 10.8.0.1)
tor_connected=$(proxychains curl -s https://check.torproject.org)
-
Script Initialization:
- The script begins by invoking the
monitor_connections
function.
- The script begins by invoking the
-
VPN and DNS Configuration:
start_vpn
is called to establish a VPN connection using the provided configuration file and credentials.- If the VPN connection is successful, DNS is configured using
configure_dns
.
-
Tor Service Management:
- Tor is restarted using the
restart_tor
function. - The Tor connection is validated using
check_tor_connection
.
- Tor is restarted using the
-
Continuous Monitoring:
- The script enters a continuous monitoring state using
monitor_connections
, periodically checking the status of both the VPN and Tor. - If either connection is lost, the script attempts to re-establish connectivity.
- The script enters a continuous monitoring state using
To run this script, execute it as a Bash script:
./vpn_tor_script.sh
The script uses different symbols to indicate the status of each process:
[ ಠ‿ಠ ]
: Success indicator[ (⊙_☉) ]
: Informational message[ ୧༼ಠ益ಠ༽୨ ]
: Error or alert message
These messages help differentiate between normal operations and issues that may require attention.
This Bash script automates the process of managing VPN and Tor services, ensuring consistent connectivity and security. By monitoring the services in real-time and re-establishing connections as needed, the script provides robust network management and privacy.