-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfreenom.py
114 lines (106 loc) · 4.18 KB
/
freenom.py
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import sys
from pathlib import Path
import checker
import dns.resolver
sys.path.insert(0, str(Path(__file__).parent.joinpath("Freenom-dns-updater").resolve()))
import freenom_dns_updater
from datetime import date
from tools import isSubdomain
class DomainCheckerFreenomDNS(checker.DomainChecker):
def check(self):
dns_update = []
for domain in self.domains:
print('Domain %s : ' % (domain.name), end=' ')
if domain.sitehost == 'extern' or isSubdomain(domain.name):
print(" SKIP")
continue
try:
anwser_dns = dns.resolver.query(domain.name, 'NS')
ns = []
ns_ok = False
for e in anwser_dns:
ns.append(str(e))
ns_ok = "dns1.alwaysdata.com." in ns and \
"dns2.alwaysdata.com." in ns
if not ns_ok:
raise Exception
print(" OK")
except Exception:
print("Alwaysdata nameservers not found in domain record!")
dns_update.append(domain)
try:
self.to_do["dns_update"] = self.to_do["dns_update"] + dns_update
except Exception:
self.to_do["dns_update"] = dns_update
class DomainCheckerFreenomAPI(checker.DomainChecker):
def __init__(self, *args, **kwargs):
super(DomainCheckerFreenomAPI, self).__init__(*args, **kwargs)
self.freenom = freenom_dns_updater.Freenom()
def __domain_in_config(self, domainname):
for d in self.domains:
if d.name == domainname:
return True
return False
def check(self, login, password):
try:
if not self.freenom.login(login, password):
raise Exception()
domains_list = self.freenom.list_domains()
except Exception:
print("Freenom API: Bad credentials. Cannot get domains list")
return
add_to_config = []
need_renew = []
for d in domains_list:
print('Domain %s : ' % (d.name), end=' ')
days_before_renew = (d.expire_date - date.today()).days
ok = True
if not self.__domain_in_config(d.name):
add_to_config.append(d.name)
print(' Not found in your config file', end=' ')
ok = False
if self.freenom.need_renew(d):
need_renew.append(d.name)
print(' Need renew :', days_before_renew,
"days", end=' ')
ok = False
if ok:
print(' OK (', days_before_renew, 'days before renew)',
end='')
print()
try:
self.to_do["add_to_config"] = self.to_do["add_to_config"] \
+ add_to_config
except Exception:
self.to_do["add_to_config"] = add_to_config
try:
self.to_do["need_renew"] = self.to_do["need_renew"] \
+ need_renew
except Exception:
self.to_do["need_renew"] = need_renew
def update(self, to_do={}):
self.to_do = to_do
try:
domains_list = self.freenom.list_domains()
except Exception:
print("Freenom API: failed to get domain list")
return
for d in domains_list:
if d.name in self.to_do["need_renew"]:
try:
if not self.freenom.renew(d):
raise Exception()
print("Renew ", d.name)
except Exception:
print("Failed to renew ", d.name)
if 'dns_update' in self.to_do:
found = False
for dto_do in self.to_do['dns_update']:
if dto_do.name in d.name:
found = True
break
if found:
if self.freenom.set_nameserver(d, ['dns1.alwaysdata.com', 'dns2.alwaysdata.com']):
print("Set alwaysdata nameservers to " + str(d.name))
else:
print("failed to et alwaysdata nameservers to " + str(d.name))