Skip to content

Commit

Permalink
parameter cron for disabling checks when being instantiated by CRON (#25
Browse files Browse the repository at this point in the history
)

* add parameter cron

* move parameter to non shared + add doc about --cron parameter

* reword help info about cron parameter + add --cron parameter in the crontab example
  • Loading branch information
unixfox authored Aug 15, 2024
1 parent d7d7c33 commit 7c10ce5
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 51 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ Full detailed documentation: https://docs.invidious.io/ipv6-rotator/
Twice a day (noon and midnight) is enough for YouTube servers. Also at the reboot of the server!
Example crontab (`crontab -e -u root`):
```
@reboot sleep 30s && python smart-ipv6-rotator.py run --ipv6range=YOURIPV6SUBNET/64
0 */12 * * * python smart-ipv6-rotator.py run --ipv6range=YOURIPV6SUBNET/64
@reboot sleep 30s && python smart-ipv6-rotator.py run --cron --ipv6range=YOURIPV6SUBNET/64
0 */12 * * * python smart-ipv6-rotator.py run --cron --ipv6range=YOURIPV6SUBNET/64
```
The `sleep` command is used in case your network takes too much time time to be ready.

Expand Down Expand Up @@ -71,6 +71,7 @@ smart-ipv6-rotator.py run [-h] [--services {google}] [--external-ipv6-ranges EXT
- `--skip-root`: Skip root check.
- `--no-services`: Completely disable the --services flag.
- `--ipv6range IPV6RANGE`: Your IPV6 range (e.g., 2407:7000:9827:4100::/64).
- `--cron`: Do not check if the IPv6 address configured will work properly. Useful for CRON and when you know that the IPv6 range is correct.

---

Expand Down
110 changes: 61 additions & 49 deletions smart_ipv6_rotator/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ def run(
services: str | None = None,
external_ipv6_ranges: str | None = None,
no_services: bool = False,
cron: bool = False,
) -> None:
"""Run the IPv6 rotator process."""

Expand All @@ -87,6 +88,9 @@ def run(
"[ERROR] Legacy database format detected! Please run `python smart-ipv6-rotator.py clean` using the old version of this script.\nhttps://github.com/iv-org/smart-ipv6-rotator"
)

if cron is True:
print("[INFO] Running without checking if the IPv6 address configured will work properly.")

root_check(skip_root)
check_ipv6_connectivity()

Expand Down Expand Up @@ -142,59 +146,61 @@ def run(

sleep(2) # Need so that the linux kernel takes into account the new ipv6 route

try:
IPROUTE.route(
"add",
dst=ICANHAZIP_IPV6_ADDRESS,
prefsrc=random_ipv6_address,
gateway=default_interface_gateway,
oif=default_interface_index,
priority=1,
)
except Exception as error:
clean_ranges(service_ranges, skip_root)
sys.exit(
"[Error] Failed to configure the test IPv6 route. The setup did not work!\n"
f" Exception:\n{error}"
)
if cron is False:

sleep(2)
try:
IPROUTE.route(
"add",
dst=ICANHAZIP_IPV6_ADDRESS,
prefsrc=random_ipv6_address,
gateway=default_interface_gateway,
oif=default_interface_index,
priority=1,
)
except Exception as error:
clean_ranges(service_ranges, skip_root)
sys.exit(
"[Error] Failed to configure the test IPv6 route. The setup did not work!\n"
f" Exception:\n{error}"
)

try:
check_new_ipv6_address = requests.get(
f"http://[{ICANHAZIP_IPV6_ADDRESS}]",
headers={"host": "ipv6.icanhazip.com"},
timeout=5,
)
except requests.exceptions.RequestException as error:
clean_ranges(service_ranges, skip_root)
sys.exit(
"[ERROR] Failed to send the request for checking the new IPv6 address! The setup did not work!\n"
" Your provider probably does not allow setting any arbitrary IPv6 address.\n"
" Or did you correctly configure the IPv6 subnet to use?\n"
f" Exception:\n{error}"
)
sleep(4)

try:
check_new_ipv6_address.raise_for_status()
except requests.HTTPError:
clean_ranges(service_ranges, skip_root)
sys.exit(
"[ERROR] icanhazip didn't return the expected status, possibly they are down right now."
)
try:
check_new_ipv6_address = requests.get(
f"http://[{ICANHAZIP_IPV6_ADDRESS}]",
headers={"host": "ipv6.icanhazip.com"},
timeout=5,
)
except requests.exceptions.RequestException as error:
clean_ranges(service_ranges, skip_root)
sys.exit(
"[ERROR] Failed to send the request for checking the new IPv6 address! The setup did not work!\n"
" Your provider probably does not allow setting any arbitrary IPv6 address.\n"
" Or did you correctly configure the IPv6 subnet to use?\n"
f" Exception:\n{error}"
)

response_new_ipv6_address = check_new_ipv6_address.text.strip()
if response_new_ipv6_address == random_ipv6_address:
print("[INFO] Correctly using the new random IPv6 address, continuing.")
else:
clean_ranges(service_ranges, skip_root)
sys.exit(
"[ERROR] The new random IPv6 is not used! The setup did not work!\n"
" That is very unexpected, check if your IPv6 routes do not have too much priority."
f" Address used: {response_new_ipv6_address}"
)
try:
check_new_ipv6_address.raise_for_status()
except requests.HTTPError:
clean_ranges(service_ranges, skip_root)
sys.exit(
"[ERROR] icanhazip didn't return the expected status, possibly they are down right now."
)

clean_ipv6_check(saved_ranges)
response_new_ipv6_address = check_new_ipv6_address.text.strip()
if response_new_ipv6_address == random_ipv6_address:
print("[INFO] Correctly using the new random IPv6 address, continuing.")
else:
clean_ranges(service_ranges, skip_root)
sys.exit(
"[ERROR] The new random IPv6 is not used! The setup did not work!\n"
" That is very unexpected, check if your IPv6 routes do not have too much priority."
f" Address used: {response_new_ipv6_address}"
)

clean_ipv6_check(saved_ranges)

try:
for ipv6_range in service_ranges:
Expand All @@ -209,7 +215,7 @@ def run(
except Exception as error:
clean_ranges(service_ranges, skip_root)
sys.exit(
f"[Error] Failed to configure the test IPv6 route. The setup did not work!\n"
f"[Error] Failed to configure the service IPv6 route. The setup did not work!\n"
f" Exception:\n{error}"
)

Expand Down Expand Up @@ -258,6 +264,12 @@ def main() -> None:
help="Your IPV6 range. Example: 2407:7000:9827:4100::/64",
required=True,
)
run_parser.add_argument(
"--cron",
action="store_true",
help="Disable checks for IPV6 address configured. Useful when being instantiated by CRON and the IPv6 range configured is correct.",
required=False,
)
run_parser.set_defaults(func=run)

clean_one_parser = subparsers.add_parser(
Expand Down

0 comments on commit 7c10ce5

Please sign in to comment.