Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
ip-trigger-storage

venv/
__pychache__/
__pycache__/

*.pyc

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Docker, docker-compose.

# Setup

Clone the repo `git clone git@github.com:vekerdyb/ip-trigger.py`
Clone the repo `git clone git@github.com:vekerdyb/ip-trigger.git`

Rename `config.py.tmp` to `config.py` and change the values. Uncomment any backend you want to use.

Expand Down
2 changes: 0 additions & 2 deletions cloudflare_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

logger = logging.getLogger(__name__)

URL = 'https://api.cloudflare.com/client/v4/'


def on_ip_change(new_ip, log_handler=logger):
log_handler.debug('Attempting to update Cloudflare IP for host {} to {}'.format(
Expand Down
20 changes: 13 additions & 7 deletions ip-trigger.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,24 @@ def curl_get(url, interface=None):

def store_ip(ip):
logger.debug('Attempting to open "{}" for appending'.format(STORAGE_FILE))
file = open(STORAGE_FILE, "a")
file.write(ip + "\n")
file.close()
with open(STORAGE_FILE, "a") as file:
file.write(ip + "\n")
logger.debug("IP {} stored in file".format(ip))


def retrieve_last_ip_from_storage():
logger.debug('Attempting to retrieve last IP from "{}"'.format(STORAGE_FILE))
last_ip = subprocess.check_output(["tail", "-1", STORAGE_FILE])
last_ip = last_ip.strip().decode("utf-8")
logger.debug('Last IP: "{}"'.format(last_ip))
return last_ip
try:
last_ip = subprocess.check_output(["tail", "-1", STORAGE_FILE])
last_ip = last_ip.strip().decode("utf-8")
if not last_ip:
logger.debug("Storage file is empty")
return ""
logger.debug('Last IP: "{}"'.format(last_ip))
return last_ip
except subprocess.CalledProcessError:
logger.debug("Storage file does not exist or cannot be accessed")
return ""


def detect_current_ip():
Expand Down
2 changes: 1 addition & 1 deletion railgun_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def _send_simple_message(from_name, to, subject, text):

def on_ip_change(new_ip, log_handler=logger):
log_handler.debug(
'Sending email to "{}", becuase IP changed to "{}"'.format(config.RAILGUN['EMAIL_RECIPIENT'], new_ip))
'Sending email to "{}", because IP changed to "{}"'.format(config.RAILGUN['EMAIL_RECIPIENT'], new_ip))

response = _send_simple_message(
from_name=config.RAILGUN['FROM_NAME'],
Expand Down