forked from igroykt/letsencrypt-nic
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclean.py
More file actions
executable file
·93 lines (79 loc) · 2.56 KB
/
clean.py
File metadata and controls
executable file
·93 lines (79 loc) · 2.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import os
import sys
import json
import time
from configparser import ConfigParser
from nic_api import DnsApi
from func import Func
try:
if getattr(sys, 'frozen', False):
script_dir = os.path.dirname(sys.executable)
else:
script_dir = os.path.dirname(os.path.realpath(__file__))
config = ConfigParser()
config.read(script_dir + os.sep + "config.ini")
except Exception as err:
raise SystemExit(f"Config parse: {err}")
USERNAME = os.getenv('NICUSER')
PASSWORD = os.getenv('NICPASS')
CLIENT_ID = os.getenv('NICID')
CLIENT_SECRET = os.getenv('NICSECRET')
SERVICE_ID = config.get('GENERAL', 'SERVICE_ID')
CERTBOT_DOMAIN = os.getenv('CERTBOT_DOMAIN')
VERBOSE = os.getenv('VERBOSE')
TOKEN_FILE = script_dir + os.sep + "nic_token.json"
def main():
try:
if VERBOSE:
print('Configuring OAuth...')
oauth_config = {
'APP_LOGIN': CLIENT_ID,
'APP_PASSWORD': CLIENT_SECRET
}
except Exception as err:
raise SystemExit(f"oauth_config error: {err}")
try:
api = DnsApi(oauth_config)
except Exception as err:
raise SystemExit(f"DnsApi error: {err}")
if os.path.exists(TOKEN_FILE):
mtime = os.path.getmtime(TOKEN_FILE)
with open(TOKEN_FILE, 'r') as file:
content = json.load(file)
expires_in = int(content['expires_in'])
if mtime + expires_in <= time.time():
if VERBOSE:
print('Token expired. Refreshing...')
os.remove(TOKEN_FILE)
try:
if VERBOSE:
print('Authorize API...')
api.authorize(
username = USERNAME,
password = PASSWORD,
token_filename = TOKEN_FILE
)
except Exception as err:
if VERBOSE:
print(f"api.authorize: {err}")
raise SystemExit(f"api.authorize: {err}")
main_domain = Func.mainDomainTail(CERTBOT_DOMAIN)
try:
if VERBOSE:
print('Extract all DNS records...')
records = api.records(SERVICE_ID, main_domain)
except Exception as err:
raise SystemExit(f"api.records error: {err}")
try:
if VERBOSE:
print('Search for TXT records...')
records_id = Func.NIC_findTXTID(records)
for id in records_id:
api.delete_record(id, SERVICE_ID, main_domain)
api.commit(SERVICE_ID, main_domain)
except Exception as err:
raise SystemExit(f"api.delete_record error: {err}")
if os.path.exists(TOKEN_FILE):
os.remove(TOKEN_FILE)
if __name__ == '__main__':
main()