diff --git a/ip_checker/README.md b/ip_checker/README.md index e69de29..7959467 100644 --- a/ip_checker/README.md +++ b/ip_checker/README.md @@ -0,0 +1,117 @@ +

IP Checker

+ +

+ A script that monitors the current VPN IP of qbittorrent-openvpn container. +
+ The script will compare your Internet Service Providers (ISP) IP Address +
+ to the current IP address of container qbittorrent-openvpn. +
+ The check will run at a specified time along with logging +
+ the current IP Address of the qbittorrent-openvpn container. +
+ If both ISP IP and VPN container IP match, that will mean +
+ that qbittorrent is running on a non encrypted internet connection. +
+ The script will then proceed to kill the qbittorrent container and send a notification +
+ to either ntfy or pushover with an **URGENT** message. +
+

+ +## Prerequisites + +The following modules are required for the script to function. They will need to be installed first. Python ships with some core modules but others will need to be installed separately. + +### Standard Python Library Modules: + +This script uses Python and multiple add on modules. + +* **os:** Provides a way to use operating system dependent functionality. Part of the standard library. + +* **time:** Used for time-related tasks. Included in the standard library. + +* **json:** Used for JSON serialization and deserialization. Part of the standard library. + +* **logging:** Used for logging events and messages. Standard library module. + +* **datetime:** Provides classes for manipulating dates and times. Standard library module. + +* **subprocess:** Used for running new applications or programs through Python. Part of the standard library. + +* **logging.handlers:** A component of the logging module, offers various handlers for logging. Standard library. + +* **configparser:** Renamed to ConfigParser in Python 3, this module is used for working with configuration files. Part of the standard library. + +* **email.mime.text:** Used for creating MIME objects of major type text. Part of the standard Python library under the email package. + +### Modules that Need to be Installed: + +* **colorlog:** This is an external library for colored logging output. You need to install it separately using pip. + +* **requests:** A popular HTTP library for Python, used for making HTTP requests. This is not included in the standard library and must be installed separately. + +To install the external modules you will first need `PIP` installed on your server. **PIP** is the package manager for Python. + +1. See if **pip** is already installed. Open a terminal and type: + + ```bash + pip -V + ``` + +2. If not, install it. + + ```bash + python get-pip.py + ``` + +3. Once **pip** is installed you can install the external modules. + + ```bash + pip install colorlog requests + ``` + +## Folder Structure + +If you have not done so already, create a new folder called `scripts` in your **~home** folder. This is where all your scripts will go. + +Inside your `scripts` folder create a new folder called `ip_checker` + +When done your folders structure should look like this: `/home/[username]/scripts/ip_checker` + +## Script + +Get the source code from this repo and save it as `ip_checker.py` in the **ip_checker** folder created in the previous step. + +You will also need to get the config file called `ip_checker_config.ini`. The .ini file will hold the credentials for **pushover** and **ntfy**. You can also set your notification preference for the script. + +Next we need to change the permissions of these files so that the server can read them. + +1. For both `ip_checker.py` and `ip_checker_config.ini` right click then Properties. + +2. Click on `Permissions` tab at top. + +3. Make sure Owner & Group reflect your `username` with `Read and Write` access to all. + +4. Check the `Execute` box to allow executing the file as a program. + +5. Close out the window. + +## Run + +Now we need to test and run the script. + +Make sure your terminal is open to the location of the `ip_checker.py` file. Should be `~/scripts/ip_checker` + +Normally to run a Python script will would use `python3 ip_checker.py` + +This will still work but because we have shebang set in the script we can just use +`./ip_checker.py` + +You should now see `[INFO]` log with current VPN IP of the **qbittorrent-openvpn** container. In the script we set the run time with `interval_seconds = 60`. This setting tells the script to check the VPN IP address of the container every minute. + +## Automation + +Now the script will need to be automated so that it starts the check continuously in the background. See system services to do so. diff --git a/ip_checker/ip_checker.py b/ip_checker/ip_checker.py index 6a69a86..f2b79d6 100755 --- a/ip_checker/ip_checker.py +++ b/ip_checker/ip_checker.py @@ -1,7 +1,7 @@ #!/usr/bin/python3 # # -# +# Make sure to set you credentials in the ip_checker_config.ini file # # @@ -42,9 +42,11 @@ logs_folder = 'logs' if not os.path.exists(logs_folder): os.makedirs(logs_folder) +# Getting the log file path def get_log_file_path(): return os.path.join(logs_folder, 'ip_checker.log') +# Rotate and archive log files every 24hrs def rotate_log_files(log_file): now = datetime.datetime.now() timestamp = now.strftime('%Y-%m-%d') @@ -55,10 +57,12 @@ def rotate_log_files(log_file): # Rename the current log file to the rotated log file os.rename(log_file, rotated_log_path) +# Formating time def format_remaining_time(remaining_time): minutes, seconds = divmod(remaining_time.seconds, 60) return f'{minutes} minutes {seconds} seconds' +# Getitng external IP address from internet provider def get_external_ip(): try: response = requests.get(f'{get_ip_address}') @@ -69,6 +73,7 @@ def get_external_ip(): logger.info(' ') return None +# Getting containers current IP address def get_vpn_container_ip(container_name): try: cmd = ["docker", "exec", container_name, "curl", "-s", get_ip_address] @@ -83,6 +88,7 @@ def get_vpn_container_ip(container_name): logger.info(' ') return None +# Function to stop the container that depends on VPN connnection. def stop_dependent_container(container_name): """ Stop the specified Docker container. @@ -95,6 +101,7 @@ def stop_dependent_container(container_name): logger.warning(f'[WARN] - Failed stopping container {container_name}: {e}') logger.info(' ') +# Notifacation alert for ntfy def send_ntfy_alert(message, urgent=False): """ Send a notification using ntfy. @@ -109,6 +116,7 @@ def send_ntfy_alert(message, urgent=False): logger.warning(f'[WARN] - Failed to send ntfy notification: {e}') logger.info(' ') +# Notifacation alert for pushover def send_pushover_alert(app_token, user_key): # Define notification title and message @@ -135,7 +143,7 @@ def send_pushover_alert(app_token, user_key): logger.info(' ') - +# Function to send an alert to the specified notification service def send_alert(alert_method): if alert_method == 'ntfy': # Send urgent ntfy notification @@ -242,32 +250,3 @@ if __name__ == "__main__": main() - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/ip_checker/ip_checker_config.ini b/ip_checker/ip_checker_config.ini new file mode 100644 index 0000000..a6a94c1 --- /dev/null +++ b/ip_checker/ip_checker_config.ini @@ -0,0 +1,10 @@ +[Pushover] +APP_TOKEN = xxxxxxxxxxxx +USER_KEY = xxxxxxxxxxxx + +[Nfty] +NFTY_URL = https://ntfy.sh + +[Settings] +# Alert method (ntfy or pushover) +ALERT_METHOD = pushover