diff options
author | pommicket <pommicket@gmail.com> | 2025-08-29 13:15:18 -0400 |
---|---|---|
committer | pommicket <pommicket@gmail.com> | 2025-08-29 13:15:18 -0400 |
commit | 1b20f518a2fe7ec139cf2107b9b0c949153a1e32 (patch) | |
tree | 9345eef8f84a754e4d19dc4d7fa32fff1f297ef6 |
Initial commit
-rwxr-xr-x | install.sh | 9 | ||||
-rw-r--r-- | requirements.txt | 1 | ||||
-rwxr-xr-x | simpleddns.py | 41 |
3 files changed, 51 insertions, 0 deletions
diff --git a/install.sh b/install.sh new file mode 100755 index 0000000..51c45fd --- /dev/null +++ b/install.sh @@ -0,0 +1,9 @@ +#!/bin/sh +NAME=simpleddns +BINARY_DIR=$HOME/.local/bin +CONFIG=${XDG_CONFIG_HOME:-$HOME/.config}/$NAME +mkdir -p $BINARY_DIR $CONFIG || exit 1 +python -m venv $CONFIG/venv || exit 1 +$CONFIG/venv/bin/pip install -U --upgrade-strategy eager -r requirements.txt || exit 1 +sed "s,/usr/bin/env python3,$CONFIG/venv/bin/python3," $NAME.py > $BINARY_DIR/$NAME || exit 1 +chmod +x $BINARY_DIR/$NAME || exit 1 diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..d166403 --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +requests ~= 2.32.5 diff --git a/simpleddns.py b/simpleddns.py new file mode 100755 index 0000000..19196ce --- /dev/null +++ b/simpleddns.py @@ -0,0 +1,41 @@ +#!/usr/bin/env python3 +import os +import sys +import requests +import stat +import subprocess +from pathlib import Path + +simpleddns_config_dir = os.getenv('SIMPLEDDNS_CONFIG_DIR') +xdg_config_dir = os.getenv('XDG_CONFIG_DIR') +if simpleddns_config_dir: + config_dir = Path(simpleddns_config_dir) +elif xdg_config_dir: + config_dir = Path(xdg_config_dir, 'simpleddns') +else: + config_dir = Path('~/.config/simpleddns').expanduser() +os.makedirs(config_dir, exist_ok = True) + +def fatal_error(message): + sys.stderr.write(f'Fatal error: {message}\n') + exit(1) + +get_ip_path = Path(config_dir, 'getip') + +if not get_ip_path.exists(): + print(f'Warning: {get_ip_path} does not exist. Putting default of "curl ifconfig.co" there') + with open(get_ip_path, 'w') as f: + f.write('curl --no-progress-meter ifconfig.co') + +def get_ip(): + if os.name == 'posix' and (get_ip_path.stat().st_mode & 0o100): + command = str(get_ip_path) + else: + command = open(get_ip_path).read() + result = subprocess.run(command, shell=True, stdout=subprocess.PIPE) + if result.returncode: + print(f'WARNING: {command} failed (exit code {result.returncode})') + return None + return result.stdout.decode(errors='replace').strip() + +print(get_ip()) |