|
1 | | -import os |
2 | | -import sys |
3 | 1 | import logging |
4 | | -from time import sleep |
5 | | -from porkbun_ddns import PorkbunDDNS |
6 | | -from porkbun_ddns.config import Config, DEFAULT_ENDPOINT |
7 | | - |
8 | | -logger = logging.getLogger('porkbun_ddns') |
9 | | -if os.getenv('DEBUG', 'False').lower() in ('true', '1', 't'): |
10 | | - logger.setLevel(logging.DEBUG) |
11 | | -else: |
12 | | - logger.setLevel(logging.INFO) |
13 | | -logger.propagate = False |
14 | | -logFormatter = logging.Formatter("%(asctime)s %(levelname)-8s %(message)s") |
15 | | -consoleHandler = logging.StreamHandler(sys.stdout) |
16 | | -consoleHandler.setFormatter(logFormatter) |
17 | | -logger.addHandler(consoleHandler) |
18 | | - |
19 | | -sleep_time = int(os.getenv('SLEEP', 300)) |
20 | | -domain = os.getenv('DOMAIN', None) |
21 | | - |
22 | | -if os.getenv('IPV4_ONLY', None) or os.getenv('IPV6_ONLY', None): |
23 | | - raise PorkbunDDNS_Error('IPV4_ONLY and IPV6_ONLY are DEPRECATED and have been removed since v1.1.0') |
24 | | - |
25 | | -public_ips = None |
26 | | -if os.getenv('PUBLIC_IPS', None): |
27 | | - public_ips = [x.strip() for x in os.getenv('PUBLIC_IPS', None).split(',')] |
28 | | -fritzbox = os.getenv('FRITZBOX', None) |
| 2 | +import sys |
| 3 | +import os |
| 4 | +import time |
| 5 | +from porkbun_ddns.config import Config, get_secret_from_env, DEFAULT_ENDPOINT |
| 6 | +from porkbun_ddns.porkbun_ddns import PorkbunDDNS |
29 | 7 |
|
30 | | -config = Config(DEFAULT_ENDPOINT, os.getenv('APIKEY'), os.getenv('SECRETAPIKEY')) |
| 8 | +logger = logging.getLogger("porkbun_ddns") |
| 9 | +logging.basicConfig(level=logging.INFO) |
31 | 10 |
|
32 | | -ipv4 = ipv6 = False |
33 | | -if os.getenv('IPV4', 'True').lower() in ('true', '1', 't'): |
34 | | - ipv4 = True |
35 | | -if os.getenv('IPV6', 'False').lower() in ('true', '1', 't'): |
36 | | - ipv6 = True |
| 11 | +def main(): |
| 12 | + domain = os.getenv("DOMAIN") |
| 13 | + apikey = get_secret_from_env("APIKEY") |
| 14 | + secretapikey = get_secret_from_env("SECRETAPIKEY") |
37 | 15 |
|
38 | | -if not all([os.getenv('DOMAIN'), os.getenv('SECRETAPIKEY'), os.getenv('APIKEY')]): |
39 | | - logger.info('Please set DOMAIN, SECRETAPIKEY and APIKEY') |
40 | | - sys.exit(1) |
41 | | - |
42 | | -if not any([ipv4, ipv6]): |
43 | | - logger.info('No Protocol selected! Please set IPV4 and/or IPV6 TRUE') |
44 | | - sys.exit(1) |
45 | | - |
46 | | -porkbun_ddns = PorkbunDDNS(config, domain, public_ips=public_ips, |
47 | | - fritzbox_ip=fritzbox, ipv4=ipv4, ipv6=ipv6) |
48 | | - |
49 | | -while True: |
50 | | - subdomains = os.getenv('SUBDOMAINS', '') |
51 | | - if subdomains: |
52 | | - for subdomain in subdomains.replace(' ', '').split(','): |
53 | | - porkbun_ddns.set_subdomain(subdomain) |
54 | | - porkbun_ddns.update_records() |
55 | | - else: |
56 | | - porkbun_ddns.update_records() |
57 | | - logger.info('Sleeping... {}s'.format(sleep_time)) |
58 | | - sleep(sleep_time) |
| 16 | + if not all([domain, apikey, secretapikey]): |
| 17 | + logger.info("Please set DOMAIN, SECRETAPIKEY and APIKEY") |
| 18 | + sys.exit(1) |
| 19 | + |
| 20 | + ipv4 = ipv6 = False |
| 21 | + if os.getenv('IPV4', 'True').lower() in ('true', '1', 't'): |
| 22 | + ipv4 = True |
| 23 | + if os.getenv('IPV6', 'False').lower() in ('true', '1', 't'): |
| 24 | + ipv6 = True |
| 25 | + |
| 26 | + if not any([ipv4, ipv6]): |
| 27 | + logger.info("No Protocol selected! Please set IPV4 and/or IPV6 TRUE") |
| 28 | + sys.exit(1) |
| 29 | + |
| 30 | + config = Config(endpoint=DEFAULT_ENDPOINT, apikey=apikey, secretapikey=secretapikey) |
| 31 | + porkbun_ddns = PorkbunDDNS(config, domain, ipv4=ipv4, ipv6=ipv6) |
| 32 | + |
| 33 | + logger.info("Starting Porkbun DDNS update process...") |
| 34 | + try: |
| 35 | + while True: |
| 36 | + subdomains = os.getenv('SUBDOMAINS', '') |
| 37 | + if subdomains: |
| 38 | + for subdomain in subdomains.replace(' ', '').split(','): |
| 39 | + porkbun_ddns.set_subdomain(subdomain) |
| 40 | + porkbun_ddns.update_records() |
| 41 | + else: |
| 42 | + porkbun_ddns.update_records() |
| 43 | + |
| 44 | + sleep_time = int(os.getenv("SLEEP", "300")) |
| 45 | + logger.info("Sleeping... {}s".format(sleep_time)) |
| 46 | + time.sleep(sleep_time) |
| 47 | + except Exception as e: |
| 48 | + logger.error(f"Failed to update DNS records: {e}") |
| 49 | + sys.exit(1) |
| 50 | + |
| 51 | +if __name__ == "__main__": |
| 52 | + main() |
0 commit comments