-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUpdate-SPFrecords_exclude.py
85 lines (71 loc) · 2.64 KB
/
Update-SPFrecords_exclude.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
import os # pour récupérer les variables d'env
from typing import List
import ovh # export ovh api
from decouple import config
exclude_domains = [
"bimscreen.fr",
"btp-consultants.fr",
"citae.fr",
"mbacity.com",
"btp-diagnostics.fr",
"navalcheck.fr",
"parkyze.com",
"btp-mornings.fr",
"citybuild.fr",
"databuildr.com",
"formactu.fr",
"groupe-btpconsultants.fr",
"nextiim.com",
"novalian.com",
"novalian.fr",
"novalian.io",
]
spf_value = str(
"v=spf1 ip4:37.59.248.160/28 ip4:185.183.65.201 include:_spf.google.com include:amazonses.com include:spf.mailjet.com include:spf.sendinblue.com -all"
)
class SPFClient:
def __init__(self, application_key, application_secret, consumer_key):
self.client = ovh.Client(
endpoint="ovh-eu",
application_key=config("OVH_APPLICATION_KEY"),
application_secret=config("OVH_APPLICATION_SECRET"),
consumer_key=config("OVH_CONSUMER_KEY"),
)
def get_zones(self) -> List[str]:
zones = self.client.get("/domain/zone")
return [i for i in zones if i not in exclude_domains]
def get_spf(self, zone):
records = self.client.get("/domain/zone/%s/record?fieldType=SPF" % zone)
for record in records:
r = self.get_record(zone, record)
if r["target"].startswith('"v=spf1'):
return record
return
def get_record(self, zone: str, record: str):
return self.client.get("/domain/zone/%s/record/%s" % (zone, record))
def set_record(self, zone, value):
return self.client.post(
"/domain/zone/%s/record" % zone, target=value, fieldType="SPF", ttl=3600
)
def set_spf(self, zone: str, spf: str):
record = self.get_spf(zone)
if not record:
print("Setting spf %s for record %s" % (spf, zone))
self.set_record(zone, spf)
else:
print("Updating spf %s for record %s" % (spf, zone))
self.update_record(zone, spf_value, str(record))
return
def update_record(self, zone: str, value: str, record_id: str):
return self.client.put(
"/domain/zone/%s/record/%s" % (zone, record_id), target=value, ttl=3600
)
def refresh_zone(self, zone: str):
print("Refresh zone %s" % (zone))
return self.client.post("/domain/zone/%s/refresh" % zone)
def set_spf_all(self, spf: str):
for zo in self.get_zones():
self.set_spf(zo, spf)
self.refresh_zone(zo)
client = SPFClient(application_key="", application_secret="", consumer_key="")
client.set_spf_all(spf_value)